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 set(COMMON_CMAKE_ARGS "-DHAVE_LLVM_LIT=ON")
7 foreach(proj ${LLVM_ENABLE_RUNTIMES})
8 set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
9 if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
10 list(APPEND runtimes ${proj_dir})
12 message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
14 string(TOUPPER "${proj}" canon_name)
15 STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
16 set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
19 function(get_compiler_rt_path path)
20 foreach(entry ${runtimes})
21 get_filename_component(projName ${entry} NAME)
22 if("${projName}" MATCHES "compiler-rt")
23 set(${path} ${entry} PARENT_SCOPE)
29 include(LLVMExternalProjectUtils)
31 if(NOT LLVM_BUILD_RUNTIMES)
32 set(EXTRA_ARGS EXCLUDE_FROM_ALL)
35 function(check_apple_target triple builtin_or_runtime)
37 compiler-rt for Darwin builds for all platforms and architectures using a \
38 single configuration. Specify only a single darwin triple (e.g. x86_64-apple-darwin) \
39 in your targets list (and not a triple for a specific platform such as macos). \
40 You can use variables such as COMPILER_RT_ENABLE_IOS and DARWIN_ios_ARCHS to \
41 control the specific platforms and architectures to build.")
43 set(seen_property ${builtin_or_runtime}_darwin_triple_seen)
44 string(REPLACE "-" ";" triple_components ${triple})
45 foreach(component ${triple_components})
46 string(TOLOWER "${component}" component_lower)
47 if(component_lower MATCHES "^darwin")
48 get_property(darwin_triple_seen GLOBAL PROPERTY ${seen_property})
49 if(darwin_triple_seen)
50 message(FATAL_ERROR "${error}")
52 set_property(GLOBAL PROPERTY ${seen_property} YES)
53 if(NOT RUNTIMES_BUILD_ALLOW_DARWIN)
54 message(FATAL_ERROR "\
55 ${error} Set RUNTIMES_BUILD_ALLOW_DARWIN to allow a single darwin triple.")
57 elseif(component_lower MATCHES "^ios|^macos|^tvos|^watchos")
58 message(FATAL_ERROR "${error}")
63 function(builtin_default_target compiler_rt_path)
64 cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
66 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)
67 # AIX should fold 32-bit & 64-bit arch libraries into a single archive.
68 if (LLVM_TARGET_TRIPLE MATCHES "aix")
69 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)
72 llvm_ExternalProject_Add(builtins
73 ${compiler_rt_path}/lib/builtins
74 DEPENDS ${ARG_DEPENDS}
75 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
76 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
77 -DLLVM_DEFAULT_TARGET_TRIPLE=${LLVM_TARGET_TRIPLE}
78 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default}
79 -DCMAKE_C_COMPILER_WORKS=ON
80 -DCMAKE_ASM_COMPILER_WORKS=ON
82 ${BUILTINS_CMAKE_ARGS}
83 PASSTHROUGH_PREFIXES COMPILER_RT
87 TARGET_TRIPLE ${LLVM_TARGET_TRIPLE}
91 function(builtin_register_target compiler_rt_path target)
92 cmake_parse_arguments(ARG "" "" "DEPENDS" ${ARGN})
94 check_apple_target(${target} builtin)
96 get_cmake_property(variableNames VARIABLES)
97 foreach(variableName ${variableNames})
98 string(FIND "${variableName}" "BUILTINS_${target}" out)
100 string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
101 string(REPLACE ";" "|" new_value "${${variableName}}")
102 list(APPEND ${target}_extra_args "-D${new_name}=${new_value}")
106 llvm_ExternalProject_Add(builtins-${target}
107 ${compiler_rt_path}/lib/builtins
108 DEPENDS ${ARG_DEPENDS}
109 CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
110 -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
111 -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
112 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
113 -DCMAKE_C_COMPILER_WORKS=ON
114 -DCMAKE_ASM_COMPILER_WORKS=ON
115 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
117 ${${target}_extra_args}
119 TARGET_TRIPLE ${target}
123 # If compiler-rt is present we need to build the builtin libraries first. This
124 # is required because the other runtimes need the builtin libraries present
125 # before the just-built compiler can pass the configuration tests.
126 get_compiler_rt_path(compiler_rt_path)
128 if(NOT LLVM_BUILTIN_TARGETS)
129 builtin_default_target(${compiler_rt_path}
130 DEPENDS clang-resource-headers)
132 if("default" IN_LIST LLVM_BUILTIN_TARGETS)
133 builtin_default_target(${compiler_rt_path}
134 DEPENDS clang-resource-headers)
135 list(REMOVE_ITEM LLVM_BUILTIN_TARGETS "default")
137 add_custom_target(builtins)
138 add_custom_target(install-builtins)
139 add_custom_target(install-builtins-stripped)
142 foreach(target ${LLVM_BUILTIN_TARGETS})
143 builtin_register_target(${compiler_rt_path} ${target}
144 DEPENDS clang-resource-headers)
146 add_dependencies(builtins builtins-${target})
147 add_dependencies(install-builtins install-builtins-${target})
148 add_dependencies(install-builtins-stripped install-builtins-${target}-stripped)
152 # We don't need to depend on the builtins if we're building instrumented
153 # because the next stage will use the same compiler used to build this stage.
154 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
155 add_dependencies(clang-bootstrap-deps builtins)
159 # Create a list with the names of all the runtime projects in all uppercase and
160 # with dashes turned to underscores. This gives us the CMake variable `prefixes`
161 # for all variables that will apply to runtimes.
162 foreach(entry ${runtimes})
163 get_filename_component(projName ${entry} NAME)
164 if(projName STREQUAL "libc")
165 # For now, we will use the name "llvmlibc" for the libc project as it is
166 # not a full libc yet. Also, if we leave it as is, the "lib" prefix gets
167 # stripped below and the targets endup having the name "c", "check-c" etc.
168 set(projName "llvmlibc")
170 string(REPLACE "-" "_" canon_name ${projName})
171 string(TOUPPER ${canon_name} canon_name)
172 list(APPEND prefixes ${canon_name})
173 if (${canon_name} STREQUAL "OPENMP")
174 list(APPEND prefixes "LIBOMP" "LIBOMPTARGET")
176 # Many compiler-rt options start with SANITIZER_ and DARWIN_ rather than
177 # COMPILER_RT_, so when compiler-rt is enabled, consider both.
178 if(canon_name STREQUAL "COMPILER_RT")
179 list(APPEND prefixes SANITIZER DARWIN)
182 string(FIND ${projName} "lib" LIB_IDX)
184 string(SUBSTRING ${projName} 3 -1 projName)
186 list(APPEND runtime_names ${projName})
189 function(runtime_default_target)
190 cmake_parse_arguments(ARG "" "" "DEPENDS;PREFIXES" ${ARGN})
192 include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
193 set(SUB_CHECK_TARGETS ${SUB_CHECK_TARGETS} PARENT_SCOPE)
194 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
196 foreach(runtime_name ${runtime_names})
197 list(APPEND extra_targets
199 install-${runtime_name}
200 install-${runtime_name}-stripped)
201 if(LLVM_INCLUDE_TESTS)
202 list(APPEND test_targets check-${runtime_name})
205 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
206 if(NOT ${component} IN_LIST SUB_COMPONENTS)
207 list(APPEND extra_targets install-${component} install-${component}-stripped)
211 if(LLVM_INCLUDE_TESTS)
212 include(${LLVM_BINARY_DIR}/runtimes/Tests.cmake OPTIONAL RESULT_VARIABLE have_tests)
213 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Tests.cmake)
215 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_TESTSUITES ${SUB_LIT_TESTSUITES})
216 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_PARAMS ${SUB_LIT_PARAMS})
217 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_EXTRA_ARGS ${SUB_LIT_EXTRA_ARGS})
219 list(APPEND test_targets runtimes-test-depends check-runtimes)
222 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)
223 # AIX should fold 32-bit & 64-bit arch libraries into a single archive.
224 if (LLVM_TARGET_TRIPLE MATCHES "aix")
225 set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)
228 llvm_ExternalProject_Add(runtimes
229 ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
230 DEPENDS ${ARG_DEPENDS}
231 # Builtins were built separately above
232 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
233 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
234 -DLLVM_DEFAULT_TARGET_TRIPLE=${LLVM_TARGET_TRIPLE}
235 -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
236 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default}
237 -DLLVM_BUILD_TOOLS=${LLVM_BUILD_TOOLS}
238 -DCMAKE_C_COMPILER_WORKS=ON
239 -DCMAKE_CXX_COMPILER_WORKS=ON
240 -DCMAKE_ASM_COMPILER_WORKS=ON
242 ${RUNTIMES_CMAKE_ARGS}
243 PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
246 EXTRA_TARGETS ${extra_targets}
250 ${SUB_INSTALL_TARGETS}
252 TARGET_TRIPLE ${LLVM_TARGET_TRIPLE}
256 # runtime_register_target(target)
257 # Utility function to register external runtime target.
258 function(runtime_register_target name target)
259 cmake_parse_arguments(ARG "" "" "DEPENDS;CMAKE_ARGS" ${ARGN})
260 include(${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake OPTIONAL)
261 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake)
263 check_apple_target(${target} runtime)
265 set(${name}_deps ${ARG_DEPENDS})
266 if(NOT name STREQUAL target)
267 list(APPEND ${name}_deps runtimes-${target})
270 foreach(runtime_name ${runtime_names})
271 set(${runtime_name}-${name} ${runtime_name})
272 set(install-${runtime_name}-${name} install-${runtime_name})
273 set(install-${runtime_name}-${name}-stripped install-${runtime_name}-stripped)
274 list(APPEND ${name}_extra_targets ${runtime_name}-${name} install-${runtime_name}-${name} install-${runtime_name}-${name}-stripped)
275 if(LLVM_INCLUDE_TESTS)
276 set(check-${runtime_name}-${name} check-${runtime_name} )
277 list(APPEND ${name}_test_targets check-${runtime_name}-${name})
281 foreach(target_name IN LISTS SUB_COMPONENTS SUB_INSTALL_TARGETS)
282 set(${target_name}-${name} ${target_name})
283 list(APPEND ${name}_extra_targets ${target_name}-${name})
286 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
287 set(${component}-${name} ${component})
288 set(install-${component}-${name} install-${component})
289 set(install-${component}-${name}-stripped install-${component}-stripped)
290 list(APPEND ${name}_extra_targets ${component}-${name} install-${component}-${name} install-${component}-${name}-stripped)
293 if(LLVM_INCLUDE_TESTS)
294 include(${LLVM_BINARY_DIR}/runtimes/${name}/Tests.cmake OPTIONAL RESULT_VARIABLE have_tests)
295 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Tests.cmake)
297 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_TESTSUITES ${SUB_LIT_TESTSUITES})
298 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_PARAMS ${SUB_LIT_PARAMS})
299 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_LIT_EXTRA_ARGS ${SUB_LIT_EXTRA_ARGS})
301 set(runtimes-test-depends-${name} runtimes-test-depends)
302 set(check-runtimes-${name} check-runtimes)
303 list(APPEND ${name}_test_targets runtimes-test-depends-${name} check-runtimes-${name})
304 list(APPEND test_targets ${${name}_test_targets})
305 foreach(target_name IN LISTS SUB_CHECK_TARGETS)
306 set(${target_name}-${name} ${target_name})
307 list(APPEND ${name}_test_targets ${target_name}-${name})
308 list(APPEND test_targets ${target_name}-${name})
310 set(test_targets "${test_targets}" PARENT_SCOPE)
313 set(${name}_extra_args ${ARG_CMAKE_ARGS})
314 get_cmake_property(variableNames VARIABLES)
315 foreach(variableName ${variableNames})
316 string(FIND "${variableName}" "RUNTIMES_${target}_" out)
318 string(REPLACE "RUNTIMES_${target}_" "" new_name ${variableName})
319 string(REPLACE ";" "|" new_value "${${variableName}}")
320 list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
323 if(NOT "${name}" STREQUAL "${target}")
324 foreach(variableName ${variableNames})
325 string(FIND "${variableName}" "RUNTIMES_${name}_" out)
327 string(REPLACE "RUNTIMES_${name}_" "" new_name ${variableName})
328 string(REPLACE ";" "|" new_value "${${variableName}}")
329 list(APPEND ${name}_extra_args "-D${new_name}=${new_value}")
334 if(NOT RUNTIMES_${name}_LLVM_ENABLE_RUNTIMES AND NOT RUNTIMES_${target}_LLVM_ENABLE_RUNTIMES)
335 string(REPLACE ";" "|" LLVM_ENABLE_RUNTIMES_PASSTHROUGH "${LLVM_ENABLE_RUNTIMES}")
336 list(APPEND ${name}_extra_args -DLLVM_ENABLE_RUNTIMES=${LLVM_ENABLE_RUNTIMES_PASSTHROUGH})
339 if(NOT RUNTIMES_${name}_LLVM_USE_LINKER AND NOT RUNTIMES_${target}_LLVM_USE_LINKER)
340 list(APPEND ${name}_extra_args -DLLVM_USE_LINKER=${LLVM_USE_LINKER})
343 llvm_ExternalProject_Add(runtimes-${name}
344 ${CMAKE_CURRENT_SOURCE_DIR}/../../runtimes
345 DEPENDS ${${name}_deps}
346 # Builtins were built separately above
347 CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
348 -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
349 -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
350 -DLLVM_ENABLE_PROJECTS_USED=${LLVM_ENABLE_PROJECTS_USED}
351 -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
352 -DCMAKE_C_COMPILER_WORKS=ON
353 -DCMAKE_CXX_COMPILER_WORKS=ON
354 -DCMAKE_ASM_COMPILER_WORKS=ON
355 -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
356 -DLLVM_RUNTIMES_TARGET=${name}
358 ${${name}_extra_args}
359 EXTRA_TARGETS ${${name}_extra_targets}
360 ${${name}_test_targets}
362 TARGET_TRIPLE ${target}
367 # Create a runtimes target that uses this file as its top-level CMake file.
368 # The runtimes target is a configuration of all the runtime libraries
369 # together in a single CMake invocaiton.
371 if("openmp" IN_LIST LLVM_ENABLE_RUNTIMES)
373 list(APPEND extra_deps opt)
376 list(APPEND extra_deps llvm-link)
379 if(NOT LLVM_RUNTIME_TARGETS)
380 runtime_default_target(
381 DEPENDS ${deps} ${extra_deps}
382 PREFIXES ${prefixes})
383 set(test_targets check-runtimes)
385 if("default" IN_LIST LLVM_RUNTIME_TARGETS)
386 runtime_default_target(
387 DEPENDS ${deps} ${extra_deps}
388 PREFIXES ${prefixes})
389 list(REMOVE_ITEM LLVM_RUNTIME_TARGETS "default")
391 add_custom_target(runtimes)
392 add_custom_target(runtimes-configure)
393 add_custom_target(install-runtimes)
394 add_custom_target(install-runtimes-stripped)
395 if(LLVM_INCLUDE_TESTS)
396 add_custom_target(check-runtimes)
397 add_custom_target(runtimes-test-depends)
400 foreach(runtime_name ${runtime_names})
401 add_custom_target(${runtime_name})
402 add_custom_target(install-${runtime_name})
403 add_custom_target(install-${runtime_name}-stripped)
405 if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
406 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
407 add_custom_target(${component})
408 add_custom_target(install-${component})
409 add_custom_target(install-${component}-stripped)
414 foreach(name ${LLVM_RUNTIME_TARGETS})
415 runtime_register_target(${name} ${name}
418 add_dependencies(runtimes runtimes-${name})
419 add_dependencies(runtimes-configure runtimes-${name}-configure)
420 add_dependencies(install-runtimes install-runtimes-${name})
421 add_dependencies(install-runtimes-stripped install-runtimes-${name}-stripped)
422 if(LLVM_INCLUDE_TESTS)
423 add_dependencies(check-runtimes check-runtimes-${name})
424 add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
426 foreach(runtime_name ${runtime_names})
427 add_dependencies(${runtime_name} ${runtime_name}-${name})
428 add_dependencies(install-${runtime_name} install-${runtime_name}-${name})
429 add_dependencies(install-${runtime_name}-stripped install-${runtime_name}-${name}-stripped)
431 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
432 add_dependencies(${component} ${component}-${name})
433 add_dependencies(install-${component} install-${component}-${name})
434 add_dependencies(install-${component}-stripped install-${component}-${name}-stripped)
438 foreach(multilib ${LLVM_RUNTIME_MULTILIBS})
439 foreach(name ${LLVM_RUNTIME_MULTILIB_${multilib}_TARGETS})
440 runtime_register_target(${name}+${multilib} ${name}
441 DEPENDS runtimes-${name}
442 CMAKE_ARGS -DLLVM_RUNTIMES_PREFIX=${name}/
443 -DLLVM_RUNTIMES_LIBDIR_SUBDIR=${multilib})
444 add_dependencies(runtimes runtimes-${name}+${multilib})
445 add_dependencies(runtimes-configure runtimes-${name}+${multilib}-configure)
446 add_dependencies(install-runtimes install-runtimes-${name}+${multilib})
447 add_dependencies(install-runtimes-stripped install-runtimes-${name}+${multilib}-stripped)
448 foreach(runtime_name ${runtime_names})
449 add_dependencies(${runtime_name} ${runtime_name}-${name}+${multilib})
450 add_dependencies(install-${runtime_name} install-${runtime_name}-${name}+${multilib})
451 add_dependencies(install-${runtime_name}-stripped install-${runtime_name}-${name}+${multilib}-stripped)
453 foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
454 add_dependencies(${component} ${component}-${name}+${multilib})
455 add_dependencies(install-${component} install-${component}-${name}+${multilib})
456 add_dependencies(install-${component}-stripped install-${component}-${name}+${multilib}-stripped)
462 if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
463 # TODO: This is a hack needed because the libcxx headers are copied into the
464 # build directory during configuration. Without that step the clang in the
465 # build directory cannot find the C++ headers in certain configurations.
466 # I need to build a mechanism for runtime projects to provide CMake code
467 # that executes at LLVM configuration time to handle this case.
468 add_dependencies(clang-bootstrap-deps runtimes-configure)
469 # We need to add the runtimes as a dependency because compiler-rt can be
470 # built as part of runtimes and we need the profile runtime for PGO
471 add_dependencies(clang-bootstrap-deps runtimes)
474 if(LLVM_INCLUDE_TESTS)
475 set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
477 set(RUNTIMES_TEST_DEPENDS
490 foreach(target ${test_targets} ${SUB_CHECK_TARGETS})
491 add_dependencies(${target} ${RUNTIMES_TEST_DEPENDS})