summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-20 18:01:39 +0800
committerruki <[email protected]>2021-02-20 18:01:39 +0800
commit266f4f7150a4def0c4fc2540ceb1fbf944cbd7ea (patch)
treebc972e25c9a7158f91c5c35049d147d5ba9bf13b
parent84d80e6642acda39989afc89992a39f371dc3ece (diff)
improve protobuf rule
-rw-r--r--xmake/modules/private/utils/batchcmds.lua13
-rw-r--r--xmake/rules/protobuf/proto.lua106
-rw-r--r--xmake/rules/protobuf/xmake.lua10
3 files changed, 16 insertions, 113 deletions
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua
index f4f5b84a8..617385508 100644
--- a/xmake/modules/private/utils/batchcmds.lua
+++ b/xmake/modules/private/utils/batchcmds.lua
@@ -25,6 +25,7 @@ import("core.base.tty")
import("core.base.colors")
import("core.project.depend")
import("core.theme.theme")
+import("core.tool.compiler")
import("private.utils.progress", {alias = "progress_utils"})
-- define module
@@ -97,6 +98,15 @@ end
-- add command
function batchcmds:add_cmd(program, argv, opt)
table.insert(self:cmds(), {program = program, argv = argv, runopt = opt})
+ self:add_depvalues(program, argv)
+end
+
+-- add compilation command
+function batchcmds:add_compcmd(sourcefiles, objectfile, opt)
+ opt = opt or {}
+ opt.target = self._TARGET -- bind target if exists
+ local program, argv = compiler.compargv(sourcefiles, objectfile, opt)
+ self:add_cmd(program, argv, {envs = opt.envs})
end
-- add command tip
@@ -154,7 +164,8 @@ function batchcmds:run(opt)
if self:empty() then
return
end
- if self:deps() then
+ local deps = self:deps()
+ if deps and deps.files then
depend.on_changed(function ()
_runcmds(self:cmds(), opt)
end, self:deps())
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index faa0cd2ee..ffb49148b 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -75,16 +75,6 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
-- get object file
local objectfile = target:objectfile(sourcefile_cx)
- -- load compiler
- local compinst = compiler.load(sourcekind, {target = target})
-
- -- get compile flags
- local configs = {includedirs = sourcefile_dir}
- if sourcekind == "cxx" then
- configs.languages = "c++11"
- end
- local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx, configs = configs})
-
-- add objectfile
table.insert(target:objectfiles(), objectfile)
@@ -93,104 +83,16 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind)
os.mkdir(sourcefile_dir)
end
- -- get compilation flags
- local argv = {sourcefile_proto}
- if prefixdir then
- table.insert(argv, "-I" .. prefixdir)
- else
- table.insert(argv, "-I" .. path.directory(sourcefile_proto))
- end
- table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir)
-
-- add commands
batchcmds:add_progress_tip(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto)
- batchcmds:add_cmd(protoc, argv)
- batchcmds:add_cmd(compinst:compargv(sourcefile_cx, objectfile, {compflags = compflags}))
+ batchcmds:add_cmd(protoc, {sourcefile_proto,
+ "-I" .. (prefixdir and prefixdir or path.directory(sourcefile_proto)),
+ (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir})
+ batchcmds:add_compcmd(sourcefile_cx, objectfile, {configs = {includedirs = sourcefile_dir, languages = (sourcekind == "cxx" and "c++11")}})
-- add deps
batchcmds:add_depfiles(sourcefile_proto)
- batchcmds:add_depvalues(compinst:program(), compflags)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end
--- build protobuf file
-function build(target, sourcefile_proto, opt, sourcekind)
-
- -- get protoc
- local protoc = _get_protoc(target, sourcekind)
-
- -- get c/c++ source file for protobuf
- local prefixdir
- local fileconfig = target:fileconfig(sourcefile_proto)
- if fileconfig then
- prefixdir = fileconfig.proto_rootdir
- end
- local rootdir = path.join(target:autogendir(), "rules", "protobuf")
- local filename = path.basename(sourcefile_proto) .. ".pb" .. (sourcekind == "cxx" and ".cc" or "-c.c")
- local sourcefile_cx = target:autogenfile(sourcefile_proto, {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)
-
- -- get object file
- local objectfile = target:objectfile(sourcefile_cx)
-
- -- load compiler
- local compinst = compiler.load(sourcekind, {target = target})
-
- -- get compile flags
- local configs = {includedirs = sourcefile_dir}
- if sourcekind == "cxx" then
- configs.languages = "c++11"
- end
- local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx, configs = configs})
-
- -- add objectfile
- table.insert(target:objectfiles(), objectfile)
-
- -- need build this object?
- local dryrun = option.get("dry-run")
- local depvalues = {compinst:program(), compflags}
- depend.on_changed(function ()
-
- -- trace progress info
- progress.show(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto)
-
- -- ensure the source file directory
- if not os.isdir(sourcefile_dir) then
- os.mkdir(sourcefile_dir)
- end
-
- -- get compilation flags
- local argv = {sourcefile_proto}
- if prefixdir then
- table.insert(argv, "-I" .. prefixdir)
- else
- table.insert(argv, "-I" .. path.directory(sourcefile_proto))
- end
- table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir)
-
- -- do compile
- os.vrunv(protoc, argv)
-
- -- trace
- if option.get("verbose") then
- print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags}))
- end
-
- -- compile c/c++ source file for protobuf
- if not dryrun then
- local dependinfo = {files = {}}
- assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags}))
- return dependinfo
- end
-
- end, { dependfile = target:dependfile(objectfile),
- lastmtime = os.mtime(objectfile),
- values = depvalues,
- files = sourcefile_proto,
- always_changed = dryrun})
-end
-
diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua
index b9d0bafdb..76f114aea 100644
--- a/xmake/rules/protobuf/xmake.lua
+++ b/xmake/rules/protobuf/xmake.lua
@@ -24,11 +24,6 @@ rule("protobuf.cpp")
-- set extension
set_extensions(".proto")
- -- build protobuf file
- before_build_file(function (target, sourcefile_proto, opt)
- import("proto").build(target, sourcefile_proto, opt, "cxx")
- end)
-
-- generate build commands
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx")
@@ -41,11 +36,6 @@ rule("protobuf.c")
-- set extension
set_extensions(".proto")
- -- build protobuf file
- before_build_file(function (target, sourcefile_proto, opt)
- import("proto").build(target, sourcefile_proto, opt, "cc")
- end)
-
-- generate build commands
before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt)
return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc")