diff options
| author | ruki <[email protected]> | 2021-12-23 12:04:03 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-23 12:04:03 +0800 |
| commit | 7107a90f0a830af0818b41815e5eaec001e7f8a6 (patch) | |
| tree | d221ab3033c6f9b24a16067b420c6630b95b5974 | |
| parent | a8d47c7ead29f76cfa3d59faa7ea233a82f45002 (diff) | |
| parent | f94275cc4a85e40ed6f05200c6a69a189f3d61df (diff) | |
Merge pull request #1937 from xmake-io/pybind
add python.library rule and pybind example
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | tests/projects/pybind/example/src/example.cpp | 38 | ||||
| -rw-r--r-- | tests/projects/pybind/example/xmake.lua | 8 | ||||
| -rw-r--r-- | tests/projects/pybind/example_with_soabi/src/example.cpp | 38 | ||||
| -rw-r--r-- | tests/projects/pybind/example_with_soabi/xmake.lua | 8 | ||||
| -rw-r--r-- | tests/projects/swig/python_c_with_soabi/src/example.c | 14 | ||||
| -rw-r--r-- | tests/projects/swig/python_c_with_soabi/src/example.i | 11 | ||||
| -rw-r--r-- | tests/projects/swig/python_c_with_soabi/xmake.lua | 8 | ||||
| -rw-r--r-- | xmake/rules/python/xmake.lua | 43 | ||||
| -rw-r--r-- | xmake/rules/swig/xmake.lua | 17 |
10 files changed, 185 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 998e9d2ff..61bba3c68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#1298](https://github.com/xmake-io/xmake/issues/1928): Support vcpkg manifest mode and select version for package/install +* [#1896](https://github.com/xmake-io/xmake/issues/1896): Add `python.library` rule to build pybind modules ### Changes @@ -1177,6 +1178,7 @@ ### 新特性 * [#1298](https://github.com/xmake-io/xmake/issues/1928): 支持 vcpkg 清单模式安装包,实现安装包的版本选择 +* [#1896](https://github.com/xmake-io/xmake/issues/1896): 添加 `python.library` 规则去构建 pybind 模块,并且支持 soabi ### 改进 diff --git a/tests/projects/pybind/example/src/example.cpp b/tests/projects/pybind/example/src/example.cpp new file mode 100644 index 000000000..cb1bad194 --- /dev/null +++ b/tests/projects/pybind/example/src/example.cpp @@ -0,0 +1,38 @@ +#include <pybind11/pybind11.h> + +#define STRINGIFY(x) #x +#define MACRO_STRINGIFY(x) STRINGIFY(x) + +int add(int i, int j) { + return i + j; +} + +namespace py = pybind11; + +PYBIND11_MODULE(example, m) { + m.doc() = R"pbdoc( + Pybind11 example plugin + ----------------------- + .. currentmodule:: example + .. autosummary:: + :toctree: _generate + add + subtract + )pbdoc"; + + m.def("add", &add, R"pbdoc( + Add two numbers + Some other explanation about the add function. + )pbdoc"); + + m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc( + Subtract two numbers + Some other explanation about the subtract function. + )pbdoc"); + +#ifdef VERSION_INFO + m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); +#else + m.attr("__version__") = "dev"; +#endif +} diff --git a/tests/projects/pybind/example/xmake.lua b/tests/projects/pybind/example/xmake.lua new file mode 100644 index 000000000..19b7b5a9e --- /dev/null +++ b/tests/projects/pybind/example/xmake.lua @@ -0,0 +1,8 @@ +add_rules("mode.release", "mode.debug") +add_requires("pybind11") + +target("example") + add_rules("python.library") + add_files("src/*.cpp") + add_packages("pybind11") + set_languages("c++11") diff --git a/tests/projects/pybind/example_with_soabi/src/example.cpp b/tests/projects/pybind/example_with_soabi/src/example.cpp new file mode 100644 index 000000000..cb1bad194 --- /dev/null +++ b/tests/projects/pybind/example_with_soabi/src/example.cpp @@ -0,0 +1,38 @@ +#include <pybind11/pybind11.h> + +#define STRINGIFY(x) #x +#define MACRO_STRINGIFY(x) STRINGIFY(x) + +int add(int i, int j) { + return i + j; +} + +namespace py = pybind11; + +PYBIND11_MODULE(example, m) { + m.doc() = R"pbdoc( + Pybind11 example plugin + ----------------------- + .. currentmodule:: example + .. autosummary:: + :toctree: _generate + add + subtract + )pbdoc"; + + m.def("add", &add, R"pbdoc( + Add two numbers + Some other explanation about the add function. + )pbdoc"); + + m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc( + Subtract two numbers + Some other explanation about the subtract function. + )pbdoc"); + +#ifdef VERSION_INFO + m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); +#else + m.attr("__version__") = "dev"; +#endif +} diff --git a/tests/projects/pybind/example_with_soabi/xmake.lua b/tests/projects/pybind/example_with_soabi/xmake.lua new file mode 100644 index 000000000..12d8ad334 --- /dev/null +++ b/tests/projects/pybind/example_with_soabi/xmake.lua @@ -0,0 +1,8 @@ +add_rules("mode.release", "mode.debug") +add_requires("pybind11") + +target("example") + add_rules("python.library", {soabi = true}) + add_files("src/*.cpp") + add_packages("pybind11") + set_languages("c++11") diff --git a/tests/projects/swig/python_c_with_soabi/src/example.c b/tests/projects/swig/python_c_with_soabi/src/example.c new file mode 100644 index 000000000..b0ddc9c26 --- /dev/null +++ b/tests/projects/swig/python_c_with_soabi/src/example.c @@ -0,0 +1,14 @@ +double My_variable = 3.0; + +/* Compute factorial of n */ +int fact(int n) { + if (n <= 1) + return 1; + else + return n*fact(n-1); +} + +/* Compute n mod m */ +int my_mod(int n, int m) { + return(n % m); +} diff --git a/tests/projects/swig/python_c_with_soabi/src/example.i b/tests/projects/swig/python_c_with_soabi/src/example.i new file mode 100644 index 000000000..34193edc7 --- /dev/null +++ b/tests/projects/swig/python_c_with_soabi/src/example.i @@ -0,0 +1,11 @@ +%module example +%{ +/* Put headers and other declarations here */ +extern double My_variable; +extern int fact(int); +extern int my_mod(int n, int m); +%} + +extern double My_variable; +extern int fact(int); +extern int my_mod(int n, int m); diff --git a/tests/projects/swig/python_c_with_soabi/xmake.lua b/tests/projects/swig/python_c_with_soabi/xmake.lua new file mode 100644 index 000000000..81dd49926 --- /dev/null +++ b/tests/projects/swig/python_c_with_soabi/xmake.lua @@ -0,0 +1,8 @@ +add_rules("mode.release", "mode.debug") +add_requires("python 3.x") + +target("example") + add_rules("swig.c", {moduletype = "python", soabi = true}) + add_files("src/example.i", {scriptdir = "share"}) + add_files("src/example.c") + add_packages("python") diff --git a/xmake/rules/python/xmake.lua b/xmake/rules/python/xmake.lua new file mode 100644 index 000000000..823495485 --- /dev/null +++ b/xmake/rules/python/xmake.lua @@ -0,0 +1,43 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- @see https://github.com/xmake-io/xmake/issues/1896 +rule("python.library") + on_load(function (target) + target:set("kind", "shared") + target:set("prefixname", "_") + local soabi = target:extraconf("rules", "python.library", "soabi") + if soabi then + import("lib.detect.find_tool") + local python = assert(find_tool("python3"), "python not found!") + local result = try { function() return os.iorunv(python.program, {"-c", "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"}) end} + if result then + result = result:trim() + if result ~= "None" then + target:set("extension", result) + end + end + else + if target:is_plat("windows") then + target:set("extension", ".pyd") + end + end + end) + diff --git a/xmake/rules/swig/xmake.lua b/xmake/rules/swig/xmake.lua index 29b2673a2..c0387a940 100644 --- a/xmake/rules/swig/xmake.lua +++ b/xmake/rules/swig/xmake.lua @@ -30,8 +30,21 @@ rule("swig.base") local moduletype = target:extraconf("rules", "swig.c", "moduletype") or target:extraconf("rules", "swig.cpp", "moduletype") if moduletype == "python" then target:set("prefixname", "_") - if target:is_plat("windows") then - target:set("extension", ".pyd") + local soabi = target:extraconf("rules", "swig.c", "soabi") or target:extraconf("rules", "swig.cpp", "soabi") + if soabi then + import("lib.detect.find_tool") + local python = assert(find_tool("python3"), "python not found!") + local result = try { function() return os.iorunv(python.program, {"-c", "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"}) end} + if result then + result = result:trim() + if result ~= "None" then + target:set("extension", result) + end + end + else + if target:is_plat("windows") then + target:set("extension", ".pyd") + end end elseif moduletype == "lua" then target:set("prefixname", "") |
