diff options
| author | ruki <[email protected]> | 2021-09-24 22:41:45 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-24 22:41:45 +0800 |
| commit | c2f3f280bcfd82618590e9872ddce19b56940ab0 (patch) | |
| tree | 2a260f12d077ef75faa2d8293521ae42b47e6f6b /xmake/rules | |
| parent | 286e9ba7ef2e83e5222dbaab40160b3c1ac967f5 (diff) | |
add swig.c and swig.cpp rules
Diffstat (limited to 'xmake/rules')
| -rw-r--r-- | xmake/rules/swig/build_module_file.lua | 58 | ||||
| -rw-r--r-- | xmake/rules/swig/xmake.lua | 27 |
2 files changed, 83 insertions, 2 deletions
diff --git a/xmake/rules/swig/build_module_file.lua b/xmake/rules/swig/build_module_file.lua new file mode 100644 index 000000000..edcd6b04b --- /dev/null +++ b/xmake/rules/swig/build_module_file.lua @@ -0,0 +1,58 @@ +--!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 build_module_file.lua +-- + +-- imports +import("lib.detect.find_tool") + +function main(target, batchcmds, sourcefile, opt) + + -- get module type + opt = opt or {} + local moduletype + local fileconfig = target:fileconfig(sourcefile) + if fileconfig then + moduletype = fileconfig.moduletype + end + assert(moduletype, "%s: unknown swig module type, please use `add_files(\"foo.c\", {moduletype = \"python\"})` to set it!", sourcefile) + + -- get swig + local swig = assert(find_tool("swig"), "swig not found!") + local sourcefile_cx = path.join(target:autogendir(), "rules", "swig", path.basename(sourcefile) .. (opt.sourcekind == "cxx" and ".cpp" or ".c")) + + -- add objectfile + local objectfile = target:objectfile(sourcefile_cx) + table.insert(target:objectfiles(), objectfile) + + -- add commands + local argv = {"-" .. moduletype, "-o", sourcefile_cx} + if opt.sourcekind == "cxx" then + table.insert(argv, "-c++") + end + table.insert(argv, sourcefile) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile) + batchcmds:mkdir(path.directory(sourcefile_cx)) + batchcmds:vrunv(swig.program, argv) + batchcmds:compile(sourcefile_cx, objectfile) + + -- add deps + batchcmds:add_depfiles(sourcefile) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) +end diff --git a/xmake/rules/swig/xmake.lua b/xmake/rules/swig/xmake.lua index 4ac852e48..1756cf1c9 100644 --- a/xmake/rules/swig/xmake.lua +++ b/xmake/rules/swig/xmake.lua @@ -18,9 +18,32 @@ -- @file xmake.lua -- -rule("swig") - set_extensions(".i") +-- references: +-- +-- https://github.com/xmake-io/xmake/issues/1622 +-- http://www.swig.org/Doc4.0/SWIGDocumentation.html#Introduction_nn4 +-- + +rule("swig.base") on_load(function (target) + target:set("kind", "shared") + if target:is_plat("windows") then + target:set("extension", ".pyd") + end + end) + +rule("swig.c") + set_extensions(".i") + add_deps("swig.base") + on_buildcmd_file(function (target, batchcmds, sourcefile, opt) + import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cc"}, opt)) + end) + +rule("swig.cpp") + set_extensions(".i") + add_deps("swig.base") + on_buildcmd_file(function (target, batchcmds, sourcefile, opt) + import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cxx"}, opt)) end) |
