diff --git a/tests/projects/other/autogen_codedep/xmake.lua b/tests/projects/other/autogen_codedep/xmake.lua index 229439fef6c..5f2c76d678c 100644 --- a/tests/projects/other/autogen_codedep/xmake.lua +++ b/tests/projects/other/autogen_codedep/xmake.lua @@ -24,6 +24,7 @@ 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") @@ -31,5 +32,4 @@ target("test") add_rules("autogen") add_files("src/main.cpp") add_files("src/*.in") - set_policy("build.across_targets_in_parallel", false) diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua index 43e28bf2173..f4b371de7d8 100644 --- a/xmake/actions/build/build.lua +++ b/xmake/actions/build/build.lua @@ -207,7 +207,7 @@ 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) @@ -215,6 +215,7 @@ function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, jobrefs, target) 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 @@ -222,7 +223,7 @@ function _add_batchjobs_for_target_and_deps(batchjobs, rootjob, jobrefs, target) 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 @@ -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 @@ -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 @@ -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 @@ -303,4 +321,3 @@ function main(targetnames, group_pattern) os.cd(curdir) end end - diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index e99f02cafe8..4e495e44903 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -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