blob: f7020fbc1661cf5d90d423ad3a87cfd55546fbff [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)
48 endif()
49 foreach(tool ${ARG_TOOLCHAIN_TOOLS})
50 if(TARGET ${tool})
51 list(APPEND TOOLCHAIN_TOOLS ${tool})
52 list(APPEND TOOLCHAIN_BINS $<TARGET_FILE:${tool}>)
53 endif()
54 endforeach()
55
56 if(NOT ARG_RUNTIME_LIBRARIES)
57 set(ARG_RUNTIME_LIBRARIES compiler-rt libcxx)
58 endif()
59 foreach(lib ${ARG_RUNTIME_LIBRARIES})
60 if(TARGET ${lib})
61 list(APPEND RUNTIME_LIBRARIES ${lib})
62 endif()
63 endforeach()
64
Chris Bieneman64adae52016-06-23 22:07:21 +000065 if(ARG_ALWAYS_CLEAN)
66 set(always_clean clean)
67 endif()
68
Chris Bieneman788c95c2015-11-11 16:14:03 +000069 list(FIND TOOLCHAIN_TOOLS clang FOUND_CLANG)
70 if(FOUND_CLANG GREATER -1)
71 set(CLANG_IN_TOOLCHAIN On)
72 endif()
73
74 if(RUNTIME_LIBRARIES AND CLANG_IN_TOOLCHAIN)
75 list(APPEND TOOLCHAIN_BINS ${RUNTIME_LIBRARIES})
76 endif()
77
Chris Bieneman788c95c2015-11-11 16:14:03 +000078 set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-stamps/)
79 set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${name}-bins/)
80
81 add_custom_target(${name}-clear
82 COMMAND ${CMAKE_COMMAND} -E remove_directory ${BINARY_DIR}
83 COMMAND ${CMAKE_COMMAND} -E remove_directory ${STAMP_DIR}
84 COMMENT "Clobbering ${name} build and stamp directories"
Chris Bienemandf04e792016-06-08 22:19:25 +000085 USES_TERMINAL
Chris Bieneman788c95c2015-11-11 16:14:03 +000086 )
87
Chris Bieneman64adae52016-06-23 22:07:21 +000088 # Find all variables that start with a prefix and propagate them through
Chris Bieneman788c95c2015-11-11 16:14:03 +000089 get_cmake_property(variableNames VARIABLES)
Chris Bieneman64adae52016-06-23 22:07:21 +000090
91 list(APPEND ARG_PASSTHROUGH_PREFIXES ${nameCanon})
92 foreach(prefix ${ARG_PASSTHROUGH_PREFIXES})
93 foreach(variableName ${variableNames})
94 if(variableName MATCHES "^${prefix}")
95 string(REPLACE ";" "\;" value "${${variableName}}")
96 list(APPEND PASSTHROUGH_VARIABLES
97 -D${variableName}=${value})
98 endif()
99 endforeach()
Chris Bieneman788c95c2015-11-11 16:14:03 +0000100 endforeach()
101
102 if(ARG_USE_TOOLCHAIN)
103 if(CLANG_IN_TOOLCHAIN)
104 set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
105 -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
106 endif()
107 list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
108 endif()
109
110 add_custom_command(
111 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
112 DEPENDS ${ARG_DEPENDS}
113 COMMAND ${CMAKE_COMMAND} -E touch ${BINARY_DIR}/CMakeCache.txt
114 COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_DIR}/${name}-mkdir
115 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
116 COMMENT "Clobbering bootstrap build and stamp directories"
117 )
118
119 add_custom_target(${name}-clobber
120 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
121
122 if(ARG_EXCLUDE_FROM_ALL)
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000123 set(exclude EXCLUDE_FROM_ALL 1)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000124 endif()
125
126 ExternalProject_Add(${name}
Chris Bieneman997ee2b2016-08-18 21:41:21 +0000127 DEPENDS ${ARG_DEPENDS} llvm-config
Chris Bieneman788c95c2015-11-11 16:14:03 +0000128 ${name}-clobber
129 PREFIX ${CMAKE_BINARY_DIR}/projects/${name}
130 SOURCE_DIR ${source_dir}
131 STAMP_DIR ${STAMP_DIR}
132 BINARY_DIR ${BINARY_DIR}
133 ${exclude}
134 CMAKE_ARGS ${${nameCanon}_CMAKE_ARGS}
135 ${compiler_args}
136 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
Chris Bieneman64adae52016-06-23 22:07:21 +0000137 -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR}
138 -DLLVM_CONFIG_PATH=$<TARGET_FILE:llvm-config>
139 -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR}
140 -DPACKAGE_VERSION=${PACKAGE_VERSION}
141 -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
142 -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
Chris Bieneman788c95c2015-11-11 16:14:03 +0000143 ${ARG_CMAKE_ARGS}
144 ${PASSTHROUGH_VARIABLES}
145 INSTALL_COMMAND ""
146 STEP_TARGETS configure build
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000147 BUILD_ALWAYS 1
148 USES_TERMINAL_CONFIGURE 1
149 USES_TERMINAL_BUILD 1
150 USES_TERMINAL_INSTALL 1
Chris Bieneman788c95c2015-11-11 16:14:03 +0000151 )
152
Chris Bieneman788c95c2015-11-11 16:14:03 +0000153 if(ARG_USE_TOOLCHAIN)
154 set(force_deps DEPENDS ${TOOLCHAIN_BINS})
155 endif()
156
Chris Bieneman79c68d52016-04-08 22:46:04 +0000157 llvm_ExternalProject_BuildCmd(run_clean clean ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000158 ExternalProject_Add_Step(${name} clean
159 COMMAND ${run_clean}
160 COMMENT "Cleaning ${name}..."
161 DEPENDEES configure
162 ${force_deps}
163 WORKING_DIRECTORY ${BINARY_DIR}
Chris Bieneman64adae52016-06-23 22:07:21 +0000164 EXCLUDE_FROM_MAIN 1
Chris Bieneman2b5a5452016-06-09 22:41:36 +0000165 USES_TERMINAL 1
Chris Bieneman788c95c2015-11-11 16:14:03 +0000166 )
167 ExternalProject_Add_StepTargets(${name} clean)
168
169 if(ARG_USE_TOOLCHAIN)
170 add_dependencies(${name}-clean ${name}-clobber)
171 set_target_properties(${name}-clean PROPERTIES
172 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp)
173 endif()
174
175 if(NOT ARG_NO_INSTALL)
176 install(CODE "execute_process\(COMMAND \${CMAKE_COMMAND} -DCMAKE_INSTALL_PREFIX=\${CMAKE_INSTALL_PREFIX} -P ${BINARY_DIR}/cmake_install.cmake \)"
177 COMPONENT ${name})
178
179 add_custom_target(install-${name}
180 DEPENDS ${name}
181 COMMAND "${CMAKE_COMMAND}"
182 -DCMAKE_INSTALL_COMPONENT=${name}
183 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
Chris Bienemandf04e792016-06-08 22:19:25 +0000184 USES_TERMINAL)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000185 endif()
186
187 # Add top-level targets
188 foreach(target ${ARG_EXTRA_TARGETS})
Chris Bieneman79c68d52016-04-08 22:46:04 +0000189 llvm_ExternalProject_BuildCmd(build_runtime_cmd ${target} ${BINARY_DIR})
Chris Bieneman788c95c2015-11-11 16:14:03 +0000190 add_custom_target(${target}
191 COMMAND ${build_runtime_cmd}
192 DEPENDS ${name}-configure
193 WORKING_DIRECTORY ${BINARY_DIR}
194 VERBATIM
Chris Bienemandf04e792016-06-08 22:19:25 +0000195 USES_TERMINAL)
Chris Bieneman788c95c2015-11-11 16:14:03 +0000196 endforeach()
197endfunction()