diff --git a/ports/zlib/vcpkg-cmake-wrapper.cmake b/ports/zlib/vcpkg-cmake-wrapper.cmake index 8624b70867e9c8..87fd81b1d4f463 100644 --- a/ports/zlib/vcpkg-cmake-wrapper.cmake +++ b/ports/zlib/vcpkg-cmake-wrapper.cmake @@ -9,4 +9,5 @@ if(CMAKE_VERSION VERSION_LESS 3.4) select_library_configurations(ZLIB) unset(ZLIB_FOUND) endif() -_find_package(${ARGS}) + +z_vcpkg_underlying_find_package(${ARGS}) diff --git a/ports/zlib/vcpkg.json b/ports/zlib/vcpkg.json index 078669f294b6f2..8dccdf32db8954 100644 --- a/ports/zlib/vcpkg.json +++ b/ports/zlib/vcpkg.json @@ -1,7 +1,7 @@ { "name": "zlib", "version": "1.2.12", - "port-version": 1, + "port-version": 2, "description": "A compression library", "homepage": "https://www.zlib.net/", "license": "Zlib", diff --git a/scripts/buildsystems/vcpkg.cmake b/scripts/buildsystems/vcpkg.cmake index 31cee14eb703bd..f034c8d52851d1 100644 --- a/scripts/buildsystems/vcpkg.cmake +++ b/scripts/buildsystems/vcpkg.cmake @@ -32,6 +32,33 @@ function(z_vcpkg_add_fatal_error ERROR) endif() endfunction() +#[===[.md: +# z_vcpkg_get_command_underscores +Get the number of underscores/overrides the given `command` has. +If a `function|command` is overwritten it will be available as `_command`. +To chain these overrides it is required for the next override to override +`_command` instead `command` and so on for further overrides. This function +finds the last of the defined commands and returns the number of `_` needed +to savely chain commands + +```cmake +z_vcpkg_get_command_underscores( ) +``` + +#]===] +function(z_vcpkg_get_command_underscores command underscores_out) + set(underscores "") + while(COMMAND "_${underscores}${command}") + set(underscores "_${underscores}") + endwhile() + set("${underscores_out}" "${underscores}" PARENT_SCOPE) +endfunction() +set(Z_VCPKG_CHAIN_COMMANDS OFF) +if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.18") + set(Z_VCPKG_CHAIN_COMMANDS ON) +endif() + + set(Z_VCPKG_CMAKE_REQUIRED_MINIMUM_VERSION "3.7.2") if(CMAKE_VERSION VERSION_LESS Z_VCPKG_CMAKE_REQUIRED_MINIMUM_VERSION) message(FATAL_ERROR "vcpkg.cmake requires at least CMake ${Z_VCPKG_CMAKE_REQUIRED_MINIMUM_VERSION}.") @@ -567,10 +594,19 @@ endif() cmake_policy(POP) # Any policies applied to the below macros and functions appear to leak into consumers +set(Z_VCPKG_ADD_EXECUTABLE_US "") +if(Z_VCPKG_CHAIN_COMMANDS) + z_vcpkg_get_command_underscores("add_executable" Z_VCPKG_ADD_EXECUTABLE_US) +endif() +set(Z_VCPKG_ADD_EXECUTABLE "${Z_VCPKG_ADD_EXECUTABLE_US}add_executable" CACHE INTERNAL "") -function(add_executable) +function(${Z_VCPKG_ADD_EXECUTABLE}) z_vcpkg_function_arguments(ARGS) - _add_executable(${ARGS}) + if(Z_VCPKG_CHAIN_COMMANDS) + cmake_language(CALL _${Z_VCPKG_ADD_EXECUTABLE} ${ARGS}) + else() + _add_executable(${ARGS}) + endif() set(target_name "${ARGV0}") list(FIND ARGV "IMPORTED" IMPORTED_IDX) @@ -608,9 +644,19 @@ function(add_executable) endif() endfunction() -function(add_library) +set(Z_VCPKG_ADD_LIBRARY_US "") +if(Z_VCPKG_CHAIN_COMMANDS) + z_vcpkg_get_command_underscores("add_library" Z_VCPKG_ADD_LIBRARY_US) +endif() +set(Z_VCPKG_ADD_LIBRARY "${Z_VCPKG_ADD_LIBRARY_US}add_library" CACHE INTERNAL "") + +function(${Z_VCPKG_ADD_LIBRARY}) z_vcpkg_function_arguments(ARGS) - _add_library(${ARGS}) + if(Z_VCPKG_CHAIN_COMMANDS) + cmake_language(CALL _${Z_VCPKG_ADD_LIBRARY} ${ARGS}) + else() + _add_library(${ARGS}) + endif() set(target_name "${ARGV0}") list(FIND ARGS "IMPORTED" IMPORTED_IDX) @@ -689,9 +735,18 @@ function(x_vcpkg_install_local_dependencies) endfunction() if(X_VCPKG_APPLOCAL_DEPS_INSTALL) - function(install) + set(Z_VCPKG_INSTALL_US "") + if(Z_VCPKG_CHAIN_COMMANDS) + z_vcpkg_get_command_underscores("install" Z_VCPKG_INSTALL_US) + endif() + set(Z_VCPKG_INSTALL "${Z_VCPKG_INSTALL_US}install" CACHE INTERNAL "") + function(${Z_VCPKG_INSTALL}) z_vcpkg_function_arguments(ARGS) - _install(${ARGS}) + if(Z_VCPKG_CHAIN_COMMANDS) + cmake_language(CALL _${Z_VCPKG_INSTALL} ${ARGS}) + else() + _install(${ARGS}) + endif() if(ARGV0 STREQUAL "TARGETS") # Will contain the list of targets @@ -739,10 +794,26 @@ endif() if(NOT DEFINED VCPKG_OVERRIDE_FIND_PACKAGE_NAME) set(VCPKG_OVERRIDE_FIND_PACKAGE_NAME find_package) endif() +set(Z_VCPKG_FIND_PACKAGE_US "") +if(Z_VCPKG_CHAIN_COMMANDS) + z_vcpkg_get_command_underscores("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" Z_VCPKG_FIND_PACKAGE_US) +endif() +set(Z_VCPKG_FIND_PACKAGE "${Z_VCPKG_FIND_PACKAGE_US}${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" CACHE INTERNAL "") + +# Helper to be used in vcpkg-cmake-wrapper.cmake instead of _find_package +macro(z_vcpkg_underlying_find_package z_vcpkg_underlying_find_package_name) + set(z_vcpkg_underlying_find_package_name_ARGN "${ARGN}") + if(Z_VCPKG_CHAIN_COMMANDS) + cmake_language(CALL _${Z_VCPKG_FIND_PACKAGE} "${z_vcpkg_underlying_find_package_name}" ${z_vcpkg_underlying_find_package_name_ARGN}) + else() + _find_package("${z_vcpkg_underlying_find_package_name}" ${z_vcpkg_underlying_find_package_name_ARGN}) + endif() +endmacro() + # 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 -macro("${VCPKG_OVERRIDE_FIND_PACKAGE_NAME}" z_vcpkg_find_package_package_name) +macro("${Z_VCPKG_FIND_PACKAGE}" z_vcpkg_find_package_package_name) 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) @@ -789,16 +860,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}) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_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) 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) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN} COMPONENTS data) else() - _find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN}) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_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}) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_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 ) @@ -810,7 +881,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}) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN}) if(CURL_FOUND) if(EXISTS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/nghttp2.lib") list(APPEND CURL_LIBRARIES @@ -819,9 +890,9 @@ 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}) + z_vcpkg_underlying_find_package(gRPC ${z_vcpkg_find_package_ARGN}) else() - _find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN}) + z_vcpkg_underlying_find_package("${z_vcpkg_find_package_package_name}" ${z_vcpkg_find_package_ARGN}) endif() foreach(z_vcpkg_find_package_backup_var IN LISTS z_vcpkg_find_package_backup_vars) diff --git a/scripts/test_ports/inf-recursion-test/portfile.cmake b/scripts/test_ports/inf-recursion-test/portfile.cmake new file mode 100644 index 00000000000000..85f23307f70cb9 --- /dev/null +++ b/scripts/test_ports/inf-recursion-test/portfile.cmake @@ -0,0 +1,3 @@ +set(VCPKG_POLICY_EMPTY_PACKAGE enabled) + +vcpkg_cmake_configure(SOURCE_PATH "${CURRENT_PORT_DIR}/project") diff --git a/scripts/test_ports/inf-recursion-test/project/CMakeLists.txt b/scripts/test_ports/inf-recursion-test/project/CMakeLists.txt new file mode 100644 index 00000000000000..cc506a0ff6d4ce --- /dev/null +++ b/scripts/test_ports/inf-recursion-test/project/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.18) + +# vcpkg can only guard against overrides before the first project() call! +macro(find_package) + _find_package(${ARGV}) +endmacro() + +function(add_library) + _add_library(${ARGV}) +endfunction() + +function(add_executable) + _add_executable(${ARGV}) +endfunction() + +project(inf-recursion-test) + +find_package(ZLIB REQUIRED) +add_library(mylib lib.cpp) +add_executable(myexe main.cpp) \ No newline at end of file diff --git a/scripts/test_ports/inf-recursion-test/project/lib.cpp b/scripts/test_ports/inf-recursion-test/project/lib.cpp new file mode 100644 index 00000000000000..612bb08df6149e --- /dev/null +++ b/scripts/test_ports/inf-recursion-test/project/lib.cpp @@ -0,0 +1 @@ +struct MyClass {}; diff --git a/scripts/test_ports/inf-recursion-test/project/main.cpp b/scripts/test_ports/inf-recursion-test/project/main.cpp new file mode 100644 index 00000000000000..790037a88d2efb --- /dev/null +++ b/scripts/test_ports/inf-recursion-test/project/main.cpp @@ -0,0 +1,5 @@ +#include +int main() { + std::puts("Hello World!"); + return 0; +} \ No newline at end of file diff --git a/scripts/test_ports/inf-recursion-test/vcpkg.json b/scripts/test_ports/inf-recursion-test/vcpkg.json new file mode 100644 index 00000000000000..06f5021bb67b68 --- /dev/null +++ b/scripts/test_ports/inf-recursion-test/vcpkg.json @@ -0,0 +1,13 @@ +{ + "name": "inf-recursion-test", + "version-date": "2022-02-20", + "description": "Test port to verify if vcpkg.cmake is robust against command recursions", + "license" : "MIT", + "dependencies" : [ + { + "name": "vcpkg-cmake", + "host": true + }, + "zlib" + ] +} diff --git a/versions/baseline.json b/versions/baseline.json index 83f70f3bdf48d6..d198013383e075 100644 --- a/versions/baseline.json +++ b/versions/baseline.json @@ -7810,7 +7810,7 @@ }, "zlib": { "baseline": "1.2.12", - "port-version": 1 + "port-version": 2 }, "zlib-ng": { "baseline": "2.0.6", diff --git a/versions/z-/zlib.json b/versions/z-/zlib.json index 523f7374adddea..45cd6b8bbc4484 100644 --- a/versions/z-/zlib.json +++ b/versions/z-/zlib.json @@ -1,5 +1,10 @@ { "versions": [ + { + "git-tree": "7630389396c4879c5ba48fde6b4844d6d2725279", + "version": "1.2.12", + "port-version": 2 + }, { "git-tree": "ecc4c064d4911faf12d8bf5fd6bcd5c556d89774", "version": "1.2.12",