[Buildroot] [PATCH v2 1/3] package/python-pybind: fix upgrade to 2.6.1

guillaume.bressaix at gmail.com guillaume.bressaix at gmail.com
Tue Jan 4 12:38:59 UTC 2022


From: "Guillaume W. Bres" <guillaume.bressaix at gmail.com>

Fixes
   http://autobuild.buildroot.net/results/b89f1de64b308dffa73675f1f31ccb0b7be5a10d
   http://autobuild.buildroot.net/results/d0287b7f64f206b0f074908c5780a3632e0cb799
   http://autobuild.buildroot.net/results/27efb545a5a719a5581c8f746d3a3555ff4216ce
   http://autobuild.buildroot.net/results/d2f0a0ad8f6c7178517df109e7d885dac9134c3a
   http://autobuild.buildroot.net/results/b57e9a3279260dae4a590f9421238fcabb2f7cab
   http://autobuild.buildroot.net/results/515e6f2fc6b5780260d98d6bb52b541ce4bf1afe
   http://autobuild.buildroot.net/results/d89c4ecc81222d4f80c951da2232d2e393fa1c69

Signed-off-by: Guillaume W. Bres <guillaume.bressaix at gmail.com>
---
setup.py now calls 'cmake', as is, internally, with DESTDIR=$(@D)
to provide some setup.py build requirements.

The autobuild failures were actually of different kinds:
  (1) 'cmake' too old causes an early crash as it is unable to parse their CMakeList.txt
   (host machine dependent)
  (2) $(@D)/pybind missing because cmake call was faulty (happened all the time)

The provided patch allows us to fully control the hidden cmake context:
  * which 'cmake' is actually called is important, as most of the time we
  end up using /usr/bin/cmake.
  By adding BR2_CMAKE_DEPENDENCY we make sure (1) never happens and
  (2) is always solved with a complete cmake context control.

We could convert python-pybind to a cmake package but it involves
so much patching and tweaking (mostly for the python install),
that it is now clear that keeping the package as is, is the best option.

I also fixed the previous package declaration / handling:
  * this is a header only lib, so installing to target does not make sense,
  and installing to staging is mandatory

  * most of the time, this package will be used at compile time,
  on host, so having the host-package option is mandatory.
  This package is then used to customize the host/usr/python
  capacity, with C++ macro bindings.
  This patch serie provides an example of use,
  that does exactly that, and works for both python2 and python3
  (refer to following patch)

---
 ...-py-improve-cmake-context-definition.patch | 48 +++++++++++++++++++
 package/python-pybind/python-pybind.mk        | 47 ++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 package/python-pybind/0001-setup-py-improve-cmake-context-definition.patch

