|
| 1 | +#===-- CMakeLists.txt ------------------------------------------------------===# |
| 2 | +# |
| 3 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +# See https://llvm.org/LICENSE.txt for license information. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +# |
| 7 | +#===------------------------------------------------------------------------===# |
| 8 | +# |
| 9 | +# Build instructions for the flang-rt library. This is file is intended to be |
| 10 | +# included using the LLVM_ENABLE_RUNTIMES mechanism. |
| 11 | +# |
| 12 | +#===------------------------------------------------------------------------===# |
| 13 | + |
| 14 | +if (NOT LLVM_RUNTIMES_BUILD) |
| 15 | + message(FATAL_ERROR "Use this CMakeLists.txt from LLVM's runtimes build system. |
| 16 | + Example: |
| 17 | + cmake <llvm-project>/runtimes -DLLVM_ENABLE_RUNTIMES=flang-rt |
| 18 | + ") |
| 19 | +endif () |
| 20 | + |
| 21 | +set(LLVM_SUBPROJECT_TITLE "Flang-RT") |
| 22 | +set(FLANG_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
| 23 | +set(FLANG_RT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}") |
| 24 | +set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../flang") |
| 25 | + |
| 26 | +# CMake 3.24 is the first version of CMake that directly recognizes Flang. |
| 27 | +# LLVM's requirement is only CMake 3.20, teach CMake 3.20-3.23 how to use Flang. |
| 28 | +if (CMAKE_VERSION VERSION_LESS "3.24") |
| 29 | + cmake_path(GET CMAKE_Fortran_COMPILER STEM _Fortran_COMPILER_STEM) |
| 30 | + if (_Fortran_COMPILER_STEM STREQUAL "flang-new" OR _Fortran_COMPILER_STEM STREQUAL "flang") |
| 31 | + include(CMakeForceCompiler) |
| 32 | + CMAKE_FORCE_Fortran_COMPILER("${CMAKE_Fortran_COMPILER}" "LLVMFlang") |
| 33 | + |
| 34 | + set(CMAKE_Fortran_COMPILER_ID "LLVMFlang") |
| 35 | + set(CMAKE_Fortran_COMPILER_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}") |
| 36 | + |
| 37 | + set(CMAKE_Fortran_SUBMODULE_SEP "-") |
| 38 | + set(CMAKE_Fortran_SUBMODULE_EXT ".mod") |
| 39 | + |
| 40 | + set(CMAKE_Fortran_PREPROCESS_SOURCE |
| 41 | + "<CMAKE_Fortran_COMPILER> -cpp <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> > <PREPROCESSED_SOURCE>") |
| 42 | + |
| 43 | + set(CMAKE_Fortran_FORMAT_FIXED_FLAG "-ffixed-form") |
| 44 | + set(CMAKE_Fortran_FORMAT_FREE_FLAG "-ffree-form") |
| 45 | + |
| 46 | + set(CMAKE_Fortran_MODDIR_FLAG "-module-dir") |
| 47 | + |
| 48 | + set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON "-cpp") |
| 49 | + set(CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF "-nocpp") |
| 50 | + set(CMAKE_Fortran_POSTPROCESS_FLAG "-ffixed-line-length-72") |
| 51 | + |
| 52 | + set(CMAKE_Fortran_COMPILE_OPTIONS_TARGET "--target=") |
| 53 | + |
| 54 | + set(CMAKE_Fortran_LINKER_WRAPPER_FLAG "-Wl,") |
| 55 | + set(CMAKE_Fortran_LINKER_WRAPPER_FLAG_SEP ",") |
| 56 | + endif () |
| 57 | +endif () |
| 58 | +enable_language(Fortran) |
| 59 | + |
| 60 | + |
| 61 | +list(APPEND CMAKE_MODULE_PATH |
| 62 | + "${FLANG_RT_SOURCE_DIR}/cmake/modules" |
| 63 | + "${FLANG_SOURCE_DIR}/cmake/modules" |
| 64 | + ) |
| 65 | +include(AddFlangRT) |
| 66 | +include(GetToolchainDirs) |
| 67 | +include(FlangCommon) |
| 68 | +include(HandleCompilerRT) |
| 69 | +include(ExtendPath) |
| 70 | + |
| 71 | + |
| 72 | +############################ |
| 73 | +# Build Mode Introspection # |
| 74 | +############################ |
| 75 | + |
| 76 | +# Determine whether we are in the runtimes/runtimes-bins directory of a |
| 77 | +# bootstrap build. |
| 78 | +set(LLVM_TREE_AVAILABLE OFF) |
| 79 | +if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION) |
| 80 | + set(LLVM_TREE_AVAILABLE ON) |
| 81 | +endif() |
| 82 | + |
| 83 | +# Path to LLVM development tools (FileCheck, llvm-lit, not, ...) |
| 84 | +set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin") |
| 85 | + |
| 86 | +# Determine build and install paths. |
| 87 | +# The build path is absolute, but the install dir is relative, CMake's install |
| 88 | +# command has to apply CMAKE_INSTALL_PREFIX itself. |
| 89 | +get_toolchain_library_subdir(toolchain_lib_subdir) |
| 90 | +if (LLVM_TREE_AVAILABLE) |
| 91 | + # In a bootstrap build emit the libraries into a default search path in the |
| 92 | + # build directory of the just-built compiler. This allows using the |
| 93 | + # just-built compiler without specifying paths to runtime libraries. |
| 94 | + # |
| 95 | + # Despite Clang in the name, get_clang_resource_dir does not depend on Clang |
| 96 | + # being added to the build. Flang uses the same resource dir as clang. |
| 97 | + include(GetClangResourceDir) |
| 98 | + get_clang_resource_dir(FLANG_RT_OUTPUT_RESOURCE_DIR PREFIX "${LLVM_LIBRARY_OUTPUT_INTDIR}/..") |
| 99 | + get_clang_resource_dir(FLANG_RT_INSTALL_RESOURCE_PATH) |
| 100 | + |
| 101 | + extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "${toolchain_lib_subdir}") |
| 102 | +else () |
| 103 | + # In a standalone runtimes build, do not write into LLVM_BINARY_DIR. It may be |
| 104 | + # read-only and/or shared by multiple runtimes with different build |
| 105 | + # configurations (e.g. Debug/Release). Use the runtime's own lib dir like any |
| 106 | + # non-toolchain library. |
| 107 | + # For the install prefix, still use the resource dir assuming that Flang will |
| 108 | + # be installed there using the same prefix. This is to not have a difference |
| 109 | + # between bootstrap and standalone runtimes builds. |
| 110 | + set(FLANG_RT_OUTPUT_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}") |
| 111 | + set(FLANG_RT_INSTALL_RESOURCE_PATH "lib${LLVM_LIBDIR_SUFFIX}/clang/${LLVM_VERSION_MAJOR}") |
| 112 | + |
| 113 | + extend_path(FLANG_RT_OUTPUT_RESOURCE_LIB_DIR "${FLANG_RT_OUTPUT_RESOURCE_DIR}" "lib${LLVM_LIBDIR_SUFFIX}") |
| 114 | +endif () |
| 115 | +extend_path(FLANG_RT_INSTALL_RESOURCE_LIB_PATH "${FLANG_RT_INSTALL_RESOURCE_PATH}" "${toolchain_lib_subdir}") |
| 116 | +cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_DIR) |
| 117 | +cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_PATH) |
| 118 | +cmake_path(NORMAL_PATH FLANG_RT_OUTPUT_RESOURCE_LIB_DIR) |
| 119 | +cmake_path(NORMAL_PATH FLANG_RT_INSTALL_RESOURCE_LIB_PATH) |
| 120 | + |
| 121 | + |
| 122 | +################# |
| 123 | +# Build Options # |
| 124 | +################# |
| 125 | + |
| 126 | +# Important: flang-rt user options must be prefixed with "FLANG_RT_". Variables |
| 127 | +# with this prefix will be forwarded in bootstrap builds. |
| 128 | + |
| 129 | +option(FLANG_RT_INCLUDE_TESTS "Generate build targets for the flang-rt unit and regression-tests." "${LLVM_INCLUDE_TESTS}") |
| 130 | + |
| 131 | + |
| 132 | +set(FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT "" CACHE STRING "Compile Flang-RT with GPU support (CUDA or OpenMP)") |
| 133 | +set_property(CACHE FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT PROPERTY STRINGS |
| 134 | + "" |
| 135 | + CUDA |
| 136 | + OpenMP |
| 137 | + ) |
| 138 | +if (NOT FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT) |
| 139 | + # Support for GPUs disabled |
| 140 | +elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA") |
| 141 | + # Support for CUDA |
| 142 | + set(FLANG_RT_LIBCUDACXX_PATH "" CACHE PATH "Path to libcu++ package installation") |
| 143 | + option(FLANG_RT_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS "Do not compile global variables' definitions when producing PTX library" OFF) |
| 144 | +elseif (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "OpenMP") |
| 145 | + # Support for OpenMP offloading |
| 146 | + set(FLANG_RT_DEVICE_ARCHITECTURES "all" CACHE STRING |
| 147 | + "List of OpenMP device architectures to be used to compile the Fortran runtime (e.g. 'gfx1103;sm_90')" |
| 148 | + ) |
| 149 | + |
| 150 | + if (FLANG_RT_DEVICE_ARCHITECTURES STREQUAL "all") |
| 151 | + # TODO: support auto detection on the build system. |
| 152 | + set(all_amdgpu_architectures |
| 153 | + "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906" |
| 154 | + "gfx908;gfx90a;gfx90c;gfx940;gfx1010;gfx1030" |
| 155 | + "gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036" |
| 156 | + "gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151" |
| 157 | + "gfx1152;gfx1153") |
| 158 | + set(all_nvptx_architectures |
| 159 | + "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62" |
| 160 | + "sm_70;sm_72;sm_75;sm_80;sm_86;sm_89;sm_90") |
| 161 | + set(all_gpu_architectures |
| 162 | + "${all_amdgpu_architectures};${all_nvptx_architectures}") |
| 163 | + set(FLANG_RT_DEVICE_ARCHITECTURES ${all_gpu_architectures}) |
| 164 | + endif() |
| 165 | + list(REMOVE_DUPLICATES FLANG_RT_DEVICE_ARCHITECTURES) |
| 166 | +else () |
| 167 | + message(FATAL_ERROR "Invalid value '${FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT}' for FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT; must be empty, 'CUDA', or 'OpenMP'") |
| 168 | +endif () |
| 169 | + |
| 170 | + |
| 171 | +option(FLANG_RT_INCLUDE_CUF "Build the CUDA Fortran runtime (libflang_rt.cuda.a)" OFF) |
| 172 | +if (FLANG_RT_INCLUDE_CUF) |
| 173 | + find_package(CUDAToolkit REQUIRED) |
| 174 | +endif() |
| 175 | + |
| 176 | + |
| 177 | +######################## |
| 178 | +# System Introspection # |
| 179 | +######################## |
| 180 | + |
| 181 | +include(CheckCXXSymbolExists) |
| 182 | +include(CheckCXXSourceCompiles) |
| 183 | +check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R) |
| 184 | +# Can't use symbol exists here as the function is overloaded in C++ |
| 185 | +check_cxx_source_compiles( |
| 186 | + "#include <string.h> |
| 187 | + int main() { |
| 188 | + char buf[4096]; |
| 189 | + return strerror_s(buf, 4096, 0); |
| 190 | + } |
| 191 | + " |
| 192 | + HAVE_DECL_STRERROR_S) |
| 193 | + |
| 194 | + |
| 195 | +# Search for clang_rt.builtins library. Need in addition to msvcrt. |
| 196 | +if (WIN32) |
| 197 | + find_compiler_rt_library(builtins FLANG_RT_BUILTINS_LIBRARY) |
| 198 | +endif () |
| 199 | + |
| 200 | + |
| 201 | +# Check whether the compiler can undefine a macro using the "-U" flag. |
| 202 | +# Aternatively, we could use |
| 203 | +# CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "GNU" |
| 204 | +# but some older versions of CMake don't define it for GCC itself. |
| 205 | +check_cxx_compiler_flag("-UTESTFLAG" FLANG_RT_SUPPORTS_UNDEFINE_FLAG) |
| 206 | + |
| 207 | +# Check whether -fno-lto is supported. |
| 208 | +check_cxx_compiler_flag(-fno-lto FLANG_RT_HAS_FNO_LTO_FLAG) |
| 209 | + |
| 210 | + |
| 211 | +# function checks |
| 212 | +find_package(Backtrace) |
| 213 | +set(HAVE_BACKTRACE ${Backtrace_FOUND}) |
| 214 | +set(BACKTRACE_HEADER ${Backtrace_HEADER}) |
| 215 | + |
| 216 | + |
| 217 | +##################### |
| 218 | +# Build Preparation # |
| 219 | +##################### |
| 220 | + |
| 221 | +if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT AND FLANG_RT_INCLUDE_TESTS) |
| 222 | + # If Fortran runtime is built as CUDA library, the linking |
| 223 | + # of targets that link flang-rt must be done |
| 224 | + # with CUDA_RESOLVE_DEVICE_SYMBOLS. |
| 225 | + # CUDA language must be enabled for CUDA_RESOLVE_DEVICE_SYMBOLS |
| 226 | + # to take effect. |
| 227 | + enable_language(CUDA) |
| 228 | +endif() |
| 229 | + |
| 230 | + |
| 231 | +# C++17 is required for flang-rt; user or other runtimes may override this. |
| 232 | +# GTest included later also requires C++17. |
| 233 | +set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to") |
| 234 | +set(CMAKE_CXX_STANDARD_REQUIRED YES) |
| 235 | + |
| 236 | + |
| 237 | +configure_file(cmake/config.h.cmake.in config.h) |
| 238 | + |
| 239 | + |
| 240 | +# The bootstrap build will create a phony target with the same as the top-level |
| 241 | +# directory ("flang-rt") and delegate it to the runtimes build dir. |
| 242 | +# AddFlangRT will add all non-EXCLUDE_FROM_ALL targets to it. |
| 243 | +add_custom_target(flang-rt) |
| 244 | + |
| 245 | + |
| 246 | +################### |
| 247 | +# Build Artifacts # |
| 248 | +################### |
| 249 | + |
| 250 | +add_subdirectory(lib) |
| 251 | + |
| 252 | +if (LLVM_INCLUDE_EXAMPLES) |
| 253 | + add_subdirectory(examples) |
| 254 | +endif () |
| 255 | + |
| 256 | +if (FLANG_RT_INCLUDE_TESTS) |
| 257 | + add_subdirectory(unittests) |
| 258 | + add_subdirectory(test) |
| 259 | +else () |
| 260 | + add_custom_target(check-flang-rt) |
| 261 | +endif() |
0 commit comments