This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCMakeLists.txt
441 lines (370 loc) · 15.7 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright (c) 2021, QuantStack and Mamba Contributors
#
# Distributed under the terms of the BSD 3-Clause License.
#
# The full license is in the file LICENSE, distributed with this software.
cmake_minimum_required(VERSION 3.20)
project(powerloader)
set(CMAKE_CXX_STANDARD 17)
set(POWERLOADER_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(POWERLOADER_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(POWERLOADER_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
if (MSVC)
message("Force Release build on Windows to prevent segfaults")
set(CMAKE_BUILD_TYPE Release)
endif()
# Versionning
# ===========
# Project version
file(STRINGS "${POWERLOADER_INCLUDE_DIR}/powerloader/powerloader.hpp" powerloader_version_defines
REGEX "#define POWERLOADER_VERSION_(MAJOR|MINOR|PATCH)")
foreach(ver ${powerloader_version_defines})
if(ver MATCHES "#define POWERLOADER_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
set(POWERLOADER_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
endif()
endforeach()
set(POWERLOADER_VERSION
"${POWERLOADER_VERSION_MAJOR}.${POWERLOADER_VERSION_MINOR}.${POWERLOADER_VERSION_PATCH}")
message(STATUS "powerloader version: v${POWERLOADER_VERSION}")
# Binary version
# See the following URL for explanations about the binary versionning
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
file(STRINGS "${POWERLOADER_INCLUDE_DIR}/powerloader/powerloader.hpp" powerloader_version_defines
REGEX "#define POWERLOADER_BINARY_(CURRENT|REVISION|AGE)")
foreach(ver ${powerloader_version_defines})
if(ver MATCHES "#define POWERLOADER_BINARY_(CURRENT|REVISION|AGE) +([^ ]+)$")
set(POWERLOADER_BINARY_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
endif()
endforeach()
set(POWERLOADER_BINARY_VERSION
${POWERLOADER_BINARY_CURRENT}.${POWERLOADER_BINARY_REVISION}.${POWERLOADER_BINARY_AGE})
message(STATUS "powerloader binary version: v${POWERLOADER_BINARY_VERSION}")
# Build options
# =============
# Compilation options
option(WITH_ZCHUNK "Enable zchunk" ON)
option(DEV "Enable dev" OFF)
option(BUILD_SHARED "Build shared powerloader library" OFF)
option(BUILD_STATIC "Build static powerloader library" OFF)
option(BUILD_EXE "Build powerloader executable" OFF)
option(WITH_ZSTD "Build powerloader with zstd" ON)
# Test options
option(ENABLE_TESTS "Enable tests" ON)
option(ENABLE_PYTHON "Enable python bindings" OFF)
if (WITH_ZCHUNK)
find_library(ZCK_LIBRARY zck REQUIRED)
find_file(ZCK_H_FILE NAMES zck.h REQUIRED)
get_filename_component(ZCK_INCLUDE_DIRS ${ZCK_H_FILE} DIRECTORY)
endif()
if (WITH_ZSTD)
find_package(zstd REQUIRED)
endif()
# Source files
# ============
set(LIBPOWERLOADER_PUBLIC_HEADERS
${POWERLOADER_INCLUDE_DIR}/powerloader/mirrors/oci.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/mirrors/s3.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/context.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/curl.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/export.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/download_target.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/downloader.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/enums.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/errors.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/fastest_mirror.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/fileio.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/mirror.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/powerloader.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/url.hpp
${POWERLOADER_INCLUDE_DIR}/powerloader/utils.hpp
)
set(LIBPOWERLOADER_SRCS
${POWERLOADER_SOURCE_DIR}/target.hpp
${POWERLOADER_SOURCE_DIR}/mirrors/oci.cpp
${POWERLOADER_SOURCE_DIR}/mirrors/s3.cpp
${POWERLOADER_SOURCE_DIR}/uploader/oci_upload.cpp
${POWERLOADER_SOURCE_DIR}/uploader/s3_upload.cpp
${POWERLOADER_SOURCE_DIR}/context.cpp
${POWERLOADER_SOURCE_DIR}/curl.cpp
${POWERLOADER_SOURCE_DIR}/download_target.cpp
${POWERLOADER_SOURCE_DIR}/downloader.cpp
${POWERLOADER_SOURCE_DIR}/fastest_mirror.cpp
${POWERLOADER_SOURCE_DIR}/mirror.cpp
${POWERLOADER_SOURCE_DIR}/target.cpp
${POWERLOADER_SOURCE_DIR}/url.cpp
${POWERLOADER_SOURCE_DIR}/utils.cpp
${POWERLOADER_SOURCE_DIR}/target.hpp
${POWERLOADER_SOURCE_DIR}/curl_internal.hpp
)
if (WITH_ZCHUNK)
list(APPEND LIBPOWERLOADER_SRCS ${POWERLOADER_SOURCE_DIR}/zck.cpp
${POWERLOADER_SOURCE_DIR}/zck.hpp)
endif()
if (WITH_ZSTD)
list(APPEND LIBPOWERLOADER_SRCS ${POWERLOADER_SOURCE_DIR}/compression.cpp)
endif()
# Targets and link
# ================
macro(libpowerloader_create_target target_name linkage deps_linkage output_name)
string(TOUPPER "${linkage}" linkage_upper)
if (NOT ${linkage_upper} MATCHES "^(SHARED|STATIC)$")
message(FATAL_ERROR "Invalid library linkage: ${linkage}")
endif ()
string(TOUPPER "${deps_linkage}" deps_linkage_upper)
if (NOT ${deps_linkage_upper} MATCHES "^(SHARED|STATIC)$")
message(FATAL_ERROR "Invalid library dependencies linkage: ${deps_linkage_upper}")
endif ()
# Output
# ======
add_library(${target_name} ${linkage_upper} ${LIBPOWERLOADER_SRCS} ${LIBPOWERLOADER_PUBLIC_HEADERS})
set_target_properties(
${target_name}
PROPERTIES
PREFIX ""
OUTPUT_NAME "${output_name}")
if (${linkage_upper} STREQUAL "STATIC")
# As a static library: no symbol import/export from any side.
target_compile_definitions(${target_name} PUBLIC POWERLOADER_STATIC)
else ()
# As a shared library: build exports symbols, usage will import.
target_compile_definitions(${target_name} PRIVATE POWERLOADER_EXPORTS)
endif()
target_include_directories(
${target_name}
PUBLIC
$<BUILD_INTERFACE:${POWERLOADER_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>
PRIVATE
${POWERLOADER_SOURCE_DIR}
)
if (MSVC)
target_compile_definitions(${target_name} PUBLIC -DNOMINMAX)
target_compile_options(${target_name}
PRIVATE
/wd4275 # Disable warning C4275: We know that we use templates instances in our symbol exports.
/wd4251 # Disable warning C4251: We know that we use templates instances in our symbol exports.
/permissive-
#/W4 # Recommended warning level 4
)
else()
target_compile_options(${target_name}
PRIVATE
-Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic
-fvisibility=hidden
)
endif()
# Commmon dependencies
# ============
find_package(tl-expected REQUIRED)
find_package(spdlog REQUIRED)
find_package(fmt REQUIRED)
if (${deps_linkage_upper} STREQUAL "STATIC")
message(" -> Statically linking against libpowerloader (static) dependencies")
if (UNIX)
set(REQUIRED_STATIC_DEPS
libcurl.a
libssh2.a
libgssapi_krb5.a
libkrb5.a
libk5crypto.a
libkrb5support.a
libcom_err.a
libssl.a
libcrypto.a
libzck.a
libnghttp2.a
libzstd.a
libz.a
)
set(STATIC_DEPS "")
foreach(LIB ${REQUIRED_STATIC_DEPS})
message("Searching for ${LIB}")
set(TMP_LIB "${LIB}-NOTFOUND")
find_library(TMP_LIB REQUIRED NAMES "${LIB}")
list(APPEND STATIC_DEPS "${TMP_LIB}")
endforeach(LIB)
message(" -> Found static dependencies:")
foreach(LIB ${STATIC_DEPS})
message(" - ${LIB}")
endforeach(LIB)
if (APPLE)
set (POWERLOADER_FORCE_DYNAMIC_LIBS resolv)
target_link_options(${target_name} PRIVATE -static-libstdc++)
find_library(SECURITY_LIBRARY Security)
find_library(SYSTEMCONFIGURATION_LIBRARY SystemConfiguration)
find_library(COREFOUNDATION_LIBRARY CoreFoundation)
message("Found library: ${SECURITY_LIBRARY}\n${COREFOUNDATION_LIBRARY}\n${SYSTEMCONFIGURATION_LIBRARY}")
target_link_libraries(${target_name} PRIVATE
${COREFOUNDATION_LIBRARY}
${SECURITY_LIBRARY}
${SYSTEMCONFIGURATION_LIBRARY}
)
elseif (UNIX)
set(POWERLOADER_FORCE_DYNAMIC_LIBS rt dl resolv)
target_link_options(${target_name} PUBLIC -static-libstdc++ -static-libgcc)
endif()
target_compile_definitions(${target_name} PUBLIC SPDLOG_FMT_EXTERNAL)
target_link_libraries(${target_name}
PUBLIC
tl::expected
spdlog::spdlog_header_only
fmt::fmt-header-only
${STATIC_DEPS}
${POWERLOADER_FORCE_DYNAMIC_LIBS}
)
elseif (WIN32)
set_target_properties(${target_name} PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
target_compile_definitions(${target_name} PUBLIC _CRT_SECURE_NO_WARNINGS CURL_SATATICLIB ZCHUNK_STATIC_LIB)
set(CMAKE_PREFIX_PATH "$ENV{VCPKG_ROOT}/installed/x64-windows-static/")
# For Windows we have a vcpkg based build system right now.
find_package(CURL CONFIG REQUIRED)
find_library(CRYPTO_LIBRARIES NAMES libcrypto REQUIRED)
find_package(zstd CONFIG REQUIRED)
find_library(ZCHUNK_LIBRARY NAMES libzck REQUIRED)
target_compile_definitions(${target_name} PUBLIC SPDLOG_FMT_EXTERNAL)
target_link_libraries(
${target_name}
PUBLIC
CURL::libcurl
${CRYPTO_LIBRARIES}
${ZCHUNK_LIBRARY}
zstd::libzstd_static
fmt::fmt-header-only
spdlog::spdlog_header_only
)
include_directories($ENV{CONDA_PREFIX}/Library/include/)
include_directories($ENV{VCPKG_ROOT}/installed/x64-windows-static/include/)
endif()
else ()
message(" -> Dynamically linking against libpowerloader (shared) dependencies")
find_package(CURL REQUIRED)
find_package(OpenSSL REQUIRED)
if (MSVC)
set(WINSOCK_LIBS wsock32 ws2_32)
endif()
target_link_libraries(${target_name}
PUBLIC
tl::expected
fmt::fmt
spdlog::spdlog_header_only
PRIVATE
${CURL_LIBRARIES}
${ZCK_LIBRARY}
${OPENSSL_LIBRARIES}
${WINSOCK_LIBS}
zstd::libzstd_shared
)
endif ()
if (WITH_ZCHUNK)
target_compile_definitions(${target_name} PUBLIC WITH_ZCHUNK)
if (MSVC)
target_compile_definitions(${target_name} PUBLIC /wd4996)
endif()
endif ()
if (WITH_ZSTD)
target_compile_definitions(${target_name} PUBLIC WITH_ZSTD)
endif ()
list(APPEND libpowerloader_targets ${target_name})
endmacro()
set(libpowerloader_targets "")
if (BUILD_SHARED)
message(STATUS "Adding shared libpowerloader target")
libpowerloader_create_target(libpowerloader SHARED SHARED libpowerloader)
endif ()
if (BUILD_STATIC)
message(STATUS "Adding static libpowerloader target")
# On Windows, a static library should use a different output name
# to avoid the conflict with the import library of a shared one.
if (WIN32)
libpowerloader_create_target(libpowerloader-static STATIC STATIC libpowerloader_static)
else ()
libpowerloader_create_target(libpowerloader-static STATIC STATIC libpowerloader)
endif ()
endif ()
if (NOT (BUILD_SHARED OR BUILD_STATIC))
message(FATAL_ERROR "Select at least a build variant for libpowerloader")
endif ()
# Dependencies for executable and tests
# =====================================
if (BUILD_SHARED)
set(powerloader_dependency libpowerloader)
elseif (BUILD_STATIC)
set(powerloader_dependency libpowerloader-static)
endif ()
# powerloader executable
# ======================
if (BUILD_EXE)
add_executable(powerloader ${POWERLOADER_SOURCE_DIR}/cli/main.cpp)
if (WIN32 AND NOT BUILD_SHARED)
set_target_properties(powerloader PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
target_include_directories(powerloader PUBLIC
${CURL_INCLUDE_DIRS}
${ZCK_INCLUDE_DIRS}
)
find_package(CLI11 REQUIRED)
find_package(yaml-cpp REQUIRED)
target_link_libraries(powerloader ${powerloader_dependency} CLI11::CLI11 yaml-cpp)
set_target_properties(powerloader PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
if (DEV)
add_executable(dpowerloader ${POWERLOADER_SOURCE_DIR}/main.cpp)
target_link_libraries(dpowerloader ${powerloader_dependency})
if (WITH_ZCHUNK)
target_compile_definitions(dpowerloader PUBLIC WITH_ZCHUNK)
endif()
endif()
if (WITH_ZCHUNK)
target_compile_definitions(powerloader PUBLIC WITH_ZCHUNK)
endif()
list(APPEND libpowerloader_targets powerloader)
endif()
# Tests
# =====
if (ENABLE_TESTS)
add_subdirectory(test)
endif()
if (ENABLE_PYTHON)
add_subdirectory(src/python)
endif()
# Installation
# ============
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(POWERLOADER_CMAKECONFIG_INSTALL_DIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for libpowerloaderConfig.cmake")
install(TARGETS ${libpowerloader_targets}
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY "${POWERLOADER_INCLUDE_DIR}/" # source directory
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" # target directory
FILES_MATCHING # install only matched files
PATTERN "*.hpp"
PATTERN "*.h"
)
# Makes the project importable from the build directory
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
# Configure 'mambaConfig.cmake' for a build tree
set(POWERLOADER_CONFIG_CODE "####### Expanded from \@POWERLOADER_CONFIG_CODE\@ #######\n")
set(POWERLOADER_CONFIG_CODE "${POWERLOADER_CONFIG_CODE}set(CMAKE_MODULE_PATH \"${CMAKE_CURRENT_SOURCE_DIR}/cmake;\${CMAKE_MODULE_PATH}\")\n")
set(POWERLOADER_CONFIG_CODE "${POWERLOADER_CONFIG_CODE}##################################################")
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${PROJECT_BINARY_DIR})
# Configure 'mambaConfig.cmake' for an install tree
set(POWERLOADER_CONFIG_CODE "")
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION ${POWERLOADER_CMAKECONFIG_INSTALL_DIR})
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${POWERLOADER_VERSION}
COMPATIBILITY AnyNewerVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${POWERLOADER_CMAKECONFIG_INSTALL_DIR})
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION ${POWERLOADER_CMAKECONFIG_INSTALL_DIR})