[AArch64,ELF] Restrict MOVZ/MOVK to non-PIC large code model (#70178)
[llvm-project.git] / flang / runtime / CMakeLists.txt
blobf75daa373705f0024308ae7face98fd524e33149
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"
30     "${LLVM_CMAKE_UTILS}"
31     "${LLVM_CMAKE_UTILS}/modules"
32     "${CLANG_CMAKE_UTILS}/modules"
33     )
35   include(AddClang)
36   include(AddLLVM)
37   include(AddFlang)
38   include(HandleLLVMOptions)
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 endif()
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(
56   "#include <string.h>
57    int main() {
58      char buf[4096];
59      return strerror_s(buf, 4096, 0);
60    }
61   "
62   HAVE_DECL_STRERROR_S)
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")
67 else()
68   set(NO_LTO_FLAGS "")
69 endif()
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)
87 set(sources
88   ISO_Fortran_binding.cpp
89   allocatable.cpp
90   array-constructor.cpp
91   assign.cpp
92   buffer.cpp
93   command.cpp
94   complex-powi.cpp
95   complex-reduction.c
96   copy.cpp
97   character.cpp
98   connection.cpp
99   derived.cpp
100   derived-api.cpp
101   descriptor.cpp
102   descriptor-io.cpp
103   dot-product.cpp
104   edit-input.cpp
105   edit-output.cpp
106   environment.cpp
107   extensions.cpp
108   extrema.cpp
109   file.cpp
110   findloc.cpp
111   format.cpp
112   inquiry.cpp
113   internal-unit.cpp
114   iostat.cpp
115   io-api.cpp
116   io-error.cpp
117   io-stmt.cpp
118   main.cpp
119   matmul-transpose.cpp
120   matmul.cpp
121   memory.cpp
122   misc-intrinsic.cpp
123   namelist.cpp
124   non-tbp-dio.cpp
125   numeric.cpp
126   ragged.cpp
127   random.cpp
128   reduction.cpp
129   pointer.cpp
130   product.cpp
131   stat.cpp
132   stop.cpp
133   sum.cpp
134   support.cpp
135   temporary-stack.cpp
136   terminator.cpp
137   time-intrinsic.cpp
138   tools.cpp
139   transformational.cpp
140   type-code.cpp
141   type-info.cpp
142   unit.cpp
143   unit-map.cpp
144   utf.cpp
147 option(FLANG_EXPERIMENTAL_CUDA_RUNTIME
148   "Compile Fortran runtime as CUDA sources (experimental)" OFF
149   )
151 # List of files that are buildable for all devices.
152 set(supported_files
153   ISO_Fortran_binding.cpp
154   assign.cpp
155   derived.cpp
156   descriptor.cpp
157   stat.cpp
158   terminator.cpp
159   tools.cpp
160   transformational.cpp
161   type-code.cpp
162   type-info.cpp
163   )
165 if (FLANG_EXPERIMENTAL_CUDA_RUNTIME)
166   if (BUILD_SHARED_LIBS)
167     message(FATAL_ERROR
168       "BUILD_SHARED_LIBS is not supported for CUDA build of Fortran runtime"
169       )
170   endif()
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")
182     # Allow varargs.
183     set(CUDA_COMPILE_OPTIONS
184       -Xclang -fcuda-allow-variadic-functions
185       )
186   endif()
187   if ("${CMAKE_CUDA_COMPILER_ID}" MATCHES "NVIDIA")
188     set(CUDA_COMPILE_OPTIONS
189       --expt-relaxed-constexpr
190       )
191   endif()
192   set_source_files_properties(${supported_files} PROPERTIES COMPILE_OPTIONS
193     "${CUDA_COMPILE_OPTIONS}"
194     )
195 endif()
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
207   # built Clang.
208   #
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
212   # compiler.
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")
220   endif()
221   if (BUILD_SHARED_LIBS)
222     message(FATAL_ERROR
223       "BUILD_SHARED_LIBS is not supported for OpenMP offload build of Fortran runtime"
224       )
225   endif()
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"
235       )
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"
239       )
240     set(all_gpu_architectures
241       "${all_amdgpu_architectures};${all_nvptx_architectures}"
242       )
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})
246     endif()
247     list(REMOVE_DUPLICATES FLANG_OMP_DEVICE_ARCHITECTURES)
249     string(REPLACE ";" "," compile_for_architectures
250       "${FLANG_OMP_DEVICE_ARCHITECTURES}"
251       )
253     set(OMP_COMPILE_OPTIONS
254       -fopenmp
255       -fvisibility=hidden
256       -fopenmp-cuda-mode
257       --offload-arch=${compile_for_architectures}
258       # Force LTO for the device part.
259       -foffload-lto
260       )
261     set_source_files_properties(${supported_files} PROPERTIES COMPILE_OPTIONS
262       "${OMP_COMPILE_OPTIONS}"
263       )
265     # Enable "declare target" in the source code.
266     set_source_files_properties(${supported_files}
267       PROPERTIES COMPILE_DEFINITIONS OMP_OFFLOAD_BUILD
268       )
269   else()
270     message(FATAL_ERROR
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}")
274   endif()
275 endif()
277 add_flang_library(FortranRuntime
278   ${sources}
279   LINK_LIBS
280   FortranDecimal
282   INSTALL_WITH_TOOLCHAIN