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
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})
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})
41 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
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}")
48 function(runtime_register_component name)
49 set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
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})
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)
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 # This variable is used by individual runtimes to locate LLVM files.
79 set(LLVM_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
81 include(CheckLibraryExists)
82 include(LLVMCheckCompilerLinkerFlag)
83 include(CheckCCompilerFlag)
84 include(CheckCXXCompilerFlag)
86 # CMake omits default compiler include paths, but in runtimes build, we use
87 # -nostdinc and -nostdinc++ and control include paths manually so this behavior
88 # is undesirable. Filtering CMAKE_{LANG}_IMPLICIT_INCLUDE_DIRECTORIES to remove
89 # paths that are inside the build directory disables this behavior.
91 # See https://gitlab.kitware.com/cmake/cmake/-/issues/19227 for further details.
93 function(filter_prefixed list prefix outvar)
95 string(FIND "${str}" "${prefix}" out)
96 if(NOT "${out}" EQUAL 0)
97 list(APPEND result ${str})
100 set(${outvar} ${result} PARENT_SCOPE)
103 filter_prefixed("${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
104 filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
105 filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES)
107 # The compiler driver may be implicitly trying to link against libunwind,
108 # which might not work if libunwind doesn't exist yet. Try to check if
109 # --unwindlib=none is supported, and use that if possible.
111 # TODO: Note that this is problematic when LLVM_USE_SANITIZER is used
112 # because some sanitizers require the unwinder and so the combination of
113 # -fsanitize=... --unwindlib=none will always result in a linking error.
114 # Currently, we counteract this issue by adding -fno-sanitize=all flag in
115 # the project specific code within */cmake/config-ix.cmake files but that's
116 # brittle. We should ideally move this to runtimes/CMakeLists.txt.
117 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
118 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
119 set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
120 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
121 # TODO: When we can require CMake 3.14, we should use
122 # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
123 # When using CMAKE_REQUIRED_FLAGS, this option gets added both to
124 # compilation and linking commands. That causes warnings in the
125 # compilation commands during cmake tests. This is normally benign, but
126 # when testing whether -Werror works, that test fails (due to the
127 # preexisting warning).
129 # Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
130 # can use --start-no-unused-arguments to silence the warnings about
131 # --unwindlib=none during compilation.
133 # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
134 # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
136 check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
137 if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
138 set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
142 # Disable use of the installed C++ standard library when building runtimes.
143 # Check for -nostdlib++ first; if there's no C++ standard library yet,
144 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
145 # (or -nodefaultlibs).
146 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
147 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
148 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
150 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
151 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
152 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
155 # Avoid checking whether the compiler is working.
156 set(LLVM_COMPILER_CHECKED ON)
158 # This can be used to detect whether we're targeting a GPU architecture.
159 if("${LLVM_RUNTIMES_TARGET}" MATCHES "^amdgcn" OR
160 "${LLVM_RUNTIMES_TARGET}" MATCHES "^nvptx64")
161 set(LLVM_RUNTIMES_GPU_BUILD ON)
164 # Handle common options used by all runtimes.
166 include(HandleLLVMOptions)
168 # Loot at the PATH first to avoid a version mismatch between the command-line
169 # python and the CMake-found version
170 set(Python3_FIND_REGISTRY LAST)
171 find_package(Python3 REQUIRED COMPONENTS Interpreter)
173 # Host triple is used by tests to check if they are running natively.
174 include(GetHostTriple)
175 get_host_triple(LLVM_HOST_TRIPLE)
176 message(STATUS "LLVM host triple: ${LLVM_HOST_TRIPLE}")
178 # TODO: We shouldn't be using LLVM_DEFAULT_TARGET_TRIPLE for runtimes since we
179 # aren't generating code, LLVM_TARGET_TRIPLE is a better fit.
180 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
181 "Default target for which the runtimes will be built.")
182 message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
184 set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
186 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
187 set(option_prefix "")
188 if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
189 set(option_prefix "/clang:")
191 set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${LLVM_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
192 execute_process(COMMAND ${print_target_triple}
193 RESULT_VARIABLE result
194 OUTPUT_VARIABLE output
195 OUTPUT_STRIP_TRAILING_WHITESPACE)
197 set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
199 string(REPLACE ";" " " print_target_triple "${print_target_triple}")
200 # TODO(#97876): Report an error.
201 message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
205 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
206 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
207 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
209 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
210 if(CMAKE_HOST_APPLE AND APPLE)
214 # This can be used to detect whether we're in the runtimes build.
215 set(LLVM_RUNTIMES_BUILD ON)
217 foreach(entry ${runtimes})
218 get_filename_component(projName ${entry} NAME)
220 # TODO: Clean this up as part of an interface standardization
221 string(REPLACE "-" "_" canon_name ${projName})
222 string(TOUPPER ${canon_name} canon_name)
224 # TODO: compiler-rt has to use standalone build for now. We tried to remove
225 # this in D57992 but this broke the build because compiler-rt assumes that
226 # LLVM and Clang are configured in the same build to set up dependencies. We
227 # should clean up the compiler-rt build and remove this eventually.
228 if ("${canon_name}" STREQUAL "COMPILER_RT")
229 set(${canon_name}_STANDALONE_BUILD ON)
232 if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
233 set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
236 # Setting a variable to let sub-projects detect which other projects
237 # will be included under here.
238 set(HAVE_${canon_name} ON)
241 if(LLVM_INCLUDE_TESTS)
242 set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
244 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
246 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
248 umbrella_lit_testsuite_begin(check-runtimes)
251 # We do this in two loops so that HAVE_* is set for each runtime before the
252 # other runtimes are added.
253 foreach(entry ${runtimes})
254 get_filename_component(projName ${entry} NAME)
256 add_subdirectory(${entry} ${projName})
259 # Define runtimes-test-depends so the parent build can use it unconditionally.
260 add_custom_target(runtimes-test-depends)
262 if(LLVM_INCLUDE_TESTS)
263 # LLVM_RUNTIMES_LIT_DEPENDS is populated when lit tests are added between
264 # umbrella_list_testsuite begin and end. The bootstrap runtimes builds
265 # currently assumes this target exists.
266 get_property(LLVM_RUNTIMES_LIT_DEPENDS GLOBAL PROPERTY LLVM_RUNTIMES_LIT_DEPENDS)
267 if(LLVM_RUNTIMES_LIT_DEPENDS)
268 # add_dependencies complains if called with no dependencies
269 add_dependencies(runtimes-test-depends ${LLVM_RUNTIMES_LIT_DEPENDS})
271 # Add a global check rule now that all subdirectories have been traversed
272 # and we know the total set of lit testsuites.
273 umbrella_lit_testsuite_end(check-runtimes)
275 if (NOT HAVE_LLVM_LIT)
276 # If built by manually invoking cmake on this directory, we don't have
277 # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
278 # invocation already generated the llvm-lit script.
279 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
280 ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
283 get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
284 string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
285 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
287 # Create empty files so the parent build can use these unconditionally.
288 file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/lit.tests)
291 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
293 list(REMOVE_DUPLICATES SUB_COMPONENTS)
294 foreach(component ${SUB_COMPONENTS})
295 if(NOT TARGET ${component})
296 message(SEND_ERROR "Missing target for runtime component ${component}!")
300 if(TARGET check-${component})
301 list(APPEND SUB_CHECK_TARGETS check-${component})
304 if(TARGET install-${component})
305 list(APPEND SUB_INSTALL_TARGETS install-${component})
309 if(LLVM_RUNTIMES_TARGET)
311 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
312 ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
315 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
316 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)