summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-27 22:40:17 +0800
committerruki <[email protected]>2021-09-27 22:40:17 +0800
commitc223db1bdd7e90d970b378e6d6755e977ceb4c6d (patch)
treeb0c16bbc54861f930a043f4a586d989a27532636
parent9128fe7d5b84a27831ae2535a16547bb0b9b7d8e (diff)
improve swig
-rw-r--r--tests/projects/swig/lua_c/src/example.c27
-rw-r--r--tests/projects/swig/lua_c/src/example.i6
-rw-r--r--tests/projects/swig/lua_c/xmake.lua4
-rw-r--r--tests/projects/swig/python_c/xmake.lua4
-rw-r--r--tests/projects/swig/python_cpp/xmake.lua4
-rw-r--r--xmake/rules/swig/build_module_file.lua11
-rw-r--r--xmake/rules/swig/xmake.lua23
7 files changed, 29 insertions, 50 deletions
diff --git a/tests/projects/swig/lua_c/src/example.c b/tests/projects/swig/lua_c/src/example.c
index c4afe9779..06f76c542 100644
--- a/tests/projects/swig/lua_c/src/example.c
+++ b/tests/projects/swig/lua_c/src/example.c
@@ -1,25 +1,4 @@
-#include <stdio.h>
-#include "lua.h"
-#include "lualib.h"
-#include "lauxlib.h"
-
-extern int luaopen_example(lua_State* L); // declare the wrapped module
-
-int main(int argc,char* argv[])
-{
- lua_State *L;
- if (argc<2)
- {
- printf("%s: <filename.lua>\n",argv[0]);
- return 0;
- }
- L=lua_open();
- luaopen_base(L); // load basic libs (eg. print)
- luaopen_example(L); // load the wrappered module
- if (luaL_loadfile(L,argv[1])==0) // load and run the file
- lua_pcall(L,0,0,0);
- else
- printf("unable to load %s\n",argv[1]);
- lua_close(L);
- return 0;
+int fact(int n) {
+ return n;
}
+
diff --git a/tests/projects/swig/lua_c/src/example.i b/tests/projects/swig/lua_c/src/example.i
index f17b34a82..5724920da 100644
--- a/tests/projects/swig/lua_c/src/example.i
+++ b/tests/projects/swig/lua_c/src/example.i
@@ -1,6 +1,2 @@
%module example
-%{
-#include "example.h"
-%}
-int gcd(int x, int y);
-extern double Foo;
+int fact(int n);
diff --git a/tests/projects/swig/lua_c/xmake.lua b/tests/projects/swig/lua_c/xmake.lua
index fc6e3dd7b..4ee0226ee 100644
--- a/tests/projects/swig/lua_c/xmake.lua
+++ b/tests/projects/swig/lua_c/xmake.lua
@@ -2,7 +2,7 @@ add_rules("mode.release", "mode.debug")
add_requires("lua")
target("example")
- add_rules("swig.c")
- add_files("src/example.i", {moduletype = "lua", scriptdir = "share"})
+ add_rules("swig.c", {moduletype = "lua"})
+ add_files("src/example.i")
add_files("src/example.c")
add_packages("lua")
diff --git a/tests/projects/swig/python_c/xmake.lua b/tests/projects/swig/python_c/xmake.lua
index 89b13cbd4..7cc23e42b 100644
--- a/tests/projects/swig/python_c/xmake.lua
+++ b/tests/projects/swig/python_c/xmake.lua
@@ -2,7 +2,7 @@ add_rules("mode.release", "mode.debug")
add_requires("python 3.x")
target("example")
- add_rules("swig.c")
- add_files("src/example.i", {moduletype = "python", scriptdir = "share"})
+ add_rules("swig.c", {moduletype = "python"})
+ add_files("src/example.i", {scriptdir = "share"})
add_files("src/example.c")
add_packages("python")
diff --git a/tests/projects/swig/python_cpp/xmake.lua b/tests/projects/swig/python_cpp/xmake.lua
index f0c322310..1aa612211 100644
--- a/tests/projects/swig/python_cpp/xmake.lua
+++ b/tests/projects/swig/python_cpp/xmake.lua
@@ -2,7 +2,7 @@ add_rules("mode.release", "mode.debug")
add_requires("python 3.x")
target("example")
- add_rules("swig.cpp")
- add_files("src/example.i", {moduletype = "python", scriptdir = "share"})
+ add_rules("swig.cpp", {moduletype = "python"})
+ add_files("src/example.i", {scriptdir = "share"})
add_files("src/example.cpp")
add_packages("python")
diff --git a/xmake/rules/swig/build_module_file.lua b/xmake/rules/swig/build_module_file.lua
index edcd6b04b..a38fb009e 100644
--- a/xmake/rules/swig/build_module_file.lua
+++ b/xmake/rules/swig/build_module_file.lua
@@ -23,16 +23,8 @@ 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
+ opt = opt or {}
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"))
@@ -41,6 +33,7 @@ function main(target, batchcmds, sourcefile, opt)
table.insert(target:objectfiles(), objectfile)
-- add commands
+ local moduletype = assert(target:data("swig.moduletype"), "swig.moduletype not found!")
local argv = {"-" .. moduletype, "-o", sourcefile_cx}
if opt.sourcekind == "cxx" then
table.insert(argv, "-c++")
diff --git a/xmake/rules/swig/xmake.lua b/xmake/rules/swig/xmake.lua
index 28f8e0563..738cc12fd 100644
--- a/xmake/rules/swig/xmake.lua
+++ b/xmake/rules/swig/xmake.lua
@@ -27,24 +27,34 @@
rule("swig.base")
on_load(function (target)
target:set("kind", "shared")
- target:set("prefixname", "_")
- if target:is_plat("windows") then
- target:set("extension", ".pyd")
+ local moduletype = target:extraconf("rules", "swig.c", "moduletype") or target:extraconf("rules", "swig.cpp", "moduletype")
+ if moduletype == "python" then
+ target:set("prefixname", "_")
+ if target:is_plat("windows") then
+ target:set("extension", ".pyd")
+ end
+ elseif moduletype == "lua" then
+ target:set("prefixname", "")
+ if not target:is_plat("windows") then
+ target:set("extension", ".so")
+ end
+ else
+ raise("unknown swig module type, please use `add_rules(\"swig.c\", {moduletype = \"python\"})` to set it!")
end
local scriptfiles = {}
for _, sourcebatch in pairs(target:sourcebatches()) do
if sourcebatch.rulename:startswith("swig.") then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
local scriptdir
- local moduletype
local fileconfig = target:fileconfig(sourcefile)
if fileconfig then
- moduletype = fileconfig.moduletype
scriptdir = fileconfig.scriptdir
end
local scriptfile = path.join(target:autogendir(), "rules", "swig", path.basename(sourcefile))
if moduletype == "python" then
scriptfile = scriptfile .. ".py"
+ elseif moduletype == "lua" then
+ scriptfile = scriptfile .. ".lua"
end
table.insert(scriptfiles, scriptfile)
if scriptdir then
@@ -54,7 +64,8 @@ rule("swig.base")
end
end
-- for custom on_install/after_install, user can use it to install them
- target:set("data", "swig.scriptfiles", scriptfiles)
+ target:data_set("swig.scriptfiles", scriptfiles)
+ target:data_set("swig.moduletype", moduletype)
end)
rule("swig.c")