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)
48 if(NOT ARG_TOOLCHAIN_TOOLS)
49 set(ARG_TOOLCHAIN_TOOLS clang lld)
50 if(NOT APPLE AND NOT WIN32)
51 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip)
54 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
56 list(APPEND TOOLCHAIN_TOOLS ${tool})
57 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
61 if(NOT ARG_RUNTIME_LIBRARIES)
62 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
64 foreach(lib ${ARG_RUNTIME_LIBRARIES})
66 list(APPEND RUNTIME_LIBRARIES ${lib})
71 set(always_clean clean)
74 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
75 if(FOUND_CLANG GREATER -1)
76 set(CLANG_IN_TOOLCHAIN On)
79 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
80 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
83 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
84 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
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"
93 # Find all variables that start with a prefix and propagate them through
94 get_cmake_property(variableNames VARIABLES)
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})
107 foreach(arg ${ARG_CMAKE_ARGS})
108 if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
109 string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
113 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
114 if(CLANG_IN_TOOLCHAIN)
115 if(_cmake_system_name STREQUAL Windows)
116 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
117 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
119 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
120 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX})
123 if(lld IN_LIST TOOLCHAIN_TOOLS)
124 if(_cmake_system_name STREQUAL Windows)
125 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
127 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
130 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
131 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
133 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
134 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
136 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
137 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
139 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
140 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
142 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
143 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
145 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
146 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
148 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
152 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
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"
164 add_custom_target(${name}-clobber
165 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
167 if(ARG_EXCLUDE_FROM_ALL)
168 set(exclude EXCLUDE_FROM_ALL 1)
172 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
175 if(CMAKE_CROSSCOMPILING)
176 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
177 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
178 -DCMAKE_LINKER=${CMAKE_LINKER}
179 -DCMAKE_AR=${CMAKE_AR}
180 -DCMAKE_RANLIB=${CMAKE_RANLIB}
181 -DCMAKE_NM=${CMAKE_NM}
182 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
183 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
184 -DCMAKE_STRIP=${CMAKE_STRIP})
185 set(llvm_config_path ${LLVM_CONFIG_PATH})
187 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
188 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_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})
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}")
205 list(APPEND cmake_args ${arg})
208 foreach(type ${flag_types})
209 list(APPEND cmake_args ${${type}_flag})
213 set(llvm_config_path "$<TARGET_FILE:llvm-config>")
214 set(cmake_args ${ARG_CMAKE_ARGS})
217 ExternalProject_Add(${name}
218 DEPENDS ${ARG_DEPENDS} llvm-config
220 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
221 SOURCE_DIR ${source_dir}
222 STAMP_DIR ${STAMP_DIR}
223 BINARY_DIR ${BINARY_DIR}
225 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
227 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
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}
234 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
235 -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
236 -DPACKAGE_VERSION=${PACKAGE_VERSION}
237 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
238 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
239 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
241 ${PASSTHROUGH_VARIABLES}
243 STEP_TARGETS configure build
245 USES_TERMINAL_CONFIGURE 1
246 USES_TERMINAL_BUILD 1
247 USES_TERMINAL_INSTALL 1
251 if(ARG_USE_TOOLCHAIN)
252 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
255 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
256 ExternalProject_Add_Step(${name} clean
258 COMMENT "Cleaning ${name}..."
261 WORKING_DIRECTORY ${BINARY_DIR}
265 ExternalProject_Add_StepTargets(${name} clean)
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)
273 if(NOT ARG_NO_INSTALL)
274 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\)"
277 add_llvm_install_targets(install-${name}
282 # Add top-level targets
283 foreach(target ${ARG_EXTRA_TARGETS})
284 if(DEFINED ${target})
285 set(external_target "${${target}}")
287 set(external_target "${target}")
289 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
290 add_custom_target(${target}
291 COMMAND ${build_runtime_cmd}
292 DEPENDS ${name}-configure
293 WORKING_DIRECTORY ${BINARY_DIR}