Skip to content

Commit

Permalink
Merge pull request #5011 from zhurongjun/dev
Browse files Browse the repository at this point in the history
add "build.fence" policy to block all targets build in depend chain
  • Loading branch information
waruqi committed Apr 26, 2024
2 parents e009e95 + 48b95cd commit 65486a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
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

0 comments on commit 65486a1

Please sign in to comment.