forked from scikit-build/cmake-python-distributions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
340 lines (291 loc) · 11.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
cmake_minimum_required(VERSION 3.6)
#
# For more details, see docs/building.rst
#
project(CMakePythonDistributions NONE)
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
mark_as_advanced(CMAKE_BUILD_TYPE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
if(NOT DEFINED CMakePythonDistributions_SUPERBUILD)
set(CMakePythonDistributions_SUPERBUILD 1)
endif()
if(CMakePythonDistributions_SUPERBUILD)
enable_language(CXX)
#-----------------------------------------------------------------------------
# Options
set(default ON)
if(WIN32 OR APPLE)
set(default OFF)
endif()
option(BUILD_CMAKE_FROM_SOURCE "Build CMake from source" ${default})
option(BUILD_VERBOSE "Build reporting additional information (e.g download progress, ...)" ON)
set(CMakePythonDistributions_ARCHIVE_DOWNLOAD_DIR "${CMAKE_BINARY_DIR}"
CACHE PATH "Directory where to download archives"
)
message(STATUS "***************************************************")
message(STATUS "Build CMake from source: ${BUILD_CMAKE_FROM_SOURCE}")
message(STATUS "***************************************************")
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeUrls.cmake)
#-----------------------------------------------------------------------------
# Which archives ?
function(check_archive_var archive_var)
if(NOT DEFINED "${archive_var}_url")
message(FATAL_ERROR "Failed to determine which archive to download: '${archive_var}_url' variable is not defined")
endif()
if(NOT DEFINED "${archive_var}_sha256")
message(FATAL_ERROR "Could you make sure variable '${archive_var}_sha256' is defined ?")
endif()
endfunction()
set(src_archive "unix_source")
if(WIN32)
set(src_archive "windows_source")
endif()
check_archive_var("${src_archive}")
set(binary_archive "linux32_binary")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(binary_archive "linux64_binary")
endif()
if(APPLE)
set(binary_archive "macosx_binary")
endif()
if(WIN32)
set(binary_archive "win32_binary")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(binary_archive "win64_binary")
endif()
endif()
check_archive_var("${binary_archive}")
#-----------------------------------------------------------------------------
include(ExternalProject)
# Add an empty external project
function(cpd_ExternalProject_Add_Empty proj depends)
set(depends_args)
if(NOT depends STREQUAL "")
set(depends_args DEPENDS ${depends})
endif()
ExternalProject_add(${proj}
SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
${depends_args}
)
endfunction()
# Note: To minimize confusion between variables defined by CMake and
# variables used in this project. The following convention applies:
# CMakeProject_xxx : Variables defined in this project
# CMAKE_xxx : Variables set by CMake
set(${PROJECT_NAME}_CMAKE_CACHE_ARG)
set(ep_download_no_progress_args)
set(ep_log_configure_build_args)
if(NOT BUILD_VERBOSE)
set(ep_download_no_progress_args
DOWNLOAD_NO_PROGRESS 1
)
set(ep_log_configure_build_args
LOG_CONFIGURE 1
LOG_BUILD 1
)
endif()
#
# CMakeProject_SOURCE_DIR: Always expect the sources (needed for `sdist`)
#
if(NOT DEFINED CMakeProject_SOURCE_DIR)
set(CMakeProject_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src")
# Download selected source archive
ExternalProject_add(CMakeProject-src-download
SOURCE_DIR ${CMakeProject_SOURCE_DIR}
URL ${${src_archive}_url}
URL_HASH SHA256=${${src_archive}_sha256}
DOWNLOAD_DIR ${CMakePythonDistributions_ARCHIVE_DOWNLOAD_DIR}
USES_TERMINAL_DOWNLOAD 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
${ep_download_no_progress_args}
)
message(STATUS "SuperBuild - CMakeProject-src-download")
message(STATUS "SuperBuild - CMakeProject-src-download - URL: ${${src_archive}_url}")
else()
cpd_ExternalProject_Add_Empty(CMakeProject-src-download "")
message(STATUS "SuperBuild - CMakeProject-src-download")
endif()
message(STATUS "SuperBuild - CMakeProject-src-download - CMakeProject_SOURCE_DIR: ${CMakeProject_SOURCE_DIR}")
if(NOT EXISTS ${CMakeProject_SOURCE_DIR})
message(FATAL_ERROR "CMakeProject_SOURCE_DIR variable is defined but corresponds to nonexistent directory")
endif()
list(APPEND ${PROJECT_NAME}_DEPENDS CMakeProject-src-download)
if(BUILD_CMAKE_FROM_SOURCE)
#
# CMakeProject_BINARY_DIR
#
if(NOT DEFINED CMakeProject_BINARY_DIR)
set(CMakeProject_BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeProject-build)
set(_cmake_cache_args)
if(DEFINED CMAKE_BUILD_TYPE)
list(APPEND _cmake_cache_args
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
)
endif()
if(DEFINED OPENSSL_ROOT_DIR)
list(APPEND _cmake_cache_args
-DOPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}
)
message(STATUS "SuperBuild - CMakeProject-build - OPENSSL_ROOT_DIR: ${OPENSSL_ROOT_DIR}")
endif()
foreach(var_name IN ITEMS
CMAKE_BUILD_PARALLEL_LEVEL
CMAKE_JOB_POOLS
CMAKE_JOB_POOL_COMPILE
CMAKE_JOB_POOL_LINK
)
if(DEFINED ${var_name})
list(APPEND _cmake_cache_args
-D${var_name}:STRING=${${var_name}}
)
message(STATUS "SuperBuild - CMakeProject-build - ${var_name}: ${${var_name}}")
endif()
endforeach()
ExternalProject_add(CMakeProject-build
SOURCE_DIR ${CMakeProject_SOURCE_DIR}
BINARY_DIR ${CMakeProject_BINARY_DIR}
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
BUILD_ALWAYS 1
CMAKE_CACHE_ARGS
-DBUILD_CursesDialog:BOOL=OFF
-DCMAKE_USE_OPENSSL:BOOL=ON
-DBUILD_TESTING:BOOL=OFF
-DCMake_INSTALL_DEPENDENCIES:BOOL=ON
-DCMAKE_INSTALL_MESSAGE:STRING=NEVER
${_cmake_cache_args}
USES_TERMINAL_CONFIGURE 1
USES_TERMINAL_BUILD 1
${ep_log_configure_build_args}
INSTALL_COMMAND ""
DEPENDS
CMakeProject-src-download
)
find_program(STRIP_EXECUTABLE strip)
if(STRIP_EXECUTABLE)
set(cmake_executable "${CMakeProject_BINARY_DIR}/bin/cmake")
set(cpack_executable "${CMakeProject_BINARY_DIR}/bin/cpack")
set(ctest_executable "${CMakeProject_BINARY_DIR}/bin/ctest")
ExternalProject_Add_Step(CMakeProject-build strip_executables
DEPENDEES build
COMMENT "Stripping CMake executables"
COMMAND ${STRIP_EXECUTABLE} ${cmake_executable}
COMMAND ${STRIP_EXECUTABLE} ${cpack_executable}
COMMAND ${STRIP_EXECUTABLE} ${ctest_executable}
)
endif()
else()
cpd_ExternalProject_Add_Empty(CMakeProject-build "CMakeProject-src-download")
endif()
message(STATUS "SuperBuild - CMakeProject-build")
message(STATUS "SuperBuild - CMakeProject-build - CMakeProject_BINARY_DIR: ${CMakeProject_BINARY_DIR}")
if(NOT EXISTS ${CMakeProject_BINARY_DIR})
message(FATAL_ERROR "CMakeProject_BINARY_DIR variable is defined but corresponds to nonexistent directory")
endif()
list(APPEND ${PROJECT_NAME}_DEPENDS CMakeProject-build)
list(APPEND ${PROJECT_NAME}_CMAKE_CACHE_ARG
-DCMakeProject_BINARY_DIR:PATH=${CMakeProject_BINARY_DIR}
)
else()
#
# CMakeProject_BINARY_DISTRIBUTION_DIR
#
if(${binary_archive}_sha256 STREQUAL "NA")
message(FATAL_ERROR "Pre-built archives not available for '${binary_archive}'. Consider setting BUILD_CMAKE_FROM_SOURCE to ON.")
endif()
set(CMakeProject_BINARY_DISTRIBUTION_DIR "${CMAKE_BINARY_DIR}/CMakeProject-binary-distribution")
# Download selected binary archive
ExternalProject_add(CMakeProject-binary-download
SOURCE_DIR ${CMakeProject_BINARY_DISTRIBUTION_DIR}
URL ${${binary_archive}_url}
URL_HASH SHA256=${${binary_archive}_sha256}
DOWNLOAD_DIR ${CMakePythonDistributions_ARCHIVE_DOWNLOAD_DIR}
USES_TERMINAL_DOWNLOAD 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
${ep_download_no_progress_args}
)
message(STATUS "SuperBuild - CMakeProject-binary-download")
message(STATUS "SuperBuild - CMakeProject-binary-download - URL: ${${binary_archive}_url}")
list(APPEND ${PROJECT_NAME}_DEPENDS CMakeProject-binary-download)
list(APPEND ${PROJECT_NAME}_CMAKE_CACHE_ARG
-DCMakeProject_BINARY_DISTRIBUTION_DIR:PATH=${CMakeProject_BINARY_DISTRIBUTION_DIR}
)
endif()
ExternalProject_add(${PROJECT_NAME}
SOURCE_DIR ${CMAKE_SOURCE_DIR}
BINARY_DIR ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-build
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
CMAKE_CACHE_ARGS
-D${PROJECT_NAME}_SUPERBUILD:BOOL=0
-DBUILD_CMAKE_FROM_SOURCE:BOOL=${BUILD_CMAKE_FROM_SOURCE}
-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}
${${PROJECT_NAME}_CMAKE_CACHE_ARG}
USES_TERMINAL_CONFIGURE 1
USES_TERMINAL_BUILD 1
INSTALL_COMMAND ""
DEPENDS
${${PROJECT_NAME}_DEPENDS}
)
message(STATUS "SuperBuild - ${PROJECT_NAME}")
# This adds an "install" target in the top-level directory. The
# target will simply include the install rules associated with the
# inner build
install(SCRIPT ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-build/cmake_install.cmake)
else()
#-----------------------------------------------------------------------------
if(BUILD_CMAKE_FROM_SOURCE)
# Install CMakeProject
install(CODE "
message(STATUS \"Install CMake project\")
include\(\"${CMakeProject_BINARY_DIR}/cmake_install.cmake\")
")
#-----------------------------------------------------------------------------
else()
set(CMAKE_INSTALL_MESSAGE "NEVER")
# Install all files from binary distribution
file(GLOB_RECURSE binary_distribution_files
LIST_DIRECTORIES FALSE
${CMakeProject_BINARY_DISTRIBUTION_DIR}/*
)
foreach(file IN LISTS binary_distribution_files)
get_filename_component(directory ${file} DIRECTORY)
file(RELATIVE_PATH relative_directory ${CMakeProject_BINARY_DISTRIBUTION_DIR} ${directory})
set(type FILES)
if(relative_directory STREQUAL "bin")
set(type PROGRAMS)
endif()
# Skip symlinks like "CMake.app/Contents/Frameworks/QtWidgets.framework/Versions/Current"
if(IS_SYMLINK ${file})
continue()
endif()
set(_permissions)
get_filename_component(filename ${file} NAME)
if(filename MATCHES "ccmake|cmake|cmake-gui|cpack|ctest")
set(_permissions PERMISSIONS
OWNER_READ OWNER_WRITE OWNER_EXECUTE
GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)
endif()
install(${type} ${file} DESTINATION "${relative_directory}" ${_permissions})
endforeach()
endif()
endif()