@@ -3,20 +3,46 @@ include_guard(GLOBAL)
33cmake_policy (SET CMP0054 NEW )
44
55macro (_userver_module_begin )
6- set (options)
6+ set (options
7+ CPM_DOWNLOAD_ONLY
8+ )
79 set (oneValueArgs # Target name, also used for package name by default
810 NAME VERSION
911 )
1012 set (multiValueArgs
1113 DEBIAN_NAMES FORMULA_NAMES RPM_NAMES PACMAN_NAMES PKG_NAMES
1214 # For version detection of manually installed packages and unknown package managers.
13- PKG_CONFIG_NAMES
15+ PKG_CONFIG_NAMES
16+ # For CPM options
17+ CPM_NAME
18+ CPM_VERSION
19+ CPM_GITHUB_REPOSITORY
20+ CPM_URL
21+ CPM_OPTIONS
22+ CPM_SOURCE_SUBDIR
23+ CPM_GIT_TAG
1424 )
1525
1626 cmake_parse_arguments (ARG "${options} " "${oneValueArgs} " "${multiValueArgs} " "${ARGN} " )
1727
1828 set (name "${ARG_NAME} " )
1929
30+ string (TOUPPER "${ARG_CPM_NAME} " ARG_CPM_NAME)
31+ string (REPLACE "-" "_" ARG_CPM_NAME "${ARG_CPM_NAME} " )
32+
33+ if (ARG_CPM_NAME)
34+ option (
35+ USERVER_DOWNLOAD_PACKAGE_${ARG_CPM_NAME}
36+ "Download and setup ${ARG_CPM_NAME} if no library of matching version was found"
37+ ${USERVER_DOWNLOAD_PACKAGES}
38+ )
39+ option (
40+ USERVER_FORCE_DOWNLOAD_${ARG_CPM_NAME}
41+ "Download ${ARG_CPM_NAME} even if there is an installed system package"
42+ ${USERVER_FORCE_DOWNLOAD_PACKAGES}
43+ )
44+ endif ()
45+
2046 if (ARG_VERSION)
2147 if (NOT ${name} _FIND_VERSION OR "${${name} _FIND_VERSION}" VERSION_LESS "${ARG_VERSION} " )
2248 set ("${name} _FIND_VERSION" "${ARG_VERSION} " )
@@ -78,6 +104,12 @@ endmacro()
78104macro (_userver_module_find_part )
79105 # Also uses ARGs left over from _userver_find_module_begin
80106
107+ # TODO: return() doesn't work inside of macro
108+ # if(USERVER_FORCE_DOWNLOAD_${ARG_CPM_NAME})
109+ # message(STATUS "Skipping ${ARG_CPM_NAME} system package search due to USERVER_FORCE_DOWNLOAD_${ARG_CPM_NAME}=TRUE")
110+ # return()
111+ # endif()
112+
81113 set (options)
82114 set (oneValueArgs PART_TYPE)
83115 set (multiValueArgs NAMES PATHS PATH_SUFFIXES)
@@ -261,12 +293,25 @@ macro(_userver_module_end)
261293 list (APPEND required_vars "${programs_variable} " )
262294 endif ()
263295 if (required_vars)
264- find_package_handle_standard_args (
265- "${current_package_name} "
266- REQUIRED_VARS ${required_vars}
267- FAIL_MESSAGE "${FULL_ERROR_MESSAGE} "
268- )
269- mark_as_advanced (${required_vars} )
296+ foreach (_CURRENT_VAR ${required_vars} )
297+ if (NOT ${_CURRENT_VAR} )
298+ set (NEED_CPM TRUE )
299+ if (USERVER_DOWNLOAD_PACKAGE_${ARG_CPM_NAME} )
300+ set (${_CURRENT_VAR} )
301+ endif ()
302+ endif ()
303+ endforeach ()
304+
305+ if (NEED_CPM AND USERVER_DOWNLOAD_PACKAGE_${ARG_CPM_NAME} )
306+ _userver_cpm_addpackage ("${current_package_name} " )
307+ else ()
308+ find_package_handle_standard_args (
309+ "${current_package_name} "
310+ REQUIRED_VARS ${required_vars}
311+ FAIL_MESSAGE "${FULL_ERROR_MESSAGE} "
312+ )
313+ mark_as_advanced (${required_vars} )
314+ endif ()
270315 else ()
271316 # Forward to another CMake module, add nice error messages if missing.
272317 set (wrapped_package_name "${current_package_name} " )
@@ -323,6 +368,29 @@ macro(_userver_module_end)
323368 endif ()
324369endmacro ()
325370
371+ macro (_userver_cpm_addpackage name )
372+ include (DownloadUsingCPM )
373+
374+ set (EXTRA_ARGS)
375+ if (ARG_CPM_DOWNLOAD_ONLY)
376+ set (EXTRA_ARGS ${EXTRA_ARGS} DOWNLOAD_ONLY)
377+ endif ()
378+ cpmaddpackage (
379+ NAME ${name}
380+ VERSION ${ARG_CPM_VERSION}
381+ GITHUB_REPOSITORY ${ARG_CPM_GITHUB_REPOSITORY}
382+ URL ${ARG_CPM_URL}
383+ OPTIONS ${ARG_CPM_OPTIONS}
384+ SOURCE_SUBDIR ${ARG_CPM_SOURCE_SUBDIR}
385+ GIT_TAG ${ARG_CPM_GIT_TAG}
386+ ${EXTRA_ARGS}
387+ )
388+ if (NOT ARG_CPM_DOWNLOAD_ONLY)
389+ mark_targets_as_system ("${${name} _SOURCE_DIR}" )
390+ endif ()
391+ set (${name} _FOUND 1)
392+ endmacro ()
393+
326394function (_userver_macos_set_default_dir variable command_args )
327395 set (default_value "" )
328396 if (CMAKE_SYSTEM_NAME MATCHES "Darwin" AND NOT DEFINED ${variable} )
0 commit comments