blob: f68f38407eaac4ab066550db533049feebfb67ba [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001include(ExternalProject)
2
3# llvm_ExternalProject_BuildCmd(out_var target)
4# Utility function for constructing command lines for external project targets
5function(llvm_ExternalProject_BuildCmd out_var target bin_dir)
6 cmake_parse_arguments(ARG "" "CONFIGURATION" "" ${ARGN})
7 if(NOT ARG_CONFIGURATION)
Andrew Walbran3d2c1972020-04-07 12:24:26 +01008 set(ARG_CONFIGURATION "$<CONFIG>")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01009 endif()
10 if (CMAKE_GENERATOR MATCHES "Make")
11 # Use special command for Makefiles to support parallelism.
12 set(${out_var} "$(MAKE)" "-C" "${bin_dir}" "${target}" PARENT_SCOPE)
13 else()
14 set(${out_var} ${CMAKE_COMMAND} --build ${bin_dir} --target ${target}
15 --config ${ARG_CONFIGURATION} PARENT_SCOPE)
16 endif()
17endfunction()
18
19# llvm_ExternalProject_Add(name source_dir ...
20# USE_TOOLCHAIN
21# Use just-built tools (see TOOLCHAIN_TOOLS)
22# EXCLUDE_FROM_ALL
23# Exclude this project from the all target
24# NO_INSTALL
25# Don't generate install targets for this project
26# ALWAYS_CLEAN
27# Always clean the sub-project before building
28# CMAKE_ARGS arguments...
29# Optional cmake arguments to pass when configuring the project
30# TOOLCHAIN_TOOLS targets...
31# Targets for toolchain tools (defaults to clang;lld)
32# DEPENDS targets...
33# Targets that this project depends on
34# EXTRA_TARGETS targets...
35# Extra targets in the subproject to generate targets for
36# PASSTHROUGH_PREFIXES prefix...
37# Extra variable prefixes (name is always included) to pass down
Andrew Walbran3d2c1972020-04-07 12:24:26 +010038# STRIP_TOOL path
39# Use provided strip tool instead of the default one.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040# )
41function(llvm_ExternalProject_Add name source_dir)
42 cmake_parse_arguments(ARG
43 "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
44 "SOURCE_DIR"
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010046 ${ARGN})
47 canonicalize_tool_name(${name} nameCanon)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020048
49 foreach(arg ${ARG_CMAKE_ARGS})
50 if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
51 string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
52 endif()
53 endforeach()
54
55 # If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
56 # reflect CMake's own default.
57 if (NOT _cmake_system_name)
58 set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
59 endif()
60
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010061 if(NOT ARG_TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020062 set(ARG_TOOLCHAIN_TOOLS clang)
63 # AIX 64-bit XCOFF and big AR format is not yet supported in some of these tools.
64 if(NOT _cmake_system_name STREQUAL AIX)
65 list(APPEND ARG_TOOLCHAIN_TOOLS lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
66 if(_cmake_system_name STREQUAL Darwin)
67 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-libtool-darwin llvm-lipo)
68 elseif(_cmake_system_name STREQUAL Windows)
69 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-lib)
70 else()
71 # TODO: These tools don't fully support Mach-O format yet.
72 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip)
73 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 endif()
75 endif()
76 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
77 if(TARGET ${tool})
78 list(APPEND TOOLCHAIN_TOOLS ${tool})
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020079
80 # $<TARGET_FILE:tgt> only works on add_executable or add_library targets
81 # The below logic mirrors cmake's own implementation
82 get_target_property(target_type "${tool}" TYPE)
83 if(NOT target_type STREQUAL "OBJECT_LIBRARY" AND
84 NOT target_type STREQUAL "UTILITY" AND
85 NOT target_type STREQUAL "GLOBAL_TARGET" AND
86 NOT target_type STREQUAL "INTERFACE_LIBRARY")
87 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
88 endif()
89
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090 endif()
91 endforeach()
92
93 if(NOT ARG_RUNTIME_LIBRARIES)
94 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
95 endif()
96 foreach(lib ${ARG_RUNTIME_LIBRARIES})
97 if(TARGET ${lib})
98 list(APPEND RUNTIME_LIBRARIES ${lib})
99 endif()
100 endforeach()
101
102 if(ARG_ALWAYS_CLEAN)
103 set(always_clean clean)
104 endif()
105
106 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
107 if(FOUND_CLANG GREATER -1)
108 set(CLANG_IN_TOOLCHAIN On)
109 endif()
110
111 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
112 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
113 endif()
114
115 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
116 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
117
118 add_custom_target(${name}-clear
119 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
120 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
121 COMMENT "Clobbering ${name} build and stamp directories"
122 USES_TERMINAL
123 )
124
125 # Find all variables that start with a prefix and propagate them through
126 get_cmake_property(variableNames VARIABLES)
127
128 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
129 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
130 foreach(variableName ${variableNames})
131 if(variableName MATCHES "^${prefix}")
132 string(REPLACE ";" "|" value "${${variableName}}")
133 list(APPEND PASSTHROUGH_VARIABLES
134 -D${variableName}=${value})
135 endif()
136 endforeach()
137 endforeach()
138
139 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
140 if(CLANG_IN_TOOLCHAIN)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100141 if(_cmake_system_name STREQUAL Windows)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200142 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
143 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
144 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100145 else()
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200146 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
147 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
148 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100149 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100150 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100151 if(lld IN_LIST TOOLCHAIN_TOOLS)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100152 if(_cmake_system_name STREQUAL Windows)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200153 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
154 elseif(NOT _cmake_system_name STREQUAL Darwin)
155 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100156 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100157 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100158 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200159 if(_cmake_system_name STREQUAL Windows)
160 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib${CMAKE_EXECUTABLE_SUFFIX})
161 else()
162 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
163 endif()
164 endif()
165 if(llvm-libtool-darwin IN_LIST TOOLCHAIN_TOOLS)
166 list(APPEND compiler_args -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin${CMAKE_EXECUTABLE_SUFFIX})
167 endif()
168 if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
169 list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 endif()
171 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200172 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100173 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100174 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200175 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100176 endif()
177 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200178 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179 endif()
180 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200181 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100182 endif()
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100183 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200184 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100185 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
187 endif()
188
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100189 if(ARG_STRIP_TOOL)
190 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
191 endif()
192
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100193 add_custom_command(
194 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
195 DEPENDS ${ARG_DEPENDS}
196 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
197 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
198 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
199 COMMENT "Clobbering bootstrap build and stamp directories"
200 )
201
202 add_custom_target(${name}-clobber
203 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
204
205 if(ARG_EXCLUDE_FROM_ALL)
206 set(exclude EXCLUDE_FROM_ALL 1)
207 endif()
208
209 if(CMAKE_SYSROOT)
210 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
211 endif()
212
213 if(CMAKE_CROSSCOMPILING)
214 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
215 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100216 -DCMAKE_LINKER=${CMAKE_LINKER}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 -DCMAKE_AR=${CMAKE_AR}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100218 -DCMAKE_RANLIB=${CMAKE_RANLIB}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200219 -DCMAKE_LIPO=${CMAKE_LIPO}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100220 -DCMAKE_NM=${CMAKE_NM}
221 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
222 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
223 -DCMAKE_STRIP=${CMAKE_STRIP})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100224 set(llvm_config_path ${LLVM_CONFIG_PATH})
Andrew Walbran16937d02019-10-22 13:54:20 +0100225
226 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
227 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
228 ${PACKAGE_VERSION})
229 set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}")
230 set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
231 foreach(type ${flag_types})
232 set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
233 endforeach()
234 string(REPLACE ";" "|" flag_string "${flag_types}")
235 foreach(arg ${ARG_CMAKE_ARGS})
236 if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
237 foreach(type ${flag_types})
238 if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
239 string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
240 set(${type}_flag "${${type}_flag} ${flag_value}")
241 endif()
242 endforeach()
243 else()
244 list(APPEND cmake_args ${arg})
245 endif()
246 endforeach()
247 foreach(type ${flag_types})
248 list(APPEND cmake_args ${${type}_flag})
249 endforeach()
250 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100251 else()
252 set(llvm_config_path "$<TARGET_FILE:llvm-config>")
Andrew Walbran16937d02019-10-22 13:54:20 +0100253 set(cmake_args ${ARG_CMAKE_ARGS})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100254 endif()
255
256 ExternalProject_Add(${name}
257 DEPENDS ${ARG_DEPENDS} llvm-config
258 ${name}-clobber
259 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
260 SOURCE_DIR ${source_dir}
261 STAMP_DIR ${STAMP_DIR}
262 BINARY_DIR ${BINARY_DIR}
263 ${exclude}
264 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
265 ${compiler_args}
266 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
267 ${sysroot_arg}
268 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
269 -DLLVM_CONFIG_PATH=${llvm_config_path}
270 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
271 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
272 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100273 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200274 -DLLVM_USE_RELATIVE_PATHS_IN_FILES=${LLVM_USE_RELATIVE_PATHS_IN_FILES}
275 -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100276 -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100277 -DPACKAGE_VERSION=${PACKAGE_VERSION}
278 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
279 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
280 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
Andrew Walbran16937d02019-10-22 13:54:20 +0100281 ${cmake_args}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100282 ${PASSTHROUGH_VARIABLES}
283 INSTALL_COMMAND ""
284 STEP_TARGETS configure build
285 BUILD_ALWAYS 1
286 USES_TERMINAL_CONFIGURE 1
287 USES_TERMINAL_BUILD 1
288 USES_TERMINAL_INSTALL 1
289 LIST_SEPARATOR |
290 )
291
292 if(ARG_USE_TOOLCHAIN)
293 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
294 endif()
295
296 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
297 ExternalProject_Add_Step(${name} clean
298 COMMAND ${run_clean}
299 COMMENT "Cleaning ${name}..."
300 DEPENDEES configure
301 ${force_deps}
302 WORKING_DIRECTORY ${BINARY_DIR}
303 EXCLUDE_FROM_MAIN 1
304 USES_TERMINAL 1
305 )
306 ExternalProject_Add_StepTargets(${name} clean)
307
308 if(ARG_USE_TOOLCHAIN)
309 add_dependencies(${name}-clean ${name}-clobber)
310 set_target_properties(${name}-clean PROPERTIES
311 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
312 endif()
313
314 if(NOT ARG_NO_INSTALL)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100315 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -DCMAKE_INSTALL_DO_STRIP=\${CMAKE_INSTALL_DO_STRIP} -P ${BINARY_DIR}/cmake_install.cmake\)"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100316 COMPONENT ${name})
317
318 add_llvm_install_targets(install-${name}
319 DEPENDS ${name}
320 COMPONENT ${name})
321 endif()
322
323 # Add top-level targets
324 foreach(target ${ARG_EXTRA_TARGETS})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100325 if(DEFINED ${target})
326 set(external_target "${${target}}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100327 else()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100328 set(external_target "${target}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100329 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100330 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
331 add_custom_target(${target}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100332 COMMAND ${build_runtime_cmd}
333 DEPENDS ${name}-configure
334 WORKING_DIRECTORY ${BINARY_DIR}
335 VERBATIM
336 USES_TERMINAL)
337 endforeach()
338endfunction()