diff options
Diffstat (limited to 'xmake/actions/build/kinds/object.lua')
| -rw-r--r-- | xmake/actions/build/kinds/object.lua | 135 |
1 files changed, 64 insertions, 71 deletions
diff --git a/xmake/actions/build/kinds/object.lua b/xmake/actions/build/kinds/object.lua index d90820693..ba24c9c0b 100644 --- a/xmake/actions/build/kinds/object.lua +++ b/xmake/actions/build/kinds/object.lua @@ -25,101 +25,94 @@ import("core.project.config") import("core.project.project") import("private.async.runjobs") --- build source files with the custom rule -function _build_files_with_rule(target, sourcebatch, opt, suffix) +-- add batch jobs for the custom rule +function _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, suffix) - -- the rule name + -- get rule local rulename = assert(sourcebatch.rulename, "unknown rule for sourcebatch!") - - -- get rule instance local ruleinst = assert(project.rule(rulename) or rule.rule(rulename), "unknown rule: %s", rulename) - -- on_build_files? - local on_build_files = ruleinst:script("build_files" .. (suffix and ("_" .. suffix) or "")) - if on_build_files then - on_build_files(target, sourcebatch, opt) + -- add batch jobs + local scriptname = "build_files" .. (suffix and ("_" .. suffix) or "") + local script = ruleinst:script(scriptname) + if script then + if ruleinst:extraconf(scriptname, "batch") then + assert(script(target, batchjobs, sourcebatch, {rootjob = rootjob}), "rule(%s):%s(): no returned job!", rulename, scriptname) + else + batchjobs:addjob("rule/" .. rulename .. "/" .. scriptname .. "/sourcefiles", function (index, total) + script(target, sourcebatch, {progress = (index * 100) / total}) + end, rootjob) + end else - -- get the build file script - local on_build_file = ruleinst:script("build_file" .. (suffix and ("_" .. suffix) or "")) - if on_build_file then - - -- get the max job count - local jobs = tonumber(option.get("jobs") or "4") - - -- run build jobs for each source file - local curdir = os.curdir() - local sourcecount = #sourcebatch.sourcefiles - runjobs("build_files", function (index) - - -- get source file - local sourcefile = sourcebatch.sourcefiles[index] - - -- do build file - on_build_file(target, sourcefile, {sourcekind = sourcebatch.sourcekind, progress = opt.progress}) - - end, {total = sourcecount, comax = jobs}) + scriptname = "build_file" .. (suffix and ("_" .. suffix) or "") + script = ruleinst:script(scriptname) + if script then + local sourcekind = sourcebatch.sourcekind + local jobname = "rule/" .. rulename .. "/" .. scriptname .. "/sourcefile/" + for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do + batchjobs:addjob(jobname .. idx, function (index, total) + script(target, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) + end, rootjob) + end end end end --- do build files -function _do_build_files(target, sourcebatch, opt) - _build_files_with_rule(target, sourcebatch, opt) -end - --- build files -function _build_files(target, sourcebatch, opt) +-- add batch jobs for target +function _add_batchjobs_for_target(batchjobs, rootjob, target, sourcebatch, suffix) - -- do before build - local before_build_files = target:script("build_files_before") - if before_build_files then - before_build_files(target, sourcebatch, opt) - end - - -- do build - local on_build_files = target:script("build_files") - if on_build_files then - opt.origin = _do_build_files - on_build_files(target, sourcebatch, opt) - opt.origin = nil + -- add batch jobs + local scriptname = "build_files" .. (suffix and ("_" .. suffix) or "") + local script = target:script(scriptname) + if script then + if target:extraconf(scriptname, "batch") then + assert(script(target, batchjobs, sourcebatch, {rootjob = rootjob}), "target(%s):%s(): no returned job!", target:name(), scriptname) + else + batchjobs:addjob(target:name() .. "/" .. scriptname .. "/sourcefiles", function (index, total) + script(target, sourcebatch, {progress = (index * 100) / total}) + end, rootjob) + end else - _do_build_files(target, sourcebatch, opt) - end - - -- do after build - local after_build_files = target:script("build_files_after") - if after_build_files then - after_build_files(target, sourcebatch, opt) + scriptname = "build_file" .. (suffix and ("_" .. suffix) or "") + script = target:script(scriptname) + if script then + local sourcekind = sourcebatch.sourcekind + local jobname = target:name() .. "/" .. scriptname .. "/sourcefile/" + for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do + batchjobs:addjob(jobname .. idx, function (index, total) + script(target, sourcefile, {sourcekind = sourcekind, progress = (index * 100) / total}) + end, rootjob) + end + end end end --- build source files -function build_sourcefiles(target, sourcebatches, opt) +-- add batch jobs for building source files +function add_batchjobs_for_sourcefiles(batchjobs, rootjob, target, sourcebatches) - -- init options - opt = opt or {} - - -- build source batches with custom rules before building other sources + -- add batch jobs for build_after for _, sourcebatch in pairs(sourcebatches) do - _build_files_with_rule(target, sourcebatch, opt, "before") + _add_batchjobs_for_rule(batchjobs, rootjob, target, sourcebatch, "after") + _add_batchjobs_for_target(batchjobs, rootjob, target, sourcebatch, "after") end - -- build source batches - local sourcetotal = target:sourcecount() + -- add source batches + local job_build_after = batchjobs:lastdep(rootjob) or rootjob for _, sourcebatch in pairs(sourcebatches) do - _build_files(target, sourcebatch, opt) + _add_batchjobs_for_target(target, job_build_after, target, sourcebatch) end - -- build source batches with custom rules after building other sources + -- add source batches with custom rules before building other sources + local job_build = batchjobs:lastdep(job_build_after) or job_build_after for _, sourcebatch in pairs(sourcebatches) do - _build_files_with_rule(target, sourcebatch, opt, "after") + _add_batchjobs_for_rule(batchjobs, job_build, target, sourcebatch, "before") + _add_batchjobs_for_target(batchjobs, job_build, target, sourcebatch, "before") end + return batchjobs:lastdep(job_build) or job_build end --- build objects for the given target -function build(target, opt) - - -- build source files - build_sourcefiles(target, target:sourcebatches(), opt) +-- add batch jobs for building object files +function main(batchjobs, rootjob, target) + return add_batchjobs_for_sourcefiles(batchjobs, rootjob, target, target:sourcebatches()) end |
