Skip to content

Commit

Permalink
after_load objectfiles for capnproto as described in #5426 and implem…
Browse files Browse the repository at this point in the history
…ented in #5437
  • Loading branch information
chriku committed Jan 31, 2025
1 parent db69821 commit 6fbf6c4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
45 changes: 39 additions & 6 deletions xmake/rules/capnproto/capnp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,45 @@ function _get_capnp(target)
return assert(target:data("capnproto.capnp"), "capnp not found!")
end

-- generate build commands
function load(target)
print(target:sourcebatches())
-- get the first sourcefile
local sourcefile_capnp
local sourcebatch = target:sourcebatches()["capnproto.cpp"]
if sourcebatch and sourcebatch.sourcefiles then
sourcefile_capnp = sourcebatch.sourcefiles[1]
end
if not sourcefile_capnp then
return
end

-- get c/c++ source file for capnproto
local prefixdir
local public
local fileconfig = target:fileconfig(sourcefile_capnp)
if fileconfig then
public = fileconfig.capnp_public
prefixdir = fileconfig.capnp_rootdir
end
local rootdir = path.join(target:autogendir(), "rules", "capnproto")
local filename = path.basename(sourcefile_capnp) .. ".capnp.c++"
local sourcefile_cx = target:autogenfile(sourcefile_capnp, {rootdir = rootdir, filename = filename})
local sourcefile_dir = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx)

print("ADDINC",sourcefile_dir)

-- add includedirs
target:add("includedirs", sourcefile_dir, {public = true})

-- add objectfile, @see https://github.com/xmake-io/xmake/issues/5426
local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)
end

-- generate build commands
function buildcmd(target, batchcmds, sourcefile_capnp, opt)
print("CAPNPROTO buildcmd",sourcefile_capnp)

-- get capnp
local capnp = _get_capnp(target)
Expand All @@ -56,12 +93,8 @@ function buildcmd(target, batchcmds, sourcefile_capnp, opt)
local sourcefile_cx = target:autogenfile(sourcefile_capnp, {rootdir = rootdir, filename = filename})
local sourcefile_dir = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx)

-- add includedirs
target:add("includedirs", sourcefile_dir, {public = public})

-- add objectfile
local objectfile = target:objectfile(sourcefile_cx)
table.insert(target:objectfiles(), objectfile)

-- add commands
batchcmds:mkdir(sourcefile_dir)
Expand All @@ -77,10 +110,10 @@ function buildcmd(target, batchcmds, sourcefile_capnp, opt)
table.insert(argv, path(prefixdir, function (p) return "--src-prefix=" .. p end))
end
table.insert(argv, "-o")
table.insert(argv, path(sourcefile_dir, function (p) return "c++:" .. p end))
table.insert(argv, path(rootdir, function (p) return "c++:" .. p end))
table.insert(argv, path(sourcefile_capnp))
batchcmds:vrunv(capnp, argv)
local configs = {includedirs = sourcefile_dir, languages = "c++14"}
local configs = {includedirs = sourcefile_dir, languages = (fileconfig and fileconfig.cpp_version) or "c++14"}
if target:is_plat("windows") then
configs.cxflags = "/TP"
end
Expand Down
4 changes: 4 additions & 0 deletions xmake/rules/capnproto/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

-- define rule: capnproto.cpp
rule("capnproto.cpp")
add_deps("c++")
set_extensions(".capnp")
after_load(function (target)
return import("capnp").load(target)
end)
before_buildcmd_file(function (target, batchcmds, sourcefile_capnp, opt)
return import("capnp").buildcmd(target, batchcmds, sourcefile_capnp, opt)
end)

0 comments on commit 6fbf6c4

Please sign in to comment.