From 7cc0ebb4753770ace452d118bb327cdd9e76dcbb Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 27 Jul 2021 14:23:52 -0700 Subject: [PATCH] fix: the CMake config in Python package had a hard coded path (#3144) --- CMakeLists.txt | 6 ++++++ tests/extra_python_package/test_files.py | 14 ++++++++++++-- tools/pybind11Config.cmake.in | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c988ea0b50..b04311fd85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,6 +203,12 @@ if(PYBIND11_INSTALL) "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") + if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") + set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") + else() + set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}") + endif() + configure_package_config_file( tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 43e93c2633..337a72dfea 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -138,6 +138,16 @@ def test_build_sdist(monkeypatch, tmpdir): ) as f: pyproject_toml = f.read() + with contextlib.closing( + tar.extractfile( + tar.getmember( + start + "pybind11/share/cmake/pybind11/pybind11Config.cmake" + ) + ) + ) as f: + contents = f.read().decode("utf8") + assert 'set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")' in contents + files = {"pybind11/{}".format(n) for n in all_files} files |= sdist_files files |= {"pybind11{}".format(n) for n in local_sdist_files} @@ -151,11 +161,11 @@ def test_build_sdist(monkeypatch, tmpdir): .substitute(version=version, extra_cmd="") .encode() ) - assert setup_py == contents + assert setup_py == contents with open(os.path.join(MAIN_DIR, "tools", "pyproject.toml"), "rb") as f: contents = f.read() - assert pyproject_toml == contents + assert pyproject_toml == contents def test_build_global_dist(monkeypatch, tmpdir): diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 6fa03a0f24..73ec104a0e 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -201,7 +201,8 @@ Using ``find_package`` with version info is not recommended except for release v @PACKAGE_INIT@ # Location of pybind11/pybind11.h -set(pybind11_INCLUDE_DIR "@CMAKE_INSTALL_FULL_INCLUDEDIR@") +# This will be relative unless explicitly set as absolute +set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@") set(pybind11_LIBRARY "") set(pybind11_DEFINITIONS USING_pybind11)