summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-03-24 22:49:41 +0800
committerruki <[email protected]>2025-04-08 15:31:55 +0800
commit502f7b6ddf39b8053c523d7a58532e0a5998c414 (patch)
tree7c000a58d79769e9c402b6d47d1a5f963bef5e75
parent812d8ec053376a541193dc943362797fd2c4541e (diff)
add target_utils
-rw-r--r--xmake/actions/build/build.lua42
-rw-r--r--xmake/actions/build/build_files.lua45
-rw-r--r--xmake/actions/build/prepare.lua87
-rw-r--r--xmake/actions/build/target_utils.lua73
-rw-r--r--xmake/modules/async/jobgraph.lua5
5 files changed, 169 insertions, 83 deletions
diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua
index d11b1841b..e890faa0a 100644
--- a/xmake/actions/build/build.lua
+++ b/xmake/actions/build/build.lua
@@ -29,6 +29,7 @@ import("core.base.hashset")
import("private.service.remote_cache.client", {alias = "remote_cache_client"})
import("private.service.distcc_build.client", {alias = "distcc_build_client"})
import("prepare", {alias = "prepare_build"})
+import("target_utils")
-- clean target for rebuilding
function _clean_target(target)
@@ -234,46 +235,7 @@ end
function get_batchjobs(targetnames, opt)
-- get root targets
- local targets_root = {}
- if targetnames then
- for _, targetname in ipairs(table.wrap(targetnames)) do
- local target = project.target(targetname)
- if target then
- table.insert(targets_root, target)
- if option.get("rebuild") then
- target:data_set("rebuilt", true)
- if not option.get("shallow") then
- for _, dep in ipairs(target:orderdeps()) do
- dep:data_set("rebuilt", true)
- end
- end
- end
- end
- end
- else
- local group_pattern = opt.group_pattern
- local depset = hashset.new()
- local targets = {}
- for _, target in ipairs(project.ordertargets()) do
- if target:is_enabled() then
- local group = target:get("group")
- if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then
- for _, depname in ipairs(target:get("deps")) do
- depset:insert(depname)
- end
- table.insert(targets, target)
- end
- end
- end
- for _, target in ipairs(targets) do
- if not depset:has(target:name()) then
- table.insert(targets_root, target)
- end
- if option.get("rebuild") then
- target:data_set("rebuilt", true)
- end
- end
- end
+ local targets_root = target_utils.get_root_targets(targetnames, opt)
-- generate batch jobs for default or all targets
local jobrefs = {}
diff --git a/xmake/actions/build/build_files.lua b/xmake/actions/build/build_files.lua
index 1b29cd6c0..ae09fc838 100644
--- a/xmake/actions/build/build_files.lua
+++ b/xmake/actions/build/build_files.lua
@@ -26,6 +26,7 @@ import("core.project.project")
import("private.async.jobpool")
import("async.runjobs")
import("kinds.object")
+import("target_utils")
import("prepare_files", {alias = "prepare_build_files"})
-- match source files
@@ -121,49 +122,7 @@ function _get_batchjobs(targetnames, opt)
local filepatterns = _get_file_patterns(opt.sourcefiles)
-- get root targets
- local targets_root = {}
- if targetnames then
- for _, targetname in ipairs(table.wrap(targetnames)) do
- local target = project.target(targetname)
- if target then
- table.insert(targets_root, target)
- if option.get("rebuild") then
- target:data_set("rebuilt", true)
- if not option.get("shallow") then
- for _, dep in ipairs(target:orderdeps()) do
- dep:data_set("rebuilt", true)
- end
- end
- end
- end
- end
- else
- local group_pattern = opt.group_pattern
- local depset = hashset.new()
- local targets = {}
- for _, target in pairs(project.targets()) do
- local group = target:get("group")
- if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then
- for _, depname in ipairs(target:get("deps")) do
- depset:insert(depname)
- end
- table.insert(targets, target)
- end
- end
- for _, target in pairs(targets) do
- if not depset:has(target:name()) then
- table.insert(targets_root, target)
- if option.get("rebuild") then
- target:data_set("rebuilt", true)
- if not option.get("shallow") then
- for _, dep in ipairs(target:orderdeps()) do
- dep:data_set("rebuilt", true)
- end
- end
- end
- end
- end
- end
+ local targets_root = target_utils.get_root_targets(targetnames, opt)
-- generate batch jobs for default or all targets
local jobrefs = {}
diff --git a/xmake/actions/build/prepare.lua b/xmake/actions/build/prepare.lua
index 52a0152af..45f0d75b9 100644
--- a/xmake/actions/build/prepare.lua
+++ b/xmake/actions/build/prepare.lua
@@ -21,6 +21,93 @@
-- imports
import("core.base.option")
import("core.project.config")
+import("async.runjobs")
+import("async.jobgraph", {alias = "async_jobgraph"})
+
+-- get prepare jobs
+function _get_prepare_jobs(targetnames, opt)
+ local jobgraph = async_jobgraph.new()
+ return jobgraph
+
+ -- get root targets
+ --[[
+ local targets_root = {}
+ if targetnames then
+ for _, targetname in ipairs(table.wrap(targetnames)) do
+ local target = project.target(targetname)
+ if target then
+ table.insert(targets_root, target)
+ if option.get("rebuild") then
+ target:data_set("rebuilt", true)
+ if not option.get("shallow") then
+ for _, dep in ipairs(target:orderdeps()) do
+ dep:data_set("rebuilt", true)
+ end
+ end
+ end
+ end
+ end
+ else
+ local group_pattern = opt.group_pattern
+ local depset = hashset.new()
+ local targets = {}
+ for _, target in ipairs(project.ordertargets()) do
+ if target:is_enabled() then
+ local group = target:get("group")
+ if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then
+ for _, depname in ipairs(target:get("deps")) do
+ depset:insert(depname)
+ end
+ table.insert(targets, target)
+ end
+ end
+ end
+ for _, target in ipairs(targets) do
+ if not depset:has(target:name()) then
+ table.insert(targets_root, target)
+ end
+ if option.get("rebuild") then
+ target:data_set("rebuilt", true)
+ end
+ end
+ end
+
+ -- generate batch jobs for default or all targets
+ local jobrefs = {}
+ local jobrefs_before = {}
+ local jobgraph = jobpool.new()
+ for _, target in ipairs(targets_root) do
+ _add_jobgraph_for_target_and_deps(jobgraph, jobgraph:rootjob(), target, jobrefs, jobrefs_before)
+ end
+
+ -- add fence jobs, @see https://github.com/xmake-io/xmake/issues/5003
+ for _, target in ipairs(project.ordertargets()) do
+ local target_job_before = jobrefs_before[target:name()]
+ if target_job_before then
+ for _, dep in ipairs(target:orderdeps()) do
+ if dep:policy("build.fence") then
+ local fence_job = jobrefs[dep:name()]
+ if fence_job then
+ jobgraph:add(fence_job, target_job_before)
+ end
+ end
+ end
+ end
+ end
+
+ return jobgraph]]
+end
function main(targetnames, opt)
+ local jobgraph = _get_prepare_jobs(targetnames, opt)
+ if jobgraph and not jobgraph:empty() then
+ local curdir = os.curdir()
+ runjobs("prepare", jobgraph, {on_exit = function (errors)
+ import("utils.progress")
+ if errors and progress.showing_without_scroll() then
+ print("")
+ end
+ end, comax = option.get("jobs") or 1, curdir = curdir})
+ os.cd(curdir)
+ end
end
diff --git a/xmake/actions/build/target_utils.lua b/xmake/actions/build/target_utils.lua
new file mode 100644
index 000000000..2c098400d
--- /dev/null
+++ b/xmake/actions/build/target_utils.lua
@@ -0,0 +1,73 @@
+--!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 target_utils.lua
+--
+
+-- imports
+import("core.base.option")
+import("core.base.hashset")
+import("core.project.config")
+import("core.project.project")
+
+-- get all root targets
+function get_root_targets(targetnames, opt)
+ opt = opt or {}
+
+ -- get root targets
+ local targets_root = {}
+ if targetnames then
+ for _, targetname in ipairs(table.wrap(targetnames)) do
+ local target = project.target(targetname)
+ if target then
+ table.insert(targets_root, target)
+ if option.get("rebuild") then
+ target:data_set("rebuilt", true)
+ if not option.get("shallow") then
+ for _, dep in ipairs(target:orderdeps()) do
+ dep:data_set("rebuilt", true)
+ end
+ end
+ end
+ end
+ end
+ else
+ local group_pattern = opt.group_pattern
+ local depset = hashset.new()
+ local targets = {}
+ for _, target in ipairs(project.ordertargets()) do
+ if target:is_enabled() then
+ local group = target:get("group")
+ if (target:is_default() and not group_pattern) or option.get("all") or (group_pattern and group and group:match(group_pattern)) then
+ for _, depname in ipairs(target:get("deps")) do
+ depset:insert(depname)
+ end
+ table.insert(targets, target)
+ end
+ end
+ end
+ for _, target in ipairs(targets) do
+ if not depset:has(target:name()) then
+ table.insert(targets_root, target)
+ end
+ if option.get("rebuild") then
+ target:data_set("rebuilt", true)
+ end
+ end
+ end
+ return targets_root
+end
diff --git a/xmake/modules/async/jobgraph.lua b/xmake/modules/async/jobgraph.lua
index cc4d0c743..3767cfe73 100644
--- a/xmake/modules/async/jobgraph.lua
+++ b/xmake/modules/async/jobgraph.lua
@@ -176,6 +176,11 @@ function jobgraph:size()
return self._size
end
+-- is empty?
+function jobgraph:empty()
+ return self:size() == 0
+end
+
-- tostring
function jobgraph:__tostring()
return string.format("<jobgraph:%s/%d>", self:name() or "anonymous", self:size())