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})
64 # Handle common options used by all runtimes.
66 include(HandleLLVMOptions)
68 foreach(entry ${runtimes})
69 get_filename_component(projName ${entry} NAME)
71 # TODO: Clean this up as part of an interface standardization
72 string(REPLACE "-" "_" canon_name ${projName})
73 string(TOUPPER ${canon_name} canon_name)
74 # The subdirectories need to treat this as standalone builds
75 set(${canon_name}_STANDALONE_BUILD On)
77 # Setting a variable to let sub-projects detect which other projects
78 # will be included under here.
79 set(HAVE_${canon_name} On)
82 # We do this in two loops so that HAVE_* is set for each runtime before the
83 # other runtimes are added.
84 foreach(entry ${runtimes})
85 get_filename_component(projName ${entry} NAME)
87 # Between each sub-project we want to cache and clear the LIT properties
88 set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
89 set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
90 set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
91 set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
93 add_subdirectory(${projName})
95 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
96 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
97 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
98 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
100 list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
101 list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
102 list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
103 list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
106 if(LLVM_INCLUDE_TESTS)
107 # Add a global check rule now that all subdirectories have been traversed
108 # and we know the total set of lit testsuites.
110 add_lit_target(check-runtimes
111 "Running all regression tests"
112 ${RUNTIMES_LIT_TESTSUITES}
113 PARAMS ${RUNTIMES_LIT_PARAMS}
114 DEPENDS ${RUNTIMES_LIT_DEPENDS}
115 ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
117 add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
120 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
122 list(REMOVE_DUPLICATES SUB_COMPONENTS)
123 foreach(component ${SUB_COMPONENTS})
124 if(NOT TARGET ${component})
125 message(SEND_ERROR "Missing target for runtime component ${component}!")
128 if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
129 message(SEND_ERROR "Missing check target for runtime component ${component}!")
133 if(TARGET install-${component})
134 list(APPEND SUB_INSTALL_TARGETS install-${component})
139 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
140 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
143 else() # if this is included from LLVM's CMake
144 include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
145 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
146 include(LLVMExternalProjectUtils)
148 # If compiler-rt is present we need to build the builtin libraries first. This
149 # is required because the other runtimes need the builtin libraries present
150 # before the just-built compiler can pass the configuration tests.
151 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt)
152 if(APPLE OR NOT LLVM_BUILTIN_TARGETS)
153 llvm_ExternalProject_Add(builtins
154 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
155 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
156 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
157 -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
158 -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
159 PASSTHROUGH_PREFIXES COMPILER_RT
162 get_cmake_property(variableNames VARIABLES)
163 add_custom_target(builtins)
164 foreach(target ${LLVM_BUILTIN_TARGETS})
165 foreach(variableName ${variableNames})
166 if(variableName MATCHES "^BUILTINS_${target}")
167 string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
168 list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
171 llvm_ExternalProject_Add(builtins-${target}
172 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
173 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
174 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
175 -DCMAKE_C_COMPILER_TARGET=${target}
176 -DCMAKE_ASM_COMPILER_TARGET=${target}
177 -DCMAKE_C_COMPILER_WORKS=On
178 -DCMAKE_ASM_COMPILER_WORKS=On
179 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On
180 ${${target}_extra_args}
181 PASSTHROUGH_PREFIXES COMPILER_RT
183 add_dependencies(builtins builtins-${target})
187 # We don't need to depend on the builtins if we're building instrumented
188 # because the next stage will use the same compiler used to build this stage.
189 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
190 add_dependencies(clang-bootstrap-deps builtins)
194 # We create a list the names of all the runtime projects in all uppercase and
195 # with dashes turned to underscores. This gives us the CMake variable prefixes
196 # for all variables that will apply to runtimes.
197 foreach(entry ${runtimes})
198 get_filename_component(projName ${entry} NAME)
199 string(REPLACE "-" "_" canon_name ${projName})
200 string(TOUPPER ${canon_name} canon_name)
201 list(APPEND prefixes ${canon_name})
203 string(FIND ${projName} "lib" LIB_IDX)
205 string(SUBSTRING ${projName} 3 -1 projName)
207 list(APPEND runtime_names ${projName})
212 foreach(runtime_name ${runtime_names})
213 list(APPEND extra_targets
215 install-${runtime_name}
216 check-${runtime_name})
219 if(LLVM_INCLUDE_TESTS)
220 set(test_targets runtimes-test-depends check-runtimes)
221 foreach(component ${SUB_COMPONENTS})
222 list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
226 # Create a runtimes target that uses this file as its top-level CMake file.
227 # The runtimes target is a configuration of all the runtime libraries
228 # together in a single CMake invocaiton.
229 llvm_ExternalProject_Add(runtimes
230 ${CMAKE_CURRENT_SOURCE_DIR}
232 # Builtins were built separately above
233 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
234 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
235 PASSTHROUGH_PREFIXES ${prefixes}
236 EXTRA_TARGETS ${extra_targets}
239 ${SUB_COMPONENT_CHECK_TARGETS}
240 ${SUB_INSTALL_TARGETS}
243 # TODO: This is a hack needed because the libcxx headers are copied into the
244 # build directory during configuration. Without that step the clang in the
245 # build directory cannot find the C++ headers in certain configurations.
246 # I need to build a mechanism for runtime projects to provide CMake code
247 # that executes at LLVM configuration time to handle this case.
248 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
249 add_dependencies(clang-bootstrap-deps runtimes-configure)
252 if(LLVM_INCLUDE_TESTS)
253 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
254 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)