1 include(ExternalProject) 2 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 "$<CONFIGURATION>") 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() 17 endfunction() 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 # ) 39 function(llvm_ExternalProject_Add name source_dir) 40 cmake_parse_arguments(ARG 41 "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN" 42 "SOURCE_DIR" 43 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES" 44 ${ARGN}) 45 canonicalize_tool_name(${name} nameCanon) 46 if(NOT ARG_TOOLCHAIN_TOOLS) 47 set(ARG_TOOLCHAIN_TOOLS clang lld) 48 if(NOT APPLE AND NOT WIN32) 49 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip) 50 endif() 51 endif() 52 foreach(tool ${ARG_TOOLCHAIN_TOOLS}) 53 if(TARGET ${tool}) 54 list(APPEND TOOLCHAIN_TOOLS ${tool}) 55 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>) 56 endif() 57 endforeach() 58 59 if(NOT ARG_RUNTIME_LIBRARIES) 60 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx) 61 endif() 62 foreach(lib ${ARG_RUNTIME_LIBRARIES}) 63 if(TARGET ${lib}) 64 list(APPEND RUNTIME_LIBRARIES ${lib}) 65 endif() 66 endforeach() 67 68 if(ARG_ALWAYS_CLEAN) 69 set(always_clean clean) 70 endif() 71 72 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG) 73 if(FOUND_CLANG GREATER -1) 74 set(CLANG_IN_TOOLCHAIN On) 75 endif() 76 77 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN) 78 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES}) 79 endif() 80 81 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/) 82 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/) 83 84 add_custom_target(${name}-clear 85 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR} 86 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR} 87 COMMENT "Clobbering ${name} build and stamp directories" 88 USES_TERMINAL 89 ) 90 91 # Find all variables that start with a prefix and propagate them through 92 get_cmake_property(variableNames VARIABLES) 93 94 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon}) 95 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES}) 96 foreach(variableName ${variableNames}) 97 if(variableName MATCHES "^${prefix}") 98 string(REPLACE ";" "|" value "${${variableName}}") 99 list(APPEND PASSTHROUGH_VARIABLES 100 -D${variableName}=${value}) 101 endif() 102 endforeach() 103 endforeach() 104 105 if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING) 106 if(CLANG_IN_TOOLCHAIN) 107 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang 108 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++) 109 endif() 110 if(lld IN_LIST TOOLCHAIN_TOOLS) 111 list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld) 112 endif() 113 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS) 114 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar) 115 endif() 116 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS) 117 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib) 118 endif() 119 if(llvm-nm IN_LIST TOOLCHAIN_TOOLS) 120 list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm) 121 endif() 122 if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS) 123 list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump) 124 endif() 125 if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS) 126 list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy) 127 endif() 128 if(llvm-strip IN_LIST TOOLCHAIN_TOOLS) 129 list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip) 130 endif() 131 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS}) 132 endif() 133 134 add_custom_command( 135 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 136 DEPENDS ${ARG_DEPENDS} 137 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt 138 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir 139 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp 140 COMMENT "Clobbering bootstrap build and stamp directories" 141 ) 142 143 add_custom_target(${name}-clobber 144 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 145 146 if(ARG_EXCLUDE_FROM_ALL) 147 set(exclude EXCLUDE_FROM_ALL 1) 148 endif() 149 150 if(CMAKE_SYSROOT) 151 set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT}) 152 endif() 153 154 if(CMAKE_CROSSCOMPILING) 155 set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 156 -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} 157 -DCMAKE_LINKER=${CMAKE_LINKER} 158 -DCMAKE_AR=${CMAKE_AR} 159 -DCMAKE_RANLIB=${CMAKE_RANLIB} 160 -DCMAKE_NM=${CMAKE_NM} 161 -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY} 162 -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP} 163 -DCMAKE_STRIP=${CMAKE_STRIP}) 164 set(llvm_config_path ${LLVM_CONFIG_PATH}) 165 else() 166 set(llvm_config_path "$<TARGET_FILE:llvm-config>") 167 endif() 168 169 ExternalProject_Add(${name} 170 DEPENDS ${ARG_DEPENDS} llvm-config 171 ${name}-clobber 172 PREFIX ${CMAKE_BINARY_DIR}/projects/${name} 173 SOURCE_DIR ${source_dir} 174 STAMP_DIR ${STAMP_DIR} 175 BINARY_DIR ${BINARY_DIR} 176 ${exclude} 177 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS} 178 ${compiler_args} 179 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 180 ${sysroot_arg} 181 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR} 182 -DLLVM_CONFIG_PATH=${llvm_config_path} 183 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR} 184 -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE} 185 -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT} 186 -DPACKAGE_VERSION=${PACKAGE_VERSION} 187 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} 188 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} 189 -DCMAKE_EXPORT_COMPILE_COMMANDS=1 190 ${ARG_CMAKE_ARGS} 191 ${PASSTHROUGH_VARIABLES} 192 INSTALL_COMMAND "" 193 STEP_TARGETS configure build 194 BUILD_ALWAYS 1 195 USES_TERMINAL_CONFIGURE 1 196 USES_TERMINAL_BUILD 1 197 USES_TERMINAL_INSTALL 1 198 LIST_SEPARATOR | 199 ) 200 201 if(ARG_USE_TOOLCHAIN) 202 set(force_deps DEPENDS ${TOOLCHAIN_BINS}) 203 endif() 204 205 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR}) 206 ExternalProject_Add_Step(${name} clean 207 COMMAND ${run_clean} 208 COMMENT "Cleaning ${name}..." 209 DEPENDEES configure 210 ${force_deps} 211 WORKING_DIRECTORY ${BINARY_DIR} 212 EXCLUDE_FROM_MAIN 1 213 USES_TERMINAL 1 214 ) 215 ExternalProject_Add_StepTargets(${name} clean) 216 217 if(ARG_USE_TOOLCHAIN) 218 add_dependencies(${name}-clean ${name}-clobber) 219 set_target_properties(${name}-clean PROPERTIES 220 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp) 221 endif() 222 223 if(NOT ARG_NO_INSTALL) 224 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\)" 225 COMPONENT ${name}) 226 227 add_llvm_install_targets(install-${name} 228 DEPENDS ${name} 229 COMPONENT ${name}) 230 endif() 231 232 # Add top-level targets 233 foreach(target ${ARG_EXTRA_TARGETS}) 234 if(DEFINED ${target}) 235 set(external_target "${${target}}") 236 else() 237 set(external_target "${target}") 238 endif() 239 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR}) 240 add_custom_target(${target} 241 COMMAND ${build_runtime_cmd} 242 DEPENDS ${name}-configure 243 WORKING_DIRECTORY ${BINARY_DIR} 244 VERBATIM 245 USES_TERMINAL) 246 endforeach() 247 endfunction() 248