From 2cef66c7a4a6bd2fdc44ce34cf6acdcdf8601a6e Mon Sep 17 00:00:00 2001 From: Kai Pastor Date: Sat, 31 Jan 2026 01:52:15 +0100 Subject: [PATCH] [vcpkg-ci-python3] Revise (#47654) --- .../vcpkg-ci-python3/portfile.cmake | 14 ++ .../vcpkg-ci-python3/project/CMakeLists.txt | 158 ++++++++++-------- .../test_ports/vcpkg-ci-python3/vcpkg.json | 10 +- 3 files changed, 114 insertions(+), 68 deletions(-) diff --git a/scripts/test_ports/vcpkg-ci-python3/portfile.cmake b/scripts/test_ports/vcpkg-ci-python3/portfile.cmake index 8c5cd4f01d..e090465322 100644 --- a/scripts/test_ports/vcpkg-ci-python3/portfile.cmake +++ b/scripts/test_ports/vcpkg-ci-python3/portfile.cmake @@ -1,6 +1,20 @@ set(VCPKG_POLICY_EMPTY_PACKAGE enabled) +set(host_python "${CURRENT_HOST_INSTALLED_DIR}/tools/python3/python3${VCPKG_HOST_EXECUTABLE_SUFFIX}") +if(VCPKG_HOST_IS_WINDOWS) + set(host_python "${CURRENT_HOST_INSTALLED_DIR}/tools/python3/python.exe") +endif() + vcpkg_cmake_configure( SOURCE_PATH "${CURRENT_PORT_DIR}/project" + OPTIONS + "-DPython_EXECUTABLE=${host_python}" + "-DPython3_EXECUTABLE=${host_python}" + OPTIONS_DEBUG + "-DEXPECTED_LIBRARY_KEYWORD=debug" + "-DEXPECTED_LIBRARY_PREFIX=${CURRENT_INSTALLED_DIR}/debug/lib" + OPTIONS_RELEASE + "-DEXPECTED_LIBRARY_KEYWORD=optimized" + "-DEXPECTED_LIBRARY_PREFIX=${CURRENT_INSTALLED_DIR}/lib" ) vcpkg_cmake_build() diff --git a/scripts/test_ports/vcpkg-ci-python3/project/CMakeLists.txt b/scripts/test_ports/vcpkg-ci-python3/project/CMakeLists.txt index 6339bfa052..97e33fa372 100644 --- a/scripts/test_ports/vcpkg-ci-python3/project/CMakeLists.txt +++ b/scripts/test_ports/vcpkg-ci-python3/project/CMakeLists.txt @@ -1,87 +1,111 @@ cmake_minimum_required(VERSION 3.12) -project(python3-test) - -set(Python_ARTIFACTS_PREFIX "_MEOW") # We need to opt-out of CMP0148 to be able to test the pre-CMake 3.12 Python # find modules. The old policy is deprecated, so, at some point, this aspect # of the test will have to go away. +# This must appear before `project` to affect vcpkg.cmake's find_package macro. if(POLICY CMP0148) cmake_policy(SET CMP0148 OLD) endif() -# The purpose of this test is to ensure that we get the expected values -# from the finders, not crosscompiling. So, let's not even go there. -# These find_package() calls aren't required because FindPythonInterp -# seems to not return a result in CI. Probably because FindPythonInterp -# prefers the system Python instead of the executable from the python3 -# port. +project(python3-test) + +# Options for matching library vs. build type +set(EXPECTED_LIBRARY_KEYWORD "not set" CACHE STRING "debug or optimized") +set(EXPECTED_LIBRARY_PREFIX "not set" CACHE STRING "link library dir for current build type") + +# A goal of this test project is to ensure that we get the expected values +# from the modern find modules with regard to Python[3]_ARTIFACTS_PREFIX. +set(Python_ARTIFACTS_PREFIX "_MEOW") +set(Python3_ARTIFACTS_PREFIX "") + +if(CMAKE_VERSION VERSION_GREATER_EQUAL 4.0 AND DEFINED Python_EXECUTABLE AND NOT DEFINED Python_MEOW_EXECUTABLE) + set(Python_MEOW_EXECUTABLE "${Python_EXECUTABLE}") + unset(Python_EXECUTABLE CACHE) +endif() + + +# Test helpers + +function(assert condition) + string(CONFIGURE "@condition@" condition_quoted @ONLY ESCAPE_QUOTES) + cmake_language(EVAL CODE " + if(${condition_quoted}) + # good + else() + message(SEND_ERROR \"Assertion failed: ${condition_quoted}\") + endif() + ") +endfunction() + +function(check_find_python_results) + cmake_parse_arguments(PARSE_ARGV 0 arg "" "EXPECTED;UNEXPECTED" "") + + assert("TARGET ${arg_EXPECTED}::Python") + assert("NOT TARGET ${arg_UNEXPECTED}::Python") + assert("${arg_EXPECTED}_LIBRARIES") + assert("NOT ${arg_UNEXPECTED}_LIBRARIES") + + string(FIND "${EXPECTED_LIBRARY_KEYWORD};${${arg_EXPECTED}_LIBRARIES}" "${EXPECTED_LIBRARY_KEYWORD};${EXPECTED_LIBRARY_PREFIX}" prefix_index) + if("${prefix_index}" EQUAL "-1") + message(SEND_ERROR + "Actual ${arg_EXPECTED} libraries: ${${arg_EXPECTED}_LIBRARIES}\n" + "Expected ${CMAKE_BUILD_TYPE} location: ${EXPECTED_LIBRARY_PREFIX}" + ) + endif() + + assert("${arg_EXPECTED}_EXECUTABLE") + assert("NOT ${arg_UNEXPECTED}_EXECUTABLE") + assert("${arg_EXPECTED}_STDLIB") + assert("NOT ${arg_UNEXPECTED}_STDLIB") + assert("TARGET ${arg_EXPECTED}::Interpreter") + assert("NOT TARGET ${arg_UNEXPECTED}::Interpreter") +endfunction() + + +### Testing the deprecated FindPythonLibs and FindPythonInterpreter modules + +# Intentionally not testing success or the *expected* result: +# The python3 port never added a vcpkg-cmake-wrapper for FindPythonLibs, +# and these find modules are deprecated now. + +find_package(PythonLibs) +if(DEFINED PythonLibs_MEOW_FOUND OR DEFINED PYTHON_MEOW_LIBRARIES) + message(FATAL_ERROR "FindPythonLibs unexpectedly prefixed the result variables") +endif() + if(NOT CMAKE_CROSSCOMPILING) find_package(PythonInterp) -endif() -find_package(PythonLibs) - -# The old find modules should NOT be prefixed. -if(DEFINED PythonInterp_MEOW_FOUND OR DEFINED PYTHON_MEOW_EXECUTABLE) - message(FATAL_ERROR "FindPythonInterp prefixed the result variables") -endif() -if(DEFINED PythonLibs_MEOW_FOUND OR DEFINED PYTHON_MEOW_LIBRARIES) - message(FATAL_ERROR "FindPythonLibs prefixed the result variables") -endif() - -function(test_result NAME TYPE EXPECTED UNEXPECTED) - if(NOT ${TYPE} ${EXPECTED}${NAME}) - message(FATAL_ERROR "${EXPECTED}${NAME} should be ${TYPE}") + if(DEFINED PythonInterp_MEOW_FOUND OR DEFINED PYTHON_MEOW_EXECUTABLE) + message(FATAL_ERROR "FindPythonInterp unexpectedly prefixed the result variables") endif() - if(${TYPE} ${UNEXPECTED}${NAME}) - message(FATAL_ERROR "${UNEXPECTED}${NAME} should not be ${TYPE}") - endif() -endfunction() - -function(test_new_finder EXPECTED UNEXPECTED) - test_result(::Python TARGET ${EXPECTED} ${UNEXPECTED}) - test_result(_LIBRARIES DEFINED ${EXPECTED} ${UNEXPECTED}) - - if(NOT CMAKE_CROSSCOMPILING) - test_result(_EXECUTABLE DEFINED ${EXPECTED} ${UNEXPECTED}) - test_result(_STDLIB DEFINED ${EXPECTED} ${UNEXPECTED}) - test_result(::Interpreter TARGET ${EXPECTED} ${UNEXPECTED}) - endif() -endfunction() - -if(NOT CMAKE_CROSSCOMPILING) - set(_INTERPRETER "Interpreter") endif() -# The new find modules should be prefixed if CMake is 4.0+ -find_package(Python REQUIRED COMPONENTS ${_INTERPRETER} Development) + +### Testing the FindPython module (artifact prefix: _MEOW) + +add_executable(main-python-target main.c) + +find_package(Python REQUIRED COMPONENTS Interpreter Development) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 4.0) - set(EXPECTED_PYTHON Python_MEOW) - set(UNEXPECTED_PYTHON Python) + # With CMake is 4.0+, the find module outputs should use the prefix. + check_find_python_results(EXPECTED Python_MEOW UNEXPECTED Python) + target_link_libraries(main-python-target PRIVATE Python_MEOW::Python) + set(libraries_to_check "${Python_MEOW_LIBRARIES}") else() - set(EXPECTED_PYTHON Python) - set(UNEXPECTED_PYTHON Python_MEOW) + check_find_python_results(EXPECTED Python UNEXPECTED Python_MEOW) + target_link_libraries(main-python-target PRIVATE Python::Python) + set(libraries_to_check "${Python_LIBRARIES}") endif() -test_new_finder(${EXPECTED_PYTHON} ${UNEXPECTED_PYTHON}) -# Also test non-prefixed. Use Python3:: to avoid conflicts with Python_MEOW:: -# The test against Python3_MEOW should never happen because the prefix variable -# should be Python3_ARTIFACT_PREFIX, but we set Python_ARTIFACT_PREFIX. -find_package(Python3 REQUIRED COMPONENTS ${_INTERPRETER} Development) -test_new_finder(Python3 Python3_MEOW) -# Test embedding the libraries found. -function(add_test_executable TARGET LIBRARIES INCLUDES) - add_executable(${TARGET} main.c) - target_link_libraries(${TARGET} PRIVATE ${LIBRARIES}) - if(INCLUDES) - target_include_directories(${TARGET} PRIVATE ${INCLUDES}) - endif() -endfunction() +### Testing the FindPython3 module (artifact prefix: empty) -# We're purposefully not testing the result of the old finders. -# The python3 port never added a vcpkg-cmake-wrapper for FindPythonLibs, -# and it seems like a poor use of time to do so at this point - the -# old finders are soft removed as of CMake 3.27. -add_test_executable(new_with_prefix "${EXPECTED_PYTHON}::Python" "") -add_test_executable(new_without_prefix "${Python3_LIBRARIES}" "${Python3_INCLUDE_DIRS}") +add_executable(main-python3-libraries main.c) + +find_package(Python3 REQUIRED COMPONENTS Interpreter Development) +check_find_python_results(EXPECTED Python3 UNEXPECTED Python3_MEOW) + +target_include_directories(main-python3-libraries PRIVATE ${Python3_INCLUDE_DIRS}) +target_link_libraries(main-python3-libraries PRIVATE ${Python3_LIBRARIES}) diff --git a/scripts/test_ports/vcpkg-ci-python3/vcpkg.json b/scripts/test_ports/vcpkg-ci-python3/vcpkg.json index a2eca55df7..fd86ea4d3a 100644 --- a/scripts/test_ports/vcpkg-ci-python3/vcpkg.json +++ b/scripts/test_ports/vcpkg-ci-python3/vcpkg.json @@ -5,7 +5,15 @@ "homepage": "https://github.com/microsoft/vcpkg", "license": "MIT", "dependencies": [ - "python3", + { + "name": "python3", + "default-features": false + }, + { + "name": "python3", + "host": true, + "default-features": false + }, { "name": "vcpkg-cmake", "host": true