diff options
| author | Chi Huu Huynh <[email protected]> | 2023-11-14 09:18:11 +0000 |
|---|---|---|
| committer | Chi Huu Huynh <[email protected]> | 2023-11-14 09:18:11 +0000 |
| commit | 575e84175d3233d7172596eb2878378228f11c12 (patch) | |
| tree | c4581182775d91b4da2e6c6084fb28e5beb34fc6 | |
| parent | f205de61c97bad8457838272b33c37d876063851 (diff) | |
split protobuf build process into two parts
| -rw-r--r-- | xmake/rules/protobuf/proto.lua | 82 | ||||
| -rw-r--r-- | xmake/rules/protobuf/xmake.lua | 20 |
2 files changed, 79 insertions, 23 deletions
diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua index 1124899c0..0dbd2460c 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -90,7 +90,7 @@ function load(target, sourcekind) end -- generate build commands -function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) +function build_proto_cmd(target, batchcmds, sourcefile_proto, opt, sourcekind) -- get protoc local protoc = _get_protoc(target, sourcekind) @@ -126,16 +126,6 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) -- add includedirs target:add("includedirs", sourcefile_dir, {public = public}) - -- add objectfile - local objectfile = target:objectfile(sourcefile_cx) - table.insert(target:objectfiles(), objectfile) - - local objectfile_grpc - if grpc_cpp_plugin then - objectfile_grpc = target:objectfile(sourcefile_cx_grpc) - table.insert(target:objectfiles(), objectfile_grpc) - end - local protoc_args = { path(sourcefile_proto), path(prefixdir and prefixdir or path.directory(sourcefile_proto), function (p) return "-I" .. p end), @@ -150,8 +140,52 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) -- add commands batchcmds:mkdir(sourcefile_dir) - batchcmds:show_progress(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.proto to %s %s", (sourcekind == "cxx" and "cpp" or "c"), sourcefile_proto) batchcmds:vrunv(protoc, protoc_args) +end + +-- generate build commands +function build_c_cmd(target, batchcmds, sourcefile_proto, opt, sourcekind) + -- get protoc + local protoc = _get_protoc(target, sourcekind) + + -- get c/c++ source file for protobuf + local prefixdir + local autogendir + local public + local grpc_cpp_plugin + local fileconfig = target:fileconfig(sourcefile_proto) + if fileconfig then + public = fileconfig.proto_public + prefixdir = fileconfig.proto_rootdir + -- custom autogen directory to access the generated header files + -- @see https://github.com/xmake-io/xmake/issues/3678 + autogendir = fileconfig.proto_autogendir + grpc_cpp_plugin = fileconfig.proto_grpc_cpp_plugin + end + local rootdir = autogendir and autogendir or 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) + + local filename_grpc + local sourcefile_cx_grpc + if grpc_cpp_plugin then + filename_grpc = path.basename(sourcefile_proto) .. ".grpc.pb.cc" + sourcefile_cx_grpc = target:autogenfile(sourcefile_proto, {rootdir = rootdir, filename = filename_grpc}) + end + + -- add objectfile + local objectfile = target:objectfile(sourcefile_cx) + table.insert(target:objectfiles(), objectfile) + + local objectfile_grpc + if grpc_cpp_plugin then + objectfile_grpc = target:objectfile(sourcefile_cx_grpc) + table.insert(target:objectfiles(), objectfile_grpc) + end + + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.proto %s file %s", (sourcekind == "cxx" and "cpp" or "c"), sourcefile_proto) batchcmds:compile(sourcefile_cx, objectfile, {configs = {includedirs = sourcefile_dir}}) if grpc_cpp_plugin then batchcmds:compile(sourcefile_cx_grpc, objectfile_grpc, {configs = {includedirs = sourcefile_dir}}) @@ -169,7 +203,7 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) end -- build batch jobs -function build_batchjobs(target, batchjobs, sourcebatch, opt, sourcekind) +function build_sourcefiles_proto(target, batchjobs, sourcebatch, opt, sourcekind) -- get the root directory of protobuf local proto_rootdir @@ -185,21 +219,31 @@ function build_batchjobs(target, batchjobs, sourcebatch, opt, sourcekind) opt = opt or {} local moduledeps, moduledeps_files = module_parser.load(target, sourcebatch, table.join(opt, {proto_rootdir = proto_rootdir})) - -- generate jobs local sourcefiles_total = #sourcebatch.sourcefiles + -- generate jobs for i = 1, sourcefiles_total do local sourcefile = sourcebatch.sourcefiles[i] local moduleinfo = moduledeps_files[sourcefile] or {} - - -- make build job moduleinfo.job = batchjobs:newjob(sourcefile, function (index, total) + -- make build job local batchcmds_ = batchcmds.new({target = target}) - buildcmd(target, batchcmds_, sourcefile, {progress = (index * 100) / total}, sourcekind) + build_proto_cmd(target, batchcmds_, sourcefile, {progress = (index * 100) / total}, sourcekind) batchcmds_:runcmds({changed = target:is_rebuilt(), dryrun = option.get("dry-run")}) end) end - - -- build batchjobs module_parser.build_batchjobs(moduledeps, batchjobs, opt.rootjob) end +function build_compiled_sourcefiles_proto(target, batchjobs, sourcebatch, opt, sourcekind) + opt = opt or {} + local sourcefiles_total = #sourcebatch.sourcefiles + -- generate jobs + for i = 1, sourcefiles_total do + local sourcefile = sourcebatch.sourcefiles[i] + batchjobs:newjob(sourcefile, function (index, total) + local batchcmds_ = batchcmds.new({target = target}) + build_c_cmd(target, batchcmds_, sourcefile, {progress = (index * 100) / total}, sourcekind) + batchcmds_:runcmds({changed = target:is_rebuilt(), dryrun = option.get("dry-run")}) + end) + end +end
\ No newline at end of file diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index c7fa4d3cb..d3fef18cc 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -26,10 +26,16 @@ rule("protobuf.cpp") import("proto").load(target, "cxx") end) before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) - import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx") + import("proto").build_proto_cmd(target, batchcmds, sourcefile_proto, opt, "cxx") + end) + on_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) + import("proto").build_c_cmd(target, batchcmds, sourcefile_proto, opt, "cxx") end) before_build_files(function (target, batchjobs, sourcebatch, opt) - import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cxx") + import("proto").build_sourcefiles_proto(target, batchjobs, sourcebatch, opt, "cxx") + end, {batch = true}) + on_build_files(function (target, batchjobs, sourcebatch, opt) + import("proto").build_compiled_sourcefiles_proto(target, batchjobs, sourcebatch, opt, "cxx") end, {batch = true}) @@ -41,10 +47,16 @@ rule("protobuf.c") import("proto").load(target, "cc") end) before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) - import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc") + import("proto").build_proto_cmd(target, batchcmds, sourcefile_proto, opt, "cc") + end) + on_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) + import("proto").build_c_cmd(target, batchcmds, sourcefile_proto, opt, "cc") end) before_build_files(function (target, batchjobs, sourcebatch, opt) - import("proto").build_batchjobs(target, batchjobs, sourcebatch, opt, "cc") + import("proto").build_sourcefiles_proto(target, batchjobs, sourcebatch, opt, "cc") + end, {batch = true}) + on_build_files(function (target, batchjobs, sourcebatch, opt) + import("proto").build_compiled_sourcefiles_proto(target, batchjobs, sourcebatch, opt, "cxx") end, {batch = true}) |
