1- # SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
1+ # SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
22#
33# SPDX-License-Identifier: LGPL-3.0-or-later
44
@@ -36,6 +36,8 @@ set(ENABLE_TESTING
3636 ON
3737 CACHE BOOL "enable testing" )
3838
39+ option (ENABLE_CPM "enable CPM module" ON )
40+
3941set (LINGLONG_USERNAME
4042 "deepin-linglong"
4143 CACHE STRING "The username for linglong package manager" )
@@ -90,120 +92,181 @@ endif()
9092list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR } /cmake)
9193include (PFL )
9294
93- set (ENABLE_CPM YES )
94-
9595if (CMAKE_VERSION VERSION_LESS "3.14" )
9696 set (ENABLE_CPM NO )
9797 message (
9898 STATUS "cmake version ${CMAKE_VERSION } not compatible with CPM.cmake." )
9999endif ()
100100
101+ # we don't support CPM_LOCAL_PACKAGES_ONLY
101102if (CPM_LOCAL_PACKAGES_ONLY)
102103 set (ENABLE_CPM NO )
103104endif ()
104105
105106if (ENABLE_CPM)
106107 include (CPM )
107- CPMFindPackage (
108- NAME tl-expected
109- VERSION
110- 1.2.0 # NOTE: Upstream cmake version doesn't match git tag, we need
111- # https://github.com/TartanLlama/expected/pull/62 in v1.1.0, but
112- # cmake version of v1.1.0 is 1.0.0. upstream fix this issue in
113- # https://github.com/TartanLlama/expected/commit/17d0058a5a5d3e0ea647884f9b29c518dd06f26e
114- # so we require tl-expected >= 1.2.0
115- GITHUB_REPOSITORY TartanLlama/expected
116- GIT_TAG v1.3.1
117- GIT_SHALLOW ON
118- OPTIONS "EXPECTED_BUILD_TESTS OFF"
119- EXCLUDE_FROM_ALL ON )
120- CPMFindPackage (
121- NAME yaml-cpp
122- VERSION 0.6.2
123- GITHUB_REPOSITORY jbeder/yaml-cpp
124- GIT_TAG 0.8.0 # NOTE: When use this project with CPM.cmake(FetchContent), we
125- # need https://github.com/jbeder/yaml-cpp/pull/583 in v0.8.0
126- EXCLUDE_FROM_ALL ON
127- OPTIONS "YAML_CPP_INSTALL ON" "YAML_CPP_BUILD_TESTS OFF" )
128- CPMFindPackage (
129- NAME nlohmann_json
130- VERSION 3.5.0
131- URL "https://github.com/nlohmann/json/archive/refs/tags/v3.5.0.tar.gz"
132- EXCLUDE_FROM_ALL ON
133- OPTIONS "JSON_BuildTests OFF" )
134- CPMFindPackage (
135- NAME CLI11
136- VERSION 2.4.1
137- GITHUB_REPOSITORY CLIUtils/CLI11
138- GIT_TAG v2.6.1
139- EXCLUDE_FROM_ALL ON
140- OPTIONS "CLI11_BUILD_TESTS OFF" )
141-
142- if (ENABLE_TESTING)
143- include (CTest )
144- CPMFindPackage (
145- NAME googletest
146- GITHUB_REPOSITORY google/googletest
147- GIT_TAG v1.14.0
148- VERSION 1.12.1
149- OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
150- FIND_PACKAGE_ARGUMENTS "NAMES GTest"
151- GIT_SHALLOW ON
152- EXCLUDE_FROM_ALL ON )
153- endif ()
154- endif ()
155-
156- set (linglong_EXTERNALS "ytj ytj::ytj" )
157-
158- find_package (fmt 5.2.1 QUIET )
159- if (NOT fmt_FOUND)
160- message (STATUS "system fmt not found, using external fmt" )
161- add_library (fmt-lib INTERFACE )
162- add_library (fmt::fmt ALIAS fmt-lib )
163- target_include_directories (fmt-lib INTERFACE external/fmt/include )
164- target_compile_definitions (fmt-lib INTERFACE FMT_HEADER_ONLY )
165108endif ()
166109
167- # NOTE: UOS v20 do not have tl-expected packaged.
168- find_package (tl-expected 1.2.0 QUIET )
169- if (NOT tl-expected_FOUND)
170- message (STATUS "tl-expected not found, using external tl-expected" )
171- # list(APPEND linglong_EXTERNALS tl-expected) # tl-expected requires 3.14
172- add_library (tl-expected INTERFACE )
173- add_library (tl::expected ALIAS tl-expected )
174- target_include_directories (tl-expected
175- INTERFACE ./external/tl-expected/include )
176- list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR } /cmake.external)
110+ if (BUILD_LINGLONG_BUILDER_UTILS_IN_BOX)
111+ set (CMAKE_FIND_LIBRARY_SUFFIXES ".a" )
177112endif ()
178113
179- find_package (nlohmann_json 3.5.0 QUIET )
180- if (NOT nlohmann_json_FOUND)
181- list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR } /cmake.fix)
182- find_package (nlohmann_json 3.5.0 REQUIRED )
183- endif ()
114+ # Use CPMFindPackage if ENABLE_CPM is enabled.
115+ # Otherwise, use find_package, find_library or
116+ # include the header-only version from external directory
117+ function (gen_library_target name )
118+ set (is_static ${BUILD_LINGLONG_BUILDER_UTILS_IN_BOX} )
119+
120+ if ("${name} " STREQUAL "tl-expected" )
121+ set (output "tl::expected" )
122+ if (ENABLE_CPM)
123+ CPMFindPackage (
124+ NAME tl-expected
125+ VERSION
126+ 1.2.0 # NOTE: Upstream cmake version doesn't match git tag, we need
127+ # https://github.com/TartanLlama/expected/pull/62 in v1.1.0, but
128+ # cmake version of v1.1.0 is 1.0.0. upstream fix this issue in
129+ # https://github.com/TartanLlama/expected/commit/17d0058a5a5d3e0ea647884f9b29c518dd06f26e
130+ # so we require tl-expected >= 1.2.0
131+ GITHUB_REPOSITORY TartanLlama/expected
132+ GIT_TAG v1.3.1
133+ GIT_SHALLOW ON
134+ OPTIONS "EXPECTED_BUILD_TESTS OFF"
135+ EXCLUDE_FROM_ALL ON )
136+ else ()
137+ find_package (tl-expected 1.2.0 QUIET )
138+ if (NOT tl-expected_FOUND)
139+ message (STATUS "tl-expected not found, using external tl-expected" )
140+ add_library (tl-expected INTERFACE )
141+ add_library (${output} ALIAS tl-expected )
142+ target_include_directories (tl-expected
143+ INTERFACE ./external/tl-expected/include )
144+ # somebody calls find_package with tl-expected later should works
145+ list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR } /cmake.external)
146+ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH } PARENT_SCOPE )
147+ endif ()
148+ endif ()
149+ elseif ("${name} " STREQUAL "nlohmann_json" )
150+ set (output "nlohmann_json::nlohmann_json" )
151+ if (ENABLE_CPM)
152+ CPMFindPackage (
153+ NAME nlohmann_json
154+ VERSION 3.5.0
155+ URL "https://github.com/nlohmann/json/archive/refs/tags/v3.5.0.tar.gz"
156+ EXCLUDE_FROM_ALL ON
157+ OPTIONS "JSON_BuildTests OFF" )
158+ else ()
159+ find_package (nlohmann_json 3.5.0 QUIET )
160+ if (NOT nlohmann_json_FOUND)
161+ list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR } /cmake.fix)
162+ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH } PARENT_SCOPE )
163+ find_package (nlohmann_json 3.5.0 REQUIRED )
164+ endif ()
165+ endif ()
166+ elseif ("${name} " STREQUAL "yaml-cpp" )
167+ set (output "yaml-cpp::yaml-cpp" )
168+ if (ENABLE_CPM)
169+ CPMFindPackage (
170+ NAME yaml-cpp
171+ VERSION 0.6.2
172+ GITHUB_REPOSITORY jbeder/yaml-cpp
173+ GIT_TAG 0.8.0 # NOTE: When use this project with CPM.cmake(FetchContent), we
174+ # need https://github.com/jbeder/yaml-cpp/pull/583 in v0.8.0
175+ EXCLUDE_FROM_ALL ON
176+ OPTIONS "YAML_CPP_INSTALL ON" "YAML_CPP_BUILD_TESTS OFF" )
177+ else ()
178+ if (is_static)
179+ message (STATUS "use static yaml-cpp" )
180+ find_library (yaml-cpp yaml-cpp )
181+ add_library (yaml-cpp INTERFACE )
182+ target_link_libraries (yaml-cpp INTERFACE ${yaml-cpp} )
183+ else ()
184+ find_package (yaml-cpp REQUIRED )
185+ endif ()
186+ endif ()
187+ if (NOT TARGET ${output} )
188+ add_library (${output} ALIAS yaml-cpp )
189+ endif ()
190+ elseif ("${name} " STREQUAL "CLI11" )
191+ set (output "CLI11::CLI11" )
192+ if (ENABLE_CPM)
193+ CPMFindPackage (
194+ NAME CLI11
195+ VERSION 2.4.1
196+ GITHUB_REPOSITORY CLIUtils/CLI11
197+ GIT_TAG v2.6.1
198+ EXCLUDE_FROM_ALL ON
199+ OPTIONS "CLI11_BUILD_TESTS OFF" )
200+ else ()
201+ find_package (CLI11 2.4.1 QUIET )
202+ if (NOT CLI11_FOUND)
203+ message (STATUS "CLI11 not found, using external CLI11" )
204+ add_library (CLI11 INTERFACE )
205+ add_library (${output} ALIAS CLI11 )
206+ target_include_directories (CLI11 INTERFACE ./external/CLI11 )
207+ endif ()
208+ endif ()
209+ elseif ("${name} " STREQUAL "gtest" OR "${name} " STREQUAL "gmock" )
210+ if ("${name} " STREQUAL "gtest" )
211+ set (output "GTest::gtest" )
212+ elseif ("${name} " STREQUAL "gmock" )
213+ set (output "GTest::gmock" )
214+ endif ()
215+ if (ENABLE_CPM)
216+ CPMFindPackage (
217+ NAME googletest
218+ GITHUB_REPOSITORY google/googletest
219+ GIT_TAG v1.14.0
220+ VERSION 1.12.1
221+ OPTIONS "INSTALL_GTEST OFF" "gtest_force_shared_crt"
222+ FIND_PACKAGE_ARGUMENTS "NAMES GTest"
223+ GIT_SHALLOW ON
224+ EXCLUDE_FROM_ALL ON )
225+ else ()
226+ find_package (GTest REQUIRED )
227+ endif ()
228+ elseif ("${name} " STREQUAL "fmt" )
229+ set (output "fmt::fmt-lib" )
230+ find_package (fmt 5.2.1 QUIET )
231+ if (NOT fmt_FOUND)
232+ message (STATUS "system fmt not found, using external fmt" )
233+ add_library (fmt-lib INTERFACE )
234+ add_library (${output} ALIAS fmt-lib )
235+ target_include_directories (fmt-lib INTERFACE external/fmt/include )
236+ target_compile_definitions (fmt-lib INTERFACE FMT_HEADER_ONLY )
237+ else ()
238+ if (is_static)
239+ message (STATUS "use fmt header only" )
240+ add_library (${output} ALIAS fmt::fmt-header-only )
241+ else ()
242+ add_library (${output} ALIAS fmt::fmt )
243+ endif ()
244+ endif ()
245+ endif ()
184246
185- # NOTE: UOS v20 do not have cli11 packaged.
186- find_package (CLI11 2.4.1 QUIET )
187- if (NOT CLI11_FOUND)
188- message (STATUS "CLI11 not found, using external CLI11" )
189- add_library (CLI11 INTERFACE )
190- add_library (CLI11::CLI11 ALIAS CLI11 )
191- target_include_directories (CLI11 INTERFACE ./external/CLI11 )
192- endif ()
247+ if (NOT TARGET ${output} )
248+ message (FATAL_ERROR "library ${name} not found target ${output} " )
249+ else ()
250+ message (STATUS "library ${name} found: ${output} " )
251+ endif ()
252+ endfunction ()
193253
194254find_package (PkgConfig REQUIRED )
195255
256+ gen_library_target (tl-expected )
257+ gen_library_target (nlohmann_json )
258+ gen_library_target (yaml-cpp )
259+ gen_library_target (CLI11 )
260+ gen_library_target (fmt )
196261if (ENABLE_TESTING)
262+ include (CTest )
197263 pkg_search_module (CRYPTO REQUIRED IMPORTED_TARGET libcrypto )
198- find_package (GTest REQUIRED )
199-
200- # compatible with old gtest
201- if (NOT TARGET GTest::gmock)
202- message (STATUS "GTest::gmock not found, alias GTest::gtest" )
203- add_library (GTest::gmock ALIAS GTest::GTest ) # available since 3.5
204- endif ()
264+ gen_library_target (gtest )
265+ gen_library_target (gmock )
205266endif ()
206267
268+ set (linglong_EXTERNALS "ytj ytj::ytj" )
269+
207270if (LINGLONG_ENABLE_WAYLAND_SEC_CTX_SUPPORT)
208271 message (STATUS "enable linglong wayland security context support" )
209272 pkg_search_module (WAYLAND_CLIENT REQUIRED IMPORTED_TARGET wayland-client )
@@ -230,15 +293,19 @@ include(GNUInstallDirs)
230293configure_file (configure.h.in ${CMAKE_CURRENT_BINARY_DIR } /configure.h @ONLY )
231294include_directories (${CMAKE_CURRENT_BINARY_DIR } )
232295
296+ pkg_search_module (cap REQUIRED IMPORTED_TARGET libcap )
297+ pkg_search_module (systemd REQUIRED IMPORTED_TARGET libsystemd )
298+
233299# build ll-build-utils only
234300if (BUILD_LINGLONG_BUILDER_UTILS_IN_BOX)
235301 pfl_init (AUTO )
236302 pfl_add_libraries (
237303 LIBS
238304 api
239305 common
240- ocppi
241306 oci-cfg-generators
307+ ocppi
308+ utils
242309 APPS
243310 ll-builder-utils
244311 uab )
@@ -303,9 +370,7 @@ message(STATUS "compiling with Qt${QT_VERSION_MAJOR}")
303370
304371pkg_search_module (glib2 REQUIRED IMPORTED_TARGET glib-2.0 )
305372pkg_search_module (ostree1 REQUIRED IMPORTED_TARGET ostree-1 )
306- pkg_search_module (systemd REQUIRED IMPORTED_TARGET libsystemd )
307373pkg_search_module (ELF REQUIRED IMPORTED_TARGET libelf )
308- pkg_search_module (cap REQUIRED IMPORTED_TARGET libcap )
309374pkg_search_module (uuid REQUIRED IMPORTED_TARGET uuid )
310375
311376set (ytj_ENABLE_TESTING NO )
0 commit comments