1 # This file handles building LLVM runtime sub-projects.
2 cmake_minimum_required(VERSION 3.20.0)
4 # This file can be used in two ways: the bootstrapping build calls it from
5 # llvm/runtimes/CMakeLists.txt where we reuse the build tree of the top-level
6 # build or it can be directly invoked in this directory. In the latter case we
7 # might be building against a LLVM install tree and might not have a valid build
8 # tree set up yet. We can detect whether we are using the bootstrapping build
9 # by checking for the HAVE_LLVM_LIT flag that is passed explicitly to
10 # llvm_ExternalProject_Add().
12 message(STATUS "Performing bootstrapping runtimes build.")
14 message(STATUS "Performing standalone runtimes build.")
16 # Add path for custom and the LLVM build's modules to the CMake module path.
17 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
18 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
21 include(${LLVM_COMMON_CMAKE_UTILS}/Modules/LLVMVersion.cmake)
23 project(Runtimes C CXX ASM)
24 set(LLVM_SUBPROJECT_TITLE "Runtimes")
25 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
27 list(INSERT CMAKE_MODULE_PATH 0
28 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
29 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
30 "${LLVM_COMMON_CMAKE_UTILS}"
31 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
32 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
33 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
36 # We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
37 # on libc++, so we put it after.
38 set(LLVM_DEFAULT_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;offload")
39 set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc")
40 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
41 "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
42 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
43 set(LLVM_ENABLE_RUNTIMES ${LLVM_DEFAULT_RUNTIMES})
46 sort_subset("${LLVM_SUPPORTED_RUNTIMES}" "${LLVM_ENABLE_RUNTIMES}" LLVM_ENABLE_RUNTIMES)
48 foreach(proj ${LLVM_ENABLE_RUNTIMES})
49 set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
50 if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
51 list(APPEND runtimes ${proj_dir})
53 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
55 string(TOUPPER "${proj}" canon_name)
56 STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
57 set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
60 function(runtime_register_component name)
61 set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
64 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
65 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
67 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
69 # If building standalone by pointing CMake at this runtimes directory,
70 # LLVM_BINARY_DIR isn't set, find_package(LLVM) will fail and these
71 # intermediate paths are unset.
72 if (NOT LLVM_BINARY_DIR)
73 set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
76 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
77 set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
78 set(LLVM_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
81 # Setting these variables will allow the sub-build to put their outputs into
82 # the library and bin directories of the top-level build.
83 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
84 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
86 # This variable makes sure that e.g. llvm-lit is found.
87 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
88 set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
90 include(CheckLibraryExists)
91 include(LLVMCheckCompilerLinkerFlag)
92 include(CheckCCompilerFlag)
93 include(CheckCXXCompilerFlag)
95 # CMake omits default compiler include paths, but in runtimes build, we use
96 # -nostdinc and -nostdinc++ and control include paths manually so this behavior
97 # is undesirable. Filtering CMAKE_{LANG}_IMPLICIT_INCLUDE_DIRECTORIES to remove
98 # paths that are inside the build directory disables this behavior.
100 # See https://gitlab.kitware.com/cmake/cmake/-/issues/19227 for further details.
102 function(filter_prefixed list prefix outvar)
104 string(FIND "${str}" "${prefix}" out)
105 if(NOT "${out}" EQUAL 0)
106 list(APPEND result ${str})
109 set(${outvar} ${result} PARENT_SCOPE)
112 filter_prefixed("${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
113 filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
114 filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES)
116 # The compiler driver may be implicitly trying to link against libunwind,
117 # which might not work if libunwind doesn't exist yet. Try to check if
118 # --unwindlib=none is supported, and use that if possible.
120 # TODO: Note that this is problematic when LLVM_USE_SANITIZER is used
121 # because some sanitizers require the unwinder and so the combination of
122 # -fsanitize=... --unwindlib=none will always result in a linking error.
123 # Currently, we counteract this issue by adding -fno-sanitize=all flag in
124 # the project specific code within */cmake/config-ix.cmake files but that's
125 # brittle. We should ideally move this to runtimes/CMakeLists.txt.
126 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
127 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
128 set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
129 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
130 # TODO: When we can require CMake 3.14, we should use
131 # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
132 # When using CMAKE_REQUIRED_FLAGS, this option gets added both to
133 # compilation and linking commands. That causes warnings in the
134 # compilation commands during cmake tests. This is normally benign, but
135 # when testing whether -Werror works, that test fails (due to the
136 # preexisting warning).
138 # Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
139 # can use --start-no-unused-arguments to silence the warnings about
140 # --unwindlib=none during compilation.
142 # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
143 # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
145 check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
146 if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
147 set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
151 # Disable use of the installed C++ standard library when building runtimes.
152 # Check for -nostdlib++ first; if there's no C++ standard library yet,
153 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
154 # (or -nodefaultlibs).
155 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
156 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
157 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
159 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
160 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
161 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
164 # Avoid checking whether the compiler is working.
165 set(LLVM_COMPILER_CHECKED ON)
167 # This can be used to detect whether we're targeting a GPU architecture.
168 if("${LLVM_RUNTIMES_TARGET}" MATCHES "^amdgcn" OR
169 "${LLVM_RUNTIMES_TARGET}" MATCHES "^nvptx64")
170 set(LLVM_RUNTIMES_GPU_BUILD ON)
173 # Handle common options used by all runtimes.
175 include(HandleLLVMOptions)
177 # Loot at the PATH first to avoid a version mismatch between the command-line
178 # python and the CMake-found version
179 set(Python3_FIND_REGISTRY LAST)
180 find_package(Python3 REQUIRED COMPONENTS Interpreter)
182 # Host triple is used by tests to check if they are running natively.
183 include(GetHostTriple)
184 get_host_triple(LLVM_HOST_TRIPLE)
185 message(STATUS "LLVM host triple: ${LLVM_HOST_TRIPLE}")
187 # TODO: We shouldn't be using LLVM_DEFAULT_TARGET_TRIPLE for runtimes since we
188 # aren't generating code, LLVM_TARGET_TRIPLE is a better fit.
189 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
190 "Default target for which the runtimes will be built.")
191 message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
193 set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
195 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
196 set(option_prefix "")
197 if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
198 set(option_prefix "/clang:")
200 set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${LLVM_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
201 execute_process(COMMAND ${print_target_triple}
202 RESULT_VARIABLE result
203 OUTPUT_VARIABLE output
204 OUTPUT_STRIP_TRAILING_WHITESPACE)
206 set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
208 string(REPLACE ";" " " print_target_triple "${print_target_triple}")
209 # TODO(#97876): Report an error.
210 message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
214 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
215 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
216 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
218 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
219 if(CMAKE_HOST_APPLE AND APPLE)
223 # This can be used to detect whether we're in the runtimes build.
224 set(LLVM_RUNTIMES_BUILD ON)
226 foreach(entry ${runtimes})
227 get_filename_component(projName ${entry} NAME)
229 # TODO: Clean this up as part of an interface standardization
230 string(REPLACE "-" "_" canon_name ${projName})
231 string(TOUPPER ${canon_name} canon_name)
233 # TODO: compiler-rt has to use standalone build for now. We tried to remove
234 # this in D57992 but this broke the build because compiler-rt assumes that
235 # LLVM and Clang are configured in the same build to set up dependencies. We
236 # should clean up the compiler-rt build and remove this eventually.
237 if ("${canon_name}" STREQUAL "COMPILER_RT")
238 set(${canon_name}_STANDALONE_BUILD ON)
241 if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
242 set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
245 # Setting a variable to let sub-projects detect which other projects
246 # will be included under here.
247 set(HAVE_${canon_name} ON)
250 if(LLVM_INCLUDE_TESTS)
251 # If built with the runtimes build (rooted at runtimes/CMakeLists.txt), we
252 # won't have llvm-lit. If built with the bootstrapping build (rooted at
253 # llvm/CMakeLists.txt), the top-level llvm CMake invocation already generated
254 # the llvm-lit script.
255 if (NOT HAVE_LLVM_LIT)
256 # Ensure that the appropriate variables for lit are set before adding any
257 # runtimes since their CMake tests configuration might depend on lit being
258 # present. This ensures that the testsuites use a local lit from the build
259 # dir rather than ${LLVM_INSTALL_DIR}/bin/llvm-lit (which may not exist if
260 # LLVM_BINARY_DIR points at an installed LLVM tree rather than a build tree).
261 set(LLVM_LIT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
262 get_llvm_lit_path(_base_dir _file_name)
263 set(LLVM_EXTERNAL_LIT "${_base_dir}/${_file_name}" CACHE STRING "Command used to spawn lit" FORCE)
264 # Avoid warning about missing llvm-lit from runtimes CMake files. This is
265 # fine since we call configure_file() to create llvm-lit at the end of this
266 # file (after recursing into all runtimes' CMake logic), so it will exist.
267 set(LLVM_EXTERNAL_LIT_MISSING_WARNED_ONCE YES CACHE INTERNAL "")
270 set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
272 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
274 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
276 umbrella_lit_testsuite_begin(check-runtimes)
279 # We do this in two loops so that HAVE_* is set for each runtime before the
280 # other runtimes are added.
281 foreach(entry ${runtimes})
282 get_filename_component(projName ${entry} NAME)
284 add_subdirectory(${entry} ${projName})
287 # Define runtimes-test-depends so the parent build can use it unconditionally.
288 add_custom_target(runtimes-test-depends)
290 if(LLVM_INCLUDE_TESTS)
291 # LLVM_RUNTIMES_LIT_DEPENDS is populated when lit tests are added between
292 # umbrella_list_testsuite begin and end. The bootstrap runtimes builds
293 # currently assumes this target exists.
294 get_property(LLVM_RUNTIMES_LIT_DEPENDS GLOBAL PROPERTY LLVM_RUNTIMES_LIT_DEPENDS)
295 if(LLVM_RUNTIMES_LIT_DEPENDS)
296 # add_dependencies complains if called with no dependencies
297 add_dependencies(runtimes-test-depends ${LLVM_RUNTIMES_LIT_DEPENDS})
299 # Add a global check rule now that all subdirectories have been traversed
300 # and we know the total set of lit testsuites.
301 umbrella_lit_testsuite_end(check-runtimes)
303 if (NOT HAVE_LLVM_LIT)
304 # If built by manually invoking cmake on this directory, we don't have
305 # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
306 # invocation already generated the llvm-lit script.
307 # NOTE: this must be called after all testsuites have been added, since
308 # otherwise the generated llvm-lit does not have all required path mappings.
309 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
310 ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
313 get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
314 string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
315 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
317 # Create empty files so the parent build can use these unconditionally.
318 file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/lit.tests)
321 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
323 list(REMOVE_DUPLICATES SUB_COMPONENTS)
324 foreach(component ${SUB_COMPONENTS})
325 if(NOT TARGET ${component})
326 message(SEND_ERROR "Missing target for runtime component ${component}!")
330 if(TARGET check-${component})
331 list(APPEND SUB_CHECK_TARGETS check-${component})
334 if(TARGET install-${component})
335 list(APPEND SUB_INSTALL_TARGETS install-${component})
339 if(LLVM_RUNTIMES_TARGET)
341 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
342 ${CMAKE_CURRENT_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
345 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
346 ${CMAKE_CURRENT_BINARY_DIR}/runtimes/Components.cmake)
350 # If the user requested 'compile_commands.json' we merge the generated JSON from
351 # the created directories.
352 if(CMAKE_EXPORT_COMPILE_COMMANDS AND NOT ("${LLVM_BINARY_DIR}" STREQUAL "${CMAKE_BINARY_DIR}"))
353 # Make a dependency so that we don't error if the file gets deleted somehow.
354 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/compile_commands.json
355 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/compile_commands.json)
357 file(TO_NATIVE_PATH "${LLVM_MAIN_SRC_DIR}/utils/merge-json.py" MERGE_JSON_PATH)
358 add_custom_command(OUTPUT ${LLVM_BINARY_DIR}/compile_commands.json
359 COMMAND ${CMAKE_COMMAND} -E touch ${LLVM_BINARY_DIR}/compile_commands.json
360 COMMAND ${Python3_EXECUTABLE} ${MERGE_JSON_PATH}
361 ${LLVM_BINARY_DIR}/compile_commands.json
362 ${CMAKE_BINARY_DIR}/compile_commands.json
363 -o ${LLVM_BINARY_DIR}/compile_commands.json
364 DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)
365 add_custom_target(merge_runtime_commands ALL DEPENDS ${LLVM_BINARY_DIR}/compile_commands.json)