Skip to content

Commit ebc9ffe

Browse files
committed
build jar in swig mode
1 parent 2a2ee54 commit ebc9ffe

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

tests/projects/swig/java_c/xmake.lua

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ target("example")
77
set_kind('shared')
88
-- set moduletype to java
99
add_rules("swig.c", {moduletype = "java"})
10+
-- test jar build
11+
-- add_rules("swig.c", {moduletype = "java" , buildjar = true})
1012
-- use swigflags to provider package name and output path of java files
1113
add_files("src/example.i", {swigflags = {
1214
"-package",

xmake/rules/swig/build_module_file.lua

+59-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020

2121
-- imports
2222
import("lib.detect.find_tool")
23+
import("utils.progress")
24+
import("core.project.depend")
25+
import("core.tool.compiler")
2326

24-
function main(target, batchcmds, sourcefile, opt)
27+
function main(target, sourcefile, opt)
2528
-- get swig
2629
opt = opt or {}
2730
local swig = assert(find_tool("swig"), "swig not found!")
@@ -63,13 +66,60 @@ function main(target, batchcmds, sourcefile, opt)
6366
end
6467

6568
table.insert(argv, sourcefile)
66-
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile)
67-
batchcmds:mkdir(path.directory(sourcefile_cx))
68-
batchcmds:vrunv(swig.program, argv)
69-
batchcmds:compile(sourcefile_cx, objectfile)
69+
depend.on_changed(function ()
70+
progress.show(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile)
71+
os.mkdir(path.directory(sourcefile_cx))
72+
os.vrunv(swig.program, argv)
73+
compiler.compile(sourcefile_cx, objectfile, {target = target})
7074

71-
-- add deps
72-
batchcmds:add_depfiles(sourcefile)
73-
batchcmds:set_depmtime(os.mtime(objectfile))
74-
batchcmds:set_depcache(target:dependfile(objectfile))
75+
local buildjar = target:extraconf("rules", "swig.c", "buildjar") or target:extraconf("rules", "swig.cpp", "buildjar")
76+
if moduletype == "java" and buildjar then
77+
local javac = assert(find_tool("javac"), "javac not found!")
78+
local jar = assert(find_tool("jar"), "jar not found!")
79+
80+
local java_src_dir = path.join(target:autogendir(), "rules", "swig")
81+
local jar_dst_dir = path.join(target:autogendir(), "rules", "swig")
82+
83+
-- user specified output path
84+
if fileconfig and fileconfig.swigflags then
85+
-- find -outdir path
86+
local idx = -1
87+
for i , par in pairs(fileconfig.swigflags) do
88+
if par == "-outdir" then
89+
idx = i
90+
end
91+
end
92+
93+
if idx > 0 then
94+
java_src_dir = fileconfig.swigflags[idx + 1]
95+
end
96+
end
97+
98+
-- get java files
99+
local autogenfiles = os.files(path.join(java_src_dir, "*.java"))
100+
101+
-- write file list
102+
local filelistname = os.tmpfile()
103+
local file = io.open(filelistname, "w")
104+
if file then
105+
for _, sourcebatch in pairs(autogenfiles) do
106+
file:print(sourcebatch)
107+
end
108+
file:close()
109+
end
110+
111+
-- compile to class file
112+
progress.show(opt.progress, "${color.build.object}compiling.javac %s class file", target:name())
113+
os.vrunv(javac.program, {"--release", "17", "-d", jar_dst_dir , "@"..filelistname})
114+
115+
-- generate jar file
116+
progress.show(opt.progress, "${color.build.object}compiling.jar %s", target:name()..".jar")
117+
os.vrunv(jar.program, {"-cf" , path.join(java_src_dir , target:name()..".jar") , jar_dst_dir})
118+
end
119+
120+
end, {dependfile = target:dependfile(objectfile),
121+
lastmtime = os.mtime(objectfile),
122+
files = sourcefile,
123+
values = argv,
124+
changed = target:is_rebuilt()})
75125
end

xmake/rules/swig/xmake.lua

+10-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ rule("swig.base")
8181
elseif moduletype == "lua" then
8282
autogenfiles = os.files(path.join(autogendir, "*.lua"))
8383
elseif moduletype == "java" then
84-
autogenfiles = os.files(path.join(autogendir, "*.java"))
84+
local buildjar = target:extraconf("rules", "swig.c", "buildjar") or target:extraconf("rules", "swig.cpp", "buildjar")
85+
if buildjar then
86+
autogenfiles = path.join(autogendir , target:name()..".jar")
87+
else
88+
autogenfiles = os.files(path.join(autogendir, "*.java"))
89+
end
8590
end
8691
if autogenfiles then
8792
table.join2(scriptfiles, autogenfiles)
@@ -100,15 +105,15 @@ rule("swig.base")
100105
rule("swig.c")
101106
set_extensions(".i")
102107
add_deps("swig.base", "c.build")
103-
on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
104-
import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cc"}, opt))
108+
on_build_file(function (target, sourcefile, opt)
109+
import("build_module_file")(target, sourcefile, table.join({sourcekind = "cc"}, opt))
105110
end)
106111

107112
rule("swig.cpp")
108113
set_extensions(".i")
109114
add_deps("swig.base", "c++.build")
110-
on_buildcmd_file(function (target, batchcmds, sourcefile, opt)
111-
import("build_module_file")(target, batchcmds, sourcefile, table.join({sourcekind = "cxx"}, opt))
115+
on_build_file(function (target, sourcefile, opt)
116+
import("build_module_file")(target, sourcefile, table.join({sourcekind = "cxx"}, opt))
112117
end)
113118

114119

0 commit comments

Comments
 (0)