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

Fix dependency on correct pybind11-abi package and add test to check that the correct abi version is used #105

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions .scripts/build_steps.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .scripts/run_win_build.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build-locally.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
{% set sha256 = "e08cb87f4773da97fa7b5f035de8763abc656d87d5773e62f6da0587d1f0ec20" %}

# this is set to PYBIND11_INTERNALS_VERSION
{% set abi_version = "4" %}
# since 2.12.0 pybind11 defines two ABI version. 4 and 5 for 3.12+ or msvc.
# https://github.com/pybind/pybind11/blob/v2.12.0/include/pybind11/detail/internals.h#L40
{% set abi_version = "4" %} # [py<312 and not win]
{% set abi_version = "5" %} # [py>=312 or win]

package:
name: pybind11-split
Expand All @@ -14,7 +17,7 @@ source:
sha256: {{ sha256 }}

build:
number: 0
number: 1

requirements:
build:
Expand All @@ -33,8 +36,9 @@ outputs:
source_files:
- include/pybind11/detail/internals.h
commands:
# make sure the internals version matches the package version
- if [ $(grep "#define PYBIND11_INTERNALS_VERSION" include/pybind11/detail/internals.h | cut -d' ' -f3) != "{{ abi_version }}" ]; then exit 1; fi
# the ABI version depends on both python version and operating system, so just check if the used version is one of the one defined in the header
- matched=false; while read -r version; do [ "$version" -eq "{{ abi_version }}" ] && { matched=true; break; }; done < <(awk '/define PYBIND11_INTERNALS_VERSION/{print $NF}' include/pybind11/detail/internals.h); [ "$matched" = true ] && exit 0 || exit 1


- name: pybind11-global
script: build-pybind11-global.sh # [unix]
Expand Down Expand Up @@ -62,6 +66,9 @@ outputs:
- name: pybind11
script: build-pybind11.sh # [unix]
script: build-pybind11.bat # [win]
build:
script_env:
- EXPECTED_PYBIND11_INTERNALS_VERSION={{ abi_version }}
requirements:
build:
- python # [build_platform != target_platform]
Expand All @@ -80,15 +87,24 @@ outputs:
run_constrained:
- pybind11-abi =={{ abi_version }}
test:
files:
- test_abi
imports:
- pybind11
script: run-test-pybind11.sh # [unix]
script: run-test-pybind11.bat # [win]
commands:
- test -f ${PREFIX}/share/cmake/pybind11/pybind11Config.cmake # [unix]
- if exist %LIBRARY_PREFIX%\share\cmake\pybind11\pybind11Config.cmake (exit 0) else (exit 1) # [win]
- test -f ${PREFIX}/include/pybind11/pybind11.h # [unix]
- if exist %LIBRARY_INC%\pybind11\pybind11.h (exit 0) else (exit 1) # [win]
- test -f $(python -c "import pybind11 as py; print(py.get_include())")/pybind11/pybind11.h # [unix]
- if exist $(python -c "import pybind11 as py; print(py.get_include())")\pybind11\pybind11.h (exit 0) else (exit 1) # [win]
requires:
- cmake
- {{ compiler('c') }}
- {{ compiler('cxx') }}
- ninja

about:
home: https://github.com/pybind/pybind11/
Expand Down
27 changes: 27 additions & 0 deletions recipe/run-test-pybind11.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cd test_abi

REM debug env variable
set

REM Cleanup the build folder
rmdir /s /q build
if errorlevel 1 exit 1

REM Configure the project
cmake -GNinja -S. -Bbuild -DCMAKE_BUILD_TYPE=Release
if errorlevel 1 exit 1

REM Build the project and extract the line with PYBIND11_INTERNALS_VERSION=
for /F "tokens=*" %%i in ('cmake --build build ^| findstr /R /C:"^PYBIND11_INTERNALS_VERSION="') do set "line=%%i"
if errorlevel 1 exit 1

REM Extract the value after the equals sign in the line PYBIND11_INTERNALS_VERSION=
for /F "tokens=2 delims==" %%a in ("%line%") do set "version=%%a"

REM Compare to the environment variable EXPECTED_PYBIND11_INTERNALS_VERSION
if "%version%"=="%EXPECTED_PYBIND11_INTERNALS_VERSION%" (
echo Versions match, compilation: "%version%" expected: "%EXPECTED_PYBIND11_INTERNALS_VERSION%"
) else (
echo Error: PYBIND11_INTERNALS_VERSION version mismatch, compilation: "%version%" expected: "%EXPECTED_PYBIND11_INTERNALS_VERSION%"
exit 1
)
30 changes: 30 additions & 0 deletions recipe/run-test-pybind11.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cd test_abi

# Debug env variables
env

# Cleanup the build folder
rm -rf ./build

# Configure the project
cmake -GNinja -S. -Bbuild -DCMAKE_BUILD_TYPE=Release

# Build the project and extract the line with PYBIND11_INTERNALS_VERSION=
line=$(cmake --build build | grep 'PYBIND11_INTERNALS_VERSION=')

# Check if the line was found
if [ -z "$line" ]; then
echo "Error: PYBIND11_INTERNALS_VERSION not found."
exit 1
fi

# Extract the value after the equals sign
version=${line#PYBIND11_INTERNALS_VERSION=}

# Compare to the environment variable EXPECTED_PYBIND11_INTERNALS_VERSION
if [ "$version" = "$EXPECTED_PYBIND11_INTERNALS_VERSION" ]; then
echo "Versions match - compilation: $version expected: $EXPECTED_PYBIND11_INTERNALS_VERSION"
else
echo "Error: Version mismatch - compilation: $version expected: $EXPECTED_PYBIND11_INTERNALS_VERSION"
exit 1
fi
7 changes: 7 additions & 0 deletions recipe/test_abi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required (VERSION 3.25)

project(pybind11testabi)

find_package(pybind11 REQUIRED)

pybind11_add_module(test_abi_module test_abi.cpp)
32 changes: 32 additions & 0 deletions recipe/test_abi/test_abi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Dummy module used only to print the PYBIND11_INTERNALS_VERSION version
// during the compilation process
#include <pybind11/detail/internals.h>

#define STRINGIFY(x) #x
#define TO_STRING(x) STRINGIFY(x)

#if defined(_MSC_VER)
// MSVC specific pragma
#pragma message ("PYBIND11_INTERNALS_VERSION=" TO_STRING(PYBIND11_INTERNALS_VERSION))
#else
#pragma message "PYBIND11_INTERNALS_VERSION=" TO_STRING(PYBIND11_INTERNALS_VERSION)
#endif

// The rest is just a copy of https://github.com/pybind/cmake_example/blob/7a94877f581a14de4de1a096fb053a55fc2a66bf/src/main.cpp
// to get a valid pybind11 module

#include <pybind11/pybind11.h>

int add(int i, int j) {
return i + j;
}

namespace py = pybind11;

PYBIND11_MODULE(cmake_example, m) {
m.def("add", &add, R"pbdoc(
Add two numbers

Some other explanation about the add function.
)pbdoc");
}
Loading