diff options
| author | ruki <[email protected]> | 2021-02-20 22:25:41 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-02-20 22:25:41 +0800 |
| commit | 19a099e0c97419c30d2991665fdd442d1b5f8f01 (patch) | |
| tree | d2e3c589cad6623271fe732e2453a1465362d840 | |
| parent | b241052a21eeab56cae0a822f29ef2ac73d20926 (diff) | |
improve batchcmds
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/utils/batchcmds.lua | 117 | ||||
| -rw-r--r-- | xmake/rules/protobuf/proto.lua | 16 |
3 files changed, 89 insertions, 48 deletions
diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 76e61d1fb..866c9680c 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -68,7 +68,7 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix batchjobs:addjob("rule/" .. rulename .. "/" .. scriptname, function (index, total) local batchcmds_ = batchcmds.new({target = target}) script(target, batchcmds_, sourcebatch, {progress = (index * 100) / total}) - batchcmds_:run({dryrun = option.get("dry-run")}) + batchcmds_:runcmds({dryrun = option.get("dry-run")}) end, rootjob) end end @@ -83,7 +83,7 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix batchjobs:addjob(sourcefile, function (index, total) local batchcmds_ = batchcmds.new({target = target}) script(target, batchcmds_, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) - batchcmds_:run({dryrun = option.get("dry-run")}) + batchcmds_:runcmds({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 index 617385508..095e5bc28 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -26,15 +26,16 @@ import("core.base.colors") import("core.project.depend") import("core.theme.theme") import("core.tool.compiler") +import("core.language.language") import("private.utils.progress", {alias = "progress_utils"}) -- define module local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_tip"}} --- show the tip message -local function _showtip(tip, progress) +-- show text +function _show(showtext, progress) if option.get("verbose") then - cprint(tip) + cprint(showtext) else local is_scroll = _g.is_scroll if is_scroll == nil then @@ -42,10 +43,10 @@ local function _showtip(tip, progress) _g.is_scroll = is_scroll end if is_scroll then - cprint(tip) + cprint(showtext) else tty.erase_line_to_start().cr() - local msg = tip + local msg = showtext local msg_plain = colors.translate(msg, {plain = true}) local maxwidth = os.getwinsize().width if #msg_plain <= maxwidth then @@ -68,23 +69,52 @@ local function _showtip(tip, progress) end end --- run the given commands -local function _runcmds(cmds, opt) - for _, cmd in ipairs(cmds) do - local tip = cmd.tip - if tip then - _showtip(tip, cmd.progress) - end - if cmd.program then - if opt.dryrun then - vprint(os.args(table.join(cmd.program, cmd.argv))) - else - os.vrunv(cmd.program, cmd.argv, cmd.runopt) - end +-- run command: show +function _runcmd_show(cmd, opt) + local showtext = cmd.showtext + if showtext then + _show(showtext, cmd.progress) + end +end + +-- run command: os.vrunv +function _runcmd_vrunv(cmd, opt) + if cmd.program then + 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 +-- run command: os.mkdir +function _runcmd_mkdir(cmd, opt) + local dir = cmd.dir + if not os.isdir(dir) then + os.mkdir(dir) + end +end + +-- run command +function _runcmd(cmd, opt) + local kind = cmd.kind + if kind == "show" then + _runcmd_show(cmd, opt) + elseif kind == "vrunv" then + _runcmd_vrunv(cmd, opt) + elseif kind == "vrunv" then + _runcmd_mkdir(cmd, opt) + end +end + +-- run commands +function _runcmds(cmds, opt) + for _, cmd in ipairs(cmds) do + _runcmd(cmd, opt) + end +end + -- is empty? no commands function batchcmds:empty() return #self:cmds() == 0 @@ -95,31 +125,48 @@ 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}) +-- add command: os.vrunv +function batchcmds:vrunv(program, argv, opt) + table.insert(self:cmds(), {kind = "vrunv", program = program, argv = argv, runopt = opt}) self:add_depvalues(program, argv) end --- add compilation command -function batchcmds:add_compcmd(sourcefiles, objectfile, opt) +-- add command: compiler.compile +function batchcmds:compile(sourcefiles, objectfile, opt) + + -- bind target if exists 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}) + opt.target = self._TARGET + + -- load compiler and get compilation command + local sourcekind = opt.sourcekind + if not sourcekind and type(sourcefiles) == "string" then + sourcekind = language.sourcekind_of(sourcefiles) + end + local compiler_inst = compiler.load(sourcekind, opt) + local program, argv = compiler_inst:compargv(sourcefiles, objectfile, opt) + + -- add compilation command and bind run environments of compiler + self:mkdir(path.directory(objectfile)) + self:vrunv(program, argv, {envs = table.join(compiler_inst:runenvs(), opt.envs)}) +end + +-- add command: os.mkdir +function batchcmds:mkdir(dir) + table.insert(self:cmds(), {kind = "mkdir", dir = dir}) end --- add command tip -function batchcmds:add_tip(format, ...) - local tip = string.format(format, ...) - table.insert(self:cmds(), {tip = tip}) +-- add command: show +function batchcmds:show(format, ...) + local showtext = string.format(format, ...) + table.insert(self:cmds(), {kind = "show", showtext = showtext}) end --- add command tip with progress -function batchcmds:add_progress_tip(progress, format, ...) +-- add command: show progress +function batchcmds:show_progress(progress, format, ...) if progress then - local tip = progress_utils.text(progress, format, ...) - table.insert(self:cmds(), {tip = tip, progress = progress}) + local showtext = progress_utils.text(progress, format, ...) + table.insert(self:cmds(), {kind = "show", showtext = showtext, progress = progress}) end end @@ -159,7 +206,7 @@ function batchcmds:set_depcache(cachefile) end -- run cmds -function batchcmds:run(opt) +function batchcmds:runcmds(opt) opt = opt or {} if self:empty() then return diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua index ffb49148b..83706adab 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -72,23 +72,17 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) -- add includedirs target:add("includedirs", sourcefile_dir) - -- get object file - local objectfile = target:objectfile(sourcefile_cx) - -- add objectfile + local objectfile = target:objectfile(sourcefile_cx) table.insert(target:objectfiles(), objectfile) - -- ensure the source file directory - if not os.isdir(sourcefile_dir) then - os.mkdir(sourcefile_dir) - end - -- add commands - batchcmds:add_progress_tip(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) - batchcmds:add_cmd(protoc, {sourcefile_proto, + batchcmds:mkdir(sourcefile_dir) + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.proto %s", sourcefile_proto) + batchcmds:vrunv(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")}}) + batchcmds:compile(sourcefile_cx, objectfile, {configs = {includedirs = sourcefile_dir, languages = (sourcekind == "cxx" and "c++11")}}) -- add deps batchcmds:add_depfiles(sourcefile_proto) |
