Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jar generate in swig mode #5536

Merged
merged 11 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/projects/swig/java_c/src/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ int fact(int n) {
return n;
}

int fact2(int n) {
return n;
}

2 changes: 2 additions & 0 deletions tests/projects/swig/java_c/src/example.i
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
extern int fact(int n);
%}
extern int fact(int n);

%include "example2.i"
4 changes: 4 additions & 0 deletions tests/projects/swig/java_c/src/example2.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%inline %{
#include "test.h"
%}
%include "test.h"
3 changes: 3 additions & 0 deletions tests/projects/swig/java_c/src/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int fact2(int n);
3 changes: 3 additions & 0 deletions tests/projects/swig/java_c/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -15,6 +17,7 @@ target("example")
"build/java/com/example/"
}})
add_files("src/example.c")
add_includedirs("src")
before_build(function()
-- ensure output path exists before running swig
os.mkdir("build/java/com/example/")
Expand Down
123 changes: 121 additions & 2 deletions xmake/rules/swig/build_module_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,62 @@

-- imports
import("lib.detect.find_tool")
import("utils.progress")
import("core.project.depend")
import("core.tool.compiler")

function main(target, batchcmds, sourcefile, opt)
function find_user_outdir(fileconfig)
-- user specified output path
if fileconfig and fileconfig.swigflags then
-- find -outdir path
for i, par in ipairs(fileconfig.swigflags) do
if par == "-outdir" then
local dirpath = fileconfig.swigflags[i + 1]
if os.isdir(dirpath) then
return dirpath
end
end
end
end
end

function jar_build(target, fileconfig, opt)
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 java_class_dir = java_src_dir

local user_outdir = find_user_outdir(fileconfig)
if user_outdir then
java_src_dir = user_outdir
end

-- get java files
local autogenfiles = os.files(path.join(java_src_dir, "*.java"))

-- write file list
local filelistname = path.join(java_src_dir, "buildlist.txt")
local file = io.open(filelistname, "w")
if file then
for _, sourcebatch in ipairs(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", java_class_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"), java_class_dir})

os.tryrm(filelistname)
end

function swig_par(target, sourcefile, opt)
-- get swig
opt = opt or {}
local swig = assert(find_tool("swig"), "swig not found!")
Expand All @@ -34,7 +88,7 @@ function main(target, batchcmds, sourcefile, opt)

-- add commands
local moduletype = assert(target:data("swig.moduletype"), "swig.moduletype not found!")
local argv = { "-" .. moduletype, "-o", sourcefile_cx }
local argv = {"-" .. moduletype, "-o", sourcefile_cx}
if opt.sourcekind == "cxx" then
table.insert(argv, "-c++")
end
Expand Down Expand Up @@ -63,6 +117,26 @@ function main(target, batchcmds, sourcefile, opt)
end

table.insert(argv, sourcefile)
return {
argv = argv,
objectfile = objectfile,
swig = swig,
sourcefile_cx = sourcefile_cx,
moduletype = moduletype,
fileconfig = fileconfig
}
end

function swig_build_cmd(target, batchcmds, sourcefile, opt, pars)
local par = swig_par(target, sourcefile, opt)

local objectfile = par.objectfile
local argv = par.argv
local swig = par.swig
local sourcefile_cx = par.sourcefile_cx
local moduletype = par.moduletype
local fileconfig = par.fileconfig

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)
Expand All @@ -73,3 +147,48 @@ function main(target, batchcmds, sourcefile, opt)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end

function swig_build_file(target, sourcefile, opt, par)
local par = swig_par(target, sourcefile, opt)

local objectfile = par.objectfile
local argv = par.argv
local swig = par.swig
local sourcefile_cx = par.sourcefile_cx
local moduletype = par.moduletype
local fileconfig = par.fileconfig

local dependfile = target:dependfile(objectfile)
local dependinfo = target:is_rebuilt() and {} or (depend.load(dependfile) or {})
if not depend.is_changed(dependinfo, {lastmtime = os.mtime(objectfile),
values = argv
}) then
return
end

progress.show(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile)
os.mkdir(path.directory(sourcefile_cx))

-- gen swig depend file , same with gcc .d
local swigdep = os.tmpfile()
local argv2 = {"-MMD", "-MF", swigdep}
table.join2(argv2, argv)

-- swig generate file and depend file
os.vrunv(swig.program, argv2)
compiler.compile(sourcefile_cx, objectfile, {target = target})

-- update depend file
local deps = io.readfile(swigdep, {continuation = "\\"})
os.tryrm(swigdep)
dependinfo.files = {sourcefile}
dependinfo.depfiles_gcc = deps
dependinfo.values = argv
depend.save(dependinfo, target:dependfile(objectfile))

-- jar build
local buildjar = target:extraconf("rules", "swig.c", "buildjar") or target:extraconf("rules", "swig.cpp", "buildjar")
if moduletype == "java" and buildjar then
jar_build(target, fileconfig, opt)
end
end
24 changes: 21 additions & 3 deletions xmake/rules/swig/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
rule("swig.base")
on_load(function (target)
target:set("kind", "shared")
local find_user_outdir = import("build_module_file").find_user_outdir
local moduletype = target:extraconf("rules", "swig.c", "moduletype") or target:extraconf("rules", "swig.cpp", "moduletype")
if moduletype == "python" then
target:set("prefixname", "_")
Expand Down Expand Up @@ -76,12 +77,23 @@ rule("swig.base")
end
local autogenfiles
local autogendir = path.join(target:autogendir(), "rules", "swig")

local user_outdir = find_user_outdir(fileconfig)
if user_outdir then
autogendir = user_outdir
end

if moduletype == "python" then
autogenfiles = os.files(path.join(autogendir, "*.py"))
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)
Expand All @@ -101,14 +113,20 @@ 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))
import("build_module_file").swig_build_cmd(target, batchcmds, sourcefile, table.join({sourcekind = "cc"}, opt))
end)
on_build_file(function (target, sourcefile, opt)
import("build_module_file").swig_build_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))
import("build_module_file").swig_build_cmd(target, batchcmds, sourcefile, table.join({sourcekind = "cxx"}, opt))
end)
on_build_file(function (target, sourcefile, opt)
ririyeye marked this conversation as resolved.
Show resolved Hide resolved
import("build_module_file").swig_build_file(target, sourcefile, table.join({sourcekind = "cxx"}, opt))
end)


Loading