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 $<CONFIGURATION> PARENT_SCOPE)
22  endif()
23endfunction()
24
25set(COMPILER_RT_SRC_ROOT ${LLVM_MAIN_SRC_DIR}/projects/compiler-rt)
26if(LLVM_BUILD_EXTERNAL_COMPILER_RT AND EXISTS ${COMPILER_RT_SRC_ROOT}/)
27  if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
28    set(cmake_3_4_USES_TERMINAL_OPTIONS
29      USES_TERMINAL_CONFIGURE 1
30      USES_TERMINAL_BUILD 1
31      USES_TERMINAL_INSTALL 1
32      )
33  endif()
34
35  # Add compiler-rt as an external project.
36  set(COMPILER_RT_PREFIX ${CMAKE_BINARY_DIR}/projects/compiler-rt)
37
38  set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/compiler-rt-stamps/)
39  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/compiler-rt-bins/)
40
41  add_custom_target(compiler-rt-clear
42    COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
43    COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
44    COMMENT "Clobberring compiler-rt build and stamp directories"
45    )
46
47  # Find all variables that start with COMPILER_RT and populate a variable with
48  # them.
49  get_cmake_property(variableNames VARIABLES)
50  foreach(variableName ${variableNames})
51    if(variableName MATCHES "^COMPILER_RT")
52      string(REPLACE ";" "\;" value "${${variableName}}")
53      list(APPEND COMPILER_RT_PASSTHROUGH_VARIABLES
54        -D${variableName}=${value})
55    endif()
56  endforeach()
57
58  ExternalProject_Add(compiler-rt
59    DEPENDS llvm-config clang
60    PREFIX ${COMPILER_RT_PREFIX}
61    SOURCE_DIR ${COMPILER_RT_SRC_ROOT}
62    STAMP_DIR ${STAMP_DIR}
63    BINARY_DIR ${BINARY_DIR}
64    CMAKE_ARGS ${CLANG_COMPILER_RT_CMAKE_ARGS}
65               -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
66               -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++
67               -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
68               -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
69               -DLLVM_CONFIG_PATH=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-config
70               -DCOMPILER_RT_OUTPUT_DIR=${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION}
71               -DCOMPILER_RT_EXEC_OUTPUT_DIR=${LLVM_RUNTIME_OUTPUT_INTDIR}
72               -DCOMPILER_RT_INSTALL_PATH:STRING=lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION}
73               -DCOMPILER_RT_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS}
74               -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
75               ${COMPILER_RT_PASSTHROUGH_VARIABLES}
76    INSTALL_COMMAND ""
77    STEP_TARGETS configure build
78    ${cmake_3_4_USES_TERMINAL_OPTIONS}
79    )
80
81  get_ext_project_build_command(run_clean_compiler_rt clean)
82  ExternalProject_Add_Step(compiler-rt clean
83    COMMAND ${run_clean_compiler_rt}
84    COMMENT "Cleaning compiler-rt..."
85    DEPENDEES configure
86    DEPENDERS build
87    DEPENDS clang
88    WORKING_DIRECTORY ${BINARY_DIR}
89    )
90
91  install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${BINARY_DIR}/cmake_install.cmake \)"
92    COMPONENT compiler-rt)
93
94  add_custom_target(install-compiler-rt
95                    DEPENDS compiler-rt
96                    COMMAND "${CMAKE_COMMAND}"
97                             -DCMAKE_INSTALL_COMPONENT=compiler-rt
98                             -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
99
100  # Add top-level targets that build specific compiler-rt runtimes.
101  set(COMPILER_RT_RUNTIMES asan builtins dfsan lsan msan profile tsan ubsan)
102  foreach(runtime ${COMPILER_RT_RUNTIMES})
103    get_ext_project_build_command(build_runtime_cmd ${runtime})
104    add_custom_target(${runtime}
105      COMMAND ${build_runtime_cmd}
106      DEPENDS compiler-rt-configure
107      WORKING_DIRECTORY ${BINARY_DIR}
108      VERBATIM)
109  endforeach()
110
111  if(LLVM_INCLUDE_TESTS)
112    # Add binaries that compiler-rt tests depend on.
113    set(COMPILER_RT_TEST_DEPENDENCIES
114      FileCheck count not llvm-nm llvm-objdump llvm-symbolizer)
115
116    # Add top-level targets for various compiler-rt test suites.
117    set(COMPILER_RT_TEST_SUITES check-asan check-asan-dynamic check-dfsan
118      check-lsan check-msan check-sanitizer check-tsan check-ubsan)
119    foreach(test_suite ${COMPILER_RT_TEST_SUITES})
120      get_ext_project_build_command(run_test_suite ${test_suite})
121      add_custom_target(${test_suite}
122        COMMAND ${run_test_suite}
123        DEPENDS compiler-rt-build ${COMPILER_RT_TEST_DEPENDENCIES}
124        WORKING_DIRECTORY ${BINARY_DIR}
125        VERBATIM)
126    endforeach()
127
128    # Add special target to run all compiler-rt test suites.
129    get_ext_project_build_command(run_check_compiler_rt check-all)
130    add_custom_target(check-compiler-rt
131      COMMAND ${run_check_compiler_rt}
132      DEPENDS compiler-rt-build ${COMPILER_RT_TEST_DEPENDENCIES}
133      WORKING_DIRECTORY ${BINARY_DIR}
134      VERBATIM)
135  endif()
136endif()
137