1# TODO: Set the install directory.
2
3include(ExternalProject)
4
5set(known_subdirs
6  "libcxx"
7  )
8
9foreach (dir ${known_subdirs})
10  if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${dir}/CMakeLists.txt)
11    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${dir})
12  endif()
13endforeach()
14
15function(get_ext_project_build_command out_var target)
16  if (CMAKE_GENERATOR MATCHES "Make")
17    # Use special command for Makefiles to support parallelism.
18    set(${out_var} "$(MAKE)" "${target}" PARENT_SCOPE)
19  else()
20    set(${out_var} ${CMAKE_COMMAND} --build . --target ${target}
21                                    --config $<CONFIG> PARENT_SCOPE)
22  endif()
23endfunction()
24
25set(COMPILER_RT_SRC_ROOT ${LLVM_MAIN_SRC_DIR}/projects/compiler-rt)
26# Fallback to the external path, if the other one isn't available.
27# This is the same behavior (try "internal", then check the LLVM_EXTERNAL_...
28# variable) as in add_llvm_external_project
29if(NOT EXISTS ${COMPILER_RT_SRC_ROOT})
30  # We don't want to set it if LLVM_EXTERNAL_COMPILER_RT_SOURCE_DIR is ""
31  if(LLVM_EXTERNAL_COMPILER_RT_SOURCE_DIR)
32    set(COMPILER_RT_SRC_ROOT ${LLVM_EXTERNAL_COMPILER_RT_SOURCE_DIR})
33  endif()
34endif()
35
36if(LLVM_BUILD_EXTERNAL_COMPILER_RT AND EXISTS ${COMPILER_RT_SRC_ROOT}/)
37
38  # Add compiler-rt as an external project.
39  set(COMPILER_RT_PREFIX ${CMAKE_BINARY_DIR}/projects/compiler-rt)
40
41  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/compiler-rt-stamps/)
42  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/compiler-rt-bins/)
43
44  add_custom_target(compiler-rt-clear
45    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
46    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
47    COMMENT "Clobberring compiler-rt build and stamp directories"
48    )
49
50  # Find all variables that start with COMPILER_RT and populate a variable with
51  # them.
52  get_cmake_property(variableNames VARIABLES)
53  foreach(variableName ${variableNames})
54    if(variableName MATCHES "^COMPILER_RT")
55      string(REPLACE ";" "\;" value "${${variableName}}")
56      list(APPEND COMPILER_RT_PASSTHROUGH_VARIABLES
57        -D${variableName}=${value})
58    endif()
59  endforeach()
60
61  set(compiler_rt_configure_deps)
62  if(TARGET cxx-headers)
63    list(APPEND compiler_rt_configure_deps "cxx-headers")
64  endif()
65  if(LLVM_INCLUDE_TESTS)
66    list(APPEND compiler_rt_configure_deps LLVMTestingSupport)
67  endif()
68
69  ExternalProject_Add(compiler-rt
70    DEPENDS llvm-config clang ${compiler_rt_configure_deps}
71    PREFIX ${COMPILER_RT_PREFIX}
72    SOURCE_DIR ${COMPILER_RT_SRC_ROOT}
73    STAMP_DIR ${STAMP_DIR}
74    BINARY_DIR ${BINARY_DIR}
75    CMAKE_ARGS ${CLANG_COMPILER_RT_CMAKE_ARGS}
76               -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
77               -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
78               -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
79               -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
80               -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
81               -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config
82               -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
83               -DCOMPILER_RT_OUTPUT_DIR=${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION}
84               -DCOMPILER_RT_EXEC_OUTPUT_DIR=${LLVM_RUNTIME_OUTPUT_INTDIR}
85               -DCOMPILER_RT_INSTALL_PATH:STRING=lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}
86               -DCOMPILER_RT_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
87               -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
88               -DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX}
89               -DLLVM_RUNTIME_OUTPUT_INTDIR=${LLVM_RUNTIME_OUTPUT_INTDIR}
90               -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
91               -DCMAKE_OSX_SYSROOT:PATH=${CMAKE_OSX_SYSROOT}
92               ${COMPILER_RT_PASSTHROUGH_VARIABLES}
93    INSTALL_COMMAND ""
94    STEP_TARGETS configure build
95    USES_TERMINAL_CONFIGURE 1
96    USES_TERMINAL_BUILD 1
97    USES_TERMINAL_INSTALL 1
98    )
99
100  get_ext_project_build_command(run_clean_compiler_rt clean)
101  ExternalProject_Add_Step(compiler-rt clean
102    COMMAND ${run_clean_compiler_rt}
103    COMMENT "Cleaning compiler-rt..."
104    DEPENDEES configure
105    DEPENDERS build
106    DEPENDS clang
107    WORKING_DIRECTORY ${BINARY_DIR}
108    )
109
110  install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${BINARY_DIR}/cmake_install.cmake \)"
111    COMPONENT compiler-rt)
112
113  add_llvm_install_targets(install-compiler-rt
114                           DEPENDS compiler-rt
115                           COMPONENT compiler-rt)
116
117  # Add top-level targets that build specific compiler-rt runtimes.
118  set(COMPILER_RT_RUNTIMES fuzzer asan builtins dfsan lsan msan profile tsan ubsan ubsan-minimal)
119  foreach(runtime ${COMPILER_RT_RUNTIMES})
120    get_ext_project_build_command(build_runtime_cmd ${runtime})
121    add_custom_target(${runtime}
122      COMMAND ${build_runtime_cmd}
123      DEPENDS compiler-rt-configure
124      WORKING_DIRECTORY ${BINARY_DIR}
125      VERBATIM USES_TERMINAL)
126  endforeach()
127
128  if(LLVM_INCLUDE_TESTS)
129    # Add binaries that compiler-rt tests depend on.
130    set(COMPILER_RT_TEST_DEPENDENCIES
131      FileCheck count not llvm-nm llvm-objdump llvm-symbolizer)
132
133    # Add top-level targets for various compiler-rt test suites.
134    set(COMPILER_RT_TEST_SUITES check-fuzzer check-asan check-hwasan check-asan-dynamic check-dfsan
135      check-lsan check-msan check-sanitizer check-tsan check-ubsan check-ubsan-minimal
136      check-profile check-cfi check-cfi-and-supported check-safestack check-gwp_asan)
137    foreach(test_suite ${COMPILER_RT_TEST_SUITES})
138      get_ext_project_build_command(run_test_suite ${test_suite})
139      add_custom_target(${test_suite}
140        COMMAND ${run_test_suite}
141        DEPENDS compiler-rt-build ${COMPILER_RT_TEST_DEPENDENCIES}
142        WORKING_DIRECTORY ${BINARY_DIR}
143        VERBATIM
144        USES_TERMINAL
145        )
146    endforeach()
147
148    # Add special target to run all compiler-rt test suites.
149    get_ext_project_build_command(run_check_compiler_rt check-all)
150    add_custom_target(check-compiler-rt
151      COMMAND ${run_check_compiler_rt}
152      DEPENDS compiler-rt-build ${COMPILER_RT_TEST_DEPENDENCIES}
153      WORKING_DIRECTORY ${BINARY_DIR}
154      VERBATIM USES_TERMINAL)
155
156    # Add special target to run all compiler-rt test suites.
157    get_ext_project_build_command(run_check_compiler_rt compiler-rt-test-depends)
158    add_custom_target(compiler-rt-test-depends
159      COMMAND ${run_check_compiler_rt}
160      DEPENDS compiler-rt-build ${COMPILER_RT_TEST_DEPENDENCIES}
161      WORKING_DIRECTORY ${BINARY_DIR}
162      VERBATIM USES_TERMINAL)
163    set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_DEPENDS compiler-rt-test-depends)
164    set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-compiler-rt)
165  endif()
166endif()
167