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
111 array-constructor.cpp
172 ${FORTRAN_MODULE_OBJECTS}
175 include(AddFlangOffloadRuntime)
177 # List of files that are buildable for all devices.
179 ISO_Fortran_binding.cpp
181 array-constructor.cpp
230 enable_cuda_compilation(FortranRuntime "${supported_files}")
231 enable_omp_offload_compilation("${supported_files}")
233 if (NOT TARGET FortranFloat128Math)
234 # If FortranFloat128Math is not defined, then we are not building
235 # standalone FortranFloat128Math library. Instead, include
236 # the relevant sources into FortranRuntime itself.
237 # The information is provided via FortranFloat128MathILib
239 get_target_property(f128_sources
240 FortranFloat128MathILib INTERFACE_SOURCES
243 # The interface may define special macros for Float128Math files,
244 # so we need to propagate them.
245 get_target_property(f128_defs
246 FortranFloat128MathILib INTERFACE_COMPILE_DEFINITIONS
248 set_property(SOURCE ${f128_sources}
249 APPEND PROPERTY COMPILE_DEFINITIONS
252 list(APPEND sources ${f128_sources})
256 if (NOT DEFINED MSVC)
257 add_flang_library(FortranRuntime
262 INSTALL_WITH_TOOLCHAIN
265 add_flang_library(FortranRuntime
270 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
271 add_flang_library(FortranRuntime.static ${sources}
273 FortranDecimal.static
274 INSTALL_WITH_TOOLCHAIN)
275 set_target_properties(FortranRuntime.static PROPERTIES FOLDER "Flang/Runtime Libraries")
276 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
277 add_flang_library(FortranRuntime.dynamic ${sources}
279 FortranDecimal.dynamic
280 INSTALL_WITH_TOOLCHAIN)
281 set_target_properties(FortranRuntime.dynamic PROPERTIES FOLDER "Flang/Runtime Libraries")
282 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
283 add_flang_library(FortranRuntime.static_dbg ${sources}
285 FortranDecimal.static_dbg
286 INSTALL_WITH_TOOLCHAIN)
287 set_target_properties(FortranRuntime.static_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
288 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
289 add_flang_library(FortranRuntime.dynamic_dbg ${sources}
291 FortranDecimal.dynamic_dbg
292 INSTALL_WITH_TOOLCHAIN)
293 set_target_properties(FortranRuntime.dynamic_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
294 add_dependencies(FortranRuntime FortranRuntime.static FortranRuntime.dynamic
295 FortranRuntime.static_dbg FortranRuntime.dynamic_dbg)
297 set_target_properties(FortranRuntime PROPERTIES FOLDER "Flang/Runtime Libraries")
299 # If FortranRuntime is part of a Flang build (and not a separate build) then
300 # add dependency to make sure that Fortran runtime library is being built after
301 # we have the Flang compiler available. This also includes the MODULE files
302 # that compile when the 'flang-new' target is built.
304 # TODO: This is a workaround and should be updated when runtime build procedure
305 # is changed to a regular runtime build. See discussion in PR #95388.
306 if (TARGET flang-new AND TARGET module_files)
307 add_dependencies(FortranRuntime flang-new module_files)