pybind / pybind/pybind11

[BUG]: pybind11-config wrong include flags

Open
#3,998 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

build system
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
Problem description

After installing the project using cmake the pybind11-config program prints wrong include flags.

% pybind11-config --includes
-I/usr/include/python3.8 -I/usr/lib/python3.8/site-packages/include

The first part (python include directory) is ok, but the pybind11 include path is wrong, there is no /usr/lib/python3.8/site-packages/include.

The problem is this part: https://github.com/pybind/pybind11/blob/e2dcd95407d5202019cecd2bb2827ee6a4a8f9f3/pybind11/commands.py#L13

This only works for not-installed versions, as soon as you install the project using cmake the path is wrong.
Probably cmake should insert its installation path into that file after installation (using configure_file).

For me I fixed it using this patch:

diff -Nur pybind11-2.9.2/CMakeLists.txt new/CMakeLists.txt
--- pybind11-2.9.2/CMakeLists.txt	2022-03-31 05:12:13.000000000 +0200
+++ new/CMakeLists.txt	2022-06-08 11:59:32.704707307 +0200
@@ -206,6 +206,8 @@
   file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
 endif()
 
+configure_file(pybind11/_config.py.in ${CMAKE_CURRENT_SOURCE_DIR}/pybind11/_config.py @ONLY)
+
 if(PYBIND11_INSTALL)
   install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
   set(PYBIND11_CMAKECONFIG_INSTALL_DIR
diff -Nur pybind11-2.9.2/pybind11/commands.py new/pybind11/commands.py
--- pybind11-2.9.2/pybind11/commands.py	2022-03-31 05:12:13.000000000 +0200
+++ new/pybind11/commands.py	2022-06-08 12:01:48.677372043 +0200
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 import os
+from ._config import HEADER_DIR
 
 DIR = os.path.abspath(os.path.dirname(__file__))
 
@@ -8,7 +9,7 @@
     # type: (bool) -> str
     installed_path = os.path.join(DIR, "include")
     source_path = os.path.join(os.path.dirname(DIR), "include")
-    return installed_path if os.path.exists(installed_path) else source_path
+    return HEADER_DIR if os.path.exists(HEADER_DIR) else ( installed_path if os.path.exists(installed_path) else source_path )
 
 
 def get_cmake_dir():
diff -Nur pybind11-2.9.2/pybind11/_config.py.in new/pybind11/_config.py.in
--- pybind11-2.9.2/pybind11/_config.py.in	1970-01-01 01:00:00.000000000 +0100
+++ new/pybind11/_config.py.in	2022-06-08 11:46:25.876860701 +0200
@@ -0,0 +1 @@
+HEADER_DIR = "@CMAKE_INSTALL_FULL_INCLUDEDIR@/pybind11"
diff -Nur pybind11-2.9.2/tests/extra_python_package/test_files.py new/tests/extra_python_package/test_files.py
--- pybind11-2.9.2/tests/extra_python_package/test_files.py	2022-03-31 05:12:13.000000000 +0200
+++ new/tests/extra_python_package/test_files.py	2022-06-08 12:22:22.899405848 +0200
@@ -63,6 +63,7 @@
 py_files = {
     "__init__.py",
     "__main__.py",
+    "_config.py",
     "_version.py",
     "_version.pyi",
     "commands.py",
Reproducible example code

Simply install it using cmake and then use pybind11-config --include

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with CMakeLists.txt and pybind11/commands.py, then inspect the proposed pybind11/_config.py.in and tests/extra_python_package/test_files.py changes. Reproduce the issue by installing with CMake and running pybind11-config --includes. Done means the installed command reports the actual pybind11 include directory and the generated configuration file is included in the package tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp, python
Domain
build-system, cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.