Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ports/python3/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ string(REPLACE "@PYTHON_VERSION_MINOR@" "${PYTHON_VERSION_MINOR}" usage_extra "$
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/usage" "${usage}\n${usage_extra}")

function(_generate_finder)
cmake_parse_arguments(PythonFinder "NO_OVERRIDE" "DIRECTORY;PREFIX" "" ${ARGN})
cmake_parse_arguments(PythonFinder "NO_OVERRIDE;SUPPORTS_ARTIFACTS_PREFIX" "DIRECTORY;PREFIX" "" ${ARGN})
configure_file(
"${CMAKE_CURRENT_LIST_DIR}/vcpkg-cmake-wrapper.cmake"
"${CURRENT_PACKAGES_DIR}/share/${PythonFinder_DIRECTORY}/vcpkg-cmake-wrapper.cmake"
Expand All @@ -359,8 +359,8 @@ function(_generate_finder)
endfunction()

message(STATUS "Installing cmake wrappers")
_generate_finder(DIRECTORY "python" PREFIX "Python")
_generate_finder(DIRECTORY "python3" PREFIX "Python3")
_generate_finder(DIRECTORY "python" PREFIX "Python" SUPPORTS_ARTIFACTS_PREFIX)
_generate_finder(DIRECTORY "python3" PREFIX "Python3" SUPPORTS_ARTIFACTS_PREFIX)
_generate_finder(DIRECTORY "pythoninterp" PREFIX "PYTHON" NO_OVERRIDE)

if (NOT VCPKG_TARGET_IS_WINDOWS)
Expand Down
82 changes: 51 additions & 31 deletions ports/python3/vcpkg-cmake-wrapper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ if(@PythonFinder_NO_OVERRIDE@)
return()
endif()

# CMake 4.0+ introduced Python_ARTIFACTS_PREFIX to add a suffix (yes, a suffix)
# to the results of find_package(Python), so we need to handle that here.
set(_PythonFinder_PREFIX "@PythonFinder_PREFIX@")
if(@PythonFinder_SUPPORTS_ARTIFACTS_PREFIX@ AND CMAKE_VERSION VERSION_GREATER_EQUAL 4.0)
string(APPEND _PythonFinder_PREFIX "${@PythonFinder_PREFIX@_ARTIFACTS_PREFIX}")
endif()

# CMake's FindPython's separation of concerns is very muddy. We only want to force vcpkg's Python
# if the consumer is using the development component. What we don't want to do is break detection
# of the system Python, which may have certain packages the user expects. But - if the user is
Expand All @@ -33,7 +40,7 @@ endif()

if(_PythonFinder_WantLibs)
find_path(
_@PythonFinder_PREFIX@_INCLUDE_DIR
_${_PythonFinder_PREFIX}_INCLUDE_DIR
NAMES "Python.h"
PATHS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include"
PATH_SUFFIXES "python@PYTHON_VERSION_MAJOR@.@PYTHON_VERSION_MINOR@"
Expand All @@ -43,15 +50,15 @@ if(_PythonFinder_WantLibs)
# Don't set the public facing hint or the finder will be unable to detect the debug library.
# Internally, it uses the same value with an underscore prepended.
find_library(
_@PythonFinder_PREFIX@_LIBRARY_RELEASE
_${_PythonFinder_PREFIX}_LIBRARY_RELEASE
NAMES
"python@PYTHON_VERSION_MAJOR@@PYTHON_VERSION_MINOR@"
"python@PYTHON_VERSION_MAJOR@.@PYTHON_VERSION_MINOR@"
PATHS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib"
NO_DEFAULT_PATH
)
find_library(
_@PythonFinder_PREFIX@_LIBRARY_DEBUG
_${_PythonFinder_PREFIX}_LIBRARY_DEBUG
NAMES
"python@PYTHON_VERSION_MAJOR@@PYTHON_VERSION_MINOR@_d"
"python@PYTHON_VERSION_MAJOR@.@PYTHON_VERSION_MINOR@d"
Expand All @@ -61,47 +68,59 @@ if(_PythonFinder_WantLibs)

if(_PythonFinder_WantInterp)
find_program(
@PythonFinder_PREFIX@_EXECUTABLE
${_PythonFinder_PREFIX}_EXECUTABLE
NAMES "python" "python@PYTHON_VERSION_MAJOR@.@PYTHON_VERSION_MINOR@"
PATHS "${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/tools/python3"
NO_DEFAULT_PATH
)
endif()

# These are duplicated as normal variables to nullify FindPython's checksum verifications.
set(_@PythonFinder_PREFIX@_INCLUDE_DIR "${_@PythonFinder_PREFIX@_INCLUDE_DIR}")
set(_@PythonFinder_PREFIX@_LIBRARY_RELEASE "${_@PythonFinder_PREFIX@_LIBRARY_RELEASE}")
set(_@PythonFinder_PREFIX@_LIBRARY_DEBUG "${_@PythonFinder_PREFIX@_LIBRARY_DEBUG}")
set(_${_PythonFinder_PREFIX}_INCLUDE_DIR "${_@PythonFinder_PREFIX@_INCLUDE_DIR}")
set(_${_PythonFinder_PREFIX}_LIBRARY_RELEASE "${_@PythonFinder_PREFIX@_LIBRARY_RELEASE}")
set(_${_PythonFinder_PREFIX}_LIBRARY_DEBUG "${_@PythonFinder_PREFIX@_LIBRARY_DEBUG}")

_find_package(${ARGS})

get_directory_property(_@PythonFinder_PREFIX@_IMPORTED_TARGETS IMPORTED_TARGETS)
if(ANDROID AND @PythonFinder_PREFIX@::Module IN_LIST _@PythonFinder_PREFIX@_IMPORTED_TARGETS)
set_property(TARGET @PythonFinder_PREFIX@::Module APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<LINK_ONLY:@PythonFinder_PREFIX@::Python>)
get_directory_property(_${_PythonFinder_PREFIX}_IMPORTED_TARGETS IMPORTED_TARGETS)
if(ANDROID AND ${_PythonFinder_PREFIX}::Module IN_LIST _${_PythonFinder_PREFIX}_IMPORTED_TARGETS)
set_property(TARGET ${_PythonFinder_PREFIX}::Module APPEND PROPERTY INTERFACE_LINK_LIBRARIES $<LINK_ONLY:${_PythonFinder_PREFIX}::Python>)
endif()
unset(_@PythonFinder_PREFIX@_IMPORTED_TARGETS)
unset(_${_PythonFinder_PREFIX}_IMPORTED_TARGETS)

if(@VCPKG_LIBRARY_LINKAGE@ STREQUAL "static")
# Python for Windows embeds the zlib module into the core, so we have to link against it.
# This is a separate extension module on Unix-like platforms.
if(WIN32)
find_package(ZLIB)
if(TARGET @PythonFinder_PREFIX@::Python)
set_property(TARGET @PythonFinder_PREFIX@::Python APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
if(TARGET ${_PythonFinder_PREFIX}::Python)
set_property(TARGET ${_PythonFinder_PREFIX}::Python APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
endif()
if(TARGET ${_PythonFinder_PREFIX}::Module)
set_property(TARGET ${_PythonFinder_PREFIX}::Module APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
endif()
if(DEFINED ${_PythonFinder_PREFIX}_LIBRARIES)
list(APPEND ${_PythonFinder_PREFIX}_LIBRARIES ${ZLIB_LIBRARIES})
endif()
endif()

if(UNIX AND NOT APPLE)
if(TARGET ${_PythonFinder_PREFIX}::Python)
set_property(TARGET ${_PythonFinder_PREFIX}::Python APPEND PROPERTY INTERFACE_LINK_LIBRARIES m)
endif()
if(TARGET @PythonFinder_PREFIX@::Module)
set_property(TARGET @PythonFinder_PREFIX@::Module APPEND PROPERTY INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
if(TARGET ${_PythonFinder_PREFIX}::Module)
set_property(TARGET ${_PythonFinder_PREFIX}::Module APPEND PROPERTY INTERFACE_LINK_LIBRARIES m)
endif()
if(DEFINED @PythonFinder_PREFIX@_LIBRARIES)
list(APPEND @PythonFinder_PREFIX@_LIBRARIES ${ZLIB_LIBRARIES})
if(DEFINED ${_PythonFinder_PREFIX}_LIBRARIES)
list(APPEND ${_PythonFinder_PREFIX}_LIBRARIES m)
endif()
endif()

if(APPLE)
find_package(Iconv)
find_package(Intl)
if(TARGET @PythonFinder_PREFIX@::Python)
get_target_property(_PYTHON_INTERFACE_LIBS @PythonFinder_PREFIX@::Python INTERFACE_LINK_LIBRARIES)
if(TARGET ${_PythonFinder_PREFIX}::Python)
get_target_property(_PYTHON_INTERFACE_LIBS ${_PythonFinder_PREFIX}::Python INTERFACE_LINK_LIBRARIES)
if(NOT _PYTHON_INTERFACE_LIBS)
set(_PYTHON_INTERFACE_LIBS "")
endif()
Expand All @@ -110,11 +129,11 @@ if(_PythonFinder_WantLibs)
Iconv::Iconv
"$<IF:$<CONFIG:Debug>,${Intl_LIBRARY_DEBUG},${Intl_LIBRARY_RELEASE}>"
)
set_property(TARGET @PythonFinder_PREFIX@::Python PROPERTY INTERFACE_LINK_LIBRARIES ${_PYTHON_INTERFACE_LIBS})
set_property(TARGET ${_PythonFinder_PREFIX}::Python PROPERTY INTERFACE_LINK_LIBRARIES ${_PYTHON_INTERFACE_LIBS})
unset(_PYTHON_INTERFACE_LIBS)
endif()
if(TARGET @PythonFinder_PREFIX@::Module)
get_target_property(_PYTHON_INTERFACE_LIBS @PythonFinder_PREFIX@::Module INTERFACE_LINK_LIBRARIES)
if(TARGET ${_PythonFinder_PREFIX}::Module)
get_target_property(_PYTHON_INTERFACE_LIBS ${_PythonFinder_PREFIX}::Module INTERFACE_LINK_LIBRARIES)
if(NOT _PYTHON_INTERFACE_LIBS)
set(_PYTHON_INTERFACE_LIBS "")
endif()
Expand All @@ -123,27 +142,28 @@ if(_PythonFinder_WantLibs)
Iconv::Iconv
"$<IF:$<CONFIG:Debug>,${Intl_LIBRARY_DEBUG},${Intl_LIBRARY_RELEASE}>"
)
set_property(TARGET @PythonFinder_PREFIX@::Module PROPERTY INTERFACE_LINK_LIBRARIES ${_PYTHON_INTERFACE_LIBS})
set_property(TARGET ${_PythonFinder_PREFIX}::Module PROPERTY INTERFACE_LINK_LIBRARIES ${_PYTHON_INTERFACE_LIBS})
unset(_PYTHON_INTERFACE_LIBS)
endif()
if(DEFINED @PythonFinder_PREFIX@_LIBRARIES)
list(APPEND @PythonFinder_PREFIX@_LIBRARIES "-framework CoreFoundation" ${Iconv_LIBRARIES} ${Intl_LIBRARIES})
if(DEFINED ${_PythonFinder_PREFIX}_LIBRARIES)
list(APPEND ${_PythonFinder_PREFIX}_LIBRARIES "-framework CoreFoundation" ${Iconv_LIBRARIES} ${Intl_LIBRARIES})
endif()
endif()
endif()
else()
_find_package(${ARGS})
endif()

if(TARGET @PythonFinder_PREFIX@::Python)
target_compile_definitions(@PythonFinder_PREFIX@::Python INTERFACE "Py_NO_LINK_LIB")
if(TARGET ${_PythonFinder_PREFIX}::Python)
target_compile_definitions(${_PythonFinder_PREFIX}::Python INTERFACE "Py_NO_LINK_LIB")
endif()
if(TARGET @PythonFinder_PREFIX@::Module)
target_compile_definitions(@PythonFinder_PREFIX@::Module INTERFACE "Py_NO_LINK_LIB")
if(TARGET ${_PythonFinder_PREFIX}::Module)
target_compile_definitions(${_PythonFinder_PREFIX}::Module INTERFACE "Py_NO_LINK_LIB")
endif()
if(TARGET @PythonFinder_PREFIX@::SABIModule)
target_compile_definitions(@PythonFinder_PREFIX@::SABIModule INTERFACE "Py_NO_LINK_LIB")
if(TARGET ${_PythonFinder_PREFIX}::SABIModule)
target_compile_definitions(${_PythonFinder_PREFIX}::SABIModule INTERFACE "Py_NO_LINK_LIB")
endif()

unset(_PythonFinder_PREFIX)
unset(_PythonFinder_WantInterp)
unset(_PythonFinder_WantLibs)
2 changes: 1 addition & 1 deletion ports/python3/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "python3",
"version": "3.12.9",
"port-version": 3,
"port-version": 4,
"description": "The Python programming language",
"homepage": "https://github.com/python/cpython",
"license": "Python-2.0",
Expand Down
6 changes: 6 additions & 0 deletions scripts/test_ports/vcpkg-ci-python3/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(VCPKG_POLICY_EMPTY_PACKAGE enabled)

vcpkg_cmake_configure(
SOURCE_PATH "${CURRENT_PORT_DIR}/project"
)
vcpkg_cmake_build()
87 changes: 87 additions & 0 deletions scripts/test_ports/vcpkg-ci-python3/project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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.
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.
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}")
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)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 4.0)
set(EXPECTED_PYTHON Python_MEOW)
set(UNEXPECTED_PYTHON Python)
else()
set(EXPECTED_PYTHON Python)
set(UNEXPECTED_PYTHON Python_MEOW)
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()

# 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}")
19 changes: 19 additions & 0 deletions scripts/test_ports/vcpkg-ci-python3/project/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <Python.h>

int main()
{
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.write_bytecode = 0;
PyConfig_SetString(&config, &config.program_name, L"test");

PyStatus status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
Py_ExitStatusException(status);
}

Py_FinalizeEx();
PyConfig_Clear(&config);
return 0;
}
14 changes: 14 additions & 0 deletions scripts/test_ports/vcpkg-ci-python3/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "vcpkg-ci-python3",
"version-string": "ci",
"description": "Port to test python3 in CI",
"homepage": "https://github.com/microsoft/vcpkg",
"license": "MIT",
"dependencies": [
"python3",
{
"name": "vcpkg-cmake",
"host": true
}
]
}
2 changes: 1 addition & 1 deletion versions/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -7706,7 +7706,7 @@
},
"python3": {
"baseline": "3.12.9",
"port-version": 3
"port-version": 4
},
"qca": {
"baseline": "2.3.7",
Expand Down
5 changes: 5 additions & 0 deletions versions/p-/python3.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"versions": [
{
"git-tree": "df599d27a7b29b37e98297ed124445bf40b25728",
"version": "3.12.9",
"port-version": 4
},
{
"git-tree": "477d229bf207c5a8544448c53da8f58ce2a7615b",
"version": "3.12.9",
Expand Down