From 5a582e2c201ecfb7a5eeb68a126e4842a2391cd2 Mon Sep 17 00:00:00 2001 From: "Wu, Zhenyu" Date: Sun, 12 Jan 2025 14:42:05 +0800 Subject: Add rule python.cython Add test python/cython/example Move test pybind to python/pybind --- tests/projects/pybind/example/src/example.cpp | 38 ------------- tests/projects/pybind/example/xmake.lua | 8 --- .../pybind/example_with_soabi/src/example.cpp | 38 ------------- tests/projects/pybind/example_with_soabi/xmake.lua | 8 --- .../projects/python/cython/example/src/example.py | 1 + tests/projects/python/cython/example/xmake.lua | 7 +++ .../projects/python/pybind/example/src/example.cpp | 38 +++++++++++++ tests/projects/python/pybind/example/xmake.lua | 8 +++ .../pybind/example_with_soabi/src/example.cpp | 38 +++++++++++++ .../python/pybind/example_with_soabi/xmake.lua | 8 +++ xmake/rules/python/cython/xmake.lua | 62 ++++++++++++++++++++++ 11 files changed, 162 insertions(+), 92 deletions(-) delete mode 100644 tests/projects/pybind/example/src/example.cpp delete mode 100644 tests/projects/pybind/example/xmake.lua delete mode 100644 tests/projects/pybind/example_with_soabi/src/example.cpp delete mode 100644 tests/projects/pybind/example_with_soabi/xmake.lua create mode 100644 tests/projects/python/cython/example/src/example.py create mode 100644 tests/projects/python/cython/example/xmake.lua create mode 100644 tests/projects/python/pybind/example/src/example.cpp create mode 100644 tests/projects/python/pybind/example/xmake.lua create mode 100644 tests/projects/python/pybind/example_with_soabi/src/example.cpp create mode 100644 tests/projects/python/pybind/example_with_soabi/xmake.lua create mode 100644 xmake/rules/python/cython/xmake.lua diff --git a/tests/projects/pybind/example/src/example.cpp b/tests/projects/pybind/example/src/example.cpp deleted file mode 100644 index cb1bad194..000000000 --- a/tests/projects/pybind/example/src/example.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#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 deleted file mode 100644 index 19b7b5a9e..000000000 --- a/tests/projects/pybind/example/xmake.lua +++ /dev/null @@ -1,8 +0,0 @@ -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 deleted file mode 100644 index cb1bad194..000000000 --- a/tests/projects/pybind/example_with_soabi/src/example.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#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 deleted file mode 100644 index 12d8ad334..000000000 --- a/tests/projects/pybind/example_with_soabi/xmake.lua +++ /dev/null @@ -1,8 +0,0 @@ -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/python/cython/example/src/example.py b/tests/projects/python/cython/example/src/example.py new file mode 100644 index 000000000..f7cf60e14 --- /dev/null +++ b/tests/projects/python/cython/example/src/example.py @@ -0,0 +1 @@ +print("Hello, world!") diff --git a/tests/projects/python/cython/example/xmake.lua b/tests/projects/python/cython/example/xmake.lua new file mode 100644 index 000000000..f7461dbc2 --- /dev/null +++ b/tests/projects/python/cython/example/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +add_requires("python 3.x") + +target("example") + add_rules("python.cython") + add_files("src/*.py") + add_packages("python") diff --git a/tests/projects/python/pybind/example/src/example.cpp b/tests/projects/python/pybind/example/src/example.cpp new file mode 100644 index 000000000..cb1bad194 --- /dev/null +++ b/tests/projects/python/pybind/example/src/example.cpp @@ -0,0 +1,38 @@ +#include + +#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/python/pybind/example/xmake.lua b/tests/projects/python/pybind/example/xmake.lua new file mode 100644 index 000000000..19b7b5a9e --- /dev/null +++ b/tests/projects/python/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/python/pybind/example_with_soabi/src/example.cpp b/tests/projects/python/pybind/example_with_soabi/src/example.cpp new file mode 100644 index 000000000..cb1bad194 --- /dev/null +++ b/tests/projects/python/pybind/example_with_soabi/src/example.cpp @@ -0,0 +1,38 @@ +#include + +#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/python/pybind/example_with_soabi/xmake.lua b/tests/projects/python/pybind/example_with_soabi/xmake.lua new file mode 100644 index 000000000..12d8ad334 --- /dev/null +++ b/tests/projects/python/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/xmake/rules/python/cython/xmake.lua b/xmake/rules/python/cython/xmake.lua new file mode 100644 index 000000000..78323dfb2 --- /dev/null +++ b/xmake/rules/python/cython/xmake.lua @@ -0,0 +1,62 @@ +--!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 Wu, Zhenyu +-- @file xmake.lua +-- + +rule("python.cython") + add_deps("python.library") + set_extensions(".py", ".pyx") + + on_load(function (target) + local language = target:extraconf("rules", "python.cython", "language") + if language == "c" then + target:add("deps", "c") + elseif language == "c++" then + target:add("deps", "c++") + end + end) + + before_buildcmd_file(function (target, batchcmds, sourcefile, opt) + import("lib.detect.find_tool") + + local cython = assert(find_tool("cython"), "cython not found! please `pip install cython`.") + local language = target:extraconf("rules", "python.cython", "language") + local ext = "c" + local arg = "-3" + if language == "c++" then + ext = "cc" + arg = arg .. "+" + end + local dirname = path.join(target:autogendir(), "rules", "python", "cython") + local sourcefile_c = path.join(dirname, path.basename(sourcefile) .. "." .. ext) + + -- add objectfile + local objectfile = target:objectfile(sourcefile_c) + table.insert(target:objectfiles(), objectfile) + + -- add commands + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.python %s", sourcefile) + batchcmds:mkdir(path.directory(sourcefile_c)) + batchcmds:vrunv(cython.program, {arg, "-o", path(sourcefile_c), path(sourcefile)}) + batchcmds:compile(sourcefile_c, objectfile) + + -- add deps + batchcmds:add_depfiles(sourcefile) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) + end) -- cgit v1.3.1