[NFC] Fixed indentation issue (#116942)
[llvm-project.git] / runtimes / CMakeLists.txt
blob832a7d0c19359260462301c25dcd10c76bb0cc2d
1 # This file handles building LLVM runtime sub-projects.
2 cmake_minimum_required(VERSION 3.20.0)
4 # Add path for custom and the LLVM build's modules to the CMake module path.
5 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
6 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
7   NO_POLICY_SCOPE)
9 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/LLVMVersion.cmake)
11 project(Runtimes C CXX ASM)
12 set(LLVM_SUBPROJECT_TITLE "Runtimes")
13 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
15 list(INSERT CMAKE_MODULE_PATH 0
16   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
17   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
18   "${LLVM_COMMON_CMAKE_UTILS}"
19   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
20   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
21   "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
24 # We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
25 # on libc++, so we put it after.
26 set(LLVM_DEFAULT_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;offload")
27 set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc")
28 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
29   "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
30 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
31   set(LLVM_ENABLE_RUNTIMES ${LLVM_DEFAULT_RUNTIMES})
32 endif()
33 include(SortSubset)
34 sort_subset("${LLVM_SUPPORTED_RUNTIMES}" "${LLVM_ENABLE_RUNTIMES}" LLVM_ENABLE_RUNTIMES)
36 foreach(proj ${LLVM_ENABLE_RUNTIMES})
37   set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
38   if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
39     list(APPEND runtimes ${proj_dir})
40   else()
41     message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
42   endif()
43   string(TOUPPER "${proj}" canon_name)
44   STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
45   set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
46 endforeach()
48 function(runtime_register_component name)
49   set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
50 endfunction()
52 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
53 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
55 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
57 # If building standalone by pointing CMake at this runtimes directory,
58 # LLVM_BINARY_DIR isn't set, find_package(LLVM) will fail and these
59 # intermediate paths are unset.
60 if (NOT LLVM_BINARY_DIR)
61   set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
62 endif()
63 if (NOT LLVM_FOUND)
64   set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
65   set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
66   set(LLVM_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
67 endif()
69 # Setting these variables will allow the sub-build to put their outputs into
70 # the library and bin directories of the top-level build.
71 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
72 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
74 # This variable makes sure that e.g. llvm-lit is found.
75 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
76 set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
78 include(CheckLibraryExists)
79 include(LLVMCheckCompilerLinkerFlag)
80 include(CheckCCompilerFlag)
81 include(CheckCXXCompilerFlag)
83 # CMake omits default compiler include paths, but in runtimes build, we use
84 # -nostdinc and -nostdinc++ and control include paths manually so this behavior
85 # is undesirable. Filtering CMAKE_{LANG}_IMPLICIT_INCLUDE_DIRECTORIES to remove
86 # paths that are inside the build directory disables this behavior.
88 # See https://gitlab.kitware.com/cmake/cmake/-/issues/19227 for further details.
90 function(filter_prefixed list prefix outvar)
91   foreach(str ${list})
92     string(FIND "${str}" "${prefix}" out)
93     if(NOT "${out}" EQUAL 0)
94       list(APPEND result ${str})
95     endif()
96   endforeach()
97   set(${outvar} ${result} PARENT_SCOPE)
98 endfunction()
100 filter_prefixed("${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
101 filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
102 filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES)
104 # The compiler driver may be implicitly trying to link against libunwind,
105 # which might not work if libunwind doesn't exist yet. Try to check if
106 # --unwindlib=none is supported, and use that if possible.
108 # TODO: Note that this is problematic when LLVM_USE_SANITIZER is used
109 # because some sanitizers require the unwinder and so the combination of
110 # -fsanitize=... --unwindlib=none will always result in a linking error.
111 # Currently, we counteract this issue by adding -fno-sanitize=all flag in
112 # the project specific code within */cmake/config-ix.cmake files but that's
113 # brittle. We should ideally move this to runtimes/CMakeLists.txt.
114 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
115 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
116   set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
117   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
118   # TODO: When we can require CMake 3.14, we should use
119   # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
120   # When using CMAKE_REQUIRED_FLAGS, this option gets added both to
121   # compilation and linking commands. That causes warnings in the
122   # compilation commands during cmake tests. This is normally benign, but
123   # when testing whether -Werror works, that test fails (due to the
124   # preexisting warning).
125   #
126   # Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
127   # can use --start-no-unused-arguments to silence the warnings about
128   # --unwindlib=none during compilation.
129   #
130   # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
131   # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
132   # below.
133   check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
134   if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
135     set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
136   endif()
137 endif()
139 # Disable use of the installed C++ standard library when building runtimes.
140 # Check for -nostdlib++ first; if there's no C++ standard library yet,
141 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
142 # (or -nodefaultlibs).
143 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
144 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
145   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
146 endif()
147 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
148 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
149   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
150 endif()
152 # Avoid checking whether the compiler is working.
153 set(LLVM_COMPILER_CHECKED ON)
155 # This can be used to detect whether we're targeting a GPU architecture.
156 if("${LLVM_RUNTIMES_TARGET}" MATCHES "^amdgcn" OR
157    "${LLVM_RUNTIMES_TARGET}" MATCHES "^nvptx64")
158   set(LLVM_RUNTIMES_GPU_BUILD ON)
159 endif()
161 # Handle common options used by all runtimes.
162 include(AddLLVM)
163 include(HandleLLVMOptions)
165 # Loot at the PATH first to avoid a version mismatch between the command-line
166 # python and the CMake-found version
167 set(Python3_FIND_REGISTRY LAST)
168 find_package(Python3 REQUIRED COMPONENTS Interpreter)
170 # Host triple is used by tests to check if they are running natively.
171 include(GetHostTriple)
172 get_host_triple(LLVM_HOST_TRIPLE)
173 message(STATUS "LLVM host triple: ${LLVM_HOST_TRIPLE}")
175 # TODO: We shouldn't be using LLVM_DEFAULT_TARGET_TRIPLE for runtimes since we
176 # aren't generating code, LLVM_TARGET_TRIPLE is a better fit.
177 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
178     "Default target for which the runtimes will be built.")
179 message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
181 set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
183 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
184   set(option_prefix "")
185   if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
186     set(option_prefix "/clang:")
187   endif()
188   set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${LLVM_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
189   execute_process(COMMAND ${print_target_triple}
190     RESULT_VARIABLE result
191     OUTPUT_VARIABLE output
192     OUTPUT_STRIP_TRAILING_WHITESPACE)
193   if(result EQUAL 0)
194     set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
195   else()
196     string(REPLACE ";" " " print_target_triple "${print_target_triple}")
197     # TODO(#97876): Report an error.
198     message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
199   endif()
200 endif()
202 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
203 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
204 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
206 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
207 if(CMAKE_HOST_APPLE AND APPLE)
208   include(UseLibtool)
209 endif()
211 # This can be used to detect whether we're in the runtimes build.
212 set(LLVM_RUNTIMES_BUILD ON)
214 foreach(entry ${runtimes})
215   get_filename_component(projName ${entry} NAME)
217   # TODO: Clean this up as part of an interface standardization
218   string(REPLACE "-" "_" canon_name ${projName})
219   string(TOUPPER ${canon_name} canon_name)
221   # TODO: compiler-rt has to use standalone build for now. We tried to remove
222   # this in D57992 but this broke the build because compiler-rt assumes that
223   # LLVM and Clang are configured in the same build to set up dependencies. We
224   # should clean up the compiler-rt build and remove this eventually.
225   if ("${canon_name}" STREQUAL "COMPILER_RT")
226     set(${canon_name}_STANDALONE_BUILD ON)
227   endif()
229   if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
230     set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
231   endif()
233   # Setting a variable to let sub-projects detect which other projects
234   # will be included under here.
235   set(HAVE_${canon_name} ON)
236 endforeach()
238 if(LLVM_INCLUDE_TESTS)
239   set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
240   if (MSVC OR XCODE)
241     set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
242   endif()
243   set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
245   umbrella_lit_testsuite_begin(check-runtimes)
246 endif()
248 # We do this in two loops so that HAVE_* is set for each runtime before the
249 # other runtimes are added.
250 foreach(entry ${runtimes})
251   get_filename_component(projName ${entry} NAME)
253   add_subdirectory(${entry} ${projName})
254 endforeach()
256 # Define runtimes-test-depends so the parent build can use it unconditionally.
257 add_custom_target(runtimes-test-depends)
259 if(LLVM_INCLUDE_TESTS)
260   # LLVM_RUNTIMES_LIT_DEPENDS is populated when lit tests are added between
261   # umbrella_list_testsuite begin and end. The bootstrap runtimes builds
262   # currently assumes this target exists.
263   get_property(LLVM_RUNTIMES_LIT_DEPENDS GLOBAL PROPERTY LLVM_RUNTIMES_LIT_DEPENDS)
264   if(LLVM_RUNTIMES_LIT_DEPENDS)
265     # add_dependencies complains if called with no dependencies
266     add_dependencies(runtimes-test-depends ${LLVM_RUNTIMES_LIT_DEPENDS})
267   endif()
268   # Add a global check rule now that all subdirectories have been traversed
269   # and we know the total set of lit testsuites.
270   umbrella_lit_testsuite_end(check-runtimes)
272   if (NOT HAVE_LLVM_LIT)
273     # If built by manually invoking cmake on this directory, we don't have
274     # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
275     # invocation already generated the llvm-lit script.
276     add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
277                      ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
278   endif()
280   get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
281   string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
282   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
283 else()
284   # Create empty files so the parent build can use these unconditionally.
285   file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/lit.tests)
286 endif()
288 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
289 if(SUB_COMPONENTS)
290   list(REMOVE_DUPLICATES SUB_COMPONENTS)
291   foreach(component ${SUB_COMPONENTS})
292     if(NOT TARGET ${component})
293       message(SEND_ERROR "Missing target for runtime component ${component}!")
294       continue()
295     endif()
297     if(TARGET check-${component})
298       list(APPEND SUB_CHECK_TARGETS check-${component})
299     endif()
301     if(TARGET install-${component})
302       list(APPEND SUB_INSTALL_TARGETS install-${component})
303     endif()
304   endforeach()
306   if(LLVM_RUNTIMES_TARGET)
307     configure_file(
308       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
309       ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
310   else()
311     configure_file(
312       ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
313       ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
314   endif()
315 endif()