summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-05 00:50:38 +0800
committerruki <[email protected]>2025-04-08 15:31:57 +0800
commit1839ec9aebb573adc36803bf3d632f97410f3666 (patch)
tree46e12ff0baae5d42b079e4d82f9bd500c5864c41
parentc6b6ae7961f8454ab8280ef8d17b5228408096c3 (diff)
sort target rules
-rw-r--r--tests/apis/rules_inject_deps/xmake.lua6
-rw-r--r--tests/apis/rules_order/xmake.lua20
-rw-r--r--xmake/actions/build/target_utils.lua37
-rw-r--r--xmake/core/project/rule.lua5
-rw-r--r--xmake/modules/async/jobgraph.lua2
5 files changed, 54 insertions, 16 deletions
diff --git a/tests/apis/rules_inject_deps/xmake.lua b/tests/apis/rules_inject_deps/xmake.lua
index cf73449bb..79b3f8252 100644
--- a/tests/apis/rules_inject_deps/xmake.lua
+++ b/tests/apis/rules_inject_deps/xmake.lua
@@ -1,10 +1,6 @@
rule("cppfront")
set_extensions(".cpp2")
- on_load(function (target)
- local rule = target:rule("c++.build"):clone()
- rule:add("deps", "cppfront", {order = true})
- target:rule_add(rule)
- end)
+ add_buildorders("cppfront", "c++.build")
on_build_file(function (target, sourcefile, opt)
print("build cppfront file")
local objectfile = target:objectfile(sourcefile:gsub("cpp2", "cpp"))
diff --git a/tests/apis/rules_order/xmake.lua b/tests/apis/rules_order/xmake.lua
index ea167dca9..8b3eb867a 100644
--- a/tests/apis/rules_order/xmake.lua
+++ b/tests/apis/rules_order/xmake.lua
@@ -1,7 +1,15 @@
rule("markdown")
- add_deps("man", {order = true})
set_extensions(".md", ".markdown")
+ add_buildorders("man", "markdown")
+
+ before_build(function (target)
+ print("before_build: markdown")
+ end)
+ after_build(function (target)
+ print("after_build: markdown")
+ end)
+
before_build_file(function (target, sourcefile)
print("before_build_file: %s", sourcefile)
end)
@@ -14,6 +22,14 @@ rule("markdown")
rule("man")
set_extensions(".man")
+
+ before_build(function (target)
+ print("before_build: man")
+ end)
+ after_build(function (target)
+ print("after_build: man")
+ end)
+
before_build_file(function (target, sourcefile)
print("before_build_file: %s", sourcefile)
end)
@@ -26,7 +42,7 @@ rule("man")
target("test")
set_kind("binary")
- add_rules("markdown")
+ add_rules("markdown", "man")
add_files("src/*.c")
add_files("src/*.md")
add_files("src/*.man")
diff --git a/xmake/actions/build/target_utils.lua b/xmake/actions/build/target_utils.lua
index 36c087548..9ee7a9c0d 100644
--- a/xmake/actions/build/target_utils.lua
+++ b/xmake/actions/build/target_utils.lua
@@ -117,7 +117,9 @@ function add_targetjobs_for_script(jobgraph, target, instance, opt)
opt = opt or {}
local has_script = false
local job_prefix = target:fullname()
- if target ~= instance then
+ if target == instance then
+ job_prefix = job_prefix .. "/target"
+ else
job_prefix = job_prefix .. "/rule/" .. instance:fullname()
end
@@ -189,13 +191,11 @@ function add_targetjobs_with_stage(jobgraph, target, stage, opt)
-- the command script name, e.g. before/after_preparecmd, before/after_buildcmd
local scriptcmd_name = stage ~= "" and (job_kind .. "cmd_" .. stage) or (job_kind .. "cmd")
- -- TODO sort rules and jobs
+ -- call target and rules script
local instances = {target}
for _, r in ipairs(target:orderules()) do
table.insert(instances, r)
end
-
- -- call target and rules script
local jobsize = jobgraph:size()
jobgraph:group(group_name, function ()
local has_script = false
@@ -204,9 +204,12 @@ function add_targetjobs_with_stage(jobgraph, target, stage, opt)
scriptcmd_name = scriptcmd_name
}
for _, instance in ipairs(instances) do
- if add_targetjobs_for_script(jobgraph, target, instance, script_opt) then
- has_script = true
- end
+ local script_group = group_name .. "/" .. instance:fullname()
+ jobgraph:group(script_group, function ()
+ if add_targetjobs_for_script(jobgraph, target, instance, script_opt) then
+ has_script = true
+ end
+ end)
-- if custom target.on_build/prepare exists, we need to ignore all scripts in rules
if has_script and instance == target and stage == "" then
break
@@ -219,6 +222,23 @@ function add_targetjobs_with_stage(jobgraph, target, stage, opt)
end
end)
+ -- sort build rules
+ for _, instance in ipairs(instances) do
+ local buildorders = table.wrap(instance:get("buildorders"))
+ for _, buildorder in ipairs(buildorders) do
+ local joborders = {}
+ for _, name in ipairs(buildorder) do
+ local script_group = group_name .. "/" .. name
+ if jobgraph:has(script_group) then
+ table.insert(joborders, script_group)
+ end
+ end
+ if #joborders > 0 then
+ jobgraph:add_orders(joborders)
+ end
+ end
+ end
+
if jobgraph:size() > jobsize then
return group_name
end
@@ -298,6 +318,7 @@ function add_targetjobs_and_deps(jobgraph, target, targetrefs, opt)
local dep = project.target(depname, {namespace = target:namespace()})
add_targetjobs_and_deps(jobgraph, dep, targetrefs, opt)
+ -- build.across_targets_in_parallel is deprecated
if dep:policy("build.fence") or dep:policy("build.across_targets_in_parallel") == false then
jobname = string.format("%s/begin_%s", target:fullname(), job_kind)
jobname_dep = string.format("%s/end_%s", dep:fullname(), job_kind)
@@ -329,7 +350,7 @@ function add_filejobs_for_script(jobgraph, target, instance, sourcebatch, opt)
local job_prefix = target:fullname()
local file_group = sourcebatch.rulename
if target == instance then
- job_prefix = job_prefix .. "/" .. file_group
+ job_prefix = job_prefix .. "/target/" .. file_group
else
job_prefix = job_prefix .. "/rule/" .. file_group
end
diff --git a/xmake/core/project/rule.lua b/xmake/core/project/rule.lua
index 63f954580..a2fd661e8 100644
--- a/xmake/core/project/rule.lua
+++ b/xmake/core/project/rule.lua
@@ -280,6 +280,11 @@ function rule.apis()
, "rule.add_deps"
, "rule.add_imports"
}
+ , groups =
+ {
+ -- rule.add_xxx
+ "rule.add_buildorders"
+ }
, script =
{
-- rule.on_xxx
diff --git a/xmake/modules/async/jobgraph.lua b/xmake/modules/async/jobgraph.lua
index be1ad57f0..4bbc53ff4 100644
--- a/xmake/modules/async/jobgraph.lua
+++ b/xmake/modules/async/jobgraph.lua
@@ -109,7 +109,7 @@ end
-- has the given job or group?
function jobgraph:has(name)
- return self._jobs[name] or self._groups[name]
+ return (self._jobs[name] or self._groups[name]) ~= nil
end
-- enter group to add jobs