Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "build.fence" policy to block all targets build in depend chain #5011

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/projects/other/autogen_codedep/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ target("autogen")
set_arch(os.arch())
add_files("src/autogen.cpp")
set_languages("c++11")
set_policy("build.fence", true)

target("test")
set_kind("binary")
add_deps("autogen")
add_rules("autogen")
add_files("src/main.cpp")
add_files("src/*.in")
set_policy("build.across_targets_in_parallel", false)

31 changes: 24 additions & 7 deletions xmake/actions/build/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,23 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target)
end

-- add batch jobs for the given target and deps
function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, jobrefs, target)
function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, target, jobrefs, jobrefs_before)
local targetjob_ref = jobrefs[target:name()]
if targetjob_ref then
batchjobs:add(targetjob_ref, rootjob)
else
local job_build_before, job_build, job_build_after = _add_batchjobs_for_target(batchjobs, rootjob, target)
if job_build_before and job_build and job_build_after then
jobrefs[target:name()] = job_build_after
jobrefs_before[target:name()] = job_build_before
for _, depname in ipairs(target:get("deps")) do
local dep = project.target(depname)
local targetjob = job_build
-- @see https://github.com/xmake-io/xmake/discussions/2500
if dep:policy("build.across_targets_in_parallel") == false then
targetjob = job_build_before
end
_add_batchjobs_for_target_and_deps(batchjobs, targetjob, jobrefs, dep)
_add_batchjobs_for_target_and_deps(batchjobs, targetjob, dep, jobrefs, jobrefs_before)
end
end
end
Expand Down Expand Up @@ -251,7 +252,7 @@ function get_batchjobs(targetnames, group_pattern)
else
local depset = hashset.new()
local targets = {}
for _, target in pairs(project.targets()) do
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
Expand All @@ -262,7 +263,7 @@ function get_batchjobs(targetnames, group_pattern)
end
end
end
for _, target in pairs(targets) do
for _, target in ipairs(targets) do
if not depset:has(target:name()) then
table.insert(targets_root, target)
end
Expand All @@ -274,10 +275,27 @@ function get_batchjobs(targetnames, group_pattern)

-- generate batch jobs for default or all targets
local jobrefs = {}
local jobrefs_before = {}
local batchjobs = jobpool.new()
for _, target in pairs(targets_root) do
_add_batchjobs_for_target_and_deps(batchjobs, batchjobs:rootjob(), jobrefs, target)
for _, target in ipairs(targets_root) do
_add_batchjobs_for_target_and_deps(batchjobs, batchjobs: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
batchjobs:add(fence_job, target_job_before)
end
end
end
end
end

return batchjobs
end

Expand All @@ -303,4 +321,3 @@ function main(targetnames, group_pattern)
os.cd(curdir)
end
end

2 changes: 2 additions & 0 deletions xmake/core/project/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ function policy.policies()
["check.auto_map_flags"] = {description = "Enable map gcc flags to the current compiler and linker automatically.", default = true, type = "boolean"},
-- We will check the compatibility of target and package licenses
["check.target_package_licenses"] = {description = "Enable check the compatibility of target and package licenses.", default = true, type = "boolean"},
-- Provide a way to block all targets build that depends on self
["build.fence"] = {description = "Block all targets build that depends on self.", default = false, type = "boolean"},
-- We can compile the source files for each target in parallel
["build.across_targets_in_parallel"] = {description = "Enable compile the source files for each target in parallel.", default = true, type = "boolean"},
-- Merge archive intead of linking for all dependent targets
Expand Down
Loading