blob: 3bc39b680ce611d8d50be6c1bce6f95589cdc49b [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 Bieneman788c95c2015-11-11 16:14:03 +00006 if (CMAKE_GENERATOR MATCHES "Make")
7 # Use special command for Makefiles to support parallelism.
Chris Bieneman79c68d52016-04-08 22:46:04 +00008 set(${out_var} "$(MAKE)" "-C" "${BINARY_DIR}" "${target}" PARENT_SCOPE)
Chris Bieneman788c95c2015-11-11 16:14:03 +00009 else()
Chris Bieneman79c68d52016-04-08 22:46:04 +000010 set(${out_var} ${CMAKE_COMMAND} --build ${bin_dir} --target ${target}
Chris Bieneman788c95c2015-11-11 16:14:03 +000011 --config $<CONFIGURATION> PARENT_SCOPE)
12 endif()
13endfunction()
14
15# llvm_ExternalProject_Add(name source_dir ...
16# USE_TOOLCHAIN
17# Use just-built tools (see TOOLCHAIN_TOOLS)
18# EXCLUDE_FROM_ALL
19# Exclude this project from the all target
20# NO_INSTALL
21# Don't generate install targets for this project
22# CMAKE_ARGS arguments...
23# Optional cmake arguments to pass when configuring the project
24# TOOLCHAIN_TOOLS targets...
25# Targets for toolchain tools (defaults to clang;lld)
26# DEPENDS targets...
27# Targets that this project depends on
28# EXTRA_TARGETS targets...
29# Extra targets in the subproject to generate targets for
30# )
31function(llvm_ExternalProject_Add name source_dir)
32 cmake_parse_arguments(ARG "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL"
33 "SOURCE_DIR"
34 "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS" ${ARGN})
35 canonicalize_tool_name(${name} nameCanon)
36 if(NOT ARG_TOOLCHAIN_TOOLS)
37 set(ARG_TOOLCHAIN_TOOLS clang lld)
38 endif()
39 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
40 if(TARGET ${tool})
41 list(APPEND TOOLCHAIN_TOOLS ${tool})
42 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
43 endif()
44 endforeach()
45
46 if(NOT ARG_RUNTIME_LIBRARIES)
47 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
48 endif()
49 foreach(lib ${ARG_RUNTIME_LIBRARIES})
50 if(TARGET ${lib})
51 list(APPEND RUNTIME_LIBRARIES ${lib})
52 endif()
53 endforeach()
54
55 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
56 if(FOUND_CLANG GREATER -1)
57 set(CLANG_IN_TOOLCHAIN On)
58 endif()
59
60 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
61 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
62 endif()
63
64 if(CMAKE_VERSION VERSION_GREATER 3.1.0)
65 set(cmake_3_1_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL 1)
Chris Bieneman79c68d52016-04-08 22:46:04 +000066 set(cmake_3_1_BUILD_ALWAYS BUILD_ALWAYS 1)
Chris Bieneman788c95c2015-11-11 16:14:03 +000067 endif()
68
69 if(CMAKE_VERSION VERSION_GREATER 3.3.20150708)
70 set(cmake_3_4_USES_TERMINAL_OPTIONS
71 USES_TERMINAL_CONFIGURE 1
72 USES_TERMINAL_BUILD 1
73 USES_TERMINAL_INSTALL 1
74 )
75 set(cmake_3_4_USES_TERMINAL USES_TERMINAL 1)
76 endif()
77
78 if(CMAKE_VERSION VERSION_GREATER 3.1.20141116)
79 set(cmake_3_2_USES_TERMINAL USES_TERMINAL)
80 endif()
81
82 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
83 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
84
85 add_custom_target(${name}-clear
86 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
87 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
88 COMMENT "Clobbering ${name} build and stamp directories"
89 ${cmake_3_2_USES_TERMINAL}
90 )
91
92 # Find all variables that start with COMPILER_RT and populate a variable with
93 # them.
94 get_cmake_property(variableNames VARIABLES)
Chris Bieneman4cb7ab62015-11-23 23:34:09 +000095 foreach(variableName ${variableNames})
96 if(variableName MATCHES "^${nameCanon}")
97 string(REPLACE ";" "\;" value "${${variableName}}")
Chris Bieneman788c95c2015-11-11 16:14:03 +000098 list(APPEND PASSTHROUGH_VARIABLES
Chris Bieneman4b44a762015-12-03 19:47:04 +000099 -D${variableName}=${value})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000100 endif()
101 endforeach()
102
103 if(ARG_USE_TOOLCHAIN)
104 if(CLANG_IN_TOOLCHAIN)
105 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
106 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
107 endif()
108 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
109 endif()
110
111 add_custom_command(
112 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
113 DEPENDS ${ARG_DEPENDS}
114 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
115 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
116 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
117 COMMENT "Clobbering bootstrap build and stamp directories"
118 )
119
120 add_custom_target(${name}-clobber
121 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
122
123 if(ARG_EXCLUDE_FROM_ALL)
124 set(exclude ${cmake_3_1_EXCLUDE_FROM_ALL})
125 endif()
126
127 ExternalProject_Add(${name}
128 DEPENDS ${ARG_DEPENDS}
129 ${name}-clobber
130 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
131 SOURCE_DIR ${source_dir}
132 STAMP_DIR ${STAMP_DIR}
133 BINARY_DIR ${BINARY_DIR}
134 ${exclude}
135 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
136 ${compiler_args}
137 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
138 ${ARG_CMAKE_ARGS}
139 ${PASSTHROUGH_VARIABLES}
140 INSTALL_COMMAND ""
141 STEP_TARGETS configure build
Chris Bieneman79c68d52016-04-08 22:46:04 +0000142 ${cmake_3_1_BUILD_ALWAYS}
Chris Bieneman788c95c2015-11-11 16:14:03 +0000143 ${cmake_3_4_USES_TERMINAL_OPTIONS}
144 )
145
Chris Bieneman79c68d52016-04-08 22:46:04 +0000146 if(CMAKE_VERSION VERSION_LESS 3.1.0)
147 set(ALWAYS_REBUILD ${CMAKE_CURRENT_BINARY_DIR}/${name}-always-rebuild)
148 add_custom_target(${name}-always-rebuild
149 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-clobber-stamp)
150
151 llvm_ExternalProject_BuildCmd(run_build all ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000152 ExternalProject_Add_Step(${name} force-rebuild
Chris Bieneman79c68d52016-04-08 22:46:04 +0000153 COMMAND ${run_build}
154 COMMENT "Forcing rebuild of ${name}"
155 DEPENDEES configure clean
156 DEPENDS ${ALWAYS_REBUILD} ${ARG_DEPENDS} ${TOOLCHAIN_BINS}
Chris Bieneman788c95c2015-11-11 16:14:03 +0000157 ${cmake_3_4_USES_TERMINAL} )
158 endif()
159
160 if(ARG_USE_TOOLCHAIN)
161 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
162 endif()
163
Chris Bieneman79c68d52016-04-08 22:46:04 +0000164 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000165 ExternalProject_Add_Step(${name} clean
166 COMMAND ${run_clean}
167 COMMENT "Cleaning ${name}..."
168 DEPENDEES configure
169 ${force_deps}
170 WORKING_DIRECTORY ${BINARY_DIR}
171 ${cmake_3_4_USES_TERMINAL}
172 )
173 ExternalProject_Add_StepTargets(${name} clean)
174
175 if(ARG_USE_TOOLCHAIN)
176 add_dependencies(${name}-clean ${name}-clobber)
177 set_target_properties(${name}-clean PROPERTIES
178 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
179 endif()
180
181 if(NOT ARG_NO_INSTALL)
182 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${BINARY_DIR}/cmake_install.cmake \)"
183 COMPONENT ${name})
184
185 add_custom_target(install-${name}
186 DEPENDS ${name}
187 COMMAND "${CMAKE_COMMAND}"
188 -DCMAKE_INSTALL_COMPONENT=${name}
189 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
190 ${cmake_3_2_USES_TERMINAL})
191 endif()
192
193 # Add top-level targets
194 foreach(target ${ARG_EXTRA_TARGETS})
Chris Bieneman79c68d52016-04-08 22:46:04 +0000195 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${target} ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000196 add_custom_target(${target}
197 COMMAND ${build_runtime_cmd}
198 DEPENDS ${name}-configure
199 WORKING_DIRECTORY ${BINARY_DIR}
200 VERBATIM
201 ${cmake_3_2_USES_TERMINAL})
202 endforeach()
203endfunction()