summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-24 12:50:52 +0800
committerGitHub <[email protected]>2021-09-24 12:50:52 +0800
commit024183f9c98dd03f22384203238ba661a5714640 (patch)
tree9913cdd96df86f6f289a3afd57c63d5105201e0d
parentfbccc2375dba05f227ff8e6ec0b61c9357ca449d (diff)
parent4a8694d08d48ebc4101cc1b35e88b3fffe56de59 (diff)
Merge pull request #1703 from xmake-io/swig
Support Swig
-rw-r--r--CHANGELOG.md2
-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--tests/projects/swig/python_cpp/src/example.cpp14
-rw-r--r--tests/projects/swig/python_cpp/src/example.i11
-rw-r--r--tests/projects/swig/python_cpp/xmake.lua8
-rw-r--r--xmake/modules/detect/tools/find_swig.lua53
-rw-r--r--xmake/rules/swig/build_module_file.lua58
-rw-r--r--xmake/rules/swig/xmake.lua73
10 files changed, 252 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8eaeea298..45b74bad2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
* [#388](https://github.com/xmake-io/xmake/issues/388): Pascal Language Support
* [#1682](https://github.com/xmake-io/xmake/issues/1682): Add optional lua5.3 backend instead of luajit to provide better compatibility
+* [#1622](https://github.com/xmake-io/xmake/issues/1622): Support Swig
### Change
@@ -1084,6 +1085,7 @@
* [#388](https://github.com/xmake-io/xmake/issues/388): Pascal 语言支持,可以使用 fpc 来编译 free pascal
* [#1682](https://github.com/xmake-io/xmake/issues/1682): 添加可选的额lua5.3 运行时替代 luajit,提供更好的平台兼容性。
+* [#1622](https://github.com/xmake-io/xmake/issues/1622): 支持 Swig
### 改进
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..89b13cbd4
--- /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", scriptdir = "share"})
+ add_files("src/example.c")
+ add_packages("python")
diff --git a/tests/projects/swig/python_cpp/src/example.cpp b/tests/projects/swig/python_cpp/src/example.cpp
new file mode 100644
index 000000000..31c659bfa
--- /dev/null
+++ b/tests/projects/swig/python_cpp/src/example.cpp
@@ -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_cpp/src/example.i b/tests/projects/swig/python_cpp/src/example.i
new file mode 100644
index 000000000..34193edc7
--- /dev/null
+++ b/tests/projects/swig/python_cpp/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_cpp/xmake.lua b/tests/projects/swig/python_cpp/xmake.lua
new file mode 100644
index 000000000..f0c322310
--- /dev/null
+++ b/tests/projects/swig/python_cpp/xmake.lua
@@ -0,0 +1,8 @@
+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_files("src/example.cpp")
+ add_packages("python")
diff --git a/xmake/modules/detect/tools/find_swig.lua b/xmake/modules/detect/tools/find_swig.lua
new file mode 100644
index 000000000..1442d712d
--- /dev/null
+++ b/xmake/modules/detect/tools/find_swig.lua
@@ -0,0 +1,53 @@
+--!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 find_swig.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find swig
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local swig = find_swig()
+-- local swig, version = find_swig({program = "swig", version = true})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- init options
+ opt = opt or {}
+ opt.check = opt.check or "-help"
+
+ -- find program
+ local program = find_program(opt.program or "swig", opt)
+
+ -- find program version
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end
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
new file mode 100644
index 000000000..c02ed1362
--- /dev/null
+++ b/xmake/rules/swig/xmake.lua
@@ -0,0 +1,73 @@
+--!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 xmake.lua
+--
+
+-- 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
+ 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"
+ end
+ table.insert(scriptfiles, scriptfile)
+ if scriptdir then
+ target:add("installfiles", scriptfile, {prefixdir = scriptdir})
+ end
+ end
+ end
+ end
+ -- for custom on_install/after_install, user can use it to install them
+ target:set("data", "swig.scriptfiles", scriptfiles)
+ 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)
+
+