[ARM] Split large truncating MVE stores
[llvm-complete.git] / cmake / modules / LLVMExternalProjectUtils.cmake
blob30901f41dd8834248087640f4b1949628b9b73bc
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 # 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 #   )
41 function(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()
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()
70   if(ARG_ALWAYS_CLEAN)
71     set(always_clean clean)
72   endif()
74   list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
75   if(FOUND_CLANG GREATER -1)
76     set(CLANG_IN_TOOLCHAIN On)
77   endif()
79   if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
80     list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
81   endif()
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"
90     USES_TERMINAL
91     )
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})
103       endif()
104     endforeach()
105   endforeach()
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}")
110     endif()
111   endforeach()
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})
118       else()
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})
121       endif()
122     endif()
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})
126       else()
127         list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
128       endif()
129     endif()
130     if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
131       list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
132     endif()
133     if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
134       list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
135     endif()
136     if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
137       list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
138     endif()
139     if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
140       list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
141     endif()
142     if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
143       list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
144     endif()
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})
147     endif()
148     list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
149   endif()
151   if(ARG_STRIP_TOOL)
152     list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
153   endif()
155   add_custom_command(
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"
162     )
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)
169   endif()
171   if(CMAKE_SYSROOT)
172     set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
173   endif()
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
189              ${PACKAGE_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})
194       endforeach()
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}")
202             endif()
203           endforeach()
204         else()
205           list(APPEND cmake_args ${arg})
206         endif()
207       endforeach()
208       foreach(type ${flag_types})
209         list(APPEND cmake_args ${${type}_flag})
210       endforeach()
211     endif()
212   else()
213     set(llvm_config_path "$<TARGET_FILE:llvm-config>")
214     set(cmake_args ${ARG_CMAKE_ARGS})
215   endif()
217   ExternalProject_Add(${name}
218     DEPENDS ${ARG_DEPENDS} llvm-config
219     ${name}-clobber
220     PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
221     SOURCE_DIR ${source_dir}
222     STAMP_DIR ${STAMP_DIR}
223     BINARY_DIR ${BINARY_DIR}
224     ${exclude}
225     CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
226                ${compiler_args}
227                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
228                ${sysroot_arg}
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
240                ${cmake_args}
241                ${PASSTHROUGH_VARIABLES}
242     INSTALL_COMMAND ""
243     STEP_TARGETS configure build
244     BUILD_ALWAYS 1
245     USES_TERMINAL_CONFIGURE 1
246     USES_TERMINAL_BUILD 1
247     USES_TERMINAL_INSTALL 1
248     LIST_SEPARATOR |
249     )
251   if(ARG_USE_TOOLCHAIN)
252     set(force_deps DEPENDS ${TOOLCHAIN_BINS})
253   endif()
255   llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
256   ExternalProject_Add_Step(${name} clean
257     COMMAND ${run_clean}
258     COMMENT "Cleaning ${name}..."
259     DEPENDEES configure
260     ${force_deps}
261     WORKING_DIRECTORY ${BINARY_DIR}
262     EXCLUDE_FROM_MAIN 1
263     USES_TERMINAL 1
264     )
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)
271   endif()
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\)"
275       COMPONENT ${name})
277     add_llvm_install_targets(install-${name}
278                              DEPENDS ${name}
279                              COMPONENT ${name})
280   endif()
282   # Add top-level targets
283   foreach(target ${ARG_EXTRA_TARGETS})
284     if(DEFINED ${target})
285       set(external_target "${${target}}")
286     else()
287       set(external_target "${target}")
288     endif()
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}
294       VERBATIM
295       USES_TERMINAL)
296   endforeach()
297 endfunction()