summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-24 22:41:45 +0800
committerruki <[email protected]>2021-09-24 22:41:45 +0800
commitc2f3f280bcfd82618590e9872ddce19b56940ab0 (patch)
tree2a260f12d077ef75faa2d8293521ae42b47e6f6b
parent286e9ba7ef2e83e5222dbaab40160b3c1ac967f5 (diff)
add swig.c and swig.cpp rules
-rw-r--r--tests/projects/swig/python_c/src/example.c14
-rw-r--r--tests/projects/swig/python_c/src/example.i11
-rw-r--r--tests/projects/swig/python_c/xmake.lua8
-rw-r--r--xmake/rules/swig/build_module_file.lua58
-rw-r--r--xmake/rules/swig/xmake.lua27
5 files changed, 116 insertions, 2 deletions
diff --git a/tests/projects/swig/python_c/src/example.c b/tests/projects/swig/python_c/src/example.c
new file mode 100644
index 000000000..31c659bfa
--- /dev/null
+++ b/tests/projects/swig/python_c/src/example.c
@@ -0,0 +1,14 @@
+double My_variable = 3.0;
+
+/* Compute factorial of n */
+int fact(int n) {
+ if (n <= 1)
+ return 1;
+ else
+ return n*fact(n-1);
+}
+
+/* Compute n mod m */
+int my_mod(int n, int m) {
+ return(n % m);
+}
diff --git a/tests/projects/swig/python_c/src/example.i b/tests/projects/swig/python_c/src/example.i
new file mode 100644
index 000000000..34193edc7
--- /dev/null
+++ b/tests/projects/swig/python_c/src/example.i
@@ -0,0 +1,11 @@
+%module example
+%{
+/* Put headers and other declarations here */
+extern double My_variable;
+extern int fact(int);
+extern int my_mod(int n, int m);
+%}
+
+extern double My_variable;
+extern int fact(int);
+extern int my_mod(int n, int m);
diff --git a/tests/projects/swig/python_c/xmake.lua b/tests/projects/swig/python_c/xmake.lua
new file mode 100644
index 000000000..2b30079b6
--- /dev/null
+++ b/tests/projects/swig/python_c/xmake.lua
@@ -0,0 +1,8 @@
+add_rules("mode.release", "mode.debug")
+add_requires("python 3.x")
+
+target("example")
+ add_rules("swig.c")
+ add_files("src/example.i", {moduletype = "python"})
+ add_files("src/example.c")
+ add_packages("python")
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)