diff options
| author | ruki <[email protected]> | 2024-07-05 23:01:33 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-07-05 23:01:33 +0800 |
| commit | 3f1be27ffff37f07bbfe3f97f946d0d35c62ce1e (patch) | |
| tree | b230521b4b090ce757fd6886cbfd010c857a7c86 | |
| parent | 71db286a686ab5ecc4f7430fe8ee8e9200027b97 (diff) | |
| parent | a388acc43c93d75879b97698034ae72778cea295 (diff) | |
Merge pull request #5302 from al1-ce/dev
Improve Vala support
| -rw-r--r-- | tests/projects/vala/includec/src/main.vala | 9 | ||||
| -rw-r--r-- | tests/projects/vala/includec/src/printer.c | 6 | ||||
| -rw-r--r-- | tests/projects/vala/includec/src/printer.vala | 3 | ||||
| -rw-r--r-- | tests/projects/vala/includec/xmake.lua | 10 | ||||
| -rw-r--r-- | xmake/rules/vala/xmake.lua | 95 |
5 files changed, 92 insertions, 31 deletions
diff --git a/tests/projects/vala/includec/src/main.vala b/tests/projects/vala/includec/src/main.vala new file mode 100644 index 000000000..acc6c7363 --- /dev/null +++ b/tests/projects/vala/includec/src/main.vala @@ -0,0 +1,9 @@ +extern void printer_from_c(); + +int main (string[] args) { + printer_from_c(); + printer_from_other_file(); + + return 0; +} + diff --git a/tests/projects/vala/includec/src/printer.c b/tests/projects/vala/includec/src/printer.c new file mode 100644 index 000000000..dc7a82c67 --- /dev/null +++ b/tests/projects/vala/includec/src/printer.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +void printer_from_c() { + printf("Calling from C"); +} + diff --git a/tests/projects/vala/includec/src/printer.vala b/tests/projects/vala/includec/src/printer.vala new file mode 100644 index 000000000..4599c3529 --- /dev/null +++ b/tests/projects/vala/includec/src/printer.vala @@ -0,0 +1,3 @@ +void printer_from_other_file() { + stdout.printf("Calling from other file"); +} diff --git a/tests/projects/vala/includec/xmake.lua b/tests/projects/vala/includec/xmake.lua new file mode 100644 index 000000000..01b3e3eb3 --- /dev/null +++ b/tests/projects/vala/includec/xmake.lua @@ -0,0 +1,10 @@ +add_rules("mode.release", "mode.debug") + +add_requires("glib") + +target("test") + set_kind("binary") + add_rules("vala") + add_files("src/*.vala") + add_files("src/*.c") + add_packages("glib") diff --git a/xmake/rules/vala/xmake.lua b/xmake/rules/vala/xmake.lua index 85a74d239..caebab9d8 100644 --- a/xmake/rules/vala/xmake.lua +++ b/xmake/rules/vala/xmake.lua @@ -20,13 +20,11 @@ rule("vala.build") set_extensions(".vala") + -- Since vala can directly compile with C files + -- we can add C sourcekinds + -- And in the end we're going to be compiling C code + set_sourcekinds("cc") on_load(function (target) - -- only vala source files? we need to patch c source kind for linker - local sourcekinds = target:sourcekinds() - if #sourcekinds == 0 then - table.insert(sourcekinds, "cc") - end - -- we disable to build across targets in parallel, because the source files may depend on other target modules target:set("policy", "build.across_targets_in_parallel", false) @@ -35,9 +33,9 @@ rule("vala.build") if not vapifile then local vapiname = target:values("vala.vapi") if vapiname then - vapifile = path.join(target:targetdir(), vapiname) + vapifile = path.absolute(path.join(target:targetdir(), vapiname)) else - vapifile = path.join(target:targetdir(), target:name() .. ".vapi") + vapifile = path.absolute(path.join(target:targetdir(), target:name() .. ".vapi")) end target:data_set("vala.vapifile", vapifile) end @@ -47,9 +45,9 @@ rule("vala.build") if not headerfile then local headername = target:values("vala.header") if headername then - headerfile = path.join(target:targetdir(), headername) + headerfile = path.absolute(path.join(target:targetdir(), headername)) else - headerfile = path.join(target:targetdir(), target:name() .. ".h") + headerfile = path.absolute(path.join(target:targetdir(), target:name() .. ".h")) end target:data_set("vala.headerfile", headerfile) end @@ -58,24 +56,21 @@ rule("vala.build") target:add("sysincludedirs", path.directory(headerfile), {public = true}) end end) - before_buildcmd_file(function (target, batchcmds, sourcefile_vala, opt) + before_buildcmd_files(function (target, batchcmds, sourcebatch, opt) + -- Here we compile vala files into C code + + -- We have to compile entire project each time + -- because otherwise valac can't resolve symbols + -- from other files, however, c files can be + -- incrementally built -- get valac import("lib.detect.find_tool") local valac = assert(find_tool("valac"), "valac not found!") - -- get c source file for vala - local sourcefile_c = target:autogenfile((sourcefile_vala:gsub(".vala$", ".c"))) - local basedir = path.directory(sourcefile_c) - - -- add objectfile - local objectfile = target:objectfile(sourcefile_c) - table.insert(target:objectfiles(), objectfile) + local argv = {"-C", "-d", target:autogendir()} -- add commands - batchcmds:show_progress(opt.progress, "${color.build.object}compiling.vala %s", sourcefile_vala) - batchcmds:mkdir(basedir) - local argv = {"-C", "-b", path(basedir)} local packages = target:values("vala.packages") if packages then for _, package in ipairs(packages) do @@ -83,6 +78,7 @@ rule("vala.build") table.insert(argv, path(package)) end end + if target:is_binary() then for _, dep in ipairs(target:orderdeps()) do if dep:is_shared() or dep:is_static() then @@ -103,22 +99,60 @@ rule("vala.build") table.insert(argv, path(headerfile)) end end - local vapidir = target:data("vala.vapidir") + + local vapidir = target:values("vala.vapidir") if vapidir then table.insert(argv, path(vapidir, function (p) return "--vapidir=" .. p end)) end - local valaflags = target:data("vala.flags") + + local valaflags = target:values("vala.flags") if valaflags then table.join2(argv, valaflags) end - table.insert(argv, path(sourcefile_vala)) - batchcmds:vrunv(valac.program, argv) - batchcmds:compile(sourcefile_c, objectfile) - -- add deps - batchcmds:add_depfiles(sourcefile_vala) - batchcmds:set_depmtime(os.mtime(objectfile)) - batchcmds:set_depcache(target:dependfile(objectfile)) + -- iterating through source files, + -- otherwise valac would fail when compiling multiple files + local lastmtime = 0 + local sourcefiles = {} + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + -- if it's only a vala file + if path.extension(sourcefile) == ".vala" then + local sourcefile_c = target:autogenfile((sourcefile:gsub(".vala$", ".c"))) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.vala %s", sourcefile) + table.insert(argv, path(sourcefile)) + table.insert(sourcefiles, sourcefile) + local sourcefile_c_mtime = os.mtime(sourcefile_c) + if sourcefile_c_mtime > lastmtime then + lastmtime = sourcefile_c_mtime + end + end + end + + if #sourcefiles > 0 then + batchcmds:vrunv(valac.program, argv) + batchcmds:add_depfiles(sourcefiles) + batchcmds:set_depmtime(lastmtime) + end + end) + + on_buildcmd_file(function (target, batchcmds, sourcefile, opt) + -- Again, only vala files need special treatment + if path.extension(sourcefile) == ".vala" then + local sourcefile_c = target:autogenfile((sourcefile:gsub(".vala$", ".c"))) + local basedir = path.directory(sourcefile_c) + + batchcmds:mkdir(basedir) + + local objectfile = target:objectfile(sourcefile_c) + table.insert(target:objectfiles(), objectfile) + + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.c %s", sourcefile_c) + batchcmds:compile(sourcefile_c, objectfile, { configs = { force = { cflags = "-w" } } }) + + batchcmds:add_depfiles(sourcefile) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) + end end) after_install(function (target) @@ -164,4 +198,3 @@ rule("vala") -- we attempt to extract symbols to the independent file and -- strip self-target binary if `set_symbols("debug")` and `set_strip("all")` are enabled add_deps("utils.symbols.extract") - |
