1 #===-- runtime/CMakeLists.txt ----------------------------------------------===#
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
7 #===------------------------------------------------------------------------===#
9 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
10 cmake_minimum_required(VERSION 3.20.0)
12 project(FlangRuntime C CXX)
14 set(CMAKE_CXX_STANDARD 17)
15 set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
16 set(CMAKE_CXX_EXTENSIONS OFF)
18 set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
20 set(LLVM_COMMON_CMAKE_UTILS "${FLANG_SOURCE_DIR}/../cmake")
21 set(LLVM_CMAKE_UTILS "${FLANG_SOURCE_DIR}/../llvm/cmake")
22 set(CLANG_CMAKE_UTILS "${FLANG_SOURCE_DIR}/../clang/cmake")
24 # Add path for custom modules
25 list(INSERT CMAKE_MODULE_PATH 0
26 "${FLANG_SOURCE_DIR}/cmake"
27 "${FLANG_SOURCE_DIR}/cmake/modules"
28 "${LLVM_COMMON_CMAKE_UTILS}"
29 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
31 "${LLVM_CMAKE_UTILS}/modules"
32 "${CLANG_CMAKE_UTILS}/modules"
38 include(HandleLLVMOptions)
40 include(TestBigEndian)
41 test_big_endian(IS_BIGENDIAN)
43 add_compile_definitions(FLANG_BIG_ENDIAN=1)
45 add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
47 include_directories(BEFORE
48 ${FLANG_SOURCE_DIR}/include)
51 include(CheckCXXSymbolExists)
52 include(CheckCXXSourceCompiles)
53 check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
54 # Can't use symbol exists here as the function is overloaded in C++
55 check_cxx_source_compiles(
59 return strerror_s(buf, 4096, 0);
64 check_cxx_compiler_flag(-fno-lto FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
65 if (FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
66 set(NO_LTO_FLAGS "-fno-lto")
71 configure_file(config.h.cmake config.h)
72 # include_directories is used here instead of target_include_directories
73 # because add_flang_library creates multiple objects (STATIC/SHARED, OBJECT)
74 # with different names
75 include_directories(AFTER ${CMAKE_CURRENT_BINARY_DIR})
77 append(${NO_LTO_FLAGS} CMAKE_C_FLAGS)
78 append(${NO_LTO_FLAGS} CMAKE_CXX_FLAGS)
80 # Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS build,
81 # to avoid an unwanted dependency on libstdc++/libc++.so.
82 add_definitions(-U_GLIBCXX_ASSERTIONS)
83 add_definitions(-U_LIBCPP_ENABLE_ASSERTIONS)
85 add_subdirectory(FortranMain)
88 ISO_Fortran_binding.cpp
147 option(FLANG_EXPERIMENTAL_CUDA_RUNTIME
148 "Compile Fortran runtime as CUDA sources (experimental)" OFF
151 # List of files that are buildable for all devices.
153 ISO_Fortran_binding.cpp
165 if (FLANG_EXPERIMENTAL_CUDA_RUNTIME)
166 if (BUILD_SHARED_LIBS)
168 "BUILD_SHARED_LIBS is not supported for CUDA build of Fortran runtime"
172 enable_language(CUDA)
174 # TODO: figure out how to make target property CUDA_SEPARABLE_COMPILATION
175 # work, and avoid setting CMAKE_CUDA_SEPARABLE_COMPILATION.
176 set(CMAKE_CUDA_SEPARABLE_COMPILATION ON)
178 # Treat all supported sources as CUDA files.
179 set_source_files_properties(${supported_files} PROPERTIES LANGUAGE CUDA)
180 set(CUDA_COMPILE_OPTIONS)
181 if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "Clang")
183 set(CUDA_COMPILE_OPTIONS
184 -Xclang -fcuda-allow-variadic-functions
187 if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA")
188 set(CUDA_COMPILE_OPTIONS
189 --expt-relaxed-constexpr
192 set_source_files_properties(${supported_files} PROPERTIES COMPILE_OPTIONS
193 "${CUDA_COMPILE_OPTIONS}"
197 set(FLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD "off" CACHE STRING
198 "Compile Fortran runtime as OpenMP target offload sources (experimental). Valid options are 'off', 'host_device', 'nohost'")
200 set(FLANG_OMP_DEVICE_ARCHITECTURES "all" CACHE STRING
201 "List of OpenMP device architectures to be used to compile the Fortran runtime (e.g. 'gfx1103;sm_90')")
203 if (NOT FLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD STREQUAL "off")
204 # 'host_device' build only works with Clang compiler currently.
205 # The build is done with the CMAKE_C/CXX_COMPILER, i.e. it does not use
206 # the in-tree built Clang. We may have a mode that would use the in-tree
209 # 'nohost' is supposed to produce an LLVM Bitcode library,
210 # and it has to be done with a C/C++ compiler producing LLVM Bitcode
211 # compatible with the LLVM toolchain version distributed with the Flang
213 # In general, the in-tree built Clang should be used for 'nohost' build.
214 # Note that 'nohost' build does not produce the host version of Flang
215 # runtime library, so there will be two separate distributable objects.
216 # 'nohost' build is a TODO.
218 if (NOT FLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD STREQUAL "host_device")
219 message(FATAL_ERROR "Unsupported OpenMP offload build of Flang runtime")
221 if (BUILD_SHARED_LIBS)
223 "BUILD_SHARED_LIBS is not supported for OpenMP offload build of Fortran runtime"
227 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND
228 "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
230 set(all_amdgpu_architectures
231 "gfx700;gfx701;gfx801;gfx803;gfx900;gfx902;gfx906"
232 "gfx908;gfx90a;gfx90c;gfx940;gfx1010;gfx1030"
233 "gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036"
234 "gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151"
236 set(all_nvptx_architectures
237 "sm_35;sm_37;sm_50;sm_52;sm_53;sm_60;sm_61;sm_62"
238 "sm_70;sm_72;sm_75;sm_80;sm_86;sm_89;sm_90"
240 set(all_gpu_architectures
241 "${all_amdgpu_architectures};${all_nvptx_architectures}"
243 # TODO: support auto detection on the build system.
244 if (FLANG_OMP_DEVICE_ARCHITECTURES STREQUAL "all")
245 set(FLANG_OMP_DEVICE_ARCHITECTURES ${all_gpu_architectures})
247 list(REMOVE_DUPLICATES FLANG_OMP_DEVICE_ARCHITECTURES)
249 string(REPLACE ";" "," compile_for_architectures
250 "${FLANG_OMP_DEVICE_ARCHITECTURES}"
253 set(OMP_COMPILE_OPTIONS
257 --offload-arch=${compile_for_architectures}
258 # Force LTO for the device part.
261 set_source_files_properties(${supported_files} PROPERTIES COMPILE_OPTIONS
262 "${OMP_COMPILE_OPTIONS}"
265 # Enable "declare target" in the source code.
266 set_source_files_properties(${supported_files}
267 PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD
271 "Flang runtime build is not supported for these compilers:\n"
272 "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}\n"
273 "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
277 add_flang_library(FortranRuntime
282 INSTALL_WITH_TOOLCHAIN