1 # This file handles building LLVM runtime sub-projects.
3 # Runtimes are different from tools or other drop-in projects because runtimes
4 # should be built with the LLVM toolchain from the build directory. This file is
5 # a first step to formalizing runtime build interfaces.
7 # In the current state this file only works with compiler-rt, other runtimes
8 # will work as the runtime build interface standardizes.
10 # Find all subdirectories containing CMake projects
12 foreach(entry ${entries})
13 if(IS_DIRECTORY ${entry} AND EXISTS ${entry}/CMakeLists.txt)
14 list(APPEND runtimes ${entry})
18 # If this file is acting as a top-level CMake invocation, this code path is
19 # triggered by the external project call for the runtimes target below.
20 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
22 function(runtime_register_component name)
23 set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
26 cmake_minimum_required(VERSION 3.4.3)
28 # Add the root project's CMake modules, and the LLVM build's modules to the
30 list(INSERT CMAKE_MODULE_PATH 0
31 "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
32 "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
33 "${LLVM_BINARY_DIR}/lib/cmake/llvm"
36 # Some of the runtimes will conditionally use the compiler-rt sanitizers
37 # to make this work smoothly we ensure that compiler-rt is added first in
38 # the list of sub-projects. This allows other sub-projects to have checks
39 # like `if(TARGET asan)` to enable building with asan.
40 foreach(entry ${runtimes})
41 if("${entry}" MATCHES "compiler-rt")
42 set(compiler_rt_path ${entry})
47 list(REMOVE_ITEM runtimes ${compiler_rt_path})
48 list(INSERT runtimes 0 ${compiler_rt_path})
51 # LLVMConfig.cmake contains a bunch of CMake variables from the LLVM build.
52 # This file is installed as part of LLVM distributions, so this can be used
53 # either from a build directory or an installed LLVM.
56 # Setting these variables will allow the sub-build to put their outputs into
57 # the library and bin directories of the top-level build.
58 set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
59 set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
61 # This variable makes sure that e.g. llvm-lit is found.
62 set(LLVM_MAIN_SRC_DIR ${LLVM_BUILD_MAIN_SRC_DIR})
65 set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
68 # Handle common options used by all runtimes.
70 include(HandleLLVMOptions)
72 foreach(entry ${runtimes})
73 get_filename_component(projName ${entry} NAME)
75 # TODO: Clean this up as part of an interface standardization
76 string(REPLACE "-" "_" canon_name ${projName})
77 string(TOUPPER ${canon_name} canon_name)
78 # The subdirectories need to treat this as standalone builds
79 set(${canon_name}_STANDALONE_BUILD On)
81 # Setting a variable to let sub-projects detect which other projects
82 # will be included under here.
83 set(HAVE_${canon_name} On)
86 # We do this in two loops so that HAVE_* is set for each runtime before the
87 # other runtimes are added.
88 foreach(entry ${runtimes})
89 get_filename_component(projName ${entry} NAME)
91 # Between each sub-project we want to cache and clear the LIT properties
92 set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
93 set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
94 set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
95 set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
97 add_subdirectory(${projName})
99 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
100 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
101 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
102 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
104 list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
105 list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
106 list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
107 list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
110 if(LLVM_INCLUDE_TESTS)
111 # Add a global check rule now that all subdirectories have been traversed
112 # and we know the total set of lit testsuites.
114 add_lit_target(check-runtimes
115 "Running all regression tests"
116 ${RUNTIMES_LIT_TESTSUITES}
117 PARAMS ${RUNTIMES_LIT_PARAMS}
118 DEPENDS ${RUNTIMES_LIT_DEPENDS}
119 ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
121 add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
124 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
126 list(REMOVE_DUPLICATES SUB_COMPONENTS)
127 foreach(component ${SUB_COMPONENTS})
128 if(NOT TARGET ${component})
129 message(SEND_ERROR "Missing target for runtime component ${component}!")
132 if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
133 message(SEND_ERROR "Missing check target for runtime component ${component}!")
137 if(TARGET install-${component})
138 list(APPEND SUB_INSTALL_TARGETS install-${component})
143 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
144 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
147 else() # if this is included from LLVM's CMake
148 include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
149 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
150 include(LLVMExternalProjectUtils)
152 if(NOT LLVM_BUILD_RUNTIMES)
153 set(EXTRA_ARGS EXCLUDE_FROM_ALL)
156 # If compiler-rt is present we need to build the builtin libraries first. This
157 # is required because the other runtimes need the builtin libraries present
158 # before the just-built compiler can pass the configuration tests.
159 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt)
160 if(NOT LLVM_BUILTIN_TARGETS)
161 llvm_ExternalProject_Add(builtins
162 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
163 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
164 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
165 -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
166 -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
167 PASSTHROUGH_PREFIXES COMPILER_RT
171 get_cmake_property(variableNames VARIABLES)
172 add_custom_target(builtins)
173 foreach(target ${LLVM_BUILTIN_TARGETS})
174 string(REPLACE "-" ";" builtin_target_list ${target})
175 foreach(item ${builtin_target_list})
176 string(TOLOWER "${item}" item_lower)
177 if(item_lower MATCHES "darwin")
178 message(FATAL_ERROR "LLVM_BUILTIN_TARGETS isn't implemented for Darwin platform!")
182 foreach(variableName ${variableNames})
183 if(variableName MATCHES "^BUILTINS_${target}")
184 string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
185 list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
188 llvm_ExternalProject_Add(builtins-${target}
189 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
190 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
191 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
192 -DCMAKE_C_COMPILER_TARGET=${target}
193 -DCMAKE_ASM_COMPILER_TARGET=${target}
194 -DCMAKE_C_COMPILER_WORKS=On
195 -DCMAKE_ASM_COMPILER_WORKS=On
196 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On
197 ${${target}_extra_args}
198 TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib
199 PASSTHROUGH_PREFIXES COMPILER_RT
202 add_dependencies(builtins builtins-${target})
206 # We don't need to depend on the builtins if we're building instrumented
207 # because the next stage will use the same compiler used to build this stage.
208 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
209 add_dependencies(clang-bootstrap-deps builtins)
213 # We create a list the names of all the runtime projects in all uppercase and
214 # with dashes turned to underscores. This gives us the CMake variable prefixes
215 # for all variables that will apply to runtimes.
216 foreach(entry ${runtimes})
217 get_filename_component(projName ${entry} NAME)
218 string(REPLACE "-" "_" canon_name ${projName})
219 string(TOUPPER ${canon_name} canon_name)
220 list(APPEND prefixes ${canon_name})
222 string(FIND ${projName} "lib" LIB_IDX)
224 string(SUBSTRING ${projName} 3 -1 projName)
226 list(APPEND runtime_names ${projName})
231 foreach(runtime_name ${runtime_names})
232 list(APPEND extra_targets
234 install-${runtime_name}
235 check-${runtime_name})
238 if(LLVM_INCLUDE_TESTS)
239 set(test_targets runtimes-test-depends check-runtimes)
240 foreach(component ${SUB_COMPONENTS})
241 list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
245 # Create a runtimes target that uses this file as its top-level CMake file.
246 # The runtimes target is a configuration of all the runtime libraries
247 # together in a single CMake invocaiton.
248 llvm_ExternalProject_Add(runtimes
249 ${CMAKE_CURRENT_SOURCE_DIR}
251 # Builtins were built separately above
252 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
253 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
254 PASSTHROUGH_PREFIXES ${prefixes}
255 EXTRA_TARGETS ${extra_targets}
258 ${SUB_COMPONENT_CHECK_TARGETS}
259 ${SUB_INSTALL_TARGETS}
263 # TODO: This is a hack needed because the libcxx headers are copied into the
264 # build directory during configuration. Without that step the clang in the
265 # build directory cannot find the C++ headers in certain configurations.
266 # I need to build a mechanism for runtime projects to provide CMake code
267 # that executes at LLVM configuration time to handle this case.
268 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
269 add_dependencies(clang-bootstrap-deps runtimes-configure)
272 if(LLVM_INCLUDE_TESTS)
273 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
274 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)