diff options
| author | ririyeye <[email protected]> | 2024-08-30 16:35:18 +0800 |
|---|---|---|
| committer | ririyeye <[email protected]> | 2024-08-30 16:35:18 +0800 |
| commit | b42c2b02ae23c7c0b4c4f43dc3adfbb7eae2c1f3 (patch) | |
| tree | 13db3556bb5d20a5fbfcbc84a097709efadf321c | |
| parent | 8a6f4c014f090c5491be5e8a8e74445c48c25fd1 (diff) | |
add swig depend file
| -rw-r--r-- | tests/projects/swig/java_c/src/example.c | 4 | ||||
| -rw-r--r-- | tests/projects/swig/java_c/src/example.i | 6 | ||||
| -rw-r--r-- | tests/projects/swig/java_c/src/example2.i | 6 | ||||
| -rw-r--r-- | tests/projects/swig/java_c/src/test.h | 3 | ||||
| -rw-r--r-- | xmake/rules/swig/build_module_file.lua | 125 |
5 files changed, 95 insertions, 49 deletions
diff --git a/tests/projects/swig/java_c/src/example.c b/tests/projects/swig/java_c/src/example.c index 06f76c542..a3f19629d 100644 --- a/tests/projects/swig/java_c/src/example.c +++ b/tests/projects/swig/java_c/src/example.c @@ -2,3 +2,7 @@ int fact(int n) { return n; } +int fact2(int n) { + return n; +} + diff --git a/tests/projects/swig/java_c/src/example.i b/tests/projects/swig/java_c/src/example.i index e952f6123..1228cb6a9 100644 --- a/tests/projects/swig/java_c/src/example.i +++ b/tests/projects/swig/java_c/src/example.i @@ -3,3 +3,9 @@ extern int fact(int n); %} extern int fact(int n); + + + + + +%include "example2.i" diff --git a/tests/projects/swig/java_c/src/example2.i b/tests/projects/swig/java_c/src/example2.i new file mode 100644 index 000000000..a486c354a --- /dev/null +++ b/tests/projects/swig/java_c/src/example2.i @@ -0,0 +1,6 @@ + + +%inline %{ +#include "test.h" +%} +%include "test.h" diff --git a/tests/projects/swig/java_c/src/test.h b/tests/projects/swig/java_c/src/test.h new file mode 100644 index 000000000..da669d859 --- /dev/null +++ b/tests/projects/swig/java_c/src/test.h @@ -0,0 +1,3 @@ +#pragma once + +int fact2(int n); diff --git a/xmake/rules/swig/build_module_file.lua b/xmake/rules/swig/build_module_file.lua index 55a346950..5ed532be2 100644 --- a/xmake/rules/swig/build_module_file.lua +++ b/xmake/rules/swig/build_module_file.lua @@ -24,6 +24,54 @@ import("utils.progress") import("core.project.depend") import("core.tool.compiler") +function jar_build(target , fileconfig) + 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}) + + os.tryrm(filelistname) +end + + + function main(target, sourcefile, opt) -- get swig opt = opt or {} @@ -66,60 +114,39 @@ function main(target, sourcefile, opt) end table.insert(argv, sourcefile) - 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}) - - 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 + 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 - if idx > 0 then - java_src_dir = fileconfig.swigflags[idx + 1] - end - end + progress.show(opt.progress, "${color.build.object}compiling.swig.%s %s", moduletype, sourcefile) + os.mkdir(path.directory(sourcefile_cx)) - -- get java files - local autogenfiles = os.files(path.join(java_src_dir, "*.java")) + -- gen swig depend file , same with gcc .d + local swigdep = os.tmpfile() + local argv2 = {"-MMD" , "-MF" , swigdep} + table.join2(argv2, argv) - -- 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 + -- swig generate file and depend file + os.vrunv(swig.program, argv2) + compiler.compile(sourcefile_cx, objectfile, {target = target}) - -- 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}) + -- 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)) - -- 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 + -- 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) + end - end, {dependfile = target:dependfile(objectfile), - lastmtime = os.mtime(objectfile), - files = sourcefile, - values = argv, - changed = target:is_rebuilt()}) end |
