diff --git a/DEVELOPERS b/DEVELOPERS index d049b99b91..7874677f7e 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -1124,6 +1124,9 @@ F: package/liquid-dsp/ F: package/pixiewps/ F: package/python-pybind/ F: package/reaver/ +F: support/testing/tests/package/br2-external/python-pybind +F: support/testing/tests/package/sample_python_pybind.py +F: support/testing/tests/package/test_python_pybind.py N: Guo Ren F: arch/Config.in.csky diff --git a/support/testing/tests/package/br2-external/python-pybind/Config.in b/support/testing/tests/package/br2-external/python-pybind/Config.in new file mode 100644 index 0000000000..70c77157b3 --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/Config.in @@ -0,0 +1 @@ +source "$BR2_EXTERNAL_PYTHON_PYBIND_PATH/package/python-pybind-example/Config.in" diff --git a/support/testing/tests/package/br2-external/python-pybind/external.desc b/support/testing/tests/package/br2-external/python-pybind/external.desc new file mode 100644 index 0000000000..eef5e0f5a0 --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/external.desc @@ -0,0 +1 @@ +name: PYTHON_PYBIND diff --git a/support/testing/tests/package/br2-external/python-pybind/external.mk b/support/testing/tests/package/br2-external/python-pybind/external.mk new file mode 100644 index 0000000000..3501f3135e --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/external.mk @@ -0,0 +1 @@ +include $(sort $(wildcard $(BR2_EXTERNAL_PYTHON_PYBIND_PATH)/package/*/*.mk)) diff --git a/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/Config.in b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/Config.in new file mode 100644 index 0000000000..1b4d1e0d2c --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/Config.in @@ -0,0 +1,6 @@ +config BR2_PACKAGE_PYTHON_PYBIND_EXAMPLE + bool "python-pybind-example" + depends on BR2_PACKAGE_PYTHON3 + select BR2_PACKAGE_PYTHON_PYBIND + help + This test creates a cpp macro later used on target in python diff --git a/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/example.cpp b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/example.cpp new file mode 100644 index 0000000000..f2eea8e48d --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/example.cpp @@ -0,0 +1,16 @@ +#include +namespace py = pybind11; + +int add (int i, int j) { + return i + j; +} + +PYBIND11_MODULE (example, m) { + // optional module description + m.doc() = "pybind11 example plugin"; + // test a module method + m.def("add", &add, "example::add adds two integer numbers"); + // test a module attribute + py::object hello = py::cast("Hello World"); + m.attr("says") = hello; +} diff --git a/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/python-pybind-example.mk b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/python-pybind-example.mk new file mode 100644 index 0000000000..a6d7e0daaf --- /dev/null +++ b/support/testing/tests/package/br2-external/python-pybind/package/python-pybind-example/python-pybind-example.mk @@ -0,0 +1,41 @@ +################################################################################ +# +# python-pybind-example +# +################################################################################ + +# this builds a C++ macro "add(a,b)" +# that we expose to host-python with a custom install +# and that the python test script will later use +PYTHON_PYBIND_EXAMPLE_DEPENDENCIES = python-pybind + +PYTHON_PYBIND_EXAMPLE_PYBIND_INCLUDE = \ + $(shell $(HOST_DIR)/usr/bin/python3 -c 'import pybind11; print(pybind11.get_include())') + +PYTHON_PYBIND_EXAMPLE_CXX_FLAGS = \ + $(TARGET_CXXFLAGS) \ + -Wall -shared -std=c++11 -fPIC \ + -I$(PYTHON_PYBIND_EXAMPLE_PYBIND_INCLUDE) \ + $(shell $(STAGING_DIR)/usr/bin/python3-config --includes --libs --ldflags) + +# .so to be installed must have exact suffix +# otherwise import() in python will not work +HOST_LIB_BINARY_SUFFIX = \ + $(shell $(STAGING_DIR)/usr/bin/python3-config --extension-suffix) + +define PYTHON_PYBIND_EXAMPLE_BUILD_CMDS + if [ -z "$(PYTHON_PYBIND_EXAMPLE_PYBIND_INCLUDE)" ]; then \ + echo "pybind11.get_include() returned empty"; \ + exit 1; \ + fi + $(TARGET_CXX) $(PYTHON_PYBIND_EXAMPLE_CXX_FLAGS) \ + $(PYTHON_PYBIND_EXAMPLE_PKGDIR)/example.cpp \ + -o $(@D)/example$(HOST_LIB_BINARY_SUFFIX) +endef + +define PYTHON_PYBIND_EXAMPLE_INSTALL_TARGET_CMDS + $(INSTALL) -D -m 755 $(@D)/example$(HOST_LIB_BINARY_SUFFIX) \ + $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/site-packages/example$(HOST_LIB_BINARY_SUFFIX) +endef + +$(eval $(generic-package)) diff --git a/support/testing/tests/package/sample_python_pybind.py b/support/testing/tests/package/sample_python_pybind.py new file mode 100644 index 0000000000..605c0bab15 --- /dev/null +++ b/support/testing/tests/package/sample_python_pybind.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +import example +print(example.add(1, 2)) +print(example.says) diff --git a/support/testing/tests/package/test_python_pybind.py b/support/testing/tests/package/test_python_pybind.py new file mode 100644 index 0000000000..5e3c3b9cc5 --- /dev/null +++ b/support/testing/tests/package/test_python_pybind.py @@ -0,0 +1,15 @@ +import os +import infra +import subprocess +from tests.package.test_python import TestPythonPackageBase + +class TestPythonPybind (TestPythonPackageBase): + __test__ = True + config = TestPythonPackageBase.config + \ + """ + BR2_PACKAGE_PYTHON3=y + BR2_PACKAGE_PYTHON_PYBIND_EXAMPLE=y + """ + sample_scripts = ["tests/package/sample_python_pybind.py"] + # ship examples macro & installs it to host + br2_external = [infra.filepath("tests/package/br2-external/python-pybind")]