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

Improve Vala support #5302

Merged
merged 6 commits into from
Jul 5, 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
9 changes: 9 additions & 0 deletions tests/projects/vala/includec/src/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern void printer_from_c();

int main (string[] args) {
printer_from_c();
printer_from_other_file();

return 0;
}

6 changes: 6 additions & 0 deletions tests/projects/vala/includec/src/printer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

void printer_from_c() {
printf("Calling from C");
}

3 changes: 3 additions & 0 deletions tests/projects/vala/includec/src/printer.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void printer_from_other_file() {
stdout.printf("Calling from other file");
}
10 changes: 10 additions & 0 deletions tests/projects/vala/includec/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")
97 changes: 65 additions & 32 deletions xmake/rules/vala/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -58,31 +56,29 @@ 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()}
al1-ce marked this conversation as resolved.
Show resolved Hide resolved

-- 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
table.insert(argv, "--pkg")
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
Expand All @@ -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" } } })
al1-ce marked this conversation as resolved.
Show resolved Hide resolved

batchcmds:add_depfiles(sourcefile)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
al1-ce marked this conversation as resolved.
Show resolved Hide resolved
end
end)

after_install(function (target)
Expand Down Expand Up @@ -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")

Loading