1include(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) 8 set(ARG_CONFIGURATION "$<CONFIG>") 9 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 38# STRIP_TOOL path 39# Use provided strip tool instead of the default one. 40# ) 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" 45 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL" 46 ${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) 51 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip) 52 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 107 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING) 108 if(CLANG_IN_TOOLCHAIN) 109 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang 110 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++) 111 endif() 112 if(lld IN_LIST TOOLCHAIN_TOOLS) 113 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld) 114 endif() 115 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS) 116 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar) 117 endif() 118 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS) 119 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib) 120 endif() 121 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS) 122 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm) 123 endif() 124 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS) 125 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump) 126 endif() 127 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS) 128 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy) 129 endif() 130 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL) 131 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip) 132 endif() 133 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS}) 134 endif() 135 136 if(ARG_STRIP_TOOL) 137 list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL}) 138 endif() 139 140 add_custom_command( 141 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 142 DEPENDS ${ARG_DEPENDS} 143 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt 144 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir 145 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 146 COMMENT "Clobbering bootstrap build and stamp directories" 147 ) 148 149 add_custom_target(${name}-clobber 150 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 151 152 if(ARG_EXCLUDE_FROM_ALL) 153 set(exclude EXCLUDE_FROM_ALL 1) 154 endif() 155 156 if(CMAKE_SYSROOT) 157 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT}) 158 endif() 159 160 if(CMAKE_CROSSCOMPILING) 161 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 162 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} 163 -DCMAKE_LINKER=${CMAKE_LINKER} 164 -DCMAKE_AR=${CMAKE_AR} 165 -DCMAKE_RANLIB=${CMAKE_RANLIB} 166 -DCMAKE_NM=${CMAKE_NM} 167 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY} 168 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP} 169 -DCMAKE_STRIP=${CMAKE_STRIP}) 170 set(llvm_config_path ${LLVM_CONFIG_PATH}) 171 172 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 173 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION 174 ${PACKAGE_VERSION}) 175 set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}") 176 set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER) 177 foreach(type ${flag_types}) 178 set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir}) 179 endforeach() 180 string(REPLACE ";" "|" flag_string "${flag_types}") 181 foreach(arg ${ARG_CMAKE_ARGS}) 182 if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS") 183 foreach(type ${flag_types}) 184 if(arg MATCHES "^-DCMAKE_${type}_FLAGS") 185 string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}") 186 set(${type}_flag "${${type}_flag} ${flag_value}") 187 endif() 188 endforeach() 189 else() 190 list(APPEND cmake_args ${arg}) 191 endif() 192 endforeach() 193 foreach(type ${flag_types}) 194 list(APPEND cmake_args ${${type}_flag}) 195 endforeach() 196 endif() 197 else() 198 set(llvm_config_path "$<TARGET_FILE:llvm-config>") 199 set(cmake_args ${ARG_CMAKE_ARGS}) 200 endif() 201 202 ExternalProject_Add(${name} 203 DEPENDS ${ARG_DEPENDS} llvm-config 204 ${name}-clobber 205 PREFIX ${CMAKE_BINARY_DIR}/projects/${name} 206 SOURCE_DIR ${source_dir} 207 STAMP_DIR ${STAMP_DIR} 208 BINARY_DIR ${BINARY_DIR} 209 ${exclude} 210 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS} 211 ${compiler_args} 212 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 213 ${sysroot_arg} 214 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR} 215 -DLLVM_CONFIG_PATH=${llvm_config_path} 216 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR} 217 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE} 218 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT} 219 -DPACKAGE_VERSION=${PACKAGE_VERSION} 220 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} 221 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} 222 -DCMAKE_EXPORT_COMPILE_COMMANDS=1 223 ${cmake_args} 224 ${PASSTHROUGH_VARIABLES} 225 INSTALL_COMMAND "" 226 STEP_TARGETS configure build 227 BUILD_ALWAYS 1 228 USES_TERMINAL_CONFIGURE 1 229 USES_TERMINAL_BUILD 1 230 USES_TERMINAL_INSTALL 1 231 LIST_SEPARATOR | 232 ) 233 234 if(ARG_USE_TOOLCHAIN) 235 set(force_deps DEPENDS ${TOOLCHAIN_BINS}) 236 endif() 237 238 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR}) 239 ExternalProject_Add_Step(${name} clean 240 COMMAND ${run_clean} 241 COMMENT "Cleaning ${name}..." 242 DEPENDEES configure 243 ${force_deps} 244 WORKING_DIRECTORY ${BINARY_DIR} 245 EXCLUDE_FROM_MAIN 1 246 USES_TERMINAL 1 247 ) 248 ExternalProject_Add_StepTargets(${name} clean) 249 250 if(ARG_USE_TOOLCHAIN) 251 add_dependencies(${name}-clean ${name}-clobber) 252 set_target_properties(${name}-clean PROPERTIES 253 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 254 endif() 255 256 if(NOT ARG_NO_INSTALL) 257 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\)" 258 COMPONENT ${name}) 259 260 add_llvm_install_targets(install-${name} 261 DEPENDS ${name} 262 COMPONENT ${name}) 263 endif() 264 265 # Add top-level targets 266 foreach(target ${ARG_EXTRA_TARGETS}) 267 if(DEFINED ${target}) 268 set(external_target "${${target}}") 269 else() 270 set(external_target "${target}") 271 endif() 272 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR}) 273 add_custom_target(${target} 274 COMMAND ${build_runtime_cmd} 275 DEPENDS ${name}-configure 276 WORKING_DIRECTORY ${BINARY_DIR} 277 VERBATIM 278 USES_TERMINAL) 279 endforeach() 280endfunction() 281