summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-20 00:34:14 +0800
committerruki <[email protected]>2021-02-20 00:34:14 +0800
commit032eeb310a9b82ba0798422de376765f875eb653 (patch)
tree5a054de0937e84ab833e43c822d3322a14581285
parentbce29072a6fe078db76cf97c33a1ac2dc032d738 (diff)
add buildcmd for protobuf
-rw-r--r--xmake/core/project/rule.lua9
-rw-r--r--xmake/rules/protobuf/proto.lua119
-rw-r--r--xmake/rules/protobuf/xmake.lua14
3 files changed, 112 insertions, 30 deletions
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index 53c1a9854..81ef99148 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -173,6 +173,9 @@ function rule.apis()
, "rule.on_package"
, "rule.on_install"
, "rule.on_uninstall"
+ , "rule.on_buildcmd"
+ , "rule.on_buildcmd_file"
+ , "rule.on_buildcmd_files"
-- rule.before_xxx
, "rule.before_run"
, "rule.before_load"
@@ -184,6 +187,9 @@ function rule.apis()
, "rule.before_package"
, "rule.before_install"
, "rule.before_uninstall"
+ , "rule.before_buildcmd"
+ , "rule.before_buildcmd_file"
+ , "rule.before_buildcmd_files"
-- rule.after_xxx
, "rule.after_run"
, "rule.after_load"
@@ -195,6 +201,9 @@ function rule.apis()
, "rule.after_package"
, "rule.after_install"
, "rule.after_uninstall"
+ , "rule.after_buildcmd"
+ , "rule.after_buildcmd_file"
+ , "rule.after_buildcmd_files"
}
}
end
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua
index f899e91c5..41227c95e 100644
--- a/xmake/rules/protobuf/proto.lua
+++ b/xmake/rules/protobuf/proto.lua
@@ -27,8 +27,8 @@ import("core.tool.compiler")
import("lib.detect.find_tool")
import("private.utils.progress")
--- build protobuf file
-function main(target, sourcekind, sourcefile_proto, opt)
+-- get protoc
+function _get_protoc(target, sourcekind)
-- find protoc
local protoc = target:data("protobuf.protoc")
@@ -49,7 +49,14 @@ function main(target, sourcekind, sourcefile_proto, opt)
end
-- get protoc
- protoc = assert(target:data(sourcekind == "cxx" and "protobuf.protoc" or "protobuf.protoc-c"), "protoc not found!")
+ return assert(target:data(sourcekind == "cxx" and "protobuf.protoc" or "protobuf.protoc-c"), "protoc not found!")
+end
+
+-- generate build commands
+function buildcmd(target, sourcekind, sourcefile_proto, opt)
+
+ -- get protoc
+ local protoc = _get_protoc(target, sourcekind)
-- get c/c++ source file for protobuf
local prefixdir
@@ -81,19 +88,6 @@ function main(target, sourcekind, sourcefile_proto, opt)
-- add objectfile
table.insert(target:objectfiles(), objectfile)
- -- load dependent info
- local dependfile = target:dependfile(objectfile)
- local dependinfo = option.get("rebuild") and {} or (depend.load(dependfile) or {})
-
- -- need build this object?
- local depvalues = {compinst:program(), compflags}
- if not depend.is_changed(dependinfo, {lastmtime = os.mtime(objectfile), values = depvalues}) then
- return
- end
-
- -- 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)
@@ -108,21 +102,90 @@ function main(target, sourcekind, sourcefile_proto, opt)
end
table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir)
- -- do compile
- os.vrunv(protoc, argv)
+ -- generate commands
+ local cmds = {}
+ table.insert(cmds, table.join(protoc, argv))
+ table.insert(cmds, table.join(compinst:compargv(sourcefile_cx, objectfile, {compflags = compflags})))
+ return cmds
+end
+
+-- build protobuf file
+function build(target, sourcekind, sourcefile_proto, opt)
+
+ -- 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})
- -- trace
- if option.get("verbose") then
- print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags}))
+ -- 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)
- -- compile c/c++ source file for protobuf
- dependinfo.files = {}
- assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags}))
+ -- 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
- -- update files and values to the dependent file
- dependinfo.values = depvalues
- table.insert(dependinfo.files, sourcefile_proto)
- depend.save(dependinfo, dependfile)
+ 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 1abaa2447..7fae3bc86 100644
--- a/xmake/rules/protobuf/xmake.lua
+++ b/xmake/rules/protobuf/xmake.lua
@@ -26,7 +26,12 @@ rule("protobuf.cpp")
-- build protobuf file
before_build_file(function (target, sourcefile_proto, opt)
- import("proto")(target, "cxx", sourcefile_proto, opt)
+ import("proto").build(target, "cxx", sourcefile_proto, opt)
+ end)
+
+ -- generate build commands
+ before_buildcmd_file(function (target, sourcefile_proto, opt)
+ return import("proto").buildcmd(target, "cxx", sourcefile_proto, opt)
end)
@@ -38,5 +43,10 @@ rule("protobuf.c")
-- build protobuf file
before_build_file(function (target, sourcefile_proto, opt)
- import("proto")(target, "cc", sourcefile_proto, opt)
+ import("proto").build(target, "cc", sourcefile_proto, opt)
+ end)
+
+ -- generate build commands
+ before_buildcmd_file(function (target, sourcefile_proto, opt)
+ return import("proto").buildcmd(target, "cc", sourcefile_proto, opt)
end)