summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-03-10 23:00:11 +0800
committerruki <[email protected]>2020-03-10 12:01:11 +0800
commitf3d3ca766a2d6486de98ae6b492ec2ff2aa60657 (patch)
treec2cc6fc35262c48fa3bc7960a872bdfde9e7b4e5
parentfb3610ea1dbfbb31cc05d7dc272fa0dd7c825893 (diff)
rewrite action/build
-rw-r--r--xmake/actions/build/build.lua79
-rw-r--r--xmake/actions/build/kinds/binary.lua14
-rw-r--r--xmake/actions/build/kinds/object.lua135
-rw-r--r--xmake/actions/build/kinds/shared.lua14
-rw-r--r--xmake/actions/build/kinds/static.lua18
-rw-r--r--xmake/core/project/rule.lua5
-rw-r--r--xmake/modules/private/async/jobpool.lua5
7 files changed, 133 insertions, 137 deletions
diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua
index cfbd75880..865d7e24c 100644
--- a/xmake/actions/build/build.lua
+++ b/xmake/actions/build/build.lua
@@ -35,27 +35,18 @@ function _clean_target(target)
end
end
--- do build the given target
-function _do_build_target(target, opt)
-
- -- build target
- if not target:isphony() then
- import("kinds." .. target:targetkind()).build(target, opt)
- end
-end
-
--- add builtin build jobs
-function _add_buildjobs_builtin(buildjobs, rootjob, target)
+-- add builtin batch jobs
+function _add_batchjobs_builtin(batchjobs, rootjob, target)
-- uses the rules script?
local job
- for _, r in irpairs(target:orderules()) do -- reverse rules order for buildjobs:addjob()
+ for _, r in irpairs(target:orderules()) do -- reverse rules order for batchjobs:addjob()
local script = r:script("build")
if script then
- if r:extraconf("build", "batch") then -- TODO extraconf/rule
- job = assert(script(target, buildjobs, {rootjob = job or rootjob}), "rule(%s):on_build(): no returned job!", r:name())
+ if r:extraconf("build", "batch") then
+ job = assert(script(target, batchjobs, {rootjob = job or rootjob}), "rule(%s):on_build(): no returned job!", r:name())
else
- job = buildjobs:addjob(r:name() .. "/build", function (index, total)
+ job = batchjobs:addjob("rule/" .. r:name() .. "/build", function (index, total)
script(target, {progress = (index * 100) / total})
end, job or rootjob)
end
@@ -63,20 +54,20 @@ function _add_buildjobs_builtin(buildjobs, rootjob, target)
end
-- uses the builtin target script
- if not job then
- -- _do_build_target(target, opt)
+ if not job and not target:isphony() then
+ job = import("kinds." .. target:targetkind(), {anonymous = true})(batchjobs, rootjob, target)
end
- return job
+ return job or rootjob
end
--- add build jobs
-function _add_buildjobs(buildjobs, rootjob, target)
+-- add batch jobs
+function _add_batchjobs(batchjobs, rootjob, target)
local job
local script = target:script("build")
if not script then
- -- do builtin build jobs
- job = _add_buildjobs_builtin(buildjobs, rootjob, target)
+ -- do builtin batch jobs
+ job = _add_batchjobs_builtin(batchjobs, rootjob, target)
elseif target:extraconf("build", "batch") then
-- do custom batch script
-- e.g.
@@ -87,7 +78,7 @@ function _add_buildjobs(buildjobs, rootjob, target)
-- end, opt.rootjob)
-- end, {batch = true})
--
- job = assert(script(target, buildjobs, {rootjob = rootjob}), "target(%s):on_build(): no returned job!", target:name())
+ job = assert(script(target, batchjobs, {rootjob = rootjob}), "target(%s):on_build(): no returned job!", target:name())
else
-- do custom script directly
-- e.g.
@@ -97,15 +88,15 @@ function _add_buildjobs(buildjobs, rootjob, target)
-- print("build it")
-- end)
--
- job = buildjobs:addjob(target:name() .. "/build", function (index, total)
+ job = batchjobs:addjob(target:name() .. "/build", function (index, total)
script(target, {progress = (index * 100) / total})
end, rootjob)
end
return job
end
--- add build jobs for the given target
-function _add_buildjobs_for_target(buildjobs, rootjob, target)
+-- add batch jobs for the given target
+function _add_batchjobs_for_target(batchjobs, rootjob, target)
-- has been disabled?
if target:get("enabled") == false then
@@ -114,7 +105,7 @@ function _add_buildjobs_for_target(buildjobs, rootjob, target)
-- add after_build job for target
local oldenvs = {}
- local job_after_build = buildjobs:addjob(target:name() .. "/after_build", function (index, total)
+ local job_after_build = batchjobs:addjob(target:name() .. "/after_build", function (index, total)
-- do after_build
local progress = (index * 100) / total
@@ -135,11 +126,11 @@ function _add_buildjobs_for_target(buildjobs, rootjob, target)
end
end, rootjob)
- -- add build jobs for target, @note only on_build script support batch jobs
- local job_build = _add_buildjobs(buildjobs, job_after_build, target)
+ -- add batch jobs for target, @note only on_build script support batch jobs
+ local job_build = _add_batchjobs(batchjobs, job_after_build, target)
-- add before_build job for target
- local job_before_build = buildjobs:addjob(target:name() .. "/before_build", function (index, total)
+ local job_before_build = batchjobs:addjob(target:name() .. "/before_build", function (index, total)
-- enter the environments of the target packages
for name, values in pairs(target:pkgenvs()) do
@@ -168,19 +159,19 @@ function _add_buildjobs_for_target(buildjobs, rootjob, target)
return job_before_build
end
--- add build jobs for the given target and deps
-function _add_buildjobs_for_target_and_deps(buildjobs, rootjob, inserted, target)
+-- add batch jobs for the given target and deps
+function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, inserted, target)
if not inserted[target:name()] then
- rootjob = _add_buildjobs_for_target(buildjobs, rootjob, target)
+ rootjob = _add_batchjobs_for_target(batchjobs, rootjob, target)
for _, depname in ipairs(target:get("deps")) do
- _add_buildjobs_for_target_and_deps(buildjobs, rootjob, inserted, project.target(depname))
+ _add_batchjobs_for_target_and_deps(batchjobs, rootjob, inserted, project.target(depname))
end
inserted[target:name()] = true
end
end
--- get build jobs
-function _get_buildjobs(targetname)
+-- get batch jobs
+function _get_batchjobs(targetname)
-- get root targets
local targets_root = {}
@@ -205,24 +196,24 @@ function _get_buildjobs(targetname)
end
end
- -- generate build jobs for default or all targets
+ -- generate batch jobs for default or all targets
local inserted = {}
- local buildjobs = jobpool.new()
+ local batchjobs = jobpool.new()
for _, target in pairs(targets_root) do
- _add_buildjobs_for_target_and_deps(buildjobs, buildjobs:rootjob(), inserted, target)
+ _add_batchjobs_for_target_and_deps(batchjobs, batchjobs:rootjob(), inserted, target)
end
- return buildjobs
+ return batchjobs
end
-- the main entry
function main(targetname)
-- build all jobs
- local buildjobs = _get_buildjobs(targetname)
- print(buildjobs)
- if buildjobs and buildjobs:count() > 0 then
+ local batchjobs = _get_batchjobs(targetname)
+ print(batchjobs)
+ if batchjobs and batchjobs:count() > 0 then
environment.enter("toolchains")
- runjobs("build", buildjobs, {comax = option.get("jobs") or 1})
+ runjobs("build", batchjobs, {comax = option.get("jobs") or 1})
environment.leave("toolchains")
end
end
diff --git a/xmake/actions/build/kinds/binary.lua b/xmake/actions/build/kinds/binary.lua
index c11fadd7c..83b47d50d 100644
--- a/xmake/actions/build/kinds/binary.lua
+++ b/xmake/actions/build/kinds/binary.lua
@@ -24,7 +24,7 @@ import("core.theme.theme")
import("core.tool.linker")
import("core.tool.compiler")
import("core.project.depend")
-import("object")
+import("object", {alias = "add_batchjobs_for_object"})
-- do link target
function _do_link_target(target, opt)
@@ -134,7 +134,7 @@ function _link_target(target, opt)
end
-- on link
- target:script("link", _on_link_target)(target, table.join(opt, {origin = _do_link_target}))
+ target:script("link", _on_link_target)(target, opt)
-- do after link for target
local after_link = target:script("link_after")
@@ -151,8 +151,10 @@ function _link_target(target, opt)
end
end
--- build binary target
-function build(target, opt)
- object.build(target, opt)
- _link_target(target, opt)
+-- add batch jobs for building binary target
+function main(batchjobs, rootjob, target)
+ local job_link = batchjobs:addjob(target:name() .. "/link", function (index, total)
+ _link_target(target, {progress = (index * 100) / total})
+ end)
+ return add_batchjobs_for_object(batchjobs, job_link, target)
end
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
diff --git a/xmake/actions/build/kinds/shared.lua b/xmake/actions/build/kinds/shared.lua
index 4625d2c5b..b92805c11 100644
--- a/xmake/actions/build/kinds/shared.lua
+++ b/xmake/actions/build/kinds/shared.lua
@@ -24,7 +24,7 @@ import("core.theme.theme")
import("core.tool.linker")
import("core.tool.compiler")
import("core.project.depend")
-import("object")
+import("object", {alias = "add_batchjobs_for_object"})
-- do link target
function _do_link_target(target, opt)
@@ -147,7 +147,7 @@ function _link_target(target, opt)
end
-- on link
- target:script("link", _on_link_target)(target, table.join(opt, {origin = _do_link_target}))
+ target:script("link", _on_link_target)(target, opt)
-- do after link for target
local after_link = target:script("link_after")
@@ -164,8 +164,10 @@ function _link_target(target, opt)
end
end
--- build shared target
-function build(target, opt)
- object.build(target, opt)
- _link_target(target, opt)
+-- add batch jobs for building shared target
+function main(batchjobs, rootjob, target)
+ local job_link = batchjobs:addjob(target:name() .. "/link", function (index, total)
+ _link_target(target, {progress = (index * 100) / total})
+ end)
+ return add_batchjobs_for_object(batchjobs, job_link, target)
end
diff --git a/xmake/actions/build/kinds/static.lua b/xmake/actions/build/kinds/static.lua
index d1bcedde6..7e3eee562 100644
--- a/xmake/actions/build/kinds/static.lua
+++ b/xmake/actions/build/kinds/static.lua
@@ -24,7 +24,7 @@ import("core.theme.theme")
import("core.tool.linker")
import("core.tool.compiler")
import("core.project.depend")
-import("object")
+import("object", {alias = "add_batchjobs_for_object"})
-- do link target
function _do_link_target(target, opt)
@@ -143,7 +143,7 @@ function _link_target(target, opt)
end
-- on link
- target:script("link", _on_link_target)(target, table.join(opt, {origin = _do_link_target}))
+ target:script("link", _on_link_target)(target, opt)
-- do after link for target
local after_link = target:script("link_after")
@@ -160,12 +160,10 @@ function _link_target(target, opt)
end
end
--- build static target
-function build(target, opt)
-
- -- build objects
- object.build(target, opt)
-
- -- link target
- _link_target(target, opt)
+-- add batch jobs for building static target
+function main(batchjobs, rootjob, target)
+ local job_link = batchjobs:addjob(target:name() .. "/link", function (index, total)
+ _link_target(target, {progress = (index * 100) / total})
+ end)
+ return add_batchjobs_for_object(batchjobs, job_link, target)
end
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index d2f630d08..43c8e3db2 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -211,6 +211,11 @@ function rule:get(name)
return self._INFO:get(name)
end
+-- get the extra configuration
+function rule:extraconf(name, item, key)
+ return self._INFO:extraconf(name, item, key)
+end
+
-- get the rule name
function rule:name()
return self._NAME
diff --git a/xmake/modules/private/async/jobpool.lua b/xmake/modules/private/async/jobpool.lua
index 94b1e6a56..dc314ad08 100644
--- a/xmake/modules/private/async/jobpool.lua
+++ b/xmake/modules/private/async/jobpool.lua
@@ -44,6 +44,11 @@ function jobpool:newjob(name, run)
return {name = name, run = run}
end
+-- get the last dependent job of the given job
+function jobpool:lastdep(job)
+ return job._deps and job._deps:size() > 0 and job._deps:last()
+end
+
-- add job to the given job node
--
-- @param name the job name