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 set(SAFE_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
69 set(SAFE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
71 include(CheckLibraryExists)
72 include(CheckCCompilerFlag)
74 check_library_exists(c fopen "" LLVM_HAS_C_LIB)
75 check_c_compiler_flag(-nodefaultlibs LLVM_HAS_NODEFAULTLIBS_FLAG)
76 if(LLVM_HAS_NODEFAULTLIBS_FLAG)
77 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
79 list(APPEND CMAKE_REQUIRED_LIBRARIES c)
83 # Handle common options used by all runtimes.
85 include(HandleLLVMOptions)
87 set(CMAKE_REQUIRED_FLAGS ${SAFE_CMAKE_REQUIRED_FLAGS})
88 set(CMAKE_REQUIRED_LIBRARIES ${SAFE_CMAKE_REQUIRED_LIBRARIES})
90 foreach(entry ${runtimes})
91 get_filename_component(projName ${entry} NAME)
93 # TODO: Clean this up as part of an interface standardization
94 string(REPLACE "-" "_" canon_name ${projName})
95 string(TOUPPER ${canon_name} canon_name)
96 # The subdirectories need to treat this as standalone builds
97 set(${canon_name}_STANDALONE_BUILD On)
99 # Setting a variable to let sub-projects detect which other projects
100 # will be included under here.
101 set(HAVE_${canon_name} On)
104 # We do this in two loops so that HAVE_* is set for each runtime before the
105 # other runtimes are added.
106 foreach(entry ${runtimes})
107 get_filename_component(projName ${entry} NAME)
109 # Between each sub-project we want to cache and clear the LIT properties
110 set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
111 set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
112 set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
113 set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
115 add_subdirectory(${projName})
117 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
118 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
119 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
120 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
122 list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
123 list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
124 list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
125 list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
128 if(LLVM_INCLUDE_TESTS)
129 # Add a global check rule now that all subdirectories have been traversed
130 # and we know the total set of lit testsuites.
132 add_lit_target(check-runtimes
133 "Running all regression tests"
134 ${RUNTIMES_LIT_TESTSUITES}
135 PARAMS ${RUNTIMES_LIT_PARAMS}
136 DEPENDS ${RUNTIMES_LIT_DEPENDS}
137 ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
139 add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
142 get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
144 list(REMOVE_DUPLICATES SUB_COMPONENTS)
145 foreach(component ${SUB_COMPONENTS})
146 if(NOT TARGET ${component})
147 message(SEND_ERROR "Missing target for runtime component ${component}!")
150 if(LLVM_INCLUDE_TESTS AND NOT TARGET check-${component})
151 message(SEND_ERROR "Missing check target for runtime component ${component}!")
155 if(TARGET install-${component})
156 list(APPEND SUB_INSTALL_TARGETS install-${component})
161 ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
162 ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
165 else() # if this is included from LLVM's CMake
166 include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
167 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
168 include(LLVMExternalProjectUtils)
170 if(NOT LLVM_BUILD_RUNTIMES)
171 set(EXTRA_ARGS EXCLUDE_FROM_ALL)
174 # If compiler-rt is present we need to build the builtin libraries first. This
175 # is required because the other runtimes need the builtin libraries present
176 # before the just-built compiler can pass the configuration tests.
177 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt)
178 if(NOT LLVM_BUILTIN_TARGETS)
179 llvm_ExternalProject_Add(builtins
180 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
181 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
182 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
183 -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
184 -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
185 PASSTHROUGH_PREFIXES COMPILER_RT
189 get_cmake_property(variableNames VARIABLES)
190 add_custom_target(builtins)
191 foreach(target ${LLVM_BUILTIN_TARGETS})
192 string(REPLACE "-" ";" builtin_target_list ${target})
193 foreach(item ${builtin_target_list})
194 string(TOLOWER "${item}" item_lower)
195 if(item_lower MATCHES "darwin")
196 message(FATAL_ERROR "LLVM_BUILTIN_TARGETS isn't implemented for Darwin platform!")
200 foreach(variableName ${variableNames})
201 if(variableName MATCHES "^BUILTINS_${target}")
202 string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
203 list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
206 llvm_ExternalProject_Add(builtins-${target}
207 ${CMAKE_CURRENT_SOURCE_DIR}/compiler-rt/lib/builtins
208 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
209 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
210 -DCMAKE_C_COMPILER_TARGET=${target}
211 -DCMAKE_ASM_COMPILER_TARGET=${target}
212 -DCMAKE_C_COMPILER_WORKS=On
213 -DCMAKE_ASM_COMPILER_WORKS=On
214 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=On
215 ${${target}_extra_args}
216 TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib
217 PASSTHROUGH_PREFIXES COMPILER_RT
220 add_dependencies(builtins builtins-${target})
224 # We don't need to depend on the builtins if we're building instrumented
225 # because the next stage will use the same compiler used to build this stage.
226 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
227 add_dependencies(clang-bootstrap-deps builtins)
231 # We create a list the names of all the runtime projects in all uppercase and
232 # with dashes turned to underscores. This gives us the CMake variable prefixes
233 # for all variables that will apply to runtimes.
234 foreach(entry ${runtimes})
235 get_filename_component(projName ${entry} NAME)
236 string(REPLACE "-" "_" canon_name ${projName})
237 string(TOUPPER ${canon_name} canon_name)
238 list(APPEND prefixes ${canon_name})
240 string(FIND ${projName} "lib" LIB_IDX)
242 string(SUBSTRING ${projName} 3 -1 projName)
244 list(APPEND runtime_names ${projName})
249 foreach(runtime_name ${runtime_names})
250 list(APPEND extra_targets
252 install-${runtime_name}
253 check-${runtime_name})
256 if(LLVM_INCLUDE_TESTS)
257 set(test_targets runtimes-test-depends check-runtimes)
258 foreach(component ${SUB_COMPONENTS})
259 list(APPEND SUB_COMPONENT_CHECK_TARGETS check-${component})
263 # Create a runtimes target that uses this file as its top-level CMake file.
264 # The runtimes target is a configuration of all the runtime libraries
265 # together in a single CMake invocaiton.
266 llvm_ExternalProject_Add(runtimes
267 ${CMAKE_CURRENT_SOURCE_DIR}
269 # Builtins were built separately above
270 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
271 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
272 PASSTHROUGH_PREFIXES ${prefixes}
273 EXTRA_TARGETS ${extra_targets}
276 ${SUB_COMPONENT_CHECK_TARGETS}
277 ${SUB_INSTALL_TARGETS}
281 # TODO: This is a hack needed because the libcxx headers are copied into the
282 # build directory during configuration. Without that step the clang in the
283 # build directory cannot find the C++ headers in certain configurations.
284 # I need to build a mechanism for runtime projects to provide CMake code
285 # that executes at LLVM configuration time to handle this case.
286 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
287 add_dependencies(clang-bootstrap-deps runtimes-configure)
290 if(LLVM_INCLUDE_TESTS)
291 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
292 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)