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 project(Runtimes C CXX ASM)
11 list(INSERT CMAKE_MODULE_PATH 0
12 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
13 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
14 "${LLVM_COMMON_CMAKE_UTILS}"
15 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
16 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake"
17 "${CMAKE_CURRENT_SOURCE_DIR}/../llvm/cmake/modules"
20 # We order libraries to mirror roughly how they are layered, except that compiler-rt can depend
21 # on libc++, so we put it after.
22 set(LLVM_DEFAULT_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp")
23 set(LLVM_SUPPORTED_RUNTIMES "${LLVM_DEFAULT_RUNTIMES};llvm-libgcc")
24 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
25 "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
26 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
27 set(LLVM_ENABLE_RUNTIMES ${LLVM_DEFAULT_RUNTIMES})
30 sort_subset("${LLVM_SUPPORTED_RUNTIMES}" "${LLVM_ENABLE_RUNTIMES}" LLVM_ENABLE_RUNTIMES)
32 foreach(proj ${LLVM_ENABLE_RUNTIMES})
33 set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
34 if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
35 list(APPEND runtimes ${proj_dir})
37 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
39 string(TOUPPER "${proj}" canon_name)
40 STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
41 set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
44 function(runtime_register_component name)
45 set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
48 find_package(LLVM PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
49 find_package(Clang PATHS "${LLVM_BINARY_DIR}" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
51 set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
53 # If building standalone by pointing CMake at this runtimes directory,
54 # LLVM_BINARY_DIR isn't set, find_package(LLVM) will fail and these
55 # intermediate paths are unset.
56 if (NOT LLVM_BINARY_DIR)
57 set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
60 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
61 set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
62 set(LLVM_LIBRARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
65 # Setting these variables will allow the sub-build to put their outputs into
66 # the library and bin directories of the top-level build.
67 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
68 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
70 # This variable makes sure that e.g. llvm-lit is found.
71 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
72 set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
74 # This variable is used by individual runtimes to locate LLVM files.
75 set(LLVM_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../llvm)
77 include(CheckLibraryExists)
78 include(LLVMCheckCompilerLinkerFlag)
79 include(CheckCCompilerFlag)
80 include(CheckCXXCompilerFlag)
82 # CMake omits default compiler include paths, but in runtimes build, we use
83 # -nostdinc and -nostdinc++ and control include paths manually so this behavior
84 # is undesirable. Filtering CMAKE_{LANG}_IMPLICIT_INCLUDE_DIRECTORIES to remove
85 # paths that are inside the build directory disables this behavior.
87 # See https://gitlab.kitware.com/cmake/cmake/-/issues/19227 for further details.
89 function(filter_prefixed list prefix outvar)
91 string(FIND "${str}" "${prefix}" out)
92 if(NOT "${out}" EQUAL 0)
93 list(APPEND result ${str})
96 set(${outvar} ${result} PARENT_SCOPE)
99 filter_prefixed("${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES)
100 filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES)
101 filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES)
103 # The compiler driver may be implicitly trying to link against libunwind,
104 # which might not work if libunwind doesn't exist yet. Try to check if
105 # --unwindlib=none is supported, and use that if possible.
107 # TODO: Note that this is problematic when LLVM_USE_SANITIZER is used
108 # because some sanitizers require the unwinder and so the combination of
109 # -fsanitize=... --unwindlib=none will always result in a linking error.
110 # Currently, we counteract this issue by adding -fno-sanitize=all flag in
111 # the project specific code within */cmake/config-ix.cmake files but that's
112 # brittle. We should ideally move this to runtimes/CMakeLists.txt.
113 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
114 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
115 set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
116 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
117 # TODO: When we can require CMake 3.14, we should use
118 # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
119 # When using CMAKE_REQUIRED_FLAGS, this option gets added both to
120 # compilation and linking commands. That causes warnings in the
121 # compilation commands during cmake tests. This is normally benign, but
122 # when testing whether -Werror works, that test fails (due to the
123 # preexisting warning).
125 # Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
126 # can use --start-no-unused-arguments to silence the warnings about
127 # --unwindlib=none during compilation.
129 # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
130 # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
132 check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
133 if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
134 set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
138 # Disable use of the installed C++ standard library when building runtimes.
139 # Check for -nostdlib++ first; if there's no C++ standard library yet,
140 # all check_cxx_compiler_flag commands will fail until we add -nostdlib++
141 # (or -nodefaultlibs).
142 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
143 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
144 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
146 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
147 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
148 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
151 # Avoid checking whether the compiler is working.
152 set(LLVM_COMPILER_CHECKED ON)
154 # Handle common options used by all runtimes.
156 include(HandleLLVMOptions)
158 find_package(Python3 REQUIRED COMPONENTS Interpreter)
160 # Host triple is used by tests to check if they are running natively.
161 include(GetHostTriple)
162 get_host_triple(LLVM_HOST_TRIPLE)
163 message(STATUS "LLVM host triple: ${LLVM_HOST_TRIPLE}")
165 # TODO: We shouldn't be using LLVM_DEFAULT_TARGET_TRIPLE for runtimes since we
166 # aren't generating code, LLVM_TARGET_TRIPLE is a better fit.
167 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
168 "Default target for which the runtimes will be built.")
169 message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
171 set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
173 option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
174 option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
175 option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
177 # Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
178 if(CMAKE_HOST_APPLE AND APPLE)
182 # This can be used to detect whether we're in the runtimes build.
183 set(LLVM_RUNTIMES_BUILD ON)
185 foreach(entry ${runtimes})
186 get_filename_component(projName ${entry} NAME)
188 # TODO: Clean this up as part of an interface standardization
189 string(REPLACE "-" "_" canon_name ${projName})
190 string(TOUPPER ${canon_name} canon_name)
192 # TODO: compiler-rt has to use standalone build for now. We tried to remove
193 # this in D57992 but this broke the build because compiler-rt assumes that
194 # LLVM and Clang are configured in the same build to set up dependencies. We
195 # should clean up the compiler-rt build and remove this eventually.
196 if ("${canon_name}" STREQUAL "COMPILER_RT")
197 set(${canon_name}_STANDALONE_BUILD ON)
200 if(LLVM_RUNTIMES_LIBDIR_SUBDIR)
201 set(${canon_name}_LIBDIR_SUBDIR "${LLVM_RUNTIMES_LIBDIR_SUBDIR}" CACHE STRING "" FORCE)
204 # Setting a variable to let sub-projects detect which other projects
205 # will be included under here.
206 set(HAVE_${canon_name} ON)
209 if(LLVM_INCLUDE_TESTS)
210 set(LIT_ARGS_DEFAULT "-sv --show-xfail --show-unsupported")
212 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
214 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
216 umbrella_lit_testsuite_begin(check-runtimes)
219 # We do this in two loops so that HAVE_* is set for each runtime before the
220 # other runtimes are added.
221 foreach(entry ${runtimes})
222 get_filename_component(projName ${entry} NAME)
224 add_subdirectory(${entry} ${projName})
227 # Define runtimes-test-depends so the parent build can use it unconditionally.
228 add_custom_target(runtimes-test-depends)
230 if(LLVM_INCLUDE_TESTS)
231 # LLVM_RUNTIMES_LIT_DEPENDS is populated when lit tests are added between
232 # umbrella_list_testsuite begin and end. The bootstrap runtimes builds
233 # currently assumes this target exists.
234 get_property(LLVM_RUNTIMES_LIT_DEPENDS GLOBAL PROPERTY LLVM_RUNTIMES_LIT_DEPENDS)
235 if(LLVM_RUNTIMES_LIT_DEPENDS)
236 # add_dependencies complains if called with no dependencies
237 add_dependencies(runtimes-test-depends ${LLVM_RUNTIMES_LIT_DEPENDS})
239 # Add a global check rule now that all subdirectories have been traversed
240 # and we know the total set of lit testsuites.
241 umbrella_lit_testsuite_end(check-runtimes)
243 if (NOT HAVE_LLVM_LIT)
244 # If built by manually invoking cmake on this directory, we don't have
245 # llvm-lit. If invoked via llvm/runtimes, the toplevel llvm cmake
246 # invocation already generated the llvm-lit script.
247 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
248 ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
251 get_property(LLVM_RUNTIMES_LIT_TESTSUITES GLOBAL PROPERTY LLVM_RUNTIMES_LIT_TESTSUITES)
252 string(REPLACE ";" "\n" LLVM_RUNTIMES_LIT_TESTSUITES "${LLVM_RUNTIMES_LIT_TESTSUITES}")
253 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/lit.tests ${LLVM_RUNTIMES_LIT_TESTSUITES})
255 # Create empty files so the parent build can use these unconditionally.
256 file(TOUCH ${CMAKE_CURRENT_BINARY_DIR}/lit.tests)
259 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
261 list(REMOVE_DUPLICATES SUB_COMPONENTS)
262 foreach(component ${SUB_COMPONENTS})
263 if(NOT TARGET ${component})
264 message(SEND_ERROR "Missing target for runtime component ${component}!")
268 if(TARGET check-${component})
269 list(APPEND SUB_CHECK_TARGETS check-${component})
272 if(TARGET install-${component})
273 list(APPEND SUB_INSTALL_TARGETS install-${component})
277 if(LLVM_RUNTIMES_TARGET)
279 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
280 ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
283 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
284 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)