diff options
| author | ruki <[email protected]> | 2021-02-20 10:25:49 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-02-20 10:25:49 +0800 |
| commit | 5f25a00cd3f3708ad133dbd1c376cfdd34a3de94 (patch) | |
| tree | 7815e0fce42dec2fb8a2b749959d0f50f0678e2b | |
| parent | bf08fd02abb79831f72598a9347e9a7b3ac87989 (diff) | |
add batchcmds
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 20 | ||||
| -rw-r--r-- | xmake/modules/private/utils/batchcmds.lua | 116 | ||||
| -rw-r--r-- | xmake/modules/private/utils/progress.lua | 10 | ||||
| -rw-r--r-- | xmake/plugins/project/clang/compile_commands.lua | 19 | ||||
| -rw-r--r-- | xmake/rules/protobuf/proto.lua | 19 | ||||
| -rw-r--r-- | xmake/rules/protobuf/xmake.lua | 12 |
6 files changed, 164 insertions, 32 deletions
diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 23cbcd3f5..76e61d1fb 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -22,9 +22,9 @@ import("core.base.option") import("core.project.rule") import("core.project.config") -import("core.project.depend") import("core.project.project") import("private.async.runjobs") +import("private.utils.batchcmds") -- add batch jobs for the custom rule function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix) @@ -66,12 +66,9 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix script = ruleinst:script(scriptname) if script then batchjobs:addjob("rule/" .. rulename .. "/" .. scriptname, function (index, total) - local cmds, dependopt = script(target, sourcebatch, {progress = (index * 100) / total}) - depend.on_changed(function () - for _, cmd in ipairs(cmds) do - os.vrunv(cmd[1], table.slice(cmd, 2)) - end - end, dependopt or {always_changed = true}) + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcebatch, {progress = (index * 100) / total}) + batchcmds_:run({dryrun = option.get("dry-run")}) end, rootjob) end end @@ -84,12 +81,9 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix local sourcekind = sourcebatch.sourcekind for _, sourcefile in ipairs(sourcebatch.sourcefiles) do batchjobs:addjob(sourcefile, function (index, total) - local cmds, dependopt = script(target, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) - depend.on_changed(function () - for _, cmd in ipairs(cmds) do - os.vrunv(cmd[1], table.slice(cmd, 2)) - end - end, dependopt or {always_changed = true}) + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) + batchcmds_:run({dryrun = option.get("dry-run")}) end, rootjob) end end diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua new file mode 100644 index 000000000..4b019e2ad --- /dev/null +++ b/xmake/modules/private/utils/batchcmds.lua @@ -0,0 +1,116 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file batchcmds.lua +-- + +-- imports +import("core.base.option") +import("core.base.object") +import("core.project.depend") + +-- define module +local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_TIPS"}} + +-- is empty? no commands +function batchcmds:empty() + return #self:cmds() == 0 +end + +-- get commands +function batchcmds:cmds() + return self._CMDS +end + +-- add command +function batchcmds:add_cmd(program, argv, opt) + table.insert(self:cmds(), {program = program, argv = argv, runopt = opt}) +end + +-- get deps +function batchcmds:deps() + return self._DEPS +end + +-- add dependent files +function batchcmds:add_depfiles(...) + local deps = self._DEPS or {} + deps.files = deps.files or {} + table.join2(deps.files, ...) + self._DEPS = deps +end + +-- add dependent values +function batchcmds:add_depvalues(...) + local deps = self._DEPS or {} + deps.values = deps.values or {} + table.join2(deps.values, ...) + self._DEPS = deps +end + +-- set the last mtime of dependent files and values +function batchcmds:set_depmtime(lastmtime) + local deps = self._DEPS or {} + deps.lastmtime = lastmtime + self._DEPS = deps +end + +-- set cache file of depend info +function batchcmds:set_depcache(cachefile) + local deps = self._DEPS or {} + deps.dependfile = cachefile + self._DEPS = deps +end + +-- run cmds +function batchcmds:run(opt) + opt = opt or {} + if self:empty() then + return + end + local function _runcmds() + for _, cmd in ipairs(self:cmds()) do + local tips = cmd.runopt and cmd.runopt.tips + if tips then + for _, tip in ipairs(tips) do + cprint(tip) + end + end + if opt.dryrun then + vprint(os.args(table.join(cmd.program, cmd.argv))) + else + os.vrunv(cmd.program, cmd.argv, cmd.runopt) + end + end + end + if self:deps() then + depend.on_changed(function () + _runcmds() + end, self:deps()) + else + _runcmds() + end +end + +-- new a batch commands for rule/xx_xxcmd_xxx() +-- +-- @params opt options, e.g. {target = ..} +-- +function new(opt) + opt = opt or {} + return batchcmds {_TARGET = opt.target, _CMDS = {}} +end diff --git a/xmake/modules/private/utils/progress.lua b/xmake/modules/private/utils/progress.lua index e00b4ab57..5c08cbf8f 100644 --- a/xmake/modules/private/utils/progress.lua +++ b/xmake/modules/private/utils/progress.lua @@ -111,6 +111,16 @@ function show(progress, format, ...) end end +-- get the message text with process +function text(progress, format, ...) + local progress_prefix = "${color.build.progress}" .. theme.get("text.build.progress_format") .. ":${clear} " + if option.get("verbose") then + return string.format(progress_prefix .. "${dim}" .. format, progress, ...) + else + return string.format(progress_prefix .. format, progress, ...) + end +end + -- build a progress indicator -- @params stream - stream to write to, will use io.stdout if not provided -- @params opt - options diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 48cd0ed96..1d3f02b6d 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -23,6 +23,7 @@ import("core.tool.compiler") import("core.project.rule") import("core.project.project") import("core.language.language") +import("private.utils.batchcmds") -- escape path function _escape_path(p) @@ -97,9 +98,12 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) local scriptname = "buildcmd_files" .. (suffix and ("_" .. suffix) or "") local script = ruleinst:script(scriptname) if script then - local cmds = script(target, sourcebatch) - for _, cmd in ipairs(cmds) do - _make_arguments(jsonfile, cmd) + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcebatch, {}) + if not batchcmds_:empty() then + for _, cmd in ipairs(batchcmds_:cmds()) do + _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + end end end @@ -110,9 +114,12 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) if script then local sourcekind = sourcebatch.sourcekind for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - local cmds = script(target, sourcefile) - for _, cmd in ipairs(cmds) do - _make_arguments(jsonfile, cmd) + local batchcmds_ = batchcmds.new({target = target}) + script(target, batchcmds_, sourcefile, {}) + if not batchcmds_:empty() then + for _, cmd in ipairs(batchcmds_:cmds()) do + _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + end end end end diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua index 41227c95e..f449e0ebc 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -53,7 +53,7 @@ function _get_protoc(target, sourcekind) end -- generate build commands -function buildcmd(target, sourcekind, sourcefile_proto, opt) +function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) -- get protoc local protoc = _get_protoc(target, sourcekind) @@ -102,15 +102,20 @@ function buildcmd(target, sourcekind, sourcefile_proto, opt) end table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir) - -- 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 + -- add commands + local progress_text = opt.progress and progress.text(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) + batchcmds:add_cmd(protoc, argv, {tips = progress_text}) + batchcmds:add_cmd(compinst:compargv(sourcefile_cx, objectfile, {compflags = compflags})) + + -- 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, sourcekind, sourcefile_proto, opt) +function build(target, sourcefile_proto, opt, sourcekind) -- get protoc local protoc = _get_protoc(target, sourcekind) diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index 7fae3bc86..b9d0bafdb 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -26,12 +26,12 @@ rule("protobuf.cpp") -- build protobuf file before_build_file(function (target, sourcefile_proto, opt) - import("proto").build(target, "cxx", sourcefile_proto, opt) + import("proto").build(target, sourcefile_proto, opt, "cxx") end) -- generate build commands - before_buildcmd_file(function (target, sourcefile_proto, opt) - return import("proto").buildcmd(target, "cxx", sourcefile_proto, opt) + before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) + return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx") end) @@ -43,10 +43,10 @@ rule("protobuf.c") -- build protobuf file before_build_file(function (target, sourcefile_proto, opt) - import("proto").build(target, "cc", sourcefile_proto, opt) + import("proto").build(target, sourcefile_proto, opt, "cc") end) -- generate build commands - before_buildcmd_file(function (target, sourcefile_proto, opt) - return import("proto").buildcmd(target, "cc", sourcefile_proto, opt) + before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) + return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc") end) |
