xref: /llvm-project/flang/runtime/CMakeLists.txt (revision e8baa792e7db0b03a842448b5e85623da48a4367)
1#===-- runtime/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
9if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
10  cmake_minimum_required(VERSION 3.20.0)
11
12  project(FlangRuntime C CXX)
13
14  set(CMAKE_CXX_STANDARD 17)
15  set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
16  set(CMAKE_CXX_EXTENSIONS OFF)
17
18  set(FLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/..")
19
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")
23
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"
30    "${LLVM_CMAKE_UTILS}"
31    "${LLVM_CMAKE_UTILS}/modules"
32    "${CLANG_CMAKE_UTILS}/modules"
33    )
34
35  include(AddClang)
36  include(AddLLVM)
37  include(AddFlang)
38  include(HandleLLVMOptions)
39
40  include(TestBigEndian)
41  test_big_endian(IS_BIGENDIAN)
42  if (IS_BIGENDIAN)
43    add_compile_definitions(FLANG_BIG_ENDIAN=1)
44  else ()
45    add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
46  endif ()
47  include_directories(BEFORE
48    ${FLANG_SOURCE_DIR}/include)
49
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."
59    )
60endif()
61
62# function checks
63find_package(Backtrace)
64set(HAVE_BACKTRACE ${Backtrace_FOUND})
65set(BACKTRACE_HEADER ${Backtrace_HEADER})
66
67include(CheckCXXSymbolExists)
68include(CheckCXXSourceCompiles)
69check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R)
70# Can't use symbol exists here as the function is overloaded in C++
71check_cxx_source_compiles(
72  "#include <string.h>
73   int main() {
74     char buf[4096];
75     return strerror_s(buf, 4096, 0);
76   }
77  "
78  HAVE_DECL_STRERROR_S)
79
80# Check if 128-bit float computations can be done via long double.
81check_cxx_source_compiles(
82  "#include <cfloat>
83   #if LDBL_MANT_DIG != 113
84   #error LDBL_MANT_DIG != 113
85   #endif
86   int main() { return 0; }
87  "
88  HAVE_LDBL_MANT_DIG_113)
89
90check_cxx_compiler_flag(-fno-lto FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
91if (FLANG_RUNTIME_HAS_FNO_LTO_FLAG)
92  set(NO_LTO_FLAGS "-fno-lto")
93else()
94  set(NO_LTO_FLAGS "")
95endif()
96
97configure_file(config.h.cmake config.h)
98# include_directories is used here instead of target_include_directories
99# because add_flang_library creates multiple objects (STATIC/SHARED, OBJECT)
100# with different names
101include_directories(AFTER ${CMAKE_CURRENT_BINARY_DIR})
102
103append(${NO_LTO_FLAGS} CMAKE_C_FLAGS)
104append(${NO_LTO_FLAGS} CMAKE_CXX_FLAGS)
105
106# Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS build,
107# to avoid an unwanted dependency on libstdc++/libc++.so.
108add_definitions(-U_GLIBCXX_ASSERTIONS)
109add_definitions(-U_LIBCPP_ENABLE_ASSERTIONS)
110
111add_subdirectory(Float128Math)
112
113set(sources
114  ISO_Fortran_binding.cpp
115  allocator-registry.cpp
116  allocatable.cpp
117  array-constructor.cpp
118  assign.cpp
119  buffer.cpp
120  character.cpp
121  command.cpp
122  complex-powi.cpp
123  complex-reduction.c
124  connection.cpp
125  copy.cpp
126  derived-api.cpp
127  derived.cpp
128  descriptor-io.cpp
129  descriptor.cpp
130  dot-product.cpp
131  edit-input.cpp
132  edit-output.cpp
133  environment.cpp
134  exceptions.cpp
135  execute.cpp
136  extensions.cpp
137  external-unit.cpp
138  extrema.cpp
139  file.cpp
140  findloc.cpp
141  format.cpp
142  inquiry.cpp
143  internal-unit.cpp
144  io-api.cpp
145  io-api-minimal.cpp
146  io-error.cpp
147  io-stmt.cpp
148  iostat.cpp
149  main.cpp
150  matmul-transpose.cpp
151  matmul.cpp
152  memory.cpp
153  misc-intrinsic.cpp
154  namelist.cpp
155  non-tbp-dio.cpp
156  numeric.cpp
157  pointer.cpp
158  product.cpp
159  pseudo-unit.cpp
160  ragged.cpp
161  random.cpp
162  reduce.cpp
163  reduction.cpp
164  stat.cpp
165  stop.cpp
166  sum.cpp
167  support.cpp
168  temporary-stack.cpp
169  terminator.cpp
170  time-intrinsic.cpp
171  tools.cpp
172  transformational.cpp
173  type-code.cpp
174  type-info.cpp
175  unit-map.cpp
176  unit.cpp
177  utf.cpp
178  ${FORTRAN_MODULE_OBJECTS}
179)
180
181include(AddFlangOffloadRuntime)
182
183# List of files that are buildable for all devices.
184set(supported_files
185  ISO_Fortran_binding.cpp
186  allocatable.cpp
187  allocator-registry.cpp
188  array-constructor.cpp
189  assign.cpp
190  buffer.cpp
191  character.cpp
192  connection.cpp
193  copy.cpp
194  derived-api.cpp
195  derived.cpp
196  descriptor.cpp
197  descriptor-io.cpp
198  dot-product.cpp
199  edit-input.cpp
200  edit-output.cpp
201  environment.cpp
202  extrema.cpp
203  external-unit.cpp
204  file.cpp
205  findloc.cpp
206  format.cpp
207  inquiry.cpp
208  internal-unit.cpp
209  io-api.cpp
210  io-api-minimal.cpp
211  io-error.cpp
212  io-stmt.cpp
213  iostat.cpp
214  matmul-transpose.cpp
215  matmul.cpp
216  memory.cpp
217  misc-intrinsic.cpp
218  namelist.cpp
219  non-tbp-dio.cpp
220  numeric.cpp
221  pointer.cpp
222  product.cpp
223  pseudo-unit.cpp
224  ragged.cpp
225  stat.cpp
226  sum.cpp
227  support.cpp
228  terminator.cpp
229  tools.cpp
230  transformational.cpp
231  type-code.cpp
232  type-info.cpp
233  unit.cpp
234  utf.cpp
235  )
236
237enable_cuda_compilation(FortranRuntime "${supported_files}")
238enable_omp_offload_compilation("${supported_files}")
239
240if (NOT TARGET FortranFloat128Math)
241  # If FortranFloat128Math is not defined, then we are not building
242  # standalone FortranFloat128Math library. Instead, include
243  # the relevant sources into FortranRuntime itself.
244  # The information is provided via FortranFloat128MathILib
245  # interface library.
246  get_target_property(f128_sources
247    FortranFloat128MathILib INTERFACE_SOURCES
248    )
249  if (f128_sources)
250    # The interface may define special macros for Float128Math files,
251    # so we need to propagate them.
252    get_target_property(f128_defs
253      FortranFloat128MathILib INTERFACE_COMPILE_DEFINITIONS
254      )
255    set_property(SOURCE ${f128_sources}
256      APPEND PROPERTY COMPILE_DEFINITIONS
257      ${f128_defs}
258      )
259    get_target_property(f128_include_dirs
260      FortranFloat128MathILib INTERFACE_INCLUDE_DIRECTORIES
261      )
262    set_property(SOURCE ${f128_sources}
263      APPEND PROPERTY INCLUDE_DIRECTORIES
264      ${f128_include_dirs}
265      )
266    list(APPEND sources ${f128_sources})
267  endif()
268endif()
269
270if (NOT DEFINED MSVC)
271  add_flang_library(FortranRuntime
272    ${sources}
273    LINK_LIBS
274    FortranDecimal
275
276    INSTALL_WITH_TOOLCHAIN
277  )
278else()
279  add_flang_library(FortranRuntime
280    ${sources}
281    LINK_LIBS
282    FortranDecimal
283  )
284  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
285  add_flang_library(FortranRuntime.static ${sources}
286    LINK_LIBS
287    FortranDecimal.static
288    INSTALL_WITH_TOOLCHAIN)
289  set_target_properties(FortranRuntime.static PROPERTIES FOLDER "Flang/Runtime Libraries")
290  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
291  add_flang_library(FortranRuntime.dynamic ${sources}
292    LINK_LIBS
293    FortranDecimal.dynamic
294    INSTALL_WITH_TOOLCHAIN)
295  set_target_properties(FortranRuntime.dynamic PROPERTIES FOLDER "Flang/Runtime Libraries")
296  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
297  add_flang_library(FortranRuntime.static_dbg ${sources}
298    LINK_LIBS
299    FortranDecimal.static_dbg
300    INSTALL_WITH_TOOLCHAIN)
301  set_target_properties(FortranRuntime.static_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
302  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
303  add_flang_library(FortranRuntime.dynamic_dbg ${sources}
304    LINK_LIBS
305    FortranDecimal.dynamic_dbg
306    INSTALL_WITH_TOOLCHAIN)
307  set_target_properties(FortranRuntime.dynamic_dbg PROPERTIES FOLDER "Flang/Runtime Libraries")
308  add_dependencies(FortranRuntime FortranRuntime.static FortranRuntime.dynamic
309    FortranRuntime.static_dbg FortranRuntime.dynamic_dbg)
310endif()
311set_target_properties(FortranRuntime PROPERTIES FOLDER "Flang/Runtime Libraries")
312
313# If FortranRuntime is part of a Flang build (and not a separate build) then
314# add dependency to make sure that Fortran runtime library is being built after
315# we have the Flang compiler available.  This also includes the MODULE files
316# that compile when the 'flang' target is built.
317#
318# TODO: This is a workaround and should be updated when runtime build procedure
319# is changed to a regular runtime build.  See discussion in PR #95388.
320if (TARGET flang AND TARGET module_files)
321  add_dependencies(FortranRuntime flang module_files)
322endif()
323
324if (FLANG_CUF_RUNTIME)
325  add_subdirectory(CUDA)
326endif()
327