1 # TODO: This file assumes the Clang toolchain so it'd be better if it lived in
2 # Clang, except there already is clang/runtime directory which contains
3 # similar although simpler functionality. We should figure out how to merge
6 # TODO: Selecting runtimes should be always performed inside the runtimes
7 # build, see runtimes/CMakeLists.txt, except that we currently check whether
8 # compiler-rt is being built to determine whether to first build builtins
9 # or not so we need that information in this file as well.
10 set(LLVM_ALL_RUNTIMES "compiler-rt;libc;libcxx;libcxxabi;libunwind;openmp")
11 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
12 "Semicolon-separated list of runtimes to build (${LLVM_ALL_RUNTIMES}), or \"all\".")
13 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
14 set(LLVM_ENABLE_RUNTIMES ${LLVM_ALL_RUNTIMES})
16 foreach(proj ${LLVM_ENABLE_RUNTIMES})
17 set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
18 if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
19 list(APPEND runtimes ${proj_dir})
21 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
23 string(TOUPPER "${proj}" canon_name)
24 STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
25 set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
28 function(get_compiler_rt_path path)
29 foreach(entry ${runtimes})
30 get_filename_component(projName ${entry} NAME)
31 if("${projName}" MATCHES "compiler-rt")
32 set(${path} ${entry} PARENT_SCOPE)
38 include(LLVMExternalProjectUtils)
40 if(NOT LLVM_BUILD_RUNTIMES)
41 set(EXTRA_ARGS EXCLUDE_FROM_ALL)
44 function(check_apple_target triple builtin_or_runtime)
46 compiler-rt for Darwin builds for all platforms and architectures using a \
47 single configuration. Specify only a single darwin triple (e.g. x86_64-apple-darwin) \
48 in your targets list (and not a triple for a specific platform such as macos). \
49 You can use variables such as COMPILER_RT_ENABLE_IOS and DARWIN_ios_ARCHS to \
50 control the specific platforms and architectures to build.")
52 set(seen_property ${builtin_or_runtime}_darwin_triple_seen)
53 string(REPLACE "-" ";" triple_components ${triple})
54 foreach(component ${triple_components})
55 string(TOLOWER "${component}" component_lower)
56 if(component_lower MATCHES "^darwin")
57 get_property(darwin_triple_seen GLOBAL PROPERTY ${seen_property})
58 if(darwin_triple_seen)
59 message(FATAL_ERROR "${error}")
61 set_property(GLOBAL PROPERTY ${seen_property} YES)
62 if(NOT RUNTIMES_BUILD_ALLOW_DARWIN)
63 message(FATAL_ERROR "\
64 ${error} Set RUNTIMES_BUILD_ALLOW_DARWIN to allow a single darwin triple.")
66 elseif(component_lower MATCHES "^ios|^macos|^tvos|^watchos")
67 message(FATAL_ERROR "${error}")
72 function(builtin_default_target compiler_rt_path)
73 cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
75 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)
76 # AIX should fold 32-bit & 64-bit arch libraries into a single archive.
77 if (TARGET_TRIPLE MATCHES "aix")
78 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)
81 llvm_ExternalProject_Add(builtins
82 ${compiler_rt_path}/lib/builtins
83 DEPENDS ${ARG_DEPENDS}
84 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
85 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
86 -DLLVM_DEFAULT_TARGET_TRIPLE=${TARGET_TRIPLE}
87 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default}
88 -DCMAKE_C_COMPILER_WORKS=ON
89 -DCMAKE_ASM_COMPILER_WORKS=ON
90 ${BUILTINS_CMAKE_ARGS}
91 PASSTHROUGH_PREFIXES COMPILER_RT
93 TARGET_TRIPLE ${TARGET_TRIPLE}
97 function(builtin_register_target compiler_rt_path target)
98 cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
100 check_apple_target(${target} builtin)
102 get_cmake_property(variableNames VARIABLES)
103 foreach(variableName ${variableNames})
104 string(FIND "${variableName}" "BUILTINS_${target}" out)
106 string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
107 string(REPLACE ";" "|" new_value "${${variableName}}")
108 list(APPEND ${target}_extra_args "-D${new_name}=${new_value}")
112 llvm_ExternalProject_Add(builtins-${target}
113 ${compiler_rt_path}/lib/builtins
114 DEPENDS ${ARG_DEPENDS}
115 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
116 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
117 -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
118 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
119 -DCMAKE_C_COMPILER_WORKS=ON
120 -DCMAKE_ASM_COMPILER_WORKS=ON
121 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
122 ${${target}_extra_args}
124 TARGET_TRIPLE ${target}
128 # If compiler-rt is present we need to build the builtin libraries first. This
129 # is required because the other runtimes need the builtin libraries present
130 # before the just-built compiler can pass the configuration tests.
131 get_compiler_rt_path(compiler_rt_path)
133 if(NOT LLVM_BUILTIN_TARGETS)
134 builtin_default_target(${compiler_rt_path}
135 DEPENDS clang-resource-headers)
137 if("default" IN_LIST LLVM_BUILTIN_TARGETS)
138 builtin_default_target(${compiler_rt_path}
139 DEPENDS clang-resource-headers)
140 list(REMOVE_ITEM LLVM_BUILTIN_TARGETS "default")
142 add_custom_target(builtins)
143 add_custom_target(install-builtins)
144 add_custom_target(install-builtins-stripped)
147 foreach(target ${LLVM_BUILTIN_TARGETS})
148 builtin_register_target(${compiler_rt_path} ${target}
149 DEPENDS clang-resource-headers)
151 add_dependencies(builtins builtins-${target})
152 add_dependencies(install-builtins install-builtins-${target})
153 add_dependencies(install-builtins-stripped install-builtins-${target}-stripped)
157 # We don't need to depend on the builtins if we're building instrumented
158 # because the next stage will use the same compiler used to build this stage.
159 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
160 add_dependencies(clang-bootstrap-deps builtins)
164 # We create a list the names of all the runtime projects in all uppercase and
165 # with dashes turned to underscores. This gives us the CMake variable prefixes
166 # for all variables that will apply to runtimes.
167 foreach(entry ${runtimes})
168 get_filename_component(projName ${entry} NAME)
169 if(projName STREQUAL "libc")
170 # For now, we will use the name "llvmlibc" for the libc project as it is
171 # not a full libc yet. Also, if we leave it as is, the "lib" prefix gets
172 # stripped below and the targets endup having the name "c", "check-c" etc.
173 set(projName "llvmlibc")
175 string(REPLACE "-" "_" canon_name ${projName})
176 string(TOUPPER ${canon_name} canon_name)
177 list(APPEND prefixes ${canon_name})
178 if (${canon_name} STREQUAL "OPENMP")
179 list(APPEND prefixes "LIBOMP" "LIBOMPTARGET")
181 # Many compiler-rt options start with SANITIZER_ rather than COMPILER_RT_,
182 # so when compiler-rt is enabled, consider both.
183 if(canon_name STREQUAL "COMPILER_RT")
184 list(APPEND prefixes SANITIZER)
187 string(FIND ${projName} "lib" LIB_IDX)
189 string(SUBSTRING ${projName} 3 -1 projName)
191 list(APPEND runtime_names ${projName})
194 function(runtime_default_target)
195 cmake_parse_arguments(ARG "" "" "DEPENDS;PREFIXES" ${ARGN})
197 include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
198 set(SUB_CHECK_TARGETS ${SUB_CHECK_TARGETS} PARENT_SCOPE)
199 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
201 foreach(runtime_name ${runtime_names})
202 list(APPEND extra_targets
204 install-${runtime_name}
205 install-${runtime_name}-stripped)
206 if(LLVM_INCLUDE_TESTS)
207 list(APPEND test_targets check-${runtime_name})
210 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
211 if(NOT ${component} IN_LIST SUB_COMPONENTS)
212 list(APPEND extra_targets ${component} install-${component} install-${component}-stripped)
216 if(LLVM_INCLUDE_TESTS)
217 list(APPEND test_targets runtimes-test-depends check-runtimes)
220 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)
221 # AIX should fold 32-bit & 64-bit arch libraries into a single archive.
222 if (TARGET_TRIPLE MATCHES "aix")
223 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)
226 llvm_ExternalProject_Add(runtimes
227 ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
228 DEPENDS ${ARG_DEPENDS}
229 # Builtins were built separately above
230 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
231 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
232 -DLLVM_DEFAULT_TARGET_TRIPLE=${TARGET_TRIPLE}
233 -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
234 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default}
235 -DLLVM_BUILD_TOOLS=${LLVM_BUILD_TOOLS}
236 -DCMAKE_C_COMPILER_WORKS=ON
237 -DCMAKE_CXX_COMPILER_WORKS=ON
238 -DCMAKE_ASM_COMPILER_WORKS=ON
239 ${RUNTIMES_CMAKE_ARGS}
240 PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
242 EXTRA_TARGETS ${extra_targets}
246 ${SUB_INSTALL_TARGETS}
248 TARGET_TRIPLE ${TARGET_TRIPLE}
252 # runtime_register_target(target)
253 # Utility function to register external runtime target.
254 function(runtime_register_target name target)
255 cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS" ${ARGN})
256 include(${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake OPTIONAL)
257 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake)
259 check_apple_target(${target} runtime)
261 set(${name}_deps ${ARG_DEPENDS})
262 if(NOT name STREQUAL target)
263 list(APPEND ${name}_deps runtimes-${target})
266 foreach(runtime_name ${runtime_names})
267 set(${runtime_name}-${name} ${runtime_name})
268 set(install-${runtime_name}-${name} install-${runtime_name})
269 set(install-${runtime_name}-${name}-stripped install-${runtime_name}-stripped)
270 list(APPEND ${name}_extra_targets ${runtime_name}-${name} install-${runtime_name}-${name} install-${runtime_name}-${name}-stripped)
271 if(LLVM_INCLUDE_TESTS)
272 set(check-${runtime_name}-${name} check-${runtime_name} )
273 list(APPEND ${name}_test_targets check-${runtime_name}-${name})
277 foreach(target_name IN LISTS SUB_COMPONENTS SUB_INSTALL_TARGETS)
278 set(${target_name}-${name} ${target_name})
279 list(APPEND ${name}_extra_targets ${target_name}-${name})
282 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
283 set(${component}-${name} ${component})
284 set(install-${component}-${name} install-${component})
285 set(install-${component}-${name}-stripped install-${component}-stripped)
286 list(APPEND ${name}_extra_targets ${component}-${name} install-${component}-${name} install-${component}-${name}-stripped)
289 if(LLVM_INCLUDE_TESTS)
290 set(runtimes-test-depends-${name} runtimes-test-depends)
291 set(check-runtimes-${name} check-runtimes)
292 list(APPEND ${name}_test_targets runtimes-test-depends-${name} check-runtimes-${name})
293 foreach(target_name IN LISTS SUB_CHECK_TARGETS)
294 set(${target_name}-${name} ${target_name})
295 list(APPEND ${name}_test_targets ${target_name}-${name})
296 list(APPEND test_targets ${target_name}-${name})
298 set(test_targets "${test_targets}" PARENT_SCOPE)
301 set(${name}_extra_args ${ARG_CMAKE_ARGS})
302 get_cmake_property(variableNames VARIABLES)
303 foreach(variableName ${variableNames})
304 string(FIND "${variableName}" "RUNTIMES_${target}_" out)
306 string(REPLACE "RUNTIMES_${target}_" "" new_name ${variableName})
307 string(REPLACE ";" "|" new_value "${${variableName}}")
308 list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
311 if(NOT "${name}" STREQUAL "${target}")
312 foreach(variableName ${variableNames})
313 string(FIND "${variableName}" "RUNTIMES_${name}_" out)
315 string(REPLACE "RUNTIMES_${name}_" "" new_name ${variableName})
316 string(REPLACE ";" "|" new_value "${${variableName}}")
317 list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
322 if(NOT RUNTIMES_${name}_LLVM_ENABLE_RUNTIMES AND NOT RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES)
323 string(REPLACE ";" "|" LLVM_ENABLE_RUNTIMES_PASSTHROUGH "${LLVM_ENABLE_RUNTIMES}")
324 list(APPEND ${name}_extra_args -DLLVM_ENABLE_RUNTIMES=${LLVM_ENABLE_RUNTIMES_PASSTHROUGH})
327 llvm_ExternalProject_Add(runtimes-${name}
328 ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
329 DEPENDS ${${name}_deps}
330 # Builtins were built separately above
331 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
332 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
333 -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
334 -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
335 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
336 -DCMAKE_C_COMPILER_WORKS=ON
337 -DCMAKE_CXX_COMPILER_WORKS=ON
338 -DCMAKE_ASM_COMPILER_WORKS=ON
339 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
340 -DLLVM_RUNTIMES_TARGET=${name}
341 ${${name}_extra_args}
342 EXTRA_TARGETS ${${name}_extra_targets}
343 ${${name}_test_targets}
345 TARGET_TRIPLE ${target}
350 # Create a runtimes target that uses this file as its top-level CMake file.
351 # The runtimes target is a configuration of all the runtime libraries
352 # together in a single CMake invocaiton.
353 if("openmp" IN_LIST LLVM_ENABLE_RUNTIMES)
354 message(STATUS "Adding dependencies opt llvm-link")
355 set(extra_deps opt llvm-link)
357 if(NOT LLVM_RUNTIME_TARGETS)
358 runtime_default_target(
359 DEPENDS ${deps} ${extra_deps}
360 PREFIXES ${prefixes})
361 set(test_targets check-runtimes)
363 if("default" IN_LIST LLVM_RUNTIME_TARGETS)
364 runtime_default_target(
365 DEPENDS ${deps} ${extra_deps}
366 PREFIXES ${prefixes})
367 list(REMOVE_ITEM LLVM_RUNTIME_TARGETS "default")
369 add_custom_target(runtimes)
370 add_custom_target(runtimes-configure)
371 add_custom_target(install-runtimes)
372 add_custom_target(install-runtimes-stripped)
373 if(LLVM_INCLUDE_TESTS)
374 add_custom_target(check-runtimes)
375 add_custom_target(runtimes-test-depends)
378 foreach(runtime_name ${runtime_names})
379 add_custom_target(${runtime_name})
380 add_custom_target(install-${runtime_name})
381 add_custom_target(install-${runtime_name}-stripped)
383 if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
384 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
385 add_custom_target(${component})
386 add_custom_target(install-${component})
387 add_custom_target(install-${component}-stripped)
392 foreach(name ${LLVM_RUNTIME_TARGETS})
393 runtime_register_target(${name} ${name}
396 add_dependencies(runtimes runtimes-${name})
397 add_dependencies(runtimes-configure runtimes-${name}-configure)
398 add_dependencies(install-runtimes install-runtimes-${name})
399 add_dependencies(install-runtimes-stripped install-runtimes-${name}-stripped)
400 if(LLVM_INCLUDE_TESTS)
401 add_dependencies(check-runtimes check-runtimes-${name})
402 add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
404 foreach(runtime_name ${runtime_names})
405 add_dependencies(${runtime_name} ${runtime_name}-${name})
406 add_dependencies(install-${runtime_name} install-${runtime_name}-${name})
407 add_dependencies(install-${runtime_name}-stripped install-${runtime_name}-${name}-stripped)
409 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
410 add_dependencies(${component} ${component}-${name})
411 add_dependencies(install-${component} install-${component}-${name})
412 add_dependencies(install-${component}-stripped install-${component}-${name}-stripped)
416 foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
417 foreach(name ${LLVM_RUNTIME_MULTILIB_${multilib}_TARGETS})
418 runtime_register_target(${name}+${multilib} ${name}
419 DEPENDS runtimes-${name}
420 CMAKE_ARGS -DLLVM_RUNTIMES_PREFIX=${name}/
421 -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib})
422 add_dependencies(runtimes runtimes-${name}+${multilib})
423 add_dependencies(runtimes-configure runtimes-${name}+${multilib}-configure)
424 add_dependencies(install-runtimes install-runtimes-${name}+${multilib})
425 add_dependencies(install-runtimes-stripped install-runtimes-${name}+${multilib}-stripped)
426 foreach(runtime_name ${runtime_names})
427 add_dependencies(${runtime_name} ${runtime_name}-${name}+${multilib})
428 add_dependencies(install-${runtime_name} install-${runtime_name}-${name}+${multilib})
429 add_dependencies(install-${runtime_name}-stripped install-${runtime_name}-${name}+${multilib}-stripped)
431 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
432 add_dependencies(${component} ${component}-${name}+${multilib})
433 add_dependencies(install-${component} install-${component}-${name}+${multilib})
434 add_dependencies(install-${component}-stripped install-${component}-${name}+${multilib}-stripped)
440 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
441 # TODO: This is a hack needed because the libcxx headers are copied into the
442 # build directory during configuration. Without that step the clang in the
443 # build directory cannot find the C++ headers in certain configurations.
444 # I need to build a mechanism for runtime projects to provide CMake code
445 # that executes at LLVM configuration time to handle this case.
446 add_dependencies(clang-bootstrap-deps runtimes-configure)
447 # We need to add the runtimes as a dependency because compiler-rt can be
448 # built as part of runtimes and we need the profile runtime for PGO
449 add_dependencies(clang-bootstrap-deps runtimes)
452 if(LLVM_INCLUDE_TESTS)
453 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
454 set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)
456 set(RUNTIMES_TEST_DEPENDS
469 foreach(target ${test_targets} ${SUB_CHECK_TARGETS})
470 add_dependencies(${target} ${RUNTIMES_TEST_DEPENDS})