Skip to content

Commit d68cfe8

Browse files
committed
General support for prefer-system dependencies
1 parent 2d96271 commit d68cfe8

File tree

4 files changed

+60
-35
lines changed

4 files changed

+60
-35
lines changed

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ else()
3838
set(WSDDN_PREFER_SYSTEM_DEFAULT OFF)
3939
endif()
4040

41-
option(WSDDN_PREFER_SYSTEM "whether to prefer system versions of dependencies (such as libxm2)" ${WSDDN_PREFER_SYSTEM_DEFAULT})
41+
option(WSDDN_PREFER_SYSTEM "whether to prefer system versions of dependencies" ${WSDDN_PREFER_SYSTEM_DEFAULT})
4242

4343
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
4444
set(WSDDN_WITH_SYSTEMD "auto" CACHE STRING "enable systemd scripts and notification support")
@@ -82,8 +82,12 @@ endif()
8282
if (TARGET modern-uuid-static)
8383
set_target_properties(modern-uuid-static PROPERTIES FOLDER "Dependencies")
8484
endif()
85-
set_target_properties(spdlog PROPERTIES FOLDER "Dependencies")
86-
set_target_properties(fmt PROPERTIES FOLDER "Dependencies")
85+
if (TARGET spdlog)
86+
set_target_properties(spdlog PROPERTIES FOLDER "Dependencies")
87+
endif()
88+
if (TARGET fmt)
89+
set_target_properties(fmt PROPERTIES FOLDER "Dependencies")
90+
endif()
8791

8892

8993
set_target_properties(wsddn PROPERTIES

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ The following flags can be passed to CMake configure step:
713713

714714
`-DWSDDN_PREFER_SYSTEM=ON|OFF`
715715

716-
This controls whether to prefer system version of 3rd party libraries or fetch, build and use static ones. Currently only `libxml2` is affected.
716+
This controls whether to prefer system version of 3rd party libraries or fetch and use them from sources.
717717

718718
On Linux:
719719

cmake/dependencies.cmake

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
include(FetchContent)
55

6+
if (DEFINED CACHE{libxml2_SOURCE_DIR} AND NOT DEFINED CACHE{WSDDN_DEPENDENCIES_VERSION})
7+
message(FATAL_ERROR
8+
"Your existing CMake cache cannot be used due to incompatible changes."
9+
"Please delete ${CMAKE_BINARY_DIR}/CMakeCache.txt and rebuild. (sorry!)")
10+
endif()
11+
set(WSDDN_DEPENDENCIES_VERSION 1 CACHE INTERNAL "version of dependencies config")
12+
613
file(READ dependencies.json DEPENDECIES_JSON)
714
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS dependencies.json)
815

