From f7f08f0e5efe3ff4e2ebff234a21e543e56fac36 Mon Sep 17 00:00:00 2001 From: Kai Pastor Date: Tue, 22 Nov 2022 06:02:25 +0100 Subject: [PATCH] Add VCPKG_FIND_PACKAGE_ --- docs/users/buildsystems/cmake-integration.md | 9 +++++++ scripts/buildsystems/vcpkg.cmake | 28 +++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/users/buildsystems/cmake-integration.md b/docs/users/buildsystems/cmake-integration.md index 9d04648e29bae0..236d5c1600bbab 100644 --- a/docs/users/buildsystems/cmake-integration.md +++ b/docs/users/buildsystems/cmake-integration.md @@ -229,3 +229,12 @@ See the `--feature-flags=` command line option for more information. When this option is turned on, every call to `find_package` is printed. Nested calls (e.g. via `find_dependency`) are indented according to nesting depth. + +### `VCPKG_FIND_PACKAGE_` + +When this option is turned set, non-nested calls to `find_package` are either +required (`VCPKG_FIND_PACKAGE_=ON`) or disabled (`VCPKG_FIND_PACKAGE_=OFF`). + +This variable is a tool to control direct dependencies and related features in vcpkg ports +which use the CMake build system. It can be used with `vcpkg_check_features` and avoids +unintented effects on transitive dependencies. diff --git a/scripts/buildsystems/vcpkg.cmake b/scripts/buildsystems/vcpkg.cmake index 4ee6740a2a9e6d..da64da6dd8a8be 100644 --- a/scripts/buildsystems/vcpkg.cmake +++ b/scripts/buildsystems/vcpkg.cmake @@ -768,6 +768,29 @@ macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name) set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN "${ARGN}") set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars "") + if(z_vcpkg_find_package_backup_id EQUAL "1") + if(VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}) + # Avoid CMake warning when both REQUIRED and CMAKE_REQUIRE_FIND_PACKAGE_ are used + if(NOT "REQUIRED" IN_LIST z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN) + list(APPEND z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars "CMAKE_REQUIRE_FIND_PACKAGE_${z_vcpkg_find_package_package_name}") + set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_CMAKE_REQUIRE_FIND_PACKAGE_${z_vcpkg_find_package_package_name} "${CMAKE_REQUIRE_FIND_PACKAGE_${z_vcpkg_find_package_package_name}}") + set(CMAKE_REQUIRE_FIND_PACKAGE_${z_vcpkg_find_package_package_name} 1) + endif() + if(VCPKG_TRACE_FIND_PACKAGE) + message(STATUS " (required by VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}=${VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}})") + endif() + elseif(DEFINED VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}) + list(APPEND z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars "CMAKE_DISABLE_FIND_PACKAGE_${z_vcpkg_find_package_package_name}") + set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_CMAKE_DISABLE_FIND_PACKAGE_${z_vcpkg_find_package_package_name} "${CMAKE_DISABLE_FIND_PACKAGE_${z_vcpkg_find_package_package_name}}") + set(CMAKE_DISABLE_FIND_PACKAGE_${z_vcpkg_find_package_package_name} 1) + if(VCPKG_TRACE_FIND_PACKAGE) + message(STATUS " (disabled by VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}=${VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name}})") + endif() + elseif(VCPKG_TRACE_FIND_PACKAGE) + message(STATUS " (could be controlled by VCPKG_FIND_PACKAGE_${z_vcpkg_find_package_package_name})") + endif() + endif() + # Workaround to set the ROOT_PATH until upstream CMake stops overriding # the ROOT_PATH at apple OS initialization phase. # See https://gitlab.kitware.com/cmake/cmake/merge_requests/3273 @@ -783,7 +806,10 @@ macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name) string(TOLOWER "${z_vcpkg_find_package_package_name}" z_vcpkg_find_package_lowercase_package_name) set(z_vcpkg_find_package_vcpkg_cmake_wrapper_path "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/${z_vcpkg_find_package_lowercase_package_name}/vcpkg-cmake-wrapper.cmake") - if(EXISTS "${z_vcpkg_find_package_vcpkg_cmake_wrapper_path}") + if(CMAKE_DISABLE_FIND_PACKAGE_${z_vcpkg_find_package_package_name}) + # Skip wrappers, fail if REQUIRED. + _find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN}) + elseif(EXISTS "${z_vcpkg_find_package_vcpkg_cmake_wrapper_path}") if(VCPKG_TRACE_FIND_PACKAGE) string(REPEAT " " "${z_vcpkg_find_package_backup_id}" z_vcpkg_find_package_indent) message(STATUS "${z_vcpkg_find_package_indent}using share/${z_vcpkg_find_package_lowercase_package_name}/vcpkg-cmake-wrapper.cmake")