Skip to content

Commit 9375cff

Browse files
authored
Merge pull request #6061 from Freed-Wu/cython
Add rule python.cython
2 parents c6e3ca7 + 43088a7 commit 9375cff

File tree

9 files changed

+100
-5
lines changed

9 files changed

+100
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, world!")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_rules("mode.debug", "mode.release")
2+
add_requires("python 3.x")
3+
4+
target("example")
5+
add_rules("python.cython")
6+
add_files("src/*.py")
7+
add_packages("python")

tests/projects/pybind/example_with_soabi/xmake.lua renamed to tests/projects/python/pybind/example/xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_rules("mode.release", "mode.debug")
22
add_requires("pybind11")
33

44
target("example")
5-
add_rules("python.library", {soabi = true})
5+
add_rules("python.module", {soabi = false})
66
add_files("src/*.cpp")
77
add_packages("pybind11")
88
set_languages("c++11")

tests/projects/pybind/example/xmake.lua renamed to tests/projects/python/pybind/example_with_soabi/xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_rules("mode.release", "mode.debug")
22
add_requires("pybind11")
33

44
target("example")
5-
add_rules("python.library")
5+
add_rules("python.module")
66
add_files("src/*.cpp")
77
add_packages("pybind11")
88
set_languages("c++11")

xmake/rules/python/cython/xmake.lua

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author Wu, Zhenyu
18+
-- @file xmake.lua
19+
--
20+
21+
rule("python.cython")
22+
add_deps("python.module")
23+
set_extensions(".py", ".pyx")
24+
25+
on_load(function (target)
26+
local language = target:extraconf("rules", "python.cython", "language")
27+
if language == "c" then
28+
target:add("deps", "c")
29+
elseif language == "c++" then
30+
target:add("deps", "c++")
31+
end
32+
end)
33+
34+
before_buildcmd_file(function (target, batchcmds, sourcefile, opt)
35+
import("lib.detect.find_tool")
36+
37+
local cython = assert(find_tool("cython"), "cython not found! please `pip install cython`.")
38+
local language = target:extraconf("rules", "python.cython", "language")
39+
local ext = "c"
40+
local arg = "-3"
41+
if language == "c++" then
42+
ext = "cc"
43+
arg = arg .. "+"
44+
end
45+
local dirname = path.join(target:autogendir(), "rules", "python", "cython")
46+
local sourcefile_c = path.join(dirname, path.basename(sourcefile) .. "." .. ext)
47+
48+
-- add objectfile
49+
local objectfile = target:objectfile(sourcefile_c)
50+
table.insert(target:objectfiles(), objectfile)
51+
52+
-- add commands
53+
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.python %s", sourcefile)
54+
batchcmds:mkdir(path.directory(sourcefile_c))
55+
batchcmds:vrunv(cython.program, {arg, "-o", path(sourcefile_c), path(sourcefile)})
56+
batchcmds:compile(sourcefile_c, objectfile)
57+
58+
-- add deps
59+
batchcmds:add_depfiles(sourcefile)
60+
batchcmds:set_depmtime(os.mtime(objectfile))
61+
batchcmds:set_depcache(target:dependfile(objectfile))
62+
end)

xmake/rules/python/library/xmake.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author ruki
18+
-- @file xmake.lua
19+
--
20+
21+
rule("python.library")
22+
on_config(function (target)
23+
wprint('deprecated: please use add_rules("python.module") instead of add_rules("python.library")')
24+
end)
25+
add_deps("python.module")

xmake/rules/python/xmake.lua renamed to xmake/rules/python/module/xmake.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
--
2020

2121
-- @see https://github.com/xmake-io/xmake/issues/1896
22-
rule("python.library")
22+
rule("python.module")
2323
on_config(function (target)
2424
target:set("kind", "shared")
2525
target:set("prefixname", "")
2626
target:add("runenvs", "PYTHONPATH", target:targetdir())
27-
local soabi = target:extraconf("rules", "python.library", "soabi")
28-
if soabi then
27+
local soabi = target:extraconf("rules", "python.module", "soabi")
28+
if soabi == nil or soabi then
2929
import("lib.detect.find_tool")
3030
local python = assert(find_tool("python3"), "python not found!")
3131
local result = try { function() return os.iorunv(python.program, {"-c", "import sysconfig; print(sysconfig.get_config_var('EXT_SUFFIX'))"}) end}

0 commit comments

Comments
 (0)