Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vcpkg.cmake] fix find_package not restoring variables correctly. #27689

Merged
merged 8 commits into from
Nov 16, 2022
59 changes: 29 additions & 30 deletions scripts/buildsystems/vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -752,39 +752,34 @@ endif()
# NOTE: this is not a function, which means that arguments _are not_ perfectly forwarded
# this is fine for `find_package`, since there are no usecases for `;` in arguments,
# so perfect forwarding is not important
set(z_vcpkg_find_package_backup_id "0")
macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name)
math(EXPR z_vcpkg_find_package_backup_id "${z_vcpkg_find_package_backup_id} + 1")
set(z_vcpkg_find_package_package_name "${z_vcpkg_find_package_package_name}")
set(z_vcpkg_find_package_ARGN "${ARGN}")
set(z_vcpkg_find_package_backup_vars)
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 "")

# 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
# Fixed in CMake 3.15
if(CMAKE_SYSTEM_NAME STREQUAL iOS)
list(APPEND z_vcpkg_find_package_backup_vars "CMAKE_FIND_ROOT_PATH")
if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(APPEND z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars "CMAKE_FIND_ROOT_PATH")
if(DEFINED CMAKE_FIND_ROOT_PATH)
set(z_vcpkg_find_package_backup_CMAKE_FIND_ROOT_PATH "${CMAKE_FIND_ROOT_PATH}")
else()
set(z_vcpkg_find_package_backup_CMAKE_FIND_ROOT_PATH)
set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_CMAKE_FIND_ROOT_PATH "${CMAKE_FIND_ROOT_PATH}")
endif()

list(APPEND CMAKE_FIND_ROOT_PATH "${VCPKG_CMAKE_FIND_ROOT_PATH}")
endif()
string(TOLOWER "${z_vcpkg_find_package_package_name}" z_vcpkg_find_package_lowercase_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}")
list(APPEND z_vcpkg_find_package_backup_vars "ARGS")
list(APPEND z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars "ARGS")
if(DEFINED ARGS)
set(z_vcpkg_find_package_backup_ARGS "${ARGS}")
else()
set(z_vcpkg_find_package_backup_ARGS)
set(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_ARGS "${ARGS}")
endif()

set(ARGS "${z_vcpkg_find_package_package_name};${z_vcpkg_find_package_ARGN}")
set(ARGS "${z_vcpkg_find_package_package_name};${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN}")
include("${z_vcpkg_find_package_vcpkg_cmake_wrapper_path}")
elseif(z_vcpkg_find_package_package_name STREQUAL "Boost" AND EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/boost")
# Checking for the boost headers disables this wrapper unless the user has installed at least one boost library
Expand All @@ -799,16 +794,16 @@ macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name)
else()
set(Boost_COMPILER "-vc140")
endif()
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN})
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
elseif(z_vcpkg_find_package_package_name STREQUAL "ICU" AND EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/unicode/utf.h")
list(FIND z_vcpkg_find_package_ARGN "COMPONENTS" z_vcpkg_find_package_COMPONENTS_IDX)
list(FIND z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN "COMPONENTS" z_vcpkg_find_package_COMPONENTS_IDX)
if(NOT z_vcpkg_find_package_COMPONENTS_IDX EQUAL -1)
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN} COMPONENTS data)
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN} COMPONENTS data)
else()
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN})
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
endif()
elseif(z_vcpkg_find_package_package_name STREQUAL "GSL" AND EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/gsl")
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN})
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
if(GSL_FOUND AND TARGET GSL::gsl)
set_property( TARGET GSL::gslcblas APPEND PROPERTY IMPORTED_CONFIGURATIONS Release )
set_property( TARGET GSL::gsl APPEND PROPERTY IMPORTED_CONFIGURATIONS Release )
Expand All @@ -820,7 +815,7 @@ macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name)
endif()
endif()
elseif("${z_vcpkg_find_package_package_name}" STREQUAL "CURL" AND EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/curl")
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN})
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
if(CURL_FOUND)
if(EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/nghttp2.lib")
list(APPEND CURL_LIBRARIES
Expand All @@ -829,18 +824,22 @@ macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name)
endif()
endif()
elseif("${z_vcpkg_find_package_lowercase_package_name}" STREQUAL "grpc" AND EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/grpc")
_find_package(gRPC ${z_vcpkg_find_package_ARGN})
_find_package(gRPC ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
else()
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN})
_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_ARGN})
endif()

foreach(z_vcpkg_find_package_backup_var IN LISTS z_vcpkg_find_package_backup_vars)
if(DEFINED z_vcpkg_find_package_backup_${z_vcpkg_find_package_backup_var})
set("${z_vcpkg_find_package_backup_var}" "${z_vcpkg_find_package_backup_${z_vcpkg_find_package_backup_var}}")
else()
set("${z_vcpkg_find_package_backup_var}")
# Do not use z_vcpkg_find_package_package_name beyond this point since it might have changed!
# Only variables using z_vcpkg_find_package_backup_id can used correctly below!
foreach(z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_var IN LISTS z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_vars)
if(DEFINED z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_var})
set("${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_var}" "${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_var}}")
endif()
unset("${z_vcpkg_find_package_${z_vcpkg_find_package_backup_id}_backup_var}")
endforeach()
math(EXPR z_vcpkg_find_package_backup_id "${z_vcpkg_find_package_backup_id} - 1")
if(z_vcpkg_find_package_backup_id LESS 0)
message(FATAL_ERROR "[vcpkg]: find_package ended with z_vcpkg_find_package_backup_id being less than 0! This is a logical error and should never happen. Please provide a cmake trace log via cmake cmd line option '--trace-expand'!")
endif()
endmacro()

cmake_policy(PUSH)
Expand Down