blob: c851eb8dbf0861e4cad3a31aefd8ac5fdc3b5a3d [file] [log] [blame]
Chris Bieneman788c95c2015-11-11 16:14:03 +00001include(ExternalProject)
2
3# llvm_ExternalProject_BuildCmd(out_var target)
4# Utility function for constructing command lines for external project targets
Chris Bieneman79c68d52016-04-08 22:46:04 +00005function(llvm_ExternalProject_BuildCmd out_var target bin_dir)
Chris Bieneman76864f92016-09-06 20:27:07 +00006 cmake_parse_arguments(ARG "" "CONFIGURATION" "" ${ARGN})
7 if(NOT ARG_CONFIGURATION)
8 set(ARG_CONFIGURATION "$<CONFIGURATION>")
9 endif()
Chris Bieneman788c95c2015-11-11 16:14:03 +000010 if (CMAKE_GENERATOR MATCHES "Make")
11 # Use special command for Makefiles to support parallelism.
Chris Bieneman76864f92016-09-06 20:27:07 +000012 set(${out_var} "$(MAKE)" "-C" "${bin_dir}" "${target}" PARENT_SCOPE)
Chris Bieneman788c95c2015-11-11 16:14:03 +000013 else()
Chris Bieneman79c68d52016-04-08 22:46:04 +000014 set(${out_var} ${CMAKE_COMMAND} --build ${bin_dir} --target ${target}
Chris Bieneman76864f92016-09-06 20:27:07 +000015 --config ${ARG_CONFIGURATION} PARENT_SCOPE)
Chris Bieneman788c95c2015-11-11 16:14:03 +000016 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
Chris Bieneman64adae52016-06-23 22:07:21 +000026# ALWAYS_CLEAN
27# Always clean the sub-project before building
Chris Bieneman788c95c2015-11-11 16:14:03 +000028# 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
Chris Bieneman64adae52016-06-23 22:07:21 +000036# PASSTHROUGH_PREFIXES prefix...
37# Extra variable prefixes (name is always included) to pass down
Chris Bieneman788c95c2015-11-11 16:14:03 +000038# )
39function(llvm_ExternalProject_Add name source_dir)
Chris Bieneman64adae52016-06-23 22:07:21 +000040 cmake_parse_arguments(ARG
41 "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
Chris Bieneman788c95c2015-11-11 16:14:03 +000042 "SOURCE_DIR"
Chris Bieneman64adae52016-06-23 22:07:21 +000043 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES"
44 ${ARGN})
Chris Bieneman788c95c2015-11-11 16:14:03 +000045 canonicalize_tool_name(${name} nameCanon)
46 if(NOT ARG_TOOLCHAIN_TOOLS)
47 set(ARG_TOOLCHAIN_TOOLS clang lld)
Petr Hosek024a17b2016-12-12 23:15:10 +000048 if(NOT APPLE AND NOT WIN32)
49 list(APPEND ARG_TOOLCHAIN_TOOLS llvm-ar llvm-ranlib)
50 endif()
Chris Bieneman788c95c2015-11-11 16:14:03 +000051 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
Chris Bieneman64adae52016-06-23 22:07:21 +000068 if(ARG_ALWAYS_CLEAN)
69 set(always_clean clean)
70 endif()
71
Chris Bieneman788c95c2015-11-11 16:14:03 +000072 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
Chris Bieneman788c95c2015-11-11 16:14:03 +000081 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"
Chris Bienemandf04e792016-06-08 22:19:25 +000088 USES_TERMINAL
Chris Bieneman788c95c2015-11-11 16:14:03 +000089 )
90
Chris Bieneman64adae52016-06-23 22:07:21 +000091 # Find all variables that start with a prefix and propagate them through
Chris Bieneman788c95c2015-11-11 16:14:03 +000092 get_cmake_property(variableNames VARIABLES)
Chris Bieneman64adae52016-06-23 22:07:21 +000093
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()
Chris Bieneman788c95c2015-11-11 16:14:03 +0000103 endforeach()
104
105 if(ARG_USE_TOOLCHAIN)
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()
Petr Hosek024a17b2016-12-12 23:15:10 +0000110 if(llvm-ar IN_LIST TOOLCHAIN_TOOLS)
111 list(APPEND compiler_args -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar)
112 endif()
113 if(llvm-ranlib IN_LIST TOOLCHAIN_TOOLS)
114 list(APPEND compiler_args -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib)
115 endif()
Chris Bieneman788c95c2015-11-11 16:14:03 +0000116 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
117 endif()
118
119 add_custom_command(
120 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
121 DEPENDS ${ARG_DEPENDS}
122 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
123 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
124 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
125 COMMENT "Clobbering bootstrap build and stamp directories"
126 )
127
128 add_custom_target(${name}-clobber
129 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
130
131 if(ARG_EXCLUDE_FROM_ALL)
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000132 set(exclude EXCLUDE_FROM_ALL 1)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000133 endif()
134
135 ExternalProject_Add(${name}
Chris Bieneman997ee2b2016-08-18 21:41:21 +0000136 DEPENDS ${ARG_DEPENDS} llvm-config
Chris Bieneman788c95c2015-11-11 16:14:03 +0000137 ${name}-clobber
138 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
139 SOURCE_DIR ${source_dir}
140 STAMP_DIR ${STAMP_DIR}
141 BINARY_DIR ${BINARY_DIR}
142 ${exclude}
143 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
144 ${compiler_args}
145 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
Chris Bieneman64adae52016-06-23 22:07:21 +0000146 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
147 -DLLVM_CONFIG_PATH=$<TARGET_FILE:llvm-config>
148 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
149 -DPACKAGE_VERSION=${PACKAGE_VERSION}
150 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
151 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
Chris Bieneman788c95c2015-11-11 16:14:03 +0000152 ${ARG_CMAKE_ARGS}
153 ${PASSTHROUGH_VARIABLES}
154 INSTALL_COMMAND ""
155 STEP_TARGETS configure build
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000156 BUILD_ALWAYS 1
157 USES_TERMINAL_CONFIGURE 1
158 USES_TERMINAL_BUILD 1
159 USES_TERMINAL_INSTALL 1
Chris Bieneman788c95c2015-11-11 16:14:03 +0000160 )
161
Chris Bieneman788c95c2015-11-11 16:14:03 +0000162 if(ARG_USE_TOOLCHAIN)
163 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
164 endif()
165
Chris Bieneman79c68d52016-04-08 22:46:04 +0000166 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000167 ExternalProject_Add_Step(${name} clean
168 COMMAND ${run_clean}
169 COMMENT "Cleaning ${name}..."
170 DEPENDEES configure
171 ${force_deps}
172 WORKING_DIRECTORY ${BINARY_DIR}
Chris Bieneman64adae52016-06-23 22:07:21 +0000173 EXCLUDE_FROM_MAIN 1
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000174 USES_TERMINAL 1
Chris Bieneman788c95c2015-11-11 16:14:03 +0000175 )
176 ExternalProject_Add_StepTargets(${name} clean)
177
178 if(ARG_USE_TOOLCHAIN)
179 add_dependencies(${name}-clean ${name}-clobber)
180 set_target_properties(${name}-clean PROPERTIES
181 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
182 endif()
183
184 if(NOT ARG_NO_INSTALL)
185 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${BINARY_DIR}/cmake_install.cmake \)"
186 COMPONENT ${name})
187
188 add_custom_target(install-${name}
189 DEPENDS ${name}
190 COMMAND "${CMAKE_COMMAND}"
191 -DCMAKE_INSTALL_COMPONENT=${name}
192 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
Chris Bienemandf04e792016-06-08 22:19:25 +0000193 USES_TERMINAL)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000194 endif()
195
196 # Add top-level targets
197 foreach(target ${ARG_EXTRA_TARGETS})
Petr Hosekab9991f2017-07-11 23:41:15 +0000198 string(REPLACE ":" ";" target_list ${target})
199 list(GET target_list 0 target)
200 list(LENGTH target_list target_list_len)
201 if(${target_list_len} GREATER 1)
202 list(GET target_list 1 target_name)
203 else()
204 set(target_name "${target}")
205 endif()
Chris Bieneman79c68d52016-04-08 22:46:04 +0000206 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${target} ${BINARY_DIR})
Petr Hosekab9991f2017-07-11 23:41:15 +0000207 add_custom_target(${target_name}
Chris Bieneman788c95c2015-11-11 16:14:03 +0000208 COMMAND ${build_runtime_cmd}
209 DEPENDS ${name}-configure
210 WORKING_DIRECTORY ${BINARY_DIR}
211 VERBATIM
Chris Bienemandf04e792016-06-08 22:19:25 +0000212 USES_TERMINAL)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000213 endforeach()
214endfunction()