[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / cmake / modules / LLVMExternalProjectUtils.cmake
blob7c91150ba725bd653e375dd3038d53249206c22c
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>")
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()
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)
24   else()
25     set(${out_var} FALSE PARENT_SCOPE)
26   endif()
27 endfunction()
30 # llvm_ExternalProject_Add(name source_dir ...
31 #   USE_TOOLCHAIN
32 #     Use just-built tools (see TOOLCHAIN_TOOLS)
33 #   EXCLUDE_FROM_ALL
34 #     Exclude this project from the all target
35 #   NO_INSTALL
36 #     Don't generate install targets for this project
37 #   ALWAYS_CLEAN
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)
43 #   DEPENDS targets...
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
49 #   STRIP_TOOL path
50 #     Use provided strip tool instead of the default one.
51 #   TARGET_TRIPLE triple
52 #     Optional target triple to pass to the compiler
53 #   )
54 function(llvm_ExternalProject_Add name source_dir)
55   cmake_parse_arguments(ARG
56     "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
57     "SOURCE_DIR"
58     "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;STRIP_TOOL;TARGET_TRIPLE"
59     ${ARGN})
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}")
65     endif()
66   endforeach()
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}")
72   endif()
74   if(NOT ARG_TARGET_TRIPLE)
75     set(target_triple ${LLVM_DEFAULT_TARGET_TRIPLE})
76   else()
77     set(target_triple ${ARG_TARGET_TRIPLE})
78   endif()
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)
91       else()
92         # TODO: These tools don't fully support Mach-O format yet.
93         list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip)
94       endif()
95     endif()
96   endif()
97   foreach(tool ${ARG_TOOLCHAIN_TOOLS})
98     if(TARGET ${tool})
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}>)
109       endif()
111     endif()
112   endforeach()
114   if(NOT ARG_RUNTIME_LIBRARIES)
115     set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
116   endif()
117   foreach(lib ${ARG_RUNTIME_LIBRARIES})
118     if(TARGET ${lib})
119       list(APPEND RUNTIME_LIBRARIES ${lib})
120     endif()
121   endforeach()
123   if(ARG_ALWAYS_CLEAN)
124     set(always_clean clean)
125   endif()
127   list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
128   if(FOUND_CLANG GREATER -1)
129     set(CLANG_IN_TOOLCHAIN On)
130   endif()
132   if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
133     list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
134   endif()
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"
143     USES_TERMINAL
144     )
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})
156       endif()
157     endforeach()
158   endforeach()
160   if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
161     if(CLANG_IN_TOOLCHAIN)
162       if(is_msvc_target)
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})
166       else()
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})
170       endif()
171     endif()
172     if(lld IN_LIST TOOLCHAIN_TOOLS)
173       if(is_msvc_target)
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})
177       endif()
178     endif()
179     if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
180       if(is_msvc_target)
181         list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib${CMAKE_EXECUTABLE_SUFFIX})
182       else()
183         list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
184       endif()
185     endif()
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})
188     endif()
189     if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
190       list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
191     endif()
192     if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
193       list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
194     endif()
195     if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
196       list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
197     endif()
198     if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
199       list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
200     endif()
201     if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
202       list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
203     endif()
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})
206     endif()
207     list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
208   endif()
210   if(ARG_STRIP_TOOL)
211     list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
212   endif()
214   add_custom_command(
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"
221     )
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)
228   endif()
230   if(CMAKE_SYSROOT)
231     set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
232   endif()
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
249              ${PACKAGE_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})
254       endforeach()
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}")
262             endif()
263           endforeach()
264         else()
265           list(APPEND cmake_args ${arg})
266         endif()
267       endforeach()
268       foreach(type ${flag_types})
269         list(APPEND cmake_args ${${type}_flag})
270       endforeach()
271     endif()
272   else()
273     set(llvm_config_path "$<TARGET_FILE:llvm-config>")
274     set(cmake_args ${ARG_CMAKE_ARGS})
275   endif()
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})
281   endif()
283   ExternalProject_Add(${name}
284     DEPENDS ${ARG_DEPENDS} llvm-config
285     ${name}-clobber
286     PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
287     SOURCE_DIR ${source_dir}
288     STAMP_DIR ${STAMP_DIR}
289     BINARY_DIR ${BINARY_DIR}
290     ${exclude}
291     CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
292                ${compiler_args}
293                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
294                ${sysroot_arg}
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
308                ${cmake_args}
309                ${PASSTHROUGH_VARIABLES}
310     INSTALL_COMMAND ""
311     STEP_TARGETS configure build
312     BUILD_ALWAYS 1
313     USES_TERMINAL_CONFIGURE 1
314     USES_TERMINAL_BUILD 1
315     USES_TERMINAL_INSTALL 1
316     LIST_SEPARATOR |
317     )
319   if(ARG_USE_TOOLCHAIN)
320     set(force_deps DEPENDS ${TOOLCHAIN_BINS})
321   endif()
323   llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
324   ExternalProject_Add_Step(${name} clean
325     COMMAND ${run_clean}
326     COMMENT "Cleaning ${name}..."
327     DEPENDEES configure
328     ${force_deps}
329     WORKING_DIRECTORY ${BINARY_DIR}
330     EXCLUDE_FROM_MAIN 1
331     USES_TERMINAL 1
332     )
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)
339   endif()
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\)"
343       COMPONENT ${name})
345     add_llvm_install_targets(install-${name}
346                              DEPENDS ${name}
347                              COMPONENT ${name})
348   endif()
350   # Add top-level targets
351   foreach(target ${ARG_EXTRA_TARGETS})
352     if(DEFINED ${target})
353       set(external_target "${${target}}")
354     else()
355       set(external_target "${target}")
356     endif()
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}
362       VERBATIM
363       USES_TERMINAL)
364   endforeach()
365 endfunction()