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)
50 # The out of tree builds of the compiler and the Fortran runtime
51 # must use the same setting of FLANG_RUNTIME_F128_MATH_LIB
52 # to be composable. Failure to synchronize this setting may result
53 # in linking errors or fatal failures in F128 runtime functions.
54 set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING
55 "Specifies the target library used for implementing IEEE-754 128-bit float \
56 math in F18 runtime, e.g. it might be libquadmath for targets where \
57 REAL(16) is mapped to __float128, or libm for targets where REAL(16) \
58 is mapped to long double, etc."
62 include(CheckCXXSymbolExists)
63 include(CheckCXXSourceCompiles)
64 check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
65 # Can't use symbol exists here as the function is overloaded in C++
66 check_cxx_source_compiles(
70 return strerror_s(buf, 4096, 0);
75 # Check if 128-bit float computations can be done via long double.
76 check_cxx_source_compiles(
78 #if LDBL_MANT_DIG != 113
79 #error LDBL_MANT_DIG != 113
81 int main() { return 0; }
83 HAVE_LDBL_MANT_DIG_113)
85 check_cxx_compiler_flag(-fno-lto FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
86 if (FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
87 set(NO_LTO_FLAGS "-fno-lto")
92 configure_file(config.h.cmake config.h)
93 # include_directories is used here instead of target_include_directories
94 # because add_flang_library creates multiple objects (STATIC/SHARED, OBJECT)
95 # with different names
96 include_directories(AFTER ${CMAKE_CURRENT_BINARY_DIR})
98 append(${NO_LTO_FLAGS} CMAKE_C_FLAGS)
99 append(${NO_LTO_FLAGS} CMAKE_CXX_FLAGS)
101 # Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS build,
102 # to avoid an unwanted dependency on libstdc++/libc++.so.
103 add_definitions(-U_GLIBCXX_ASSERTIONS)
104 add_definitions(-U_LIBCPP_ENABLE_ASSERTIONS)
106 add_subdirectory(Float128Math)
109 ISO_Fortran_binding.cpp
110 allocator-registry.cpp
112 array-constructor.cpp
173 ${FORTRAN_MODULE_OBJECTS}
176 include(AddFlangOffloadRuntime)
178 # List of files that are buildable for all devices.
180 ISO_Fortran_binding.cpp
182 allocator-registry.cpp
183 array-constructor.cpp
232 enable_cuda_compilation(FortranRuntime "${supported_files}")
233 enable_omp_offload_compilation("${supported_files}")
235 if (NOT TARGET FortranFloat128Math)
236 # If FortranFloat128Math is not defined, then we are not building
237 # standalone FortranFloat128Math library. Instead, include
238 # the relevant sources into FortranRuntime itself.
239 # The information is provided via FortranFloat128MathILib
241 get_target_property(f128_sources
242 FortranFloat128MathILib INTERFACE_SOURCES
245 # The interface may define special macros for Float128Math files,
246 # so we need to propagate them.
247 get_target_property(f128_defs
248 FortranFloat128MathILib INTERFACE_COMPILE_DEFINITIONS
250 set_property(SOURCE ${f128_sources}
251 APPEND PROPERTY COMPILE_DEFINITIONS
254 get_target_property(f128_include_dirs
255 FortranFloat128MathILib INTERFACE_INCLUDE_DIRECTORIES
257 set_property(SOURCE ${f128_sources}
258 APPEND PROPERTY INCLUDE_DIRECTORIES
261 list(APPEND sources ${f128_sources})
265 if (NOT DEFINED MSVC)
266 add_flang_library(FortranRuntime
271 INSTALL_WITH_TOOLCHAIN
274 add_flang_library(FortranRuntime
279 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
280 add_flang_library(FortranRuntime.static ${sources}
282 FortranDecimal.static
283 INSTALL_WITH_TOOLCHAIN)
284 set_target_properties(FortranRuntime.static PROPERTIES FOLDER "Flang/Runtime Libraries")
285 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
286 add_flang_library(FortranRuntime.dynamic ${sources}
288 FortranDecimal.dynamic
289 INSTALL_WITH_TOOLCHAIN)
290 set_target_properties(FortranRuntime.dynamic PROPERTIES FOLDER "Flang/Runtime Libraries")
291 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
292 add_flang_library(FortranRuntime.static_dbg ${sources}
294 FortranDecimal.static_dbg
295 INSTALL_WITH_TOOLCHAIN)
296 set_target_properties(FortranRuntime.static_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
297 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
298 add_flang_library(FortranRuntime.dynamic_dbg ${sources}
300 FortranDecimal.dynamic_dbg
301 INSTALL_WITH_TOOLCHAIN)
302 set_target_properties(FortranRuntime.dynamic_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
303 add_dependencies(FortranRuntime FortranRuntime.static FortranRuntime.dynamic
304 FortranRuntime.static_dbg FortranRuntime.dynamic_dbg)
306 set_target_properties(FortranRuntime PROPERTIES FOLDER "Flang/Runtime Libraries")
308 # If FortranRuntime is part of a Flang build (and not a separate build) then
309 # add dependency to make sure that Fortran runtime library is being built after
310 # we have the Flang compiler available. This also includes the MODULE files
311 # that compile when the 'flang' target is built.
313 # TODO: This is a workaround and should be updated when runtime build procedure
314 # is changed to a regular runtime build. See discussion in PR #95388.
315 if (TARGET flang AND TARGET module_files)
316 add_dependencies(FortranRuntime flang module_files)
319 if (FLANG_CUF_RUNTIME)
320 add_subdirectory(CUDA)