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