[03/33] pyo3.bbclass: move from meta-python

Message ID 20220314224837.834776-3-richard.purdie@linuxfoundation.org
State Accepted, archived
Commit f3130197ed93fd60b4cad326940b02a5554ed089
Headers show
Series [01/33] python3-setuptools-rust-native: from meta-python | expand

Commit Message

Richard Purdie March 14, 2022, 10:48 p.m. UTC
From: Tim Orling <tim.orling@konsulko.com>

PyO3 provides Rust bindings for Python, including tools for creating native
Python extension modules. Running and interacting with Python code from a
Rust binary is also supported.

This class sets up the cross-compilation environment.

Export PYO3_CROSS, PYO3_CROSS_LIB_DIR, PYO3_CROSS_INCLUDE_DIR and CARGO_BUILD_TARGET
to inform tools like setuptools-rust we are cross-compiling.

Export RUSTFLAGS so cargo can find 'std' and 'core' for target

Dynamically generate PyO3 config file and export PYO3_CONFIG_FILE
absolute path. This is the trick that finally made pyo3 work.

Signed-off-by: Tim Orling <tim.orling@konsulko.com>
---
 meta/classes/pyo3.bbclass | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 meta/classes/pyo3.bbclass

Patch

diff --git a/meta/classes/pyo3.bbclass b/meta/classes/pyo3.bbclass
new file mode 100644
index 00000000000..6ce21329c2e
--- /dev/null
+++ b/meta/classes/pyo3.bbclass
@@ -0,0 +1,30 @@ 
+#
+# This class helps make sure that Python extensions built with PyO3
+# and setuptools_rust properly set up the environment for cross compilation
+#
+
+inherit cargo python3-dir siteinfo
+
+export PYO3_CROSS="1"
+export PYO3_CROSS_PYTHON_VERSION="${PYTHON_BASEVERSION}"
+export PYO3_CROSS_LIB_DIR="${STAGING_LIBDIR}"
+export CARGO_BUILD_TARGET="${HOST_SYS}"
+export RUSTFLAGS
+export PYO3_PYTHON="${PYTHON}"
+export PYO3_CONFIG_FILE="${WORKDIR}/pyo3.config"
+
+pyo3_do_configure () {
+    cat > ${WORKDIR}/pyo3.config << EOF
+implementation=CPython
+version=${PYTHON_BASEVERSION}
+shared=true
+abi3=false
+lib_name=${PYTHON_DIR}
+lib_dir=${STAGING_LIBDIR}
+pointer_width=${SITEINFO_BITS}
+build_flags=WITH_THREAD
+suppress_build_script_link_lines=false
+EOF
+}
+
+EXPORT_FUNCTIONS do_configure