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 # is_msvc_triple(out_var triple)
20 # Checks whether the passed triple refers to an MSVC environment
21 function(is_msvc_triple out_var triple)
22 if (triple MATCHES ".*-windows-msvc.*")
23 set(${out_var} TRUE PARENT_SCOPE)
25 set(${out_var} FALSE PARENT_SCOPE)
30 # llvm_ExternalProject_Add(name source_dir ...
32 # Use just-built tools (see TOOLCHAIN_TOOLS)
34 # Exclude this project from the all target
36 # Don't generate install targets for this project
38 # Always clean the sub-project before building
39 # CMAKE_ARGS arguments...
40 # Optional cmake arguments to pass when configuring the project
41 # TOOLCHAIN_TOOLS targets...
42 # Targets for toolchain tools (defaults to clang;lld)
44 # Targets that this project depends on
45 # EXTRA_TARGETS targets...
46 # Extra targets in the subproject to generate targets for
47 # PASSTHROUGH_PREFIXES prefix...
48 # Extra variable prefixes (name is always included) to pass down
50 # Use provided strip tool instead of the default one.
51 # TARGET_TRIPLE triple
52 # Optional target triple to pass to the compiler
54 function(llvm_ExternalProject_Add name source_dir)
55 cmake_parse_arguments(ARG
56 "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
58 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL;TARGET_TRIPLE"
60 canonicalize_tool_name(${name} nameCanon)
62 foreach(arg ${ARG_CMAKE_ARGS})
63 if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
64 string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
68 # If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
69 # reflect CMake's own default.
70 if (NOT _cmake_system_name)
71 set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
74 if(NOT ARG_TARGET_TRIPLE)
75 set(target_triple ${LLVM_DEFAULT_TARGET_TRIPLE})
77 set(target_triple ${ARG_TARGET_TRIPLE})
80 is_msvc_triple(is_msvc_target ${target_triple})
82 if(NOT ARG_TOOLCHAIN_TOOLS)
83 set(ARG_TOOLCHAIN_TOOLS clang)
84 # AIX 64-bit XCOFF and big AR format is not yet supported in some of these tools.
85 if(NOT _cmake_system_name STREQUAL AIX)
86 list(APPEND ARG_TOOLCHAIN_TOOLS lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
87 if(_cmake_system_name STREQUAL Darwin)
88 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-libtool-darwin llvm-lipo)
89 elseif(is_msvc_target)
90 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-lib)
92 # TODO: These tools don't fully support Mach-O format yet.
93 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip)
97 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
99 list(APPEND TOOLCHAIN_TOOLS ${tool})
101 # $<TARGET_FILE:tgt> only works on add_executable or add_library targets
102 # The below logic mirrors cmake's own implementation
103 get_target_property(target_type "${tool}" TYPE)
104 if(NOT target_type STREQUAL "OBJECT_LIBRARY" AND
105 NOT target_type STREQUAL "UTILITY" AND
106 NOT target_type STREQUAL "GLOBAL_TARGET" AND
107 NOT target_type STREQUAL "INTERFACE_LIBRARY")
108 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
114 if(NOT ARG_RUNTIME_LIBRARIES)
115 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
117 foreach(lib ${ARG_RUNTIME_LIBRARIES})
119 list(APPEND RUNTIME_LIBRARIES ${lib})
124 set(always_clean clean)
127 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
128 if(FOUND_CLANG GREATER -1)
129 set(CLANG_IN_TOOLCHAIN On)
132 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
133 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
136 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
137 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
139 add_custom_target(${name}-clear
140 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
141 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
142 COMMENT "Clobbering ${name} build and stamp directories"
146 # Find all variables that start with a prefix and propagate them through
147 get_cmake_property(variableNames VARIABLES)
149 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
150 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
151 foreach(variableName ${variableNames})
152 if(variableName MATCHES "^${prefix}")
153 string(REPLACE ";" "|" value "${${variableName}}")
154 list(APPEND PASSTHROUGH_VARIABLES
155 -D${variableName}=${value})
160 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
161 if(CLANG_IN_TOOLCHAIN)
163 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
164 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
165 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
167 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
168 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
169 -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
172 if(lld IN_LIST TOOLCHAIN_TOOLS)
174 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
175 elseif(NOT _cmake_system_name STREQUAL Darwin)
176 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
179 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
181 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib${CMAKE_EXECUTABLE_SUFFIX})
183 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
186 if(llvm-libtool-darwin IN_LIST TOOLCHAIN_TOOLS)
187 list(APPEND compiler_args -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin${CMAKE_EXECUTABLE_SUFFIX})
189 if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
190 list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
192 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
193 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
195 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
196 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
198 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
199 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
201 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
202 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
204 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
205 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
207 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
211 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
215 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
216 DEPENDS ${ARG_DEPENDS}
217 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
218 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
219 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
220 COMMENT "Clobbering bootstrap build and stamp directories"
223 add_custom_target(${name}-clobber
224 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
226 if(ARG_EXCLUDE_FROM_ALL)
227 set(exclude EXCLUDE_FROM_ALL 1)
231 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
234 if(CMAKE_CROSSCOMPILING)
235 set(compiler_args -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER}
236 -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
237 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
238 -DCMAKE_LINKER=${CMAKE_LINKER}
239 -DCMAKE_AR=${CMAKE_AR}
240 -DCMAKE_RANLIB=${CMAKE_RANLIB}
241 -DCMAKE_NM=${CMAKE_NM}
242 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
243 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
244 -DCMAKE_STRIP=${CMAKE_STRIP})
245 set(llvm_config_path ${LLVM_CONFIG_PATH})
247 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
248 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
250 set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}")
251 set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
252 foreach(type ${flag_types})
253 set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
255 string(REPLACE ";" "|" flag_string "${flag_types}")
256 foreach(arg ${ARG_CMAKE_ARGS})
257 if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
258 foreach(type ${flag_types})
259 if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
260 string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
261 set(${type}_flag "${${type}_flag} ${flag_value}")
265 list(APPEND cmake_args ${arg})
268 foreach(type ${flag_types})
269 list(APPEND cmake_args ${${type}_flag})
273 set(llvm_config_path "$<TARGET_FILE:llvm-config>")
274 set(cmake_args ${ARG_CMAKE_ARGS})
277 if(ARG_TARGET_TRIPLE)
278 list(APPEND compiler_args -DCMAKE_C_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
279 list(APPEND compiler_args -DCMAKE_CXX_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
280 list(APPEND compiler_args -DCMAKE_ASM_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
283 ExternalProject_Add(${name}
284 DEPENDS ${ARG_DEPENDS} llvm-config
286 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
287 SOURCE_DIR ${source_dir}
288 STAMP_DIR ${STAMP_DIR}
289 BINARY_DIR ${BINARY_DIR}
291 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
293 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
295 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
296 -DLLVM_CONFIG_PATH=${llvm_config_path}
297 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
298 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
299 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
300 -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
301 -DLLVM_USE_RELATIVE_PATHS_IN_FILES=${LLVM_USE_RELATIVE_PATHS_IN_FILES}
302 -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
303 -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
304 -DPACKAGE_VERSION=${PACKAGE_VERSION}
305 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
306 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
307 -DCMAKE_EXPORT_COMPILE_COMMANDS=1
309 ${PASSTHROUGH_VARIABLES}
311 STEP_TARGETS configure build
313 USES_TERMINAL_CONFIGURE 1
314 USES_TERMINAL_BUILD 1
315 USES_TERMINAL_INSTALL 1
319 if(ARG_USE_TOOLCHAIN)
320 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
323 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
324 ExternalProject_Add_Step(${name} clean
326 COMMENT "Cleaning ${name}..."
329 WORKING_DIRECTORY ${BINARY_DIR}
333 ExternalProject_Add_StepTargets(${name} clean)
335 if(ARG_USE_TOOLCHAIN)
336 add_dependencies(${name}-clean ${name}-clobber)
337 set_target_properties(${name}-clean PROPERTIES
338 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
341 if(NOT ARG_NO_INSTALL)
342 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\)"
345 add_llvm_install_targets(install-${name}
350 # Add top-level targets
351 foreach(target ${ARG_EXTRA_TARGETS})
352 if(DEFINED ${target})
353 set(external_target "${${target}}")
355 set(external_target "${target}")
357 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
358 add_custom_target(${target}
359 COMMAND ${build_runtime_cmd}
360 DEPENDS ${name}-configure
361 WORKING_DIRECTORY ${BINARY_DIR}