OSDN Git Service

Support for multiarch runtimes layout
[android-x86/external-llvm.git] / runtimes / CMakeLists.txt
1 # This file handles building LLVM runtime sub-projects.
2
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.
6
7 # In the current state this file only works with compiler-rt, other runtimes
8 # will work as the runtime build interface standardizes.
9
10 # Find all subdirectories containing CMake projects
11 file(GLOB entries *)
12 foreach(entry ${entries})
13   if(IS_DIRECTORY ${entry} AND EXISTS ${entry}/CMakeLists.txt)
14     list(APPEND runtimes ${entry})
15   endif()
16 endforeach()
17
18 # Side-by-side subprojects layout.
19 set(LLVM_ALL_RUNTIMES "libcxx;libcxxabi;libunwind;compiler-rt")
20 set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
21   "Semicolon-separated list of runtimes to build (${LLVM_ALL_RUNTIMES}), or \"all\".")
22 if(LLVM_ENABLE_RUNTIMES STREQUAL "all" )
23   set(LLVM_ENABLE_RUNTIMES ${LLVM_ALL_RUNTIMES})
24 endif()
25 foreach(proj ${LLVM_ENABLE_RUNTIMES})
26   set(proj_dir "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
27   if(IS_DIRECTORY ${proj_dir} AND EXISTS ${proj_dir}/CMakeLists.txt)
28     list(APPEND runtimes ${proj_dir})
29   else()
30     message(FATAL_ERROR "LLVM_ENABLE_RUNTIMES requests ${proj} but directory not found: ${proj_dir}")
31   endif()
32   string(TOUPPER "${proj}" canon_name)
33   STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})
34   set(LLVM_EXTERNAL_${canon_name}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../${proj}")
35 endforeach()
36
37 function(get_compiler_rt_path path)
38   foreach(entry ${runtimes})
39     get_filename_component(projName ${entry} NAME)
40     if("${projName}" MATCHES "compiler-rt")
41       set(${path} ${entry} PARENT_SCOPE)
42       return()
43     endif()
44   endforeach()
45 endfunction()
46
47 # If this file is acting as a top-level CMake invocation, this code path is
48 # triggered by the external project call for the runtimes target below.
49 if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
50
51   function(runtime_register_component name)
52     set_property(GLOBAL APPEND PROPERTY SUB_COMPONENTS ${name})
53   endfunction()
54
55   cmake_minimum_required(VERSION 3.4.3)
56   project(Runtimes C CXX ASM)
57
58   # Add the root project's CMake modules, and the LLVM build's modules to the
59   # CMake module path.
60   list(INSERT CMAKE_MODULE_PATH 0
61     "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
62     "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules"
63     "${LLVM_LIBRARY_DIR}/cmake/llvm"
64   )
65
66   # Some of the runtimes will conditionally use the compiler-rt sanitizers
67   # to make this work smoothly we ensure that compiler-rt is added first in
68   # the list of sub-projects. This allows other sub-projects to have checks
69   # like `if(TARGET asan)` to enable building with asan.
70   get_compiler_rt_path(compiler_rt_path)
71   if(compiler_rt_path)
72     list(REMOVE_ITEM runtimes ${compiler_rt_path})
73     if(NOT LLVM_BUILD_COMPILER_RT)
74       list(INSERT runtimes 0 ${compiler_rt_path})
75     endif()
76   endif()
77
78   # LLVMConfig.cmake contains a bunch of CMake variables from the LLVM build.
79   # This file is installed as part of LLVM distributions, so this can be used
80   # either from a build directory or an installed LLVM.
81   include(LLVMConfig)
82
83   # Setting these variables will allow the sub-build to put their outputs into
84   # the library and bin directories of the top-level build.
85   set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_DIR})
86   set(LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_TOOLS_BINARY_DIR})
87
88   # This variable makes sure that e.g. llvm-lit is found.
89   set(LLVM_MAIN_SRC_DIR ${LLVM_BUILD_MAIN_SRC_DIR})
90
91   if(APPLE)
92     set(LLVM_ENABLE_LIBCXX ON CACHE BOOL "")
93   endif()
94
95   set(SAFE_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
96   set(SAFE_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
97
98   include(CheckLibraryExists)
99   include(CheckCCompilerFlag)
100
101   check_library_exists(c fopen "" LLVM_HAS_C_LIB)
102   check_c_compiler_flag(-nodefaultlibs LLVM_HAS_NODEFAULTLIBS_FLAG)
103   if(LLVM_HAS_NODEFAULTLIBS_FLAG)
104     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
105     if(LLVM_HAS_C_LIB)
106       list(APPEND CMAKE_REQUIRED_LIBRARIES c)
107     endif()
108   endif()
109
110   # Avoid checking whether the compiler is working.
111   set(LLVM_COMPILER_CHECKED ON)
112   # Enable warnings, otherwise -w gets added to the cflags by HandleLLVMOptions
113   # resulting in unjustified successes by check_cxx_compiler_flag.
114   set(LLVM_ENABLE_WARNINGS ON)
115
116   # Handle common options used by all runtimes.
117   include(AddLLVM)
118   include(HandleLLVMOptions)
119
120   set(CMAKE_REQUIRED_FLAGS ${SAFE_CMAKE_REQUIRED_FLAGS})
121   set(CMAKE_REQUIRED_LIBRARIES ${SAFE_CMAKE_REQUIRED_LIBRARIES})
122
123   if(NOT LLVM_RUNTIMES_PREFIX)
124     set(LLVM_RUNTIMES_PREFIX "${LLVM_RUNTIMES_TARGET}/")
125   endif()
126
127   string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
128          ${PACKAGE_VERSION})
129
130   foreach(entry ${runtimes})
131     get_filename_component(projName ${entry} NAME)
132
133     # TODO: Clean this up as part of an interface standardization
134     string(REPLACE "-" "_" canon_name ${projName})
135     string(TOUPPER ${canon_name} canon_name)
136     # The subdirectories need to treat this as standalone builds
137     set(${canon_name}_STANDALONE_BUILD On)
138
139     # Setting a variable to let sub-projects detect which other projects
140     # will be included under here.
141     set(HAVE_${canon_name} On)
142   endforeach()
143
144   set(SAFE_LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
145   set(SAFE_LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
146   set(SAFE_LLVM_RUNTIME_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
147
148   # We do this in two loops so that HAVE_* is set for each runtime before the
149   # other runtimes are added.
150   foreach(entry ${runtimes})
151     get_filename_component(projName ${entry} NAME)
152
153     # Between each sub-project we want to cache and clear the LIT properties
154     set_property(GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
155     set_property(GLOBAL PROPERTY LLVM_LIT_PARAMS)
156     set_property(GLOBAL PROPERTY LLVM_LIT_DEPENDS)
157     set_property(GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
158
159     add_subdirectory(${entry} ${projName})
160
161     get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
162     get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
163     get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
164     get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
165
166     list(APPEND RUNTIMES_LIT_TESTSUITES ${LLVM_LIT_TESTSUITES})
167     list(APPEND RUNTIMES_LIT_PARAMS ${LLVM_LIT_PARAMS})
168     list(APPEND RUNTIMES_LIT_DEPENDS ${LLVM_LIT_DEPENDS})
169     list(APPEND RUNTIMES_LIT_EXTRA_ARGS ${LLVM_LIT_EXTRA_ARGS})
170
171     if(LLVM_RUNTIMES_TARGET)
172       if(NOT "${projName}" MATCHES "compiler-rt")
173         set(LLVM_BINARY_DIR "${SAFE_LLVM_BINARY_DIR}")
174         set(LLVM_LIBRARY_OUTPUT_INTDIR "${SAFE_LLVM_LIBRARY_OUTPUT_INTDIR}")
175         set(LLVM_RUNTIME_OUTPUT_INTDIR "${SAFE_LLVM_RUNTIME_OUTPUT_INTDIR}")
176       endif()
177     endif()
178   endforeach()
179
180   if(LLVM_INCLUDE_TESTS)
181     # Add a global check rule now that all subdirectories have been traversed
182     # and we know the total set of lit testsuites.
183
184     add_lit_target(check-runtimes
185       "Running all regression tests"
186       ${RUNTIMES_LIT_TESTSUITES}
187       PARAMS ${RUNTIMES_LIT_PARAMS}
188       DEPENDS ${RUNTIMES_LIT_DEPENDS}
189       ARGS ${RUNTIMES_LIT_EXTRA_ARGS}
190       )
191     add_custom_target(runtimes-test-depends DEPENDS ${RUNTIMES_LIT_DEPENDS})
192   endif()
193
194   get_property(SUB_COMPONENTS GLOBAL PROPERTY SUB_COMPONENTS)
195   if(SUB_COMPONENTS)
196     list(REMOVE_DUPLICATES SUB_COMPONENTS)
197     foreach(component ${SUB_COMPONENTS})
198       if(NOT TARGET ${component})
199         message(SEND_ERROR "Missing target for runtime component ${component}!")
200         continue()
201       endif()
202
203       if(TARGET check-${component})
204         list(APPEND SUB_CHECK_TARGETS check-${component})
205       endif()
206
207       if(TARGET install-${component})
208         list(APPEND SUB_INSTALL_TARGETS install-${component})
209       endif()
210       if(TARGET install-${component}-stripped)
211         list(APPEND SUB_INSTALL_TARGETS install-${component}-stripped)
212       endif()
213     endforeach()
214
215     if(LLVM_RUNTIMES_TARGET)
216       configure_file(
217         ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
218         ${LLVM_BINARY_DIR}/runtimes/${LLVM_RUNTIMES_TARGET}/Components.cmake)
219     else()
220       configure_file(
221         ${CMAKE_CURRENT_SOURCE_DIR}/Components.cmake.in
222         ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
223     endif()
224   endif()
225
226 else() # if this is included from LLVM's CMake
227   include(LLVMExternalProjectUtils)
228
229   if(NOT LLVM_BUILD_RUNTIMES)
230     set(EXTRA_ARGS EXCLUDE_FROM_ALL)
231   endif()
232
233   function(builtin_default_target compiler_rt_path)
234     llvm_ExternalProject_Add(builtins
235                              ${compiler_rt_path}/lib/builtins
236                              CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
237                                         -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
238                                         -DLLVM_DEFAULT_TARGET_TRIPLE=${TARGET_TRIPLE}
239                                         -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
240                                         -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
241                                         -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
242                                         -DCMAKE_C_COMPILER_WORKS=ON
243                                         -DCMAKE_ASM_COMPILER_WORKS=ON
244                              PASSTHROUGH_PREFIXES COMPILER_RT
245                                                   CLANG_DEFAULT
246                              USE_TOOLCHAIN
247                              ${EXTRA_ARGS})
248   endfunction()
249
250   function(builtin_register_target compiler_rt_path target)
251     string(REPLACE "-" ";" builtin_target_list ${target})
252     foreach(item ${builtin_target_list})
253       string(TOLOWER "${item}" item_lower)
254       if(item_lower MATCHES "darwin")
255         message(FATAL_ERROR "LLVM_BUILTIN_TARGETS isn't implemented for Darwin platform!")
256       endif()
257     endforeach()
258
259     get_cmake_property(variableNames VARIABLES)
260     foreach(variableName ${variableNames})
261       if(variableName MATCHES "^BUILTINS_${target}")
262         string(REPLACE "BUILTINS_${target}_" "" new_name ${variableName})
263         list(APPEND ${target}_extra_args "-D${new_name}=${${variableName}}")
264       endif()
265     endforeach()
266
267     llvm_ExternalProject_Add(builtins-${target}
268                              ${compiler_rt_path}/lib/builtins
269                              CMAKE_ARGS -DLLVM_LIBRARY_OUTPUT_INTDIR=${LLVM_LIBRARY_DIR}
270                                         -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_TOOLS_BINARY_DIR}
271                                         -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
272                                         -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
273                                         -DCMAKE_C_COMPILER_TARGET=${target}
274                                         -DCMAKE_ASM_COMPILER_TARGET=${target}
275                                         -DCMAKE_C_COMPILER_WORKS=ON
276                                         -DCMAKE_ASM_COMPILER_WORKS=ON
277                                         -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
278                                         ${${target}_extra_args}
279                              PASSTHROUGH_PREFIXES CLANG_DEFAULT
280                              TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip
281                              USE_TOOLCHAIN
282                              ${EXTRA_ARGS})
283   endfunction()
284
285   # If compiler-rt is present we need to build the builtin libraries first. This
286   # is required because the other runtimes need the builtin libraries present
287   # before the just-built compiler can pass the configuration tests.
288   get_compiler_rt_path(compiler_rt_path)
289   if(compiler_rt_path)
290     if(NOT LLVM_BUILTIN_TARGETS)
291       builtin_default_target(${compiler_rt_path})
292     else()
293       if("default" IN_LIST LLVM_BUILTIN_TARGETS)
294         builtin_default_target(${compiler_rt_path})
295         list(REMOVE_ITEM LLVM_BUILTIN_TARGETS "default")
296       else()
297         add_custom_target(builtins)
298         add_custom_target(install-builtins)
299         add_custom_target(install-builtins-stripped)
300       endif()
301
302       foreach(target ${LLVM_BUILTIN_TARGETS})
303         builtin_register_target(${compiler_rt_path} ${target})
304
305         add_dependencies(builtins builtins-${target})
306         add_dependencies(install-builtins install-builtins-${target})
307         add_dependencies(install-builtins-stripped install-builtins-${target}-stripped)
308       endforeach()
309     endif()
310     set(deps builtins)
311     # We don't need to depend on the builtins if we're building instrumented
312     # because the next stage will use the same compiler used to build this stage.
313     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
314       add_dependencies(clang-bootstrap-deps builtins)
315     endif()
316   endif()
317
318   # We create a list the names of all the runtime projects in all uppercase and
319   # with dashes turned to underscores. This gives us the CMake variable prefixes
320   # for all variables that will apply to runtimes.
321   foreach(entry ${runtimes})
322     get_filename_component(projName ${entry} NAME)
323     string(REPLACE "-" "_" canon_name ${projName})
324     string(TOUPPER ${canon_name} canon_name)
325     list(APPEND prefixes ${canon_name})
326
327     string(FIND ${projName} "lib" LIB_IDX)
328     if(LIB_IDX EQUAL 0)
329       string(SUBSTRING ${projName} 3 -1 projName)
330     endif()
331     list(APPEND runtime_names ${projName})
332   endforeach()
333
334   function(runtime_default_target)
335     cmake_parse_arguments(ARG "" "" "DEPS;PREFIXES" ${ARGN})
336
337     include(${LLVM_BINARY_DIR}/runtimes/Components.cmake OPTIONAL)
338     set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/Components.cmake)
339
340     foreach(runtime_name ${runtime_names})
341       list(APPEND extra_targets
342         ${runtime_name}
343         install-${runtime_name}
344         install-${runtime_name}-stripped)
345       if(LLVM_INCLUDE_TESTS)
346         list(APPEND test_targets check-${runtime_name})
347       endif()
348     endforeach()
349     foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
350       if(NOT ${component} IN_LIST SUB_COMPONENTS)
351         list(APPEND extra_targets ${component} install-${component} install-${component}-stripped)
352       endif()
353     endforeach()
354
355     if(LLVM_INCLUDE_TESTS)
356       list(APPEND test_targets runtimes-test-depends check-runtimes)
357     endif()
358
359     llvm_ExternalProject_Add(runtimes
360                              ${CMAKE_CURRENT_SOURCE_DIR}
361                              DEPENDS ${ARG_DEPS}
362                              # Builtins were built separately above
363                              CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
364                                         -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
365                                         -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
366                                         -DLLVM_DEFAULT_TARGET_TRIPLE=${TARGET_TRIPLE}
367                                         -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
368                                         -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE}
369                                         -DCMAKE_CXX_COMPILER_TARGET=${TARGET_TRIPLE}
370                                         -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE}
371                                         -DCMAKE_C_COMPILER_WORKS=ON
372                                         -DCMAKE_CXX_COMPILER_WORKS=ON
373                                         -DCMAKE_ASM_COMPILER_WORKS=ON
374                              PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
375                                                   CLANG_DEFAULT
376                                                   ${ARG_PREFIXES}
377                              EXTRA_TARGETS ${extra_targets}
378                                            ${test_targets}
379                                            ${SUB_COMPONENTS}
380                                            ${SUB_CHECK_TARGETS}
381                                            ${SUB_INSTALL_TARGETS}
382                              USE_TOOLCHAIN
383                              ${EXTRA_ARGS})
384   endfunction()
385
386   # runtime_register_target(target)
387   #   Utility function to register external runtime target.
388   function(runtime_register_target name target)
389     cmake_parse_arguments(ARG "" "" "DEPS" ${ARGN})
390     include(${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake OPTIONAL)
391     set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LLVM_BINARY_DIR}/runtimes/${name}/Components.cmake)
392
393     set(${name}_deps ${ARG_DEPS})
394     if(NOT name STREQUAL target)
395       list(APPEND ${name}_deps runtimes-${target})
396     endif()
397
398     foreach(runtime_name ${runtime_names})
399       set(${runtime_name}-${name} ${runtime_name})
400       set(install-${runtime_name}-${name} install-${runtime_name})
401       set(install-${runtime_name}-${name}-stripped install-${runtime_name}-stripped)
402       list(APPEND ${name}_extra_targets ${runtime_name}-${name} install-${runtime_name}-${name} install-${runtime_name}-${name}-stripped)
403       if(LLVM_INCLUDE_TESTS)
404         set(check-${runtime_name}-${name} check-${runtime_name} )
405         list(APPEND ${name}_test_targets check-${runtime_name}-${name})
406       endif()
407     endforeach()
408
409     foreach(target_name IN LISTS SUB_COMPONENTS SUB_INSTALL_TARGETS)
410       set(${target_name}-${name} ${target_name})
411       list(APPEND ${name}_extra_targets ${target_name}-${name})
412     endforeach()
413
414     foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
415       if(NOT "${target_name}:${target_name}-${component}" IN_LIST extra_targets)
416         list(APPEND ${name}_extra_targets
417           "${target_name}:${target_name}-${component}"
418           "${target_name}:${target_name}-install-${component}")
419       endif()
420     endforeach()
421
422     if(LLVM_INCLUDE_TESTS)
423       set(runtimes-test-depends-${name} runtimes-test-depends)
424       set(check-runtimes-${name} check-runtimes)
425       list(APPEND ${name}_test_targets runtimes-test-depends-${name} check-runtimes-${name})
426       foreach(target_name IN LISTS SUB_CHECK_TARGETS)
427         set(${target_name}-${name} ${target_name})
428         list(APPEND ${name}_test_targets ${target_name}-${name})
429         list(APPEND test_targets ${target_name}-${name})
430       endforeach()
431       set(test_targets "${test_targets}" PARENT_SCOPE)
432     endif()
433
434     get_cmake_property(variableNames VARIABLES)
435     foreach(variableName ${variableNames})
436       if(variableName MATCHES "^RUNTIMES_${name}")
437         string(REPLACE "RUNTIMES_${name}_" "" new_name ${variableName})
438         list(APPEND ${name}_extra_args "-D${new_name}=${${variableName}}")
439       elseif(variableName MATCHES "^RUNTIMES_${target}")
440         string(REPLACE "RUNTIMES_${target}_" "" new_name ${variableName})
441         list(APPEND ${name}_extra_args "-D${new_name}=${${variableName}}")
442       endif()
443     endforeach()
444
445     llvm_ExternalProject_Add(runtimes-${name}
446                              ${CMAKE_CURRENT_SOURCE_DIR}
447                              DEPENDS ${${name}_deps}
448                              # Builtins were built separately above
449                              CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off
450                                         -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
451                                         -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR}
452                                         -DLLVM_DEFAULT_TARGET_TRIPLE=${target}
453                                         -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON
454                                         -DCMAKE_C_COMPILER_TARGET=${target}
455                                         -DCMAKE_CXX_COMPILER_TARGET=${target}
456                                         -DCMAKE_ASM_COMPILER_TARGET=${target}
457                                         -DCMAKE_C_COMPILER_WORKS=ON
458                                         -DCMAKE_CXX_COMPILER_WORKS=ON
459                                         -DCMAKE_ASM_COMPILER_WORKS=ON
460                                         -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
461                                         -DLLVM_RUNTIMES_TARGET=${name}
462                                         ${${name}_extra_args}
463                              PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
464                                                   CLANG_DEFAULT
465                              TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip
466                              EXTRA_TARGETS ${${name}_extra_targets}
467                                            ${${name}_test_targets}
468                              USE_TOOLCHAIN
469                              ${EXTRA_ARGS})
470   endfunction()
471
472   if(runtimes)
473     # Create a runtimes target that uses this file as its top-level CMake file.
474     # The runtimes target is a configuration of all the runtime libraries
475     # together in a single CMake invocaiton.
476     if(NOT LLVM_RUNTIME_TARGETS)
477       runtime_default_target(
478         DEPS ${deps}
479         PREFIXES ${prefixes}
480         )
481     else()
482       if("default" IN_LIST LLVM_RUNTIME_TARGETS)
483         runtime_default_target(
484           DEPS ${deps}
485           PREFIXES ${prefixes})
486         list(REMOVE_ITEM LLVM_RUNTIME_TARGETS "default")
487       else()
488         add_custom_target(runtimes)
489         add_custom_target(runtimes-configure)
490         add_custom_target(install-runtimes)
491         add_custom_target(install-runtimes-stripped)
492         if(LLVM_INCLUDE_TESTS)
493           add_custom_target(check-runtimes)
494           add_custom_target(runtimes-test-depends)
495           set(test_targets "")
496         endif()
497         if(LLVM_RUNTIME_DISTRIBUTION_COMPONENTS)
498           foreach(component ${LLVM_RUNTIME_DISTRIBUTION_COMPONENTS})
499             add_custom_target(${component})
500             add_custom_target(install-${component})
501           endforeach()
502         endif()
503       endif()
504
505       foreach(name ${LLVM_RUNTIME_TARGETS})
506         runtime_register_target(${name} ${name}
507           DEPS ${deps}
508           )
509
510         add_dependencies(runtimes runtimes-${name})
511         add_dependencies(runtimes-configure runtimes-${name}-configure)
512         add_dependencies(install-runtimes install-runtimes-${name})
513         add_dependencies(install-runtimes-stripped install-runtimes-${name}-stripped)
514         if(LLVM_INCLUDE_TESTS)
515           add_dependencies(check-runtimes check-runtimes-${name})
516           add_dependencies(runtimes-test-depends runtimes-test-depends-${name})
517         endif()
518       endforeach()
519
520       foreach(sanitizer ${LLVM_RUNTIME_SANITIZERS})
521         if (sanitizer STREQUAL "Address")
522           set(sanitizer_name "asan")
523         elseif (sanitizer STREQUAL "Memory")
524           set(sanitizer_name "msan")
525         elseif (sanitizer STREQUAL "Thread")
526           set(sanitizer_name "tsan")
527         elseif (sanitizer STREQUAL "Undefined")
528           set(sanitizer_name "ubsan")
529         else()
530           message(FATAL_ERROR "Unsupported value of LLVM_RUNTIME_TARGET_SANITIZERS: ${sanitizers}")
531         endif()
532         foreach(name ${LLVM_RUNTIME_SANITIZER_${sanitizer}_TARGETS})
533           runtime_register_target(${name}-${sanitizer_name} ${name}
534             DEPS runtimes-${name}
535             CMAKE_ARGS -DLLVM_USE_SANITIZER=${sanitizer}
536                        -DLLVM_RUNTIMES_PREFIX=${name}/
537                        -DLLVM_RUNTIMES_LIBDIR_SUFFIX=/${sanitizer_name}
538             )
539           add_dependencies(runtimes runtimes-${name}-${sanitizer_name})
540           add_dependencies(runtimes-configure runtimes-${name}-${sanitizer_name}-configure)
541           add_dependencies(install-runtimes install-runtimes-${name}-${sanitizer_name})
542           add_dependencies(install-runtimes-stripped install-runtimes-${name}-${sanitizer_name}-stripped)
543         endforeach()
544       endforeach()
545     endif()
546
547     # TODO: This is a hack needed because the libcxx headers are copied into the
548     # build directory during configuration. Without that step the clang in the
549     # build directory cannot find the C++ headers in certain configurations.
550     # I need to build a mechanism for runtime projects to provide CMake code
551     # that executes at LLVM configuration time to handle this case.
552     if(NOT LLVM_BUILD_INSTRUMENTED AND CLANG_ENABLE_BOOTSTRAP)
553       add_dependencies(clang-bootstrap-deps runtimes-configure)
554     endif()
555
556     if(LLVM_INCLUDE_TESTS)
557       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS runtimes-test-depends)
558       set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-runtimes)
559
560       set(RUNTIMES_TEST_DEPENDS
561           FileCheck
562           count
563           llvm-nm
564           llvm-objdump
565           llvm-xray
566           not
567           obj2yaml
568           sancov
569           sanstats
570         )
571       foreach(target ${test_targets} ${SUB_CHECK_TARGETS})
572         add_dependencies(${target} ${RUNTIMES_TEST_DEPENDS})
573       endforeach()
574     endif()
575   endif()
576 endif()