From 5f25a00cd3f3708ad133dbd1c376cfdd34a3de94 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 10:25:49 +0800 Subject: add batchcmds --- xmake/modules/private/utils/batchcmds.lua | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 xmake/modules/private/utils/batchcmds.lua (limited to 'xmake/modules/private/utils/batchcmds.lua') 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 -- cgit v1.3.1 From 3749765647847d0c0155f34dde8d1e32447b87b9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 11:14:26 +0800 Subject: add tip and progress tip --- xmake/modules/private/utils/batchcmds.lua | 93 +++++++++++++++++++----- xmake/plugins/project/clang/compile_commands.lua | 8 +- xmake/rules/protobuf/proto.lua | 4 +- xmake/rules/protobuf/xmake.lua | 6 +- 4 files changed, 87 insertions(+), 24 deletions(-) (limited to 'xmake/modules/private/utils/batchcmds.lua') diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 4b019e2ad..f4f5b84a8 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -21,10 +21,68 @@ -- imports import("core.base.option") import("core.base.object") +import("core.base.tty") +import("core.base.colors") import("core.project.depend") +import("core.theme.theme") +import("private.utils.progress", {alias = "progress_utils"}) -- define module -local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_TIPS"}} +local batchcmds = batchcmds or object { _init = {"_TARGET", "_CMDS", "_DEPS", "_tip"}} + +-- show the tip message +local function _showtip(tip, progress) + if option.get("verbose") then + cprint(tip) + else + local is_scroll = _g.is_scroll + if is_scroll == nil then + is_scroll = theme.get("text.build.progress_style") == "scroll" + _g.is_scroll = is_scroll + end + if is_scroll then + cprint(tip) + else + tty.erase_line_to_start().cr() + local msg = tip + local msg_plain = colors.translate(msg, {plain = true}) + local maxwidth = os.getwinsize().width + if #msg_plain <= maxwidth then + cprintf(msg) + else + -- windows width is too small? strip the partial message in middle + local partlen = math.floor(maxwidth / 2) - 3 + local sep = msg_plain:sub(partlen + 1, #msg_plain - partlen - 1) + local split = msg:split(sep, {plain = true, strict = true}) + cprintf(table.concat(split, "...")) + end + if math.floor(progress) == 100 then + print("") + _g.showing_without_scroll = false + else + _g.showing_without_scroll = true + end + io.flush() + end + 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 + end + end +end -- is empty? no commands function batchcmds:empty() @@ -41,6 +99,20 @@ function batchcmds:add_cmd(program, argv, opt) table.insert(self:cmds(), {program = program, argv = argv, runopt = opt}) end +-- add command tip +function batchcmds:add_tip(format, ...) + local tip = string.format(format, ...) + table.insert(self:cmds(), {tip = tip}) +end + +-- add command tip with progress +function batchcmds:add_progress_tip(progress, format, ...) + if progress then + local tip = progress_utils.text(progress, format, ...) + table.insert(self:cmds(), {tip = tip, progress = progress}) + end +end + -- get deps function batchcmds:deps() return self._DEPS @@ -82,27 +154,12 @@ function batchcmds:run(opt) 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() + _runcmds(self:cmds(), opt) end, self:deps()) else - _runcmds() + _runcmds(self:cmds(), opt) end end diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 1d3f02b6d..41d83bd57 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -102,7 +102,9 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) 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)) + if cmd.program then + _make_arguments(jsonfile, table.join(cmd.program, cmd.argv)) + end end end end @@ -118,7 +120,9 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) 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)) + if cmd.program then + _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 f449e0ebc..faa0cd2ee 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -103,8 +103,8 @@ function buildcmd(target, batchcmds, sourcefile_proto, opt, sourcekind) table.insert(argv, (sourcekind == "cxx" and "--cpp_out=" or "--c_out=") .. sourcefile_dir) -- 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_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})) -- add deps diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index b9d0bafdb..d812cc1a7 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -24,10 +24,11 @@ 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) + end)]] -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) @@ -41,10 +42,11 @@ 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) + end)]] -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) -- cgit v1.3.1 From 266f4f7150a4def0c4fc2540ceb1fbf944cbd7ea Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 18:01:39 +0800 Subject: improve protobuf rule --- xmake/modules/private/utils/batchcmds.lua | 13 +++- xmake/rules/protobuf/proto.lua | 106 ++---------------------------- xmake/rules/protobuf/xmake.lua | 10 --- 3 files changed, 16 insertions(+), 113 deletions(-) (limited to 'xmake/modules/private/utils/batchcmds.lua') 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") -- cgit v1.3.1 From 19a099e0c97419c30d2991665fdd442d1b5f8f01 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 22:25:41 +0800 Subject: improve batchcmds --- xmake/actions/build/kinds/object.lua | 4 +- xmake/modules/private/utils/batchcmds.lua | 117 +++++++++++++++++++++--------- xmake/rules/protobuf/proto.lua | 16 ++-- 3 files changed, 89 insertions(+), 48 deletions(-) (limited to 'xmake/modules/private/utils/batchcmds.lua') 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) -- cgit v1.3.1 From 3e95639d6b634f5ba4565636a8fd524ad5827939 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 22:46:38 +0800 Subject: add link cmd to batchcmds --- xmake/modules/private/utils/batchcmds.lua | 58 +++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'xmake/modules/private/utils/batchcmds.lua') diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 095e5bc28..21ed948a5 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.linker") import("core.tool.compiler") import("core.language.language") import("private.utils.progress", {alias = "progress_utils"}) @@ -77,6 +78,15 @@ function _runcmd_show(cmd, opt) end end +-- run command: os.runv +function _runcmd_runv(cmd, opt) + if cmd.program then + if not opt.dryrun then + os.runv(cmd.program, cmd.argv, cmd.runopt) + end + end +end + -- run command: os.vrunv function _runcmd_vrunv(cmd, opt) if cmd.program then @@ -88,10 +98,21 @@ function _runcmd_vrunv(cmd, opt) end end +-- run command: os.execv +function _runcmd_execv(cmd, opt) + if cmd.program then + if opt.dryrun then + print(os.args(table.join(cmd.program, cmd.argv))) + else + os.execv(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 + if not opt.dryrun and not os.isdir(dir) then os.mkdir(dir) end end @@ -101,9 +122,13 @@ function _runcmd(cmd, opt) local kind = cmd.kind if kind == "show" then _runcmd_show(cmd, opt) + elseif kind == "runv" then + _runcmd_runv(cmd, opt) elseif kind == "vrunv" then _runcmd_vrunv(cmd, opt) - elseif kind == "vrunv" then + elseif kind == "execv" then + _runcmd_execv(cmd, opt) + elseif kind == "mkdir" then _runcmd_mkdir(cmd, opt) end end @@ -125,12 +150,24 @@ function batchcmds:cmds() return self._CMDS end +-- add command: os.runv +function batchcmds:runv(program, argv, opt) + table.insert(self:cmds(), {kind = "runv", program = program, argv = argv, runopt = opt}) + self:add_depvalues(program, argv) +end + -- 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 command: os.execv +function batchcmds:execv(program, argv, opt) + table.insert(self:cmds(), {kind = "execv", program = program, argv = argv, runopt = opt}) + self:add_depvalues(program, argv) +end + -- add command: compiler.compile function batchcmds:compile(sourcefiles, objectfile, opt) @@ -151,6 +188,23 @@ function batchcmds:compile(sourcefiles, objectfile, opt) self:vrunv(program, argv, {envs = table.join(compiler_inst:runenvs(), opt.envs)}) end +-- add command: linker.link +function batchcmds:link(objectfiles, targetfile, opt) + + -- bind target if exists + local target = self._TARGET + opt = opt or {} + opt.target = target + + -- load linker and get link command + local linker_inst = target and target:linker() or linker.load(opt.targetkind, opt.sourcekinds, opt) + local program, argv = linker_inst:linkargv(objectfiles, targetfile, opt) + + -- add link command and bind run environments of linker + self:mkdir(path.directory(targetfile)) + self:vrunv(program, argv, {envs = table.join(linker_inst:runenvs(), opt.envs)}) +end + -- add command: os.mkdir function batchcmds:mkdir(dir) table.insert(self:cmds(), {kind = "mkdir", dir = dir}) -- cgit v1.3.1 From 84ae35cd1b165ea51d0b5997a1ca23edb17f58b2 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 22:57:19 +0800 Subject: add more batch cmds --- xmake/modules/private/utils/batchcmds.lua | 90 +++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 16 deletions(-) (limited to 'xmake/modules/private/utils/batchcmds.lua') diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 21ed948a5..371735321 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -82,7 +82,7 @@ end function _runcmd_runv(cmd, opt) if cmd.program then if not opt.dryrun then - os.runv(cmd.program, cmd.argv, cmd.runopt) + os.runv(cmd.program, cmd.argv, cmd.opt) end end end @@ -93,7 +93,7 @@ function _runcmd_vrunv(cmd, opt) if opt.dryrun then vprint(os.args(table.join(cmd.program, cmd.argv))) else - os.vrunv(cmd.program, cmd.argv, cmd.runopt) + os.vrunv(cmd.program, cmd.argv, cmd.opt) end end end @@ -104,7 +104,7 @@ function _runcmd_execv(cmd, opt) if opt.dryrun then print(os.args(table.join(cmd.program, cmd.argv))) else - os.execv(cmd.program, cmd.argv, cmd.runopt) + os.execv(cmd.program, cmd.argv, cmd.opt) end end end @@ -117,19 +117,57 @@ function _runcmd_mkdir(cmd, opt) end end +-- run command: os.rm +function _runcmd_rm(cmd, opt) + local filepath = cmd.filepath + if not opt.dryrun then + os.tryrm(filepath) + end +end + +-- run command: os.cp +function _runcmd_cp(cmd, opt) + if not opt.dryrun then + os.cp(opt.srcpath, opt.dstpath, opt.opt) + end +end + +-- run command: os.mv +function _runcmd_mv(cmd, opt) + if not opt.dryrun then + os.mv(opt.srcpath, opt.dstpath, opt.opt) + end +end + +-- run command: os.ln +function _runcmd_ln(cmd, opt) + if not opt.dryrun then + os.ln(opt.srcpath, opt.dstpath, opt.opt) + end +end + -- run command function _runcmd(cmd, opt) local kind = cmd.kind - if kind == "show" then - _runcmd_show(cmd, opt) - elseif kind == "runv" then - _runcmd_runv(cmd, opt) - elseif kind == "vrunv" then - _runcmd_vrunv(cmd, opt) - elseif kind == "execv" then - _runcmd_execv(cmd, opt) - elseif kind == "mkdir" then - _runcmd_mkdir(cmd, opt) + local maps = _g.maps + if not maps then + maps = + { + show = _runcmd_show, + runv = _runcmd_runv, + vrunv = _runcmd_vrunv, + execv = _runcmd_execv, + mkdir = _runcmd_mkdir, + rm = _runcmd_rm, + cp = _runcmd_cp, + mv = _runcmd_mv, + ln = _runcmd_ln + } + _g.maps = maps + end + local script = maps[kind] + if script then + script(cmd, opt) end end @@ -152,19 +190,19 @@ end -- add command: os.runv function batchcmds:runv(program, argv, opt) - table.insert(self:cmds(), {kind = "runv", program = program, argv = argv, runopt = opt}) + table.insert(self:cmds(), {kind = "runv", program = program, argv = argv, opt = opt}) self:add_depvalues(program, argv) end -- add command: os.vrunv function batchcmds:vrunv(program, argv, opt) - table.insert(self:cmds(), {kind = "vrunv", program = program, argv = argv, runopt = opt}) + table.insert(self:cmds(), {kind = "vrunv", program = program, argv = argv, opt = opt}) self:add_depvalues(program, argv) end -- add command: os.execv function batchcmds:execv(program, argv, opt) - table.insert(self:cmds(), {kind = "execv", program = program, argv = argv, runopt = opt}) + table.insert(self:cmds(), {kind = "execv", program = program, argv = argv, opt = opt}) self:add_depvalues(program, argv) end @@ -210,6 +248,26 @@ function batchcmds:mkdir(dir) table.insert(self:cmds(), {kind = "mkdir", dir = dir}) end +-- add command: os.rm +function batchcmds:rm(filepath) + table.insert(self:cmds(), {kind = "rm", filepath = filepath}) +end + +-- add command: os.cp +function batchcmds:cp(srcpath, dstpath, opt) + table.insert(self:cmds(), {kind = "cp", srcpath = srcpath, dstpath = dstpath, opt = opt}) +end + +-- add command: os.mv +function batchcmds:mv(srcpath, dstpath, opt) + table.insert(self:cmds(), {kind = "mv", srcpath = srcpath, dstpath = dstpath, opt = opt}) +end + +-- add command: os.ln +function batchcmds:ln(srcpath, dstpath, opt) + table.insert(self:cmds(), {kind = "ln", srcpath = srcpath, dstpath = dstpath, opt = opt}) +end + -- add command: show function batchcmds:show(format, ...) local showtext = string.format(format, ...) -- cgit v1.3.1