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 5 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;
}

6 changes: 6 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,9 @@
extern int fact(int n);
%}
extern int fact(int n);





ririyeye marked this conversation as resolved.
Show resolved Hide resolved
%include "example2.i"
6 changes: 6 additions & 0 deletions tests/projects/swig/java_c/src/example2.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


ririyeye marked this conversation as resolved.
Show resolved Hide resolved
%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);
2 changes: 2 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 Down
109 changes: 108 additions & 1 deletion xmake/rules/swig/build_module_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,59 @@

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

function main(target, batchcmds, sourcefile, opt)
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 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, 两边空格不对

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swigflags 是 array ,用 ipairs

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也挪到 autogendir 里去

local file = io.open(filelistname, "w")
if file then
for _, sourcebatch in pairs(autogenfiles) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 ipairs

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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. 两边加空格

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jar_dst_dir 后面的空格去掉


-- 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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. 两边空格,, 前面的空格去掉


os.tryrm(filelistname)
end



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删掉多余的空行

function swig_par(target, sourcefile, opt)
-- get swig
opt = opt or {}
local swig = assert(find_tool("swig"), "swig not found!")
Expand Down Expand Up @@ -63,6 +114,17 @@ function main(target, batchcmds, sourcefile, opt)
end

table.insert(argv, sourcefile)
return { argv = argv , objectfile = objectfile , swig = swig , sourcefile_cx = sourcefile_cx}
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

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


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删掉一个空行

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 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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, 前面空格去掉

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 = "\\"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有这的 空格

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, 两边空格不对

jar_build(target, fileconfig, opt)

end

end
37 changes: 32 additions & 5 deletions xmake/rules/swig/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,38 @@ rule("swig.base")
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
local scriptdir
local fileconfig = target:fileconfig(sourcefile)

local autogenfiles
local autogendir = path.join(target:autogendir(), "rules", "swig")

if fileconfig then
scriptdir = fileconfig.scriptdir
if 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
autogendir = fileconfig.swigflags[idx + 1]
end
end
end
local autogenfiles
local autogendir = path.join(target:autogendir(), "rules", "swig")

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 ,.. 两边空格不对

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 +122,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