summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorririyeye <[email protected]>2024-08-29 15:02:58 +0800
committerririyeye <[email protected]>2024-08-29 15:02:58 +0800
commitebc9ffe55beac2b7f8a284e51990612d61e4d1b7 (patch)
tree966837d5d4ceab4621c693852cca756b62f1a3ab
parent2a2ee54fe65cd4035bc25b01391e63170c952c38 (diff)
build jar in swig mode
-rw-r--r--tests/projects/swig/java_c/xmake.lua2
-rw-r--r--xmake/rules/swig/build_module_file.lua68
-rw-r--r--xmake/rules/swig/xmake.lua15
3 files changed, 71 insertions, 14 deletions
diff --git a/tests/projects/swig/java_c/xmake.lua b/tests/projects/swig/java_c/xmake.lua
index 65e871ce6..c1dfccf0d 100644
--- a/tests/projects/swig/java_c/xmake.lua
+++ b/tests/projects/swig/java_c/xmake.lua
@@ -7,6 +7,8 @@ target("example")
set_kind('shared')
-- set moduletype to java
add_rules("swig.c", {moduletype = "java"})
+ -- test jar build
+ -- add_rules("swig.c", {moduletype = "java" , buildjar = true})
-- use swigflags to provider package name and output path of java files
add_files("src/example.i", {swigflags = {
"-package",
diff --git a/xmake/rules/swig/build_module_file.lua b/xmake/rules/swig/build_module_file.lua
index 2eecd5fb0..55a346950 100644
--- a/xmake/rules/swig/build_module_file.lua
+++ b/xmake/rules/swig/build_module_file.lua
@@ -20,8 +20,11 @@
-- imports
import("lib.detect.find_tool")
+import("utils.progress")
+import("core.project.depend")
+import("core.tool.compiler")
-function main(target, batchcmds, sourcefile, opt)
+function main(target, sourcefile, opt)
-- get swig
opt = opt or {}
local swig = assert(find_tool("swig"), "swig not found!")
@@ -63,13 +66,60 @@ function main(target, batchcmds, sourcefile, opt)
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)
+ depend.on_changed(function ()
+ progress.show(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile)
+ os.mkdir(path.directory(sourcefile_cx))
+ os.vrunv(swig.program, argv)
+ compiler.compile(sourcefile_cx, objectfile, {target = target})
- -- add deps
- batchcmds:add_depfiles(sourcefile)
- batchcmds:set_depmtime(os.mtime(objectfile))
- batchcmds:set_depcache(target:dependfile(objectfile))
+ local buildjar = target:extraconf("rules", "swig.c", "buildjar") or target:extraconf("rules", "swig.cpp", "buildjar")
+ if moduletype == "java" and buildjar then
+ local javac = assert(find_tool("javac"), "javac not found!")
+ local jar = assert(find_tool("jar"), "jar not found!")
+
+ local java_src_dir = path.join(target:autogendir(), "rules", "swig")
+ local jar_dst_dir = path.join(target:autogendir(), "rules", "swig")
+
+ -- user specified output path
+ if fileconfig and fileconfig.swigflags then
+ -- find -outdir path
+ local idx = -1
+ for i , par in pairs(fileconfig.swigflags) do
+ if par == "-outdir" then
+ idx = i
+ end
+ end
+
+ if idx > 0 then
+ java_src_dir = fileconfig.swigflags[idx + 1]
+ end
+ end
+
+ -- get java files
+ local autogenfiles = os.files(path.join(java_src_dir, "*.java"))
+
+ -- write file list
+ local filelistname = os.tmpfile()
+ local file = io.open(filelistname, "w")
+ if file then
+ for _, sourcebatch in pairs(autogenfiles) do
+ file:print(sourcebatch)
+ end
+ file:close()
+ end
+
+ -- compile to class file
+ progress.show(opt.progress, "${color.build.object}compiling.javac %s class file", target:name())
+ os.vrunv(javac.program, {"--release", "17", "-d", jar_dst_dir , "@"..filelistname})
+
+ -- generate jar file
+ progress.show(opt.progress, "${color.build.object}compiling.jar %s", target:name()..".jar")
+ os.vrunv(jar.program, {"-cf" , path.join(java_src_dir , target:name()..".jar") , jar_dst_dir})
+ end
+
+ end, {dependfile = target:dependfile(objectfile),
+ lastmtime = os.mtime(objectfile),
+ files = sourcefile,
+ values = argv,
+ changed = target:is_rebuilt()})
end
diff --git a/xmake/rules/swig/xmake.lua b/xmake/rules/swig/xmake.lua
index 05db9a3a1..daa273231 100644
--- a/xmake/rules/swig/xmake.lua
+++ b/xmake/rules/swig/xmake.lua
@@ -81,7 +81,12 @@ rule("swig.base")
elseif moduletype == "lua" then
autogenfiles = os.files(path.join(autogendir, "*.lua"))
elseif moduletype == "java" then
- autogenfiles = os.files(path.join(autogendir, "*.java"))
+ local buildjar = target:extraconf("rules", "swig.c", "buildjar") or target:extraconf("rules", "swig.cpp", "buildjar")
+ if buildjar then
+ autogenfiles = path.join(autogendir , target:name()..".jar")
+ else
+ autogenfiles = os.files(path.join(autogendir, "*.java"))
+ end
end
if autogenfiles then
table.join2(scriptfiles, autogenfiles)
@@ -100,15 +105,15 @@ rule("swig.base")
rule("swig.c")
set_extensions(".i")
add_deps("swig.base", "c.build")
- on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
- import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cc"}, opt))
+ on_build_file(function (target, sourcefile, opt)
+ import("build_module_file")(target, sourcefile, table.join({sourcekind = "cc"}, opt))
end)
rule("swig.cpp")
set_extensions(".i")
add_deps("swig.base", "c++.build")
- on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
- import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cxx"}, opt))
+ on_build_file(function (target, sourcefile, opt)
+ import("build_module_file")(target, sourcefile, table.join({sourcekind = "cxx"}, opt))
end)