|
| 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 | + set_extensions(".py", ".pyx") |
| 23 | + before_buildcmd_file(function(target, batchcmds, sourcefile, opt) |
| 24 | + local language = target:extraconf("rules", "python.cython", "language") |
| 25 | + local ext |
| 26 | + local arg = "-3" |
| 27 | + if language == "c" then |
| 28 | + ext = "c" |
| 29 | + local rule = target:rule("python.cython"):clone() |
| 30 | + rule:add("deps", "c", {order = true}) |
| 31 | + target:rule_add(rule) |
| 32 | + elseif language == "c++" then |
| 33 | + ext = "cc" |
| 34 | + arg = arg .. "+" |
| 35 | + local rule = target:rule("python.cython"):clone() |
| 36 | + rule:add("deps", "c++", {order = true}) |
| 37 | + target:rule_add(rule) |
| 38 | + end |
| 39 | + local dirname = path.join(target:autogendir(), "rules", "python", "cython") |
| 40 | + local sourcefile_c = path.join(dirname, path.basename(sourcefile) .. "." .. ext) |
| 41 | + |
| 42 | + -- add objectfile |
| 43 | + local objectfile = target:objectfile(sourcefile_c) |
| 44 | + table.insert(target:objectfiles(), objectfile) |
| 45 | + |
| 46 | + -- add commands |
| 47 | + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.python %s", sourcefile) |
| 48 | + batchcmds:mkdir(path.directory(sourcefile_c)) |
| 49 | + import("lib.detect.find_tool") |
| 50 | + local cython = find_tool("cython") |
| 51 | + assert(cython, "cython not found! please `pip install cython`.") |
| 52 | + batchcmds:vrunv(cython.program, |
| 53 | + { arg, "-o", path(sourcefile_c), path(sourcefile) }) |
| 54 | + batchcmds:compile(sourcefile_c, objectfile) |
| 55 | + |
| 56 | + -- add deps |
| 57 | + batchcmds:add_depfiles(sourcefile) |
| 58 | + batchcmds:set_depmtime(os.mtime(objectfile)) |
| 59 | + batchcmds:set_depcache(target:dependfile(objectfile)) |
| 60 | + end) |
0 commit comments