1 include(ExternalProject)
3 # llvm_ExternalProject_BuildCmd(out_var target)
4 # Utility function for constructing command lines for external project targets
5 function(llvm_ExternalProject_BuildCmd out_var target bin_dir)
6 cmake_parse_arguments(ARG "" "CONFIGURATION" "" ${ARGN})
7 if(NOT ARG_CONFIGURATION)
8 set(ARG_CONFIGURATION "$<CONFIG>")
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)
14 set(${out_var} ${CMAKE_COMMAND} --build ${bin_dir} --target ${target}
15 --config ${ARG_CONFIGURATION} PARENT_SCOPE)
19 # llvm_ExternalProject_Add(name source_dir ...
21 # Use just-built tools (see TOOLCHAIN_TOOLS)
23 # Exclude this project from the all target
25 # Don't generate install targets for this project
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)
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
39 # Use provided strip tool instead of the default one.
41 function(llvm_ExternalProject_Add name source_dir)
42 cmake_parse_arguments(ARG
43 "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
45 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL"
47 canonicalize_tool_name(${name} nameCanon)
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}")
55 if(NOT ARG_TOOLCHAIN_TOOLS)
56 set(ARG_TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
57 if(_cmake_system_name STREQUAL Darwin)
58 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-libtool-darwin llvm-lipo)
60 # TODO: These tools don't fully support Mach-O format yet.
61 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip)
64 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
66 list(APPEND TOOLCHAIN_TOOLS ${tool})
68 # $<TARGET_FILE:tgt> only works on add_executable or add_library targets
69 # The below logic mirrors cmake's own implementation
70 get_target_property(target_type "${tool}" TYPE)
71 if(NOT target_type STREQUAL "OBJECT_LIBRARY" AND
72 NOT target_type STREQUAL "UTILITY" AND
73 NOT target_type STREQUAL "GLOBAL_TARGET" AND
74 NOT target_type STREQUAL "INTERFACE_LIBRARY")
75 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
81 if(NOT ARG_RUNTIME_LIBRARIES)
82 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
84 foreach(lib ${ARG_RUNTIME_LIBRARIES})
86 list(APPEND RUNTIME_LIBRARIES ${lib})
91 set(always_clean clean)
94 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
95 if(FOUND_CLANG GREATER -1)
96 set(CLANG_IN_TOOLCHAIN On)
99 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
100 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
103 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
104 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
106 add_custom_target(${name}-clear
107 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
108 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
109 COMMENT "Clobbering ${name} build and stamp directories"
113 # Find all variables that start with a prefix and propagate them through
114 get_cmake_property(variableNames VARIABLES)
116 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
117 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
118 foreach(variableName ${variableNames})
119 if(variableName MATCHES "^${prefix}")
120 string(REPLACE ";" "|" value "${${variableName}}")
121 list(APPEND PASSTHROUGH_VARIABLES
122 -D${variableName}=${value})
127 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
128 if(CLANG_IN_TOOLCHAIN)
129 if(_cmake_system_name STREQUAL Windows)
130 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
131 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
132 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
134 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
135 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
136 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
139 if(lld IN_LIST TOOLCHAIN_TOOLS)
140 if(_cmake_system_name STREQUAL Windows)
141 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
142 elseif(NOT _cmake_system_name STREQUAL Darwin)
143 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
146 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
147 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
149 if(llvm-libtool-darwin IN_LIST TOOLCHAIN_TOOLS)
150 list(APPEND compiler_args -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin${CMAKE_EXECUTABLE_SUFFIX})
152 if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
153 list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
155 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
156 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
158 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
159 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
161 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
162 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
164 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
165 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
167 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
168 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
170 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
174 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
178 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
179 DEPENDS ${ARG_DEPENDS}
180 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
181 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
182 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
183 COMMENT "Clobbering bootstrap build and stamp directories"
186 add_custom_target(${name}-clobber
187 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
189 if(ARG_EXCLUDE_FROM_ALL)
190 set(exclude EXCLUDE_FROM_ALL 1)
194 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
197 if(CMAKE_CROSSCOMPILING)
198 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
199 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
200 -DCMAKE_LINKER=${CMAKE_LINKER}
201 -DCMAKE_AR=${CMAKE_AR}
202 -DCMAKE_RANLIB=${CMAKE_RANLIB}
203 -DCMAKE_NM=${CMAKE_NM}
204 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
205 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
206 -DCMAKE_STRIP=${CMAKE_STRIP})
207 set(llvm_config_path ${LLVM_CONFIG_PATH})
209 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
210 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
212 set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}")
213 set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
214 foreach(type ${flag_types})
215 set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
217 string(REPLACE ";" "|" flag_string "${flag_types}")
218 foreach(arg ${ARG_CMAKE_ARGS})
219 if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
220 foreach(type ${flag_types})
221 if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
222 string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
223 set(${type}_flag "${${type}_flag} ${flag_value}")
227 list(APPEND cmake_args ${arg})
230 foreach(type ${flag_types})
231 list(APPEND cmake_args ${${type}_flag})
235 set(llvm_config_path "$<TARGET_FILE:llvm-config>")
236 set(cmake_args ${ARG_CMAKE_ARGS})
239 ExternalProject_Add(${name}
240 DEPENDS ${ARG_DEPENDS} llvm-config
242 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
243 SOURCE_DIR ${source_dir}
244 STAMP_DIR ${STAMP_DIR}
245 BINARY_DIR ${BINARY_DIR}
247 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
249 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
251 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
252 -DLLVM_CONFIG_PATH=${llvm_config_path}
253 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
254 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
255 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
256 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
257 -DLLVM_USE_RELATIVE_PATHS_IN_FILES=${LLVM_USE_RELATIVE_PATHS_IN_FILES}
258 -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
259 -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
260 -DPACKAGE_VERSION=${PACKAGE_VERSION}
261 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
262 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
263 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
265 ${PASSTHROUGH_VARIABLES}
267 STEP_TARGETS configure build
269 USES_TERMINAL_CONFIGURE 1
270 USES_TERMINAL_BUILD 1
271 USES_TERMINAL_INSTALL 1
275 if(ARG_USE_TOOLCHAIN)
276 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
279 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
280 ExternalProject_Add_Step(${name} clean
282 COMMENT "Cleaning ${name}..."
285 WORKING_DIRECTORY ${BINARY_DIR}
289 ExternalProject_Add_StepTargets(${name} clean)
291 if(ARG_USE_TOOLCHAIN)
292 add_dependencies(${name}-clean ${name}-clobber)
293 set_target_properties(${name}-clean PROPERTIES
294 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
297 if(NOT ARG_NO_INSTALL)
298 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\)"
301 add_llvm_install_targets(install-${name}
306 # Add top-level targets
307 foreach(target ${ARG_EXTRA_TARGETS})
308 if(DEFINED ${target})
309 set(external_target "${${target}}")
311 set(external_target "${target}")
313 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
314 add_custom_target(${target}
315 COMMAND ${build_runtime_cmd}
316 DEPENDS ${name}-configure
317 WORKING_DIRECTORY ${BINARY_DIR}