diff --git a/package/python-pybind/0001-setup-py-improve-cmake-context-definition.patch b/package/python-pybind/0001-setup-py-improve-cmake-context-definition.patch
new file mode 100644
index 0000000000..0952d44464
--- /dev/null
+++ b/package/python-pybind/0001-setup-py-improve-cmake-context-definition.patch
@@ -0,0 +1,48 @@
+From aeee170bc90d89973268f286c8bfa12afd8f330c Mon Sep 17 00:00:00 2001
+From: "Guillaume W. Bres" <guillaume.bressaix at gmail.com>
+Date: Thu, 9 Dec 2021 20:30:08 +0100
+Subject: [PATCH] setup.py: improve cmake context definition
+
+Use two env. variables to define which cmake
+is internally called and pass a toolchain file
+for complex cross-compilation contexts.
+
+Signed-off-by: Guillaume W. Bres <guillaume.bressaix at gmail.com>
+---
+ setup.py | 7 +++++--
+ 1 file changed, 5 insertions(+), 2 deletions(-)
+
+diff --git a/setup.py b/setup.py
+index 3a03279..2dff721 100644
+--- a/setup.py
++++ b/setup.py
+@@ -23,6 +23,8 @@ VERSION_REGEX = re.compile(
+ # files, and the sys.prefix files (CMake and headers).
+
+ global_sdist = os.environ.get("PYBIND11_GLOBAL_SDIST", False)
++global_cmake = os.environ.get("PYBIND11_CMAKE", "unknown")
++global_cmake_toolchain_file = os.environ.get("PYBIND11_CMAKE_TOOLCHAIN_FILE", "unknown")
+
+ setup_py = "tools/setup_global.py.in" if global_sdist else "tools/setup_main.py.in"
+ extra_cmd = 'cmdclass["sdist"] = SDist\n'
+@@ -101,14 +103,15 @@ def remove_output(*sources):
+ with remove_output("pybind11/include", "pybind11/share"):
+     # Generate the files if they are not present.
+     with TemporaryDirectory() as tmpdir:
+-        cmd = ["cmake", "-S", ".", "-B", tmpdir] + [
++        cmd = [global_cmake, "-S", ".", "-B", tmpdir] + [
+             "-DCMAKE_INSTALL_PREFIX=pybind11",
++            "-DCMAKE_TOOLCHAIN_FILE={:s}".format(global_cmake_toolchain_file),
+             "-DBUILD_TESTING=OFF",
+             "-DPYBIND11_NOPYTHON=ON",
+         ]
+         cmake_opts = dict(cwd=DIR, stdout=sys.stdout, stderr=sys.stderr)
+         subprocess.check_call(cmd, **cmake_opts)
+-        subprocess.check_call(["cmake", "--install", tmpdir], **cmake_opts)
++        subprocess.check_call([global_cmake, "--install", tmpdir], **cmake_opts)
+
+     txt = get_and_replace(setup_py, version=version, extra_cmd=extra_cmd)
+     code = compile(txt, setup_py, "exec")
+--
+1.8.3.1
+
diff --git a/package/python-pybind/python-pybind.mk b/package/python-pybind/python-pybind.mk
index a6a1bdb976..442ed3328f 100644
--- a/package/python-pybind/python-pybind.mk
+++ b/package/python-pybind/python-pybind.mk
@@ -10,4 +10,51 @@ PYTHON_PYBIND_LICENSE = BSD-3-Clause
 PYTHON_PYBIND_LICENSE_FILES = LICENSE
 PYTHON_PYBIND_SETUP_TYPE = setuptools
 
+# Only installs header files
+PYTHON_PYBIND_INSTALL_TARGET = NO
+PYTHON_PYBIND_INSTALL_STAGING = YES
+
+# their cmakelist.txt file needs a recent cmake
+PYTHON_PYBIND_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
+HOST_PYTHON_PYBIND_DEPENDENCIES += $(BR2_CMAKE_HOST_DEPENDENCY)
+
+# every single setup.py interaction calls 'cmake' (as is) intrinsicly.
+# we deliver a patch to customize setup.py to define the cmake context
+# using the following variables
+PYTHON_PYBIND_MAKE_ENV = $(MAKE_ENV)
+PYTHON_PYBIND_SETUPTOOLS_ENV = $(PKG_PYTHON_SETUPTOOLS_ENV)
+PYTHON_PYBIND_SETUPTOOLS_ENV += PYBIND11_CMAKE=$(BR2_CMAKE)
+PYTHON_PYBIND_SETUPTOOLS_ENV += PYBIND11_CMAKE_TOOLCHAIN_FILE=$(HOST_DIR)/share/buildroot/toolchainfile.cmake
+
+HOST_PYTHON_PYBIND_MAKE_ENV = $(MAKE_ENV)
+HOST_PYTHON_PYBIND_SETUPTOOLS_ENV = $(PKG_PYTHON_SETUPTOOLS_ENV)
+HOST_PYTHON_PYBIND_SETUPTOOLS_ENV += PYBIND11_CMAKE=$(BR2_CMAKE)
+HOST_PYTHON_PYBIND_SETUPTOOLS_ENV += PYBIND11_CMAKE_TOOLCHAIN_FILE=$(HOST_DIR)/share/buildroot/toolchainfile.cmake
+
+define PYTHON_PYBIND_BUILD_CMDS
+	cd $(@D); \
+		$(PYTHON_PYBIND_SETUPTOOLS_ENV) $(HOST_DIR)/usr/bin/python setup.py build
+endef
+
+define HOST_PYTHON_PYBIND_BUILD_CMDS
+	cd $(@D); \
+		$(PYTHON_PYBIND_SETUPTOOLS_ENV) $(HOST_DIR)/usr/bin/python setup.py build
+endef
+
+# define installation destinations properly
+define PYTHON_PYBIND_INSTALL_STAGING_CMDS
+	cd $(@D); \
+		$(PYTHON_PYBIND_SETUPTOOLS_ENV) \
+			PREFIX=$(STAGING_DIR)/usr \
+				$(HOST_DIR)/bin/python setup.py install
+endef
+
+define HOST_PYTHON_PYBIND_INSTALL_CMDS
+	cd $(@D); \
+		$(PYTHON_PYBIND_SETUPTOOLS_ENV) \
+			PREFIX=$(HOST_DIR)/usr \
+				$(HOST_DIR)/bin/python setup.py install
+endef
+
 $(eval $(python-package))
+$(eval $(host-python-package))
-- 
2.30.2




More information about the buildroot mailing list