From 032eeb310a9b82ba0798422de376765f875eb653 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 00:34:14 +0800 Subject: add buildcmd for protobuf --- xmake/core/project/rule.lua | 9 ++++ xmake/rules/protobuf/proto.lua | 119 +++++++++++++++++++++++++++++++---------- xmake/rules/protobuf/xmake.lua | 14 ++++- 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) -- cgit v1.3.1 From 6b4a46cfb23bae8f32594ca72223326f9821d849 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 00:37:00 +0800 Subject: add xx_buildcmd_files for build/objects --- xmake/actions/build/kinds/object.lua | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index baf2d2f11..98141fbf9 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -32,7 +32,7 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!") local ruleinst = assert(project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) - -- add batch jobs + -- add batch jobs for xx_build_files local scriptname = "build_files" .. (suffix and ("_" .. suffix) or "") local script = ruleinst:script(scriptname) if script then @@ -43,7 +43,10 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix script(target, sourcebatch, {progress = (index * 100) / total}) end, rootjob) end - else + end + + -- add batch jobs for xx_build_file + if not script then scriptname = "build_file" .. (suffix and ("_" .. suffix) or "") script = ruleinst:script(scriptname) if script then @@ -55,6 +58,37 @@ function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix end end end + + -- add batch jobs for xx_buildcmd_files + if not script then + scriptname = "buildcmd_files" .. (suffix and ("_" .. suffix) or "") + script = ruleinst:script(scriptname) + if script then + batchjobs:addjob("rule/" .. rulename .. "/" .. scriptname, function (index, total) + local cmds = script(target, sourcebatch, {progress = (index * 100) / total}) + for _, cmd in ipairs(cmds) do + os.vrunv(cmd[1], table.slice(cmd, 2)) + end + end, rootjob) + end + end + + -- add batch jobs for xx_buildcmd_file + if not script then + scriptname = "buildcmd_file" .. (suffix and ("_" .. suffix) or "") + script = ruleinst:script(scriptname) + if script then + local sourcekind = sourcebatch.sourcekind + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + batchjobs:addjob(sourcefile, function (index, total) + local cmds = script(target, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) + for _, cmd in ipairs(cmds) do + os.vrunv(cmd[1], table.slice(cmd, 2)) + end + end, rootjob) + end + end + end end -- add batch jobs for target -- cgit v1.3.1 From 514905243e16f94e472390a7d211e7bca70ddb71 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 00:44:36 +0800 Subject: improve compile_commands --- .gitignore | 3 + xmake/plugins/project/clang/compile_commands.lua | 76 +++++++++++++++++------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 43b458963..1631dd04d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,9 @@ winenv/ *.7z *.run +# for compiler +compile_commands.json + # Makefile generation /core/xmake.config.h /core/.config.mak diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index ba257bb35..7a4a5e8d5 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -20,8 +20,8 @@ -- imports import("core.tool.compiler") +import("core.project.rule") import("core.project.project") -import("core.language.language") -- escape path function _escape_path(p) @@ -47,19 +47,8 @@ function _translate_arguments(arguments) return args end --- make the object -function _make_object(jsonfile, target, sourcefile, objectfile) - - -- get the source file kind - local sourcekind = language.sourcekind_of(sourcefile) - - -- make the object for the *.o/obj? ignore it directly - if sourcekind == "obj" or sourcekind == "lib" then - return - end - - -- get compile arguments - local arguments = table.join(compiler.compargv(sourcefile, objectfile, {target = target, sourcekind = sourcekind})) +-- make command +function _make_arguments(jsonfile, arguments, sourcefile) -- translate some unsupported arguments arguments = _translate_arguments(arguments) @@ -82,11 +71,59 @@ function _make_object(jsonfile, target, sourcefile, objectfile) _g.firstline = false end +-- make commands for object rules +function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) + + -- get rule + local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!") + local ruleinst = assert(project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) + + -- generate commands for xx_buildcmd_files + 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, "") + end + end + + -- generate commands for xx_buildcmd_file + if not script then + scriptname = "buildcmd_file" .. (suffix and ("_" .. suffix) or "") + script = ruleinst:script(scriptname) + 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, sourcefile) + end + end + end + end +end + +-- make commands for objects +function _make_commands_for_objects(jsonfile, target, sourcebatch) + local sourcekind = sourcebatch.sourcekind + if sourcekind then + for index, sourcefile in ipairs(sourcebatch.sourcefiles) do + local objectfile = sourcebatch.objectfiles[index] + local arguments = table.join(compiler.compargv(sourcefile, objectfile, {target = target, sourcekind = sourcekind})) + _make_arguments(jsonfile, arguments, sourcefile) + end + return true + end +end + -- make objects -function _make_objects(jsonfile, target, sourcekind, sourcebatch) - for index, objectfile in ipairs(sourcebatch.objectfiles) do - _make_object(jsonfile, target, sourcebatch.sourcefiles[index], objectfile) +function _make_objects(jsonfile, target, sourcebatch) + _make_commands_for_objectrules(jsonfile, target, sourcebatch, "before") + if not _make_commands_for_objects(jsonfile, target, sourcebatch) then + _make_commands_for_objectrules(jsonfile, target, sourcebatch) end + _make_commands_for_objectrules(jsonfile, target, sourcebatch, "after") end -- make target @@ -99,10 +136,7 @@ function _make_target(jsonfile, target) -- build source batches for _, sourcebatch in pairs(target:sourcebatches()) do - local sourcekind = sourcebatch.sourcekind - if sourcekind then - _make_objects(jsonfile, target, sourcekind, sourcebatch) - end + _make_objects(jsonfile, target, sourcebatch) end end -- cgit v1.3.1 From 9dec8733c04c91f1dd145b5ff4455db6e940e140 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 00:46:00 +0800 Subject: improve compile_commands --- xmake/plugins/project/clang/compile_commands.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 7a4a5e8d5..48cd0ed96 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -22,6 +22,7 @@ import("core.tool.compiler") import("core.project.rule") import("core.project.project") +import("core.language.language") -- escape path function _escape_path(p) @@ -50,6 +51,20 @@ end -- make command function _make_arguments(jsonfile, arguments, sourcefile) + -- attempt to get source file from arguments + if not sourcefile then + for _, arg in ipairs(arguments) do + local sourcekind = try {function () return language.sourcekind_of(path.filename(arg)) end} + if sourcekind and os.isfile(arg) then + sourcefile = arg + break + end + end + if not sourcefile then + return + end + end + -- translate some unsupported arguments arguments = _translate_arguments(arguments) @@ -84,7 +99,7 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) if script then local cmds = script(target, sourcebatch) for _, cmd in ipairs(cmds) do - _make_arguments(jsonfile, cmd, "") + _make_arguments(jsonfile, cmd) end end @@ -97,7 +112,7 @@ function _make_commands_for_objectrules(jsonfile, target, sourcebatch, suffix) for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local cmds = script(target, sourcefile) for _, cmd in ipairs(cmds) do - _make_arguments(jsonfile, cmd, sourcefile) + _make_arguments(jsonfile, cmd) end end end -- cgit v1.3.1 From bf08fd02abb79831f72598a9347e9a7b3ac87989 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 00:55:30 +0800 Subject: support depend for buildcmd --- xmake/actions/build/kinds/object.lua | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index 98141fbf9..23cbcd3f5 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -22,6 +22,7 @@ import("core.base.option") import("core.project.rule") import("core.project.config") +import("core.project.depend") import("core.project.project") import("private.async.runjobs") @@ -65,10 +66,12 @@ 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 = script(target, sourcebatch, {progress = (index * 100) / total}) - for _, cmd in ipairs(cmds) do - os.vrunv(cmd[1], table.slice(cmd, 2)) - end + 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}) end, rootjob) end end @@ -81,10 +84,12 @@ 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 = script(target, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) - for _, cmd in ipairs(cmds) do - os.vrunv(cmd[1], table.slice(cmd, 2)) - end + 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}) end, rootjob) end end -- cgit v1.3.1 From 5f25a00cd3f3708ad133dbd1c376cfdd34a3de94 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 10:25:49 +0800 Subject: add batchcmds --- xmake/actions/build/kinds/object.lua | 20 ++-- xmake/modules/private/utils/batchcmds.lua | 116 +++++++++++++++++++++++ xmake/modules/private/utils/progress.lua | 10 ++ xmake/plugins/project/clang/compile_commands.lua | 19 ++-- xmake/rules/protobuf/proto.lua | 19 ++-- xmake/rules/protobuf/xmake.lua | 12 +-- 6 files changed, 164 insertions(+), 32 deletions(-) create mode 100644 xmake/modules/private/utils/batchcmds.lua 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) -- 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(-) 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 84d80e6642acda39989afc89992a39f371dc3ece Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 11:20:34 +0800 Subject: revert proto rule script --- xmake/rules/protobuf/xmake.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index d812cc1a7..b9d0bafdb 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -24,11 +24,10 @@ 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) @@ -42,11 +41,10 @@ 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(-) 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 b241052a21eeab56cae0a822f29ef2ac73d20926 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 20:47:40 +0800 Subject: fix install packages --- xmake/modules/private/action/require/impl/install_packages.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/private/action/require/impl/install_packages.lua b/xmake/modules/private/action/require/impl/install_packages.lua index aa94e8682..b7adf5400 100644 --- a/xmake/modules/private/action/require/impl/install_packages.lua +++ b/xmake/modules/private/action/require/impl/install_packages.lua @@ -422,7 +422,7 @@ function main(requires, opt) -- fetch and register packages (with system) from local first runjobs("fetch_packages", function (index) local instance = packages[index] - if instance and (not option.get("force") or (option.get("shallow") and instance:is_toplevel())) then + if instance and (not option.get("force") or (option.get("shallow") and not instance:is_toplevel())) then instance:envs_enter() instance:fetch() instance:envs_leave() -- 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(-) 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(-) 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(-) 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 From b038b2b86af516d3d49d7cc3fd6226ad23944564 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 20 Feb 2021 23:23:53 +0800 Subject: improve lex and yacc rules --- xmake/rules/lex_yacc/lex/xmake.lua | 89 ++++++----------------------------- xmake/rules/lex_yacc/yacc/xmake.lua | 92 +++++++------------------------------ xmake/rules/protobuf/proto.lua | 6 --- xmake/rules/protobuf/xmake.lua | 9 ---- 4 files changed, 30 insertions(+), 166 deletions(-) diff --git a/xmake/rules/lex_yacc/lex/xmake.lua b/xmake/rules/lex_yacc/lex/xmake.lua index 0e28a2ad7..cbdb766e4 100644 --- a/xmake/rules/lex_yacc/lex/xmake.lua +++ b/xmake/rules/lex_yacc/lex/xmake.lua @@ -20,93 +20,32 @@ -- define rule: lex rule("lex") - - -- set extension set_extensions(".l", ".ll") - - -- load lex/flex - before_load(function (target) - import("core.project.config") - import("lib.detect.find_tool") - local lex = config.get("__lex") - if not lex then - lex = find_tool("flex") or find_tool("lex") - if lex and lex.program then - config.set("__lex", lex.program) - cprint("checking for Lex ... ${color.success}%s", lex.program) - else - cprint("checking for Lex ... ${color.nothing}${text.nothing}") - raise("lex/flex not found!") - end - end - end) - - -- build lex file - on_build_file(function (target, sourcefile_lex, opt) + on_buildcmd_file(function (target, batchcmds, sourcefile_lex, opt) -- imports - import("core.base.option") - import("core.theme.theme") - import("core.project.config") - import("core.project.depend") - import("core.tool.compiler") - import("private.utils.progress") + import("lib.detect.find_tool") -- get lex - local lex = assert(config.get("__lex"), "lex not found!") - - -- get extension: .l/.ll - local extension = path.extension(sourcefile_lex) + local lex = assert(find_tool("flex") or find_tool("lex"), "lex not found!") -- get c/c++ source file for lex + local extension = path.extension(sourcefile_lex) local sourcefile_cx = path.join(target:autogendir(), "rules", "lex_yacc", path.basename(sourcefile_lex) .. (extension == ".ll" and ".cpp" or ".c")) - local sourcefile_dir = path.directory(sourcefile_cx) - - -- get object file - local objectfile = target:objectfile(sourcefile_cx) - - -- load compiler - local compinst = compiler.load((extension == ".ll" and "cxx" or "cc"), {target = target}) - - -- get compile flags - local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx}) -- add objectfile + local objectfile = target:objectfile(sourcefile_cx) 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.lex %s", sourcefile_lex) - - -- ensure the source file directory - if not os.isdir(sourcefile_dir) then - os.mkdir(sourcefile_dir) - end - - -- compile lex - os.vrunv(lex, {"-o", sourcefile_cx, sourcefile_lex}) - - -- trace - if option.get("verbose") then - print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags})) - end - - -- compile c/c++ source file for lex - dependinfo.files = {} - assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags})) + -- add commands + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.lex %s", sourcefile_lex) + batchcmds:mkdir(path.directory(sourcefile_cx)) + batchcmds:vrunv(lex.program, {"-o", sourcefile_cx, sourcefile_lex}) + batchcmds:compile(sourcefile_cx, objectfile) - -- update files and values to the dependent file - dependinfo.values = depvalues - table.insert(dependinfo.files, sourcefile_lex) - depend.save(dependinfo, dependfile) + -- add deps + batchcmds:add_depfiles(sourcefile_lex) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) end) diff --git a/xmake/rules/lex_yacc/yacc/xmake.lua b/xmake/rules/lex_yacc/yacc/xmake.lua index a93f458c0..f93db2ccb 100644 --- a/xmake/rules/lex_yacc/yacc/xmake.lua +++ b/xmake/rules/lex_yacc/yacc/xmake.lua @@ -20,96 +20,36 @@ -- define rule: yacc rule("yacc") - - -- set extension set_extensions(".y", ".yy") - - -- load yacc/bison - before_load(function (target) - import("core.project.config") - import("lib.detect.find_tool") - local yacc = config.get("__yacc") - if not yacc then - yacc = find_tool("bison") or find_tool("yacc") - if yacc and yacc.program then - config.set("__yacc", yacc.program) - cprint("checking for Yacc ... ${color.success}%s", yacc.program) - else - cprint("checking for Yacc ... ${color.nothing}${text.nothing}") - raise("yacc/bison not found!") - end - end - end) - - -- build yacc file - before_build_file(function (target, sourcefile_yacc, opt) + before_buildcmd_file(function (target, batchcmds, sourcefile_yacc, opt) -- imports - import("core.base.option") - import("core.theme.theme") - import("core.project.config") - import("core.project.depend") - import("core.tool.compiler") - import("private.utils.progress") + import("lib.detect.find_tool") -- get yacc - local yacc = assert(config.get("__yacc"), "yacc not found!") - - -- get extension: .l/.ll - local extension = path.extension(sourcefile_yacc) + local yacc = assert(find_tool("bison") or find_tool("yacc"), "yacc/bison not found!") -- get c/c++ source file for yacc - local sourcefile_cx = path.join(target:autogendir(), "rules", "lex_yacc", path.basename(sourcefile_yacc) .. ".tab" .. (extension == ".yy" and ".cpp" or ".c")) - local sourcefile_dir = path.directory(sourcefile_cx) - - -- get object file - local objectfile = target:objectfile(sourcefile_cx) - - -- load compiler - local compinst = compiler.load((extension == ".yy" and "cxx" or "cc"), {target = target}) - - -- get compile flags - local compflags = compinst:compflags({target = target, sourcefile = sourcefile_cx}) + local extension = path.extension(sourcefile_yacc) + local sourcefile_cx = path.join(target:autogendir(), "rules", "yacc_yacc", path.basename(sourcefile_yacc) .. ".tab" .. (extension == ".yy" and ".cpp" or ".c")) -- add objectfile + local objectfile = target:objectfile(sourcefile_cx) table.insert(target:objectfiles(), objectfile) -- add includedirs + local sourcefile_dir = path.directory(sourcefile_cx) target:add("includedirs", sourcefile_dir) - -- 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.yacc %s", sourcefile_yacc) - - -- ensure the source file directory - if not os.isdir(sourcefile_dir) then - os.mkdir(sourcefile_dir) - end - - -- compile yacc - os.vrunv(yacc, {"-d", "-o", sourcefile_cx, sourcefile_yacc}) - - -- trace - if option.get("verbose") then - print(compinst:compcmd(sourcefile_cx, objectfile, {compflags = compflags})) - end - - -- compile c/c++ source file for yacc - dependinfo.files = {} - assert(compinst:compile(sourcefile_cx, objectfile, {dependinfo = dependinfo, compflags = compflags})) + -- add commands + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.yacc %s", sourcefile_yacc) + batchcmds:mkdir(sourcefile_dir) + batchcmds:vrunv(yacc.program, {"-d", "-o", sourcefile_cx, sourcefile_yacc}) + batchcmds:compile(sourcefile_cx, objectfile) - -- update files and values to the dependent file - dependinfo.values = depvalues - table.insert(dependinfo.files, sourcefile_yacc) - depend.save(dependinfo, dependfile) + -- add deps + batchcmds:add_depfiles(sourcefile_yacc) + batchcmds:set_depmtime(os.mtime(objectfile)) + batchcmds:set_depcache(target:dependfile(objectfile)) end) diff --git a/xmake/rules/protobuf/proto.lua b/xmake/rules/protobuf/proto.lua index 83706adab..7a29d8b4a 100644 --- a/xmake/rules/protobuf/proto.lua +++ b/xmake/rules/protobuf/proto.lua @@ -19,13 +19,7 @@ -- -- imports -import("core.base.option") -import("core.theme.theme") -import("core.project.config") -import("core.project.depend") -import("core.tool.compiler") import("lib.detect.find_tool") -import("private.utils.progress") -- get protoc function _get_protoc(target, sourcekind) diff --git a/xmake/rules/protobuf/xmake.lua b/xmake/rules/protobuf/xmake.lua index 76f114aea..e28a9cbb6 100644 --- a/xmake/rules/protobuf/xmake.lua +++ b/xmake/rules/protobuf/xmake.lua @@ -20,23 +20,14 @@ -- define rule: protobuf.cpp rule("protobuf.cpp") - - -- set extension set_extensions(".proto") - - -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cxx") end) - -- define rule: protobuf.c rule("protobuf.c") - - -- set extension set_extensions(".proto") - - -- generate build commands before_buildcmd_file(function (target, batchcmds, sourcefile_proto, opt) return import("proto").buildcmd(target, batchcmds, sourcefile_proto, opt, "cc") end) -- cgit v1.3.1