@@ -19,10 +26,16 @@ function(fetch_dependency name #extras for FetchContent_Declare
1926
foreach(i RANGE 1 ${ARGC})
2027
list(APPEND extras ${ARGV${i}})
2128
endforeach()
29+
if (WSDDN_PREFER_SYSTEM)
30+
set(prefer_system FIND_PACKAGE_ARGS)
31+
else()
32+
set(prefer_system "")
33+
endif()
2234
FetchContent_Declare(${name}
2335
URL ${url}
2436
URL_HASH MD5=${md5}
2537
${extras}
38+
${prefer_system}
2639
)
2740
set(deplist ${DECLARED_DEPENDENCIES})
2841
list(APPEND deplist ${name})
@@ -37,30 +50,20 @@ fetch_dependency(isptr)
3750
fetch_dependency(ptl)
3851
fetch_dependency(modern-uuid)
3952

40-
if (WSDDN_PREFER_SYSTEM)
41-
find_package(LibXml2)
42-
endif()
43-
44-
if (NOT LibXml2_FOUND)
45-
46-
message(STATUS "libxm2 will be built from sources and statically linked")
47-
48-
set(LIBXML2_WITH_ICONV OFF)
49-
set(LIBXML2_WITH_LZMA OFF)
50-
set(LIBXML2_WITH_HTML OFF)
51-
set(LIBXML2_WITH_HTTP OFF)
52-
set(LIBXML2_WITH_FTP OFF)
53-
set(LIBXML2_WITH_TESTS OFF)
54-
set(LIBXML2_WITH_ZLIB OFF)
55-
set(LIBXML2_WITH_PYTHON OFF)
56-
set(LIBXML2_WITH_LEGACY OFF)
57-
set(LIBXML2_WITH_MODULES OFF)
58-
set(LIBXML2_WITH_PROGRAMS OFF)
5953

60-
fetch_dependency(libxml2)
61-
62-
endif()
54+
set(LIBXML2_WITH_ICONV OFF)
55+
set(LIBXML2_WITH_LZMA OFF)
56+
set(LIBXML2_WITH_HTML OFF)
57+
set(LIBXML2_WITH_HTTP OFF)
58+
set(LIBXML2_WITH_FTP OFF)
59+
set(LIBXML2_WITH_TESTS OFF)
60+
set(LIBXML2_WITH_ZLIB OFF)
61+
set(LIBXML2_WITH_PYTHON OFF)
62+
set(LIBXML2_WITH_LEGACY OFF)
63+
set(LIBXML2_WITH_MODULES OFF)
64+
set(LIBXML2_WITH_PROGRAMS OFF)
6365

66+
fetch_dependency(LibXml2)
6467

6568
set(FMT_INSTALL OFF)
6669
fetch_dependency(fmt)
@@ -80,19 +83,37 @@ fetch_dependency(asio)
8083

8184
FetchContent_MakeAvailable(${DECLARED_DEPENDENCIES})
8285

86+
foreach(dep ${DECLARED_DEPENDENCIES})
87+
string(TOUPPER ${dep} udep)
88+
string(TOLOWER ${dep} ldep)
89+
if (DEFINED ${ldep}_SOURCE_DIR)
90+
message(STATUS "${dep} will be built from sources and statically linked")
91+
else()
92+
if (DEFINED ${ldep}_VERSION)
93+
message(STATUS "${dep} will be used from system (current version: ${${ldep}_VERSION})")
94+
else()
95+
if (DEFINED ${udep}_VERSION_STRING)
96+
message(STATUS "${dep} will be used from system (current version: ${${udep}_VERSION_STRING})")
97+
else()
98+
message(STATUS "${dep} will be used from system")
99+
endif()
100+
endif()
101+
endif()
102+
endforeach()
103+
83104
get_directory_property(KNOWN_SUBDIRECTORIES SUBDIRECTORIES)
84105
foreach(dir ${KNOWN_SUBDIRECTORIES})
85106
if (IS_DIRECTORY ${dir})
86107
foreach(dep ${DECLARED_DEPENDENCIES})
87-
#check if the subdirectory is "under" the dependency source dir
88-
string(FIND ${dir} ${${dep}_SOURCE_DIR} match_pos)
89-
if (match_pos EQUAL 0)
90-
#and, if so, exclude it from all to prevent installation
91-
set_property(DIRECTORY ${dir} PROPERTY EXCLUDE_FROM_ALL YES)
92-
break()
108+
if (DEFINED ${dep}_SOURCE_DIR)
109+
#check if the subdirectory is "under" the dependency source dir
110+
string(FIND ${dir} ${${dep}_SOURCE_DIR} match_pos)
111+
if (match_pos EQUAL 0)
112+
#and, if so, exclude it from all to prevent installation
113+
set_property(DIRECTORY ${dir} PROPERTY EXCLUDE_FROM_ALL YES)
114+
break()
115+
endif()
93116
endif()
94117
endforeach()
95118
endif()
96119
endforeach()
97-
98-

dependencies.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"sha256": "f9095609a2226f3aa6dbfcd4726a8521a56f4fd2f426b0898d92acd1f133aa6d",
2828
"md5": "574eb3ad0a8e5685cdc9e7712804900f"
2929
},
30-
"libxml2": {
30+
"LibXml2": {
3131
"version": "v2.14.5",
3232
"url": "https://gitlab.gnome.org/GNOME/libxml2/-/archive/${version}/libxml2-${version}.tar.gz",
3333
"homepage": "https://gitlab.gnome.org/GNOME/libxml2",

0 commit comments

Comments
 (0)