blob: 9a6adab16567383991b30ca7270b24b3941d2cca [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)
48 if(NOT ARG_TOOLCHAIN_TOOLS)
49 set(ARG_TOOLCHAIN_TOOLS clang lld)
50 if(NOT APPLE AND NOT WIN32)
Andrew Scullcdfcccc2018-10-05 20:58:37 +010051 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip)
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052 endif()
53 endif()
54 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
55 if(TARGET ${tool})
56 list(APPEND TOOLCHAIN_TOOLS ${tool})
57 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
58 endif()
59 endforeach()
60
61 if(NOT ARG_RUNTIME_LIBRARIES)
62 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
63 endif()
64 foreach(lib ${ARG_RUNTIME_LIBRARIES})
65 if(TARGET ${lib})
66 list(APPEND RUNTIME_LIBRARIES ${lib})
67 endif()
68 endforeach()
69
70 if(ARG_ALWAYS_CLEAN)
71 set(always_clean clean)
72 endif()
73
74 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
75 if(FOUND_CLANG GREATER -1)
76 set(CLANG_IN_TOOLCHAIN On)
77 endif()
78
79 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
80 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
81 endif()
82
83 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
84 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
85
86 add_custom_target(${name}-clear
87 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
88 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
89 COMMENT "Clobbering ${name} build and stamp directories"
90 USES_TERMINAL
91 )
92
93 # Find all variables that start with a prefix and propagate them through
94 get_cmake_property(variableNames VARIABLES)
95
96 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
97 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
98 foreach(variableName ${variableNames})
99 if(variableName MATCHES "^${prefix}")
100 string(REPLACE ";" "|" value "${${variableName}}")
101 list(APPEND PASSTHROUGH_VARIABLES
102 -D${variableName}=${value})
103 endif()
104 endforeach()
105 endforeach()
106
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100107 foreach(arg ${ARG_CMAKE_ARGS})
108 if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
109 string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
110 endif()
111 endforeach()
112
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
114 if(CLANG_IN_TOOLCHAIN)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100115 if(_cmake_system_name STREQUAL Windows)
116 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl
117 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl)
118 else()
119 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
120 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
121 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100122 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100123 if(lld IN_LIST TOOLCHAIN_TOOLS)
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100124 if(_cmake_system_name STREQUAL Windows)
125 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link)
126 else()
127 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld)
128 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100129 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100130 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
131 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
132 endif()
133 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
134 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
135 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100136 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
137 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm)
138 endif()
139 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
140 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump)
141 endif()
142 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
143 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
144 endif()
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100145 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100146 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
147 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
149 endif()
150
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100151 if(ARG_STRIP_TOOL)
152 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
153 endif()
154
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 add_custom_command(
156 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
157 DEPENDS ${ARG_DEPENDS}
158 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
159 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
160 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
161 COMMENT "Clobbering bootstrap build and stamp directories"
162 )
163
164 add_custom_target(${name}-clobber
165 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
166
167 if(ARG_EXCLUDE_FROM_ALL)
168 set(exclude EXCLUDE_FROM_ALL 1)
169 endif()
170
171 if(CMAKE_SYSROOT)
172 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
173 endif()
174
175 if(CMAKE_CROSSCOMPILING)
176 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
177 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100178 -DCMAKE_LINKER=${CMAKE_LINKER}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100179 -DCMAKE_AR=${CMAKE_AR}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100180 -DCMAKE_RANLIB=${CMAKE_RANLIB}
181 -DCMAKE_NM=${CMAKE_NM}
182 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
183 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
184 -DCMAKE_STRIP=${CMAKE_STRIP})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100185 set(llvm_config_path ${LLVM_CONFIG_PATH})
Andrew Walbran16937d02019-10-22 13:54:20 +0100186
187 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
188 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
189 ${PACKAGE_VERSION})
190 set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}")
191 set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
192 foreach(type ${flag_types})
193 set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
194 endforeach()
195 string(REPLACE ";" "|" flag_string "${flag_types}")
196 foreach(arg ${ARG_CMAKE_ARGS})
197 if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
198 foreach(type ${flag_types})
199 if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
200 string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
201 set(${type}_flag "${${type}_flag} ${flag_value}")
202 endif()
203 endforeach()
204 else()
205 list(APPEND cmake_args ${arg})
206 endif()
207 endforeach()
208 foreach(type ${flag_types})
209 list(APPEND cmake_args ${${type}_flag})
210 endforeach()
211 endif()
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100212 else()
213 set(llvm_config_path "$<TARGET_FILE:llvm-config>")
Andrew Walbran16937d02019-10-22 13:54:20 +0100214 set(cmake_args ${ARG_CMAKE_ARGS})
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100215 endif()
216
217 ExternalProject_Add(${name}
218 DEPENDS ${ARG_DEPENDS} llvm-config
219 ${name}-clobber
220 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
221 SOURCE_DIR ${source_dir}
222 STAMP_DIR ${STAMP_DIR}
223 BINARY_DIR ${BINARY_DIR}
224 ${exclude}
225 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
226 ${compiler_args}
227 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
228 ${sysroot_arg}
229 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
230 -DLLVM_CONFIG_PATH=${llvm_config_path}
231 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
232 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
233 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100234 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
235 -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100236 -DPACKAGE_VERSION=${PACKAGE_VERSION}
237 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
238 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
239 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
Andrew Walbran16937d02019-10-22 13:54:20 +0100240 ${cmake_args}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100241 ${PASSTHROUGH_VARIABLES}
242 INSTALL_COMMAND ""
243 STEP_TARGETS configure build
244 BUILD_ALWAYS 1
245 USES_TERMINAL_CONFIGURE 1
246 USES_TERMINAL_BUILD 1
247 USES_TERMINAL_INSTALL 1
248 LIST_SEPARATOR |
249 )
250
251 if(ARG_USE_TOOLCHAIN)
252 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
253 endif()
254
255 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
256 ExternalProject_Add_Step(${name} clean
257 COMMAND ${run_clean}
258 COMMENT "Cleaning ${name}..."
259 DEPENDEES configure
260 ${force_deps}
261 WORKING_DIRECTORY ${BINARY_DIR}
262 EXCLUDE_FROM_MAIN 1
263 USES_TERMINAL 1
264 )
265 ExternalProject_Add_StepTargets(${name} clean)
266
267 if(ARG_USE_TOOLCHAIN)
268 add_dependencies(${name}-clean ${name}-clobber)
269 set_target_properties(${name}-clean PROPERTIES
270 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
271 endif()
272
273 if(NOT ARG_NO_INSTALL)
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100274 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 +0100275 COMPONENT ${name})
276
277 add_llvm_install_targets(install-${name}
278 DEPENDS ${name}
279 COMPONENT ${name})
280 endif()
281
282 # Add top-level targets
283 foreach(target ${ARG_EXTRA_TARGETS})
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100284 if(DEFINED ${target})
285 set(external_target "${${target}}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286 else()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100287 set(external_target "${target}")
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100288 endif()
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100289 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
290 add_custom_target(${target}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100291 COMMAND ${build_runtime_cmd}
292 DEPENDS ${name}-configure
293 WORKING_DIRECTORY ${BINARY_DIR}
294 VERBATIM
295 USES_TERMINAL)
296 endforeach()
297endfunction()