Skip to content

Commit

Permalink
Add rule python.cython
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Jan 14, 2025
1 parent 5bc5ce5 commit 4954d0e
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions xmake/rules/python/cython/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--!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")
set_extensions(".py", ".pyx")
before_buildcmd_file(function(target, batchcmds, sourcefile, opt)
local language = target:extraconf("rules", "python.cython", "language")
local ext
local arg = "-3"
if language == "c" then
ext = "c"
local rule = target:rule("python.cython"):clone()
rule:add("deps", "c", {order = true})
target:rule_add(rule)
elseif language == "c++" then
ext = "cc"
arg = arg .. "+"
local rule = target:rule("python.cython"):clone()
rule:add("deps", "c++", {order = true})
target:rule_add(rule)
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))
import("lib.detect.find_tool")
local cython = find_tool("cython")
assert(cython, "cython not found! please `pip install cython`.")
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)

0 comments on commit 4954d0e

Please sign in to comment.