[MemorySSA] Report unoptimized as None, not MayAlias.
[llvm-project.git] / llvm / cmake / modules / LLVMExternalProjectUtils.cmake
blobdb3a536b0c249568d8347f633fb4a77726da346a
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)
49   foreach(arg ${ARG_CMAKE_ARGS})
50     if(arg MATCHES "^-DCMAKE_SYSTEM_NAME=")
51       string(REGEX REPLACE "^-DCMAKE_SYSTEM_NAME=(.*)$" "\\1" _cmake_system_name "${arg}")
52     endif()
53   endforeach()
55   if(NOT ARG_TOOLCHAIN_TOOLS)
56     set(ARG_TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib llvm-nm llvm-objdump)
57     if(_cmake_system_name STREQUAL Darwin)
58       list(APPEND ARG_TOOLCHAIN_TOOLS llvm-libtool-darwin llvm-lipo)
59     else()
60       # TODO: These tools don't fully support Mach-O format yet.
61       list(APPEND ARG_TOOLCHAIN_TOOLS llvm-objcopy llvm-strip)
62     endif()
63   endif()
64   foreach(tool ${ARG_TOOLCHAIN_TOOLS})
65     if(TARGET ${tool})
66       list(APPEND TOOLCHAIN_TOOLS ${tool})
68       # $<TARGET_FILE:tgt> only works on add_executable or add_library targets
69       # The below logic mirrors cmake's own implementation
70       get_target_property(target_type "${tool}" TYPE)
71       if(NOT target_type STREQUAL "OBJECT_LIBRARY" AND
72          NOT target_type STREQUAL "UTILITY" AND
73          NOT target_type STREQUAL "GLOBAL_TARGET" AND
74          NOT target_type STREQUAL "INTERFACE_LIBRARY")
75         list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
76       endif()
78     endif()
79   endforeach()
81   if(NOT ARG_RUNTIME_LIBRARIES)
82     set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
83   endif()
84   foreach(lib ${ARG_RUNTIME_LIBRARIES})
85     if(TARGET ${lib})
86       list(APPEND RUNTIME_LIBRARIES ${lib})
87     endif()
88   endforeach()
90   if(ARG_ALWAYS_CLEAN)
91     set(always_clean clean)
92   endif()
94   list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
95   if(FOUND_CLANG GREATER -1)
96     set(CLANG_IN_TOOLCHAIN On)
97   endif()
99   if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
100     list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
101   endif()
103   set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
104   set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
106   add_custom_target(${name}-clear
107     COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
108     COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
109     COMMENT "Clobbering ${name} build and stamp directories"
110     USES_TERMINAL
111     )
113   # Find all variables that start with a prefix and propagate them through
114   get_cmake_property(variableNames VARIABLES)
116   list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
117   foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
118     foreach(variableName ${variableNames})
119       if(variableName MATCHES "^${prefix}")
120         string(REPLACE ";" "|" value "${${variableName}}")
121         list(APPEND PASSTHROUGH_VARIABLES
122           -D${variableName}=${value})
123       endif()
124     endforeach()
125   endforeach()
127   if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
128     if(CLANG_IN_TOOLCHAIN)
129       if(_cmake_system_name STREQUAL Windows)
130         set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
131                           -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
132                           -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
133       else()
134         set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
135                           -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
136                           -DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
137       endif()
138     endif()
139     if(lld IN_LIST TOOLCHAIN_TOOLS)
140       if(_cmake_system_name STREQUAL Windows)
141         list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
142       elseif(NOT _cmake_system_name STREQUAL Darwin)
143         list(APPEND compiler_args -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
144       endif()
145     endif()
146     if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
147       list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
148     endif()
149     if(llvm-libtool-darwin IN_LIST TOOLCHAIN_TOOLS)
150       list(APPEND compiler_args -DCMAKE_LIBTOOL=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-libtool-darwin${CMAKE_EXECUTABLE_SUFFIX})
151     endif()
152     if(llvm-lipo IN_LIST TOOLCHAIN_TOOLS)
153       list(APPEND compiler_args -DCMAKE_LIPO=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lipo${CMAKE_EXECUTABLE_SUFFIX})
154     endif()
155     if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
156       list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
157     endif()
158     if(llvm-nm IN_LIST TOOLCHAIN_TOOLS)
159       list(APPEND compiler_args -DCMAKE_NM=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-nm${CMAKE_EXECUTABLE_SUFFIX})
160     endif()
161     if(llvm-objdump IN_LIST TOOLCHAIN_TOOLS)
162       list(APPEND compiler_args -DCMAKE_OBJDUMP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objdump${CMAKE_EXECUTABLE_SUFFIX})
163     endif()
164     if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
165       list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy${CMAKE_EXECUTABLE_SUFFIX})
166     endif()
167     if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_STRIP_TOOL)
168       list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip${CMAKE_EXECUTABLE_SUFFIX})
169     endif()
170     list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
171   endif()
173   if(ARG_STRIP_TOOL)
174     list(APPEND compiler_args -DCMAKE_STRIP=${ARG_STRIP_TOOL})
175   endif()
177   add_custom_command(
178     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
179     DEPENDS ${ARG_DEPENDS}
180     COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
181     COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
182     COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
183     COMMENT "Clobbering bootstrap build and stamp directories"
184     )
186   add_custom_target(${name}-clobber
187     DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
189   if(ARG_EXCLUDE_FROM_ALL)
190     set(exclude EXCLUDE_FROM_ALL 1)
191   endif()
193   if(CMAKE_SYSROOT)
194     set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT})
195   endif()
197   if(CMAKE_CROSSCOMPILING)
198     set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
199                       -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
200                       -DCMAKE_LINKER=${CMAKE_LINKER}
201                       -DCMAKE_AR=${CMAKE_AR}
202                       -DCMAKE_RANLIB=${CMAKE_RANLIB}
203                       -DCMAKE_NM=${CMAKE_NM}
204                       -DCMAKE_OBJCOPY=${CMAKE_OBJCOPY}
205                       -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP}
206                       -DCMAKE_STRIP=${CMAKE_STRIP})
207     set(llvm_config_path ${LLVM_CONFIG_PATH})
209     if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
210       string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
211              ${PACKAGE_VERSION})
212       set(resource_dir "${LLVM_LIBRARY_DIR}/clang/${CLANG_VERSION}")
213       set(flag_types ASM C CXX MODULE_LINKER SHARED_LINKER EXE_LINKER)
214       foreach(type ${flag_types})
215         set(${type}_flag -DCMAKE_${type}_FLAGS=-resource-dir=${resource_dir})
216       endforeach()
217       string(REPLACE ";" "|" flag_string "${flag_types}")
218       foreach(arg ${ARG_CMAKE_ARGS})
219         if(arg MATCHES "^-DCMAKE_(${flag_string})_FLAGS")
220           foreach(type ${flag_types})
221             if(arg MATCHES "^-DCMAKE_${type}_FLAGS")
222               string(REGEX REPLACE "^-DCMAKE_${type}_FLAGS=(.*)$" "\\1" flag_value "${arg}")
223               set(${type}_flag "${${type}_flag} ${flag_value}")
224             endif()
225           endforeach()
226         else()
227           list(APPEND cmake_args ${arg})
228         endif()
229       endforeach()
230       foreach(type ${flag_types})
231         list(APPEND cmake_args ${${type}_flag})
232       endforeach()
233     endif()
234   else()
235     set(llvm_config_path "$<TARGET_FILE:llvm-config>")
236     set(cmake_args ${ARG_CMAKE_ARGS})
237   endif()
239   ExternalProject_Add(${name}
240     DEPENDS ${ARG_DEPENDS} llvm-config
241     ${name}-clobber
242     PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
243     SOURCE_DIR ${source_dir}
244     STAMP_DIR ${STAMP_DIR}
245     BINARY_DIR ${BINARY_DIR}
246     ${exclude}
247     CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
248                ${compiler_args}
249                -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
250                ${sysroot_arg}
251                -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
252                -DLLVM_CONFIG_PATH=${llvm_config_path}
253                -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
254                -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE}
255                -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT}
256                -DLLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO=${LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO}
257                -DLLVM_USE_RELATIVE_PATHS_IN_FILES=${LLVM_USE_RELATIVE_PATHS_IN_FILES}
258                -DLLVM_LIT_ARGS=${LLVM_LIT_ARGS}
259                -DLLVM_SOURCE_PREFIX=${LLVM_SOURCE_PREFIX}
260                -DPACKAGE_VERSION=${PACKAGE_VERSION}
261                -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
262                -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
263                -DCMAKE_EXPORT_COMPILE_COMMANDS=1
264                ${cmake_args}
265                ${PASSTHROUGH_VARIABLES}
266     INSTALL_COMMAND ""
267     STEP_TARGETS configure build
268     BUILD_ALWAYS 1
269     USES_TERMINAL_CONFIGURE 1
270     USES_TERMINAL_BUILD 1
271     USES_TERMINAL_INSTALL 1
272     LIST_SEPARATOR |
273     )
275   if(ARG_USE_TOOLCHAIN)
276     set(force_deps DEPENDS ${TOOLCHAIN_BINS})
277   endif()
279   llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
280   ExternalProject_Add_Step(${name} clean
281     COMMAND ${run_clean}
282     COMMENT "Cleaning ${name}..."
283     DEPENDEES configure
284     ${force_deps}
285     WORKING_DIRECTORY ${BINARY_DIR}
286     EXCLUDE_FROM_MAIN 1
287     USES_TERMINAL 1
288     )
289   ExternalProject_Add_StepTargets(${name} clean)
291   if(ARG_USE_TOOLCHAIN)
292     add_dependencies(${name}-clean ${name}-clobber)
293     set_target_properties(${name}-clean PROPERTIES
294       SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
295   endif()
297   if(NOT ARG_NO_INSTALL)
298     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\)"
299       COMPONENT ${name})
301     add_llvm_install_targets(install-${name}
302                              DEPENDS ${name}
303                              COMPONENT ${name})
304   endif()
306   # Add top-level targets
307   foreach(target ${ARG_EXTRA_TARGETS})
308     if(DEFINED ${target})
309       set(external_target "${${target}}")
310     else()
311       set(external_target "${target}")
312     endif()
313     llvm_ExternalProject_BuildCmd(build_runtime_cmd ${external_target} ${BINARY_DIR})
314     add_custom_target(${target}
315       COMMAND ${build_runtime_cmd}
316       DEPENDS ${name}-configure
317       WORKING_DIRECTORY ${BINARY_DIR}
318       VERBATIM
319       USES_TERMINAL)
320   endforeach()
321 endfunction()