summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-04-24 09:25:20 +0800
committerGitHub <[email protected]>2025-04-24 09:25:20 +0800
commite49b9dd96d4a1ed7b8aa30ff8e4bd3eef724a59d (patch)
tree2f1a05acfc39d8f49ee738ef6bcbc148cec7f2c6
parent4333d588d626270677cf5de375437498a0fb68e4 (diff)
parenta26ab0a2df198a1af1c3a68d63fd16b042b01486 (diff)
Merge pull request #6360 from Arthapz/update-module-policies
(C++ modules support) Update module policies
-rw-r--r--tests/projects/c++/modules/test_headerunits.lua12
-rw-r--r--xmake/core/project/policy.lua26
-rw-r--r--xmake/rules/c++/modules/clang/scanner.lua6
-rw-r--r--xmake/rules/c++/modules/clang/support.lua4
-rw-r--r--xmake/rules/c++/modules/config.lua24
-rw-r--r--xmake/rules/c++/modules/gcc/scanner.lua5
-rw-r--r--xmake/rules/c++/modules/gcc/support.lua8
-rw-r--r--xmake/rules/c++/modules/msvc/scanner.lua5
-rw-r--r--xmake/rules/c++/modules/msvc/support.lua4
9 files changed, 71 insertions, 23 deletions
diff --git a/tests/projects/c++/modules/test_headerunits.lua b/tests/projects/c++/modules/test_headerunits.lua
index d34c70757..264cca6e6 100644
--- a/tests/projects/c++/modules/test_headerunits.lua
+++ b/tests/projects/c++/modules/test_headerunits.lua
@@ -22,12 +22,12 @@ function main(t)
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "15.0") >= 0 then
-- clang headerunit are bugged
- -- os.exec("xmake f --toolchain=clang -c --yes --policies=build.c++.modules.std:n,build.c++.clang.fallbackscanner")
+ -- os.exec("xmake f --toolchain=clang -c --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
-- _build()
-- if semver.compare(clang.version, "17.0") >= 0 then
-- os.exec("xmake clean -a")
-- -- clang-scan-deps dependency detection doesn't support header units atm
- -- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes --policies=build.c++.modules.std:n,build.c++.clang.fallbackscanner")
+ -- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
-- _build()
-- end
end
@@ -40,14 +40,14 @@ function main(t)
end
elseif is_subhost("msys") then
-- on windows, mingw modulemapper doesn't handle headeunit path correctly, but it's working with mingw on macOS / Linux
- -- os.exec("xmake f -c -p mingw --yes --policies=build.c++.modules.std:n,build.c++.gcc.fallbackscanner")
+ -- os.exec("xmake f -c -p mingw --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
-- _build()
elseif is_host("linux") then
local gcc = find_tool("gcc", {version = true})
if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 and
os.arch() ~= "arm64" then -- gcc/arm64: internal compiler error: in core_vals, at cp/module.cc:6108
-- gcc dependency detection doesn't support header units atm
- os.exec("xmake f -c --yes --policies=build.c++.modules.std:n,build.c++.gcc.fallbackscanner")
+ os.exec("xmake f -c --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
_build()
end
local clang = find_tool("clang", {version = true})
@@ -55,14 +55,14 @@ function main(t)
if semver.compare(clang.version, "19.0") >= 0 then
os.exec("xmake clean -a")
-- clang-scan-deps dependency detection doesn't support header units atm
- os.exec("xmake f --toolchain=clang -c --yes --policies=build.c++.modules.std:n,build.c++.clang.fallbackscanner")
+ os.exec("xmake f --toolchain=clang -c --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
_build()
end
-- libc++ headerunit are bugged
-- if semver.compare(clang.version, "17.0") >= 0 then
-- os.exec("xmake clean -a")
-- -- clang-scan-deps dependency detection doesn't support header units atm
- -- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes --policies=build.c++.modules.std:n,build.c++.clang.fallbackscanner")
+ -- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes --policies=build.c++.modules.std:n,build.c++.modules.fallbackscanner:y")
-- _build()
-- end
end
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index c4da9fdd3..005775517 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -76,22 +76,30 @@ function policy.policies()
["build.c++.modules.std"] = {description = "Enable std modules.", default = true, type = "boolean"},
-- Enable unreferenced and non-public named module culling
["build.c++.modules.culling"] = {description = "Enable unrefereced and non-public named module culling.", default = true, type = "boolean"},
- -- Try to reuse compiled module bmi file if targets flags permit it
- ["build.c++.modules.tryreuse"] = {description = "Try to reuse compiled module if possible.", default = true, type = "boolean"},
- -- Enable module taking defines acbount for bmi reuse discrimination
+ -- Always reuse compiled module bmi file
+ ["build.c++.modules.reuse"] = {description = "Reuse compiled module artifacts if possible.", default = true, type = "boolean"},
+ ["build.c++.modules.tryreuse"] = {description = "Try to reuse compiled module if possible. (deprecated)", default = false, type = "boolean"},
+ -- If target will not reuse modules from target deps if defines are different
+ ["build.c++.modules.reuse.strict"] = {description = "Enable strict defines comparison when trying to reuse module.", default = false, type = "boolean"},
["build.c++.modules.tryreuse.discriminate_on_defines"] = {description = "Enable defines module reuse discrimination.", default = false, type = "boolean"},
+ -- Force C++ modules fallback dependency scanner for all compilers
+ ["build.c++.modules.fallbackscanner"] = {description = "Force fallback module dependency scanner.", default = false, type = "boolean"},
-- Force C++ modules fallback dependency scanner for clang
- ["build.c++.clang.fallbackscanner"] = {description = "Force clang fallback module dependency scanner.", default = false, type = "boolean"},
+ ["build.c++.modules.clang.fallbackscanner"] = {description = "Force clang fallback module dependency scanner.", default = false, type = "boolean"},
+ ["build.c++.clang.fallbackscanner"] = {description = "Force clang fallback module dependency scanner. (deprecated)", default = false, type = "boolean"},
-- Force C++ modules fallback dependency scanner for msvc
- ["build.c++.msvc.fallbackscanner"] = {description = "Force msvc fallback module dependency scanner.", default = false, type = "boolean"},
- -- Set the default vs runtime, e.g. MT, MD
- ["build.c++.msvc.runtime"] = {description = "Set the default vs runtime.", type = "string", values = {"MT", "MD"}},
+ ["build.c++.modules.msvc.fallbackscanner"] = {description = "Force msvc fallback module dependency scanner.", default = false, type = "boolean"},
+ ["build.c++.msvc.fallbackscanner"] = {description = "Force msvc fallback module dependency scanner. (deprecated)", default = false, type = "boolean"},
-- Force C++ modules fallback dependency scanner for gcc
- ["build.c++.gcc.fallbackscanner"] = {description = "Force gcc fallback module dependency scanner.", default = false, type = "boolean"},
+ ["build.c++.modules.gcc.fallbackscanner"] = {description = "Force gcc fallback module dependency scanner.", default = false, type = "boolean"},
+ ["build.c++.gcc.fallbackscanner"] = {description = "Force gcc fallback module dependency scanner. (deprecated)", default = false, type = "boolean"},
-- Force to enable new cxx11 abi in C++ modules for gcc
-- If in the future, gcc can support it well, we'll turn it on by default
-- https://github.com/xmake-io/xmake/issues/3855
- ["build.c++.gcc.modules.cxx11abi"] = {description = "Force to enable new cxx11 abi in C++ modules for gcc.", type = "boolean"},
+ ["build.c++.modules.gcc.cxx11abi"] = {description = "Force to enable new cxx11 abi in C++ modules for gcc.", type = "boolean"},
+ ["build.c++.gcc.modules.cxx11abi"] = {description = "Force to enable new cxx11 abi in C++ modules for gcc. (deprecated)", type = "boolean", default = false},
+ -- Set the default vs runtime, e.g. MT, MD
+ ["build.c++.msvc.runtime"] = {description = "Set the default vs runtime.", type = "string", values = {"MT", "MD"}},
-- Enable cuda device link
["build.cuda.devlink"] = {description = "Enable Cuda devlink.", type = "boolean"},
-- Enable build jobgraph
diff --git a/xmake/rules/c++/modules/clang/scanner.lua b/xmake/rules/c++/modules/clang/scanner.lua
index 5b7d10658..2b4738f65 100644
--- a/xmake/rules/c++/modules/clang/scanner.lua
+++ b/xmake/rules/c++/modules/clang/scanner.lua
@@ -32,7 +32,9 @@ function generate_dependency_for(target, sourcefile, opt)
local changed = false
local dependfile = target:dependfile(sourcefile)
local flags = compinst:compflags({sourcefile = sourcefile, target = target}) or {}
- local fileconfig = target:fileconfig(sourcefile)
+ local fallbackscanner = target:policy("build.c++.modules.fallbackscanner") or
+ target:policy("build.c++.modules.clang.fallbackscanner") or
+ target:policy("build.c++.clang.fallbackscanner")
depend.on_changed(function()
if opt.progress then
@@ -42,7 +44,7 @@ function generate_dependency_for(target, sourcefile, opt)
local outputdir = support.get_outputdir(target, sourcefile)
local jsonfile = path.translate(path.join(outputdir, path.filename(sourcefile) .. ".json"))
local has_clangscandepssupport = support.has_clangscandepssupport(target)
- if has_clangscandepssupport and not target:policy("build.c++.clang.fallbackscanner") then
+ if has_clangscandepssupport and not fallbackscanner then
-- We need absolute path of clang to use clang-scan-deps
-- See https://clang.llvm.org/docs/StandardCPlusPlusModules.html#possible-issues-failed-to-find-system-headers
local clang_path = compinst:program()
diff --git a/xmake/rules/c++/modules/clang/support.lua b/xmake/rules/c++/modules/clang/support.lua
index adfd5730c..08a453ffa 100644
--- a/xmake/rules/c++/modules/clang/support.lua
+++ b/xmake/rules/c++/modules/clang/support.lua
@@ -149,7 +149,9 @@ function strip_flags(target, flags)
"-cxx-isystem",
"-Q",
}
- if not target:policy("build.c++.modules.tryreuse.discriminate_on_defines") then
+ local strict = target:policy("build.c++.modules.reuse.strict") or
+ target:policy("build.c++.modules.tryreuse.discriminate_on_defines")
+ if not strict then
table.join2(strippable_flags, {"-D", "-U"})
end
local output = {}
diff --git a/xmake/rules/c++/modules/config.lua b/xmake/rules/c++/modules/config.lua
index a39e4c4e0..4edbe1e1b 100644
--- a/xmake/rules/c++/modules/config.lua
+++ b/xmake/rules/c++/modules/config.lua
@@ -25,6 +25,10 @@ function main(target)
-- we disable to build across targets in parallel, because the source files may depend on other target modules
-- @see https://github.com/xmake-io/xmake/issues/1858
if support.contains_modules(target) then
+
+ -- unity build can't work with modules
+ assert(not target:rule("c++.unity_build"), "C++ unity build is not compatible with C++ modules")
+
-- @note this will cause cross-parallel builds to be disabled for all sub-dependent targets,
-- even if some sub-targets do not contain C++ modules.
--
@@ -45,6 +49,26 @@ function main(target)
-- mark this target with modules
target:data_set("cxx.has_modules", true)
+ -- warn about deprecated policies
+ if target:policy("build.c++.gcc.modules.cxx11abi") then
+ wprint("build.c++.gcc.modules.cxx11abi is deprecated, please use build.c++.modules.gcc.cxx11abi")
+ end
+ if target:policy("build.c++.clang.fallbackscanner") then
+ wprint("build.c++.clang.fallbackscanner is deprecated, please use build.c++.modules.clang.fallbackscanner")
+ end
+ if target:policy("build.c++.gcc.fallbackscanner") then
+ wprint("build.c++.gcc.fallbackscanner is deprecated, please use build.c++.modules.gcc.fallbackscanner")
+ end
+ if target:policy("build.c++.msvc.fallbackscanner") then
+ wprint("build.c++.msvc.fallbackscanner is deprecated, please use build.c++.modules.msvc.fallbackscanner")
+ end
+ if target:policy("build.c++.modules.tryreuse") then
+ wprint("build.c++.modules.tryreuse is deprecated, please use build.c++.modules.reuse")
+ end
+ if target:policy("build.c++.modules.tryreuse.discriminate_on_defines") then
+ wprint("build.c++.modules.tryreuse.discriminate_on_defines is deprecated, please use build.c++.modules.reuse.strict")
+ end
+
-- moduleonly modules are implicitly public
if target:is_moduleonly() then
local sourcebatch = target:sourcebatches()["c++.build.modules.builder"]
diff --git a/xmake/rules/c++/modules/gcc/scanner.lua b/xmake/rules/c++/modules/gcc/scanner.lua
index 1496d17dd..8abf08c44 100644
--- a/xmake/rules/c++/modules/gcc/scanner.lua
+++ b/xmake/rules/c++/modules/gcc/scanner.lua
@@ -37,6 +37,9 @@ function generate_dependency_for(target, sourcefile, opt)
local dependfile = target:dependfile(sourcefile)
local flags = compinst:compflags({sourcefile = sourcefile, target = target}) or {}
local changed = false
+ local fallbackscanner = target:policy("build.c++.modules.fallbackscanner") or
+ target:policy("build.c++.modules.gcc.fallbackscanner") or
+ target:policy("build.c++.gcc.fallbackscanner")
depend.on_changed(function()
if opt.progress then
@@ -46,7 +49,7 @@ function generate_dependency_for(target, sourcefile, opt)
local outputdir = support.get_outputdir(target, sourcefile)
local jsonfile = path.translate(path.join(outputdir, path.filename(sourcefile) .. ".json"))
local has_depsflags = depsformatflag and depsfileflag and depstargetflag
- if has_depsflags and not target:policy("build.c++.gcc.fallbackscanner") then
+ if has_depsflags and not fallbackscanner then
local ifile = path.translate(path.join(outputdir, path.filename(sourcefile) .. ".i"))
local dfile = path.translate(path.join(outputdir, path.filename(sourcefile) .. ".d"))
local compflags = table.join(flags or {}, baselineflags, {sourcefile, "-MT", jsonfile, "-MD", "-MF", dfile, depsformatflag, depsfileflag .. jsonfile, depstargetflag .. target:objectfile(sourcefile), "-o", ifile})
diff --git a/xmake/rules/c++/modules/gcc/support.lua b/xmake/rules/c++/modules/gcc/support.lua
index 52d8af79f..792e975f5 100644
--- a/xmake/rules/c++/modules/gcc/support.lua
+++ b/xmake/rules/c++/modules/gcc/support.lua
@@ -60,7 +60,9 @@ function load(target)
-- @see https://github.com/xmake-io/xmake/issues/2716#issuecomment-1225057760
-- https://github.com/xmake-io/xmake/issues/3855
- if target:policy("build.c++.gcc.modules.cxx11abi") then
+ local cxx11abi = target:policy("build.c++.modules.gcc.cxx11abi") or
+ target:policy("build.c++.gcc.modules.cxx11abi")
+ if cxx11abi then
target:add("cxxflags", "-D_GLIBCXX_USE_CXX11_ABI=1")
else
target:add("cxxflags", "-D_GLIBCXX_USE_CXX11_ABI=0")
@@ -81,7 +83,9 @@ function strip_flags(target, flags)
"-Q",
"-fmodule-mapper",
}
- if not target:policy("build.c++.modules.tryreuse.discriminate_on_defines") then
+ local strict = target:policy("build.c++.modules.reuse.strict") or
+ target:policy("build.c++.modules.tryreuse.discriminate_on_defines")
+ if not strict then
table.join2(strippable_flags, {"-D", "-U"})
end
local output = {}
diff --git a/xmake/rules/c++/modules/msvc/scanner.lua b/xmake/rules/c++/modules/msvc/scanner.lua
index b4d68df37..07342a55e 100644
--- a/xmake/rules/c++/modules/msvc/scanner.lua
+++ b/xmake/rules/c++/modules/msvc/scanner.lua
@@ -38,13 +38,16 @@ function generate_dependency_for(target, sourcefile, opt)
local compinst = target:compiler("cxx")
local flags = compinst:compflags({sourcefile = sourcefile, target = target}) or {}
local changed = false
+ local fallbackscanner = target:policy("build.c++.modules.fallbackscanner") or
+ target:policy("build.c++.modules.msvc.fallbackscanner") or
+ target:policy("build.c++.msvc.fallbackscanner")
depend.on_changed(function ()
progress.show(opt.progress, "${color.build.target}<%s> generating.module.deps %s", target:fullname(), sourcefile)
local outputdir = support.get_outputdir(target, sourcefile)
local jsonfile = path.join(outputdir, path.filename(sourcefile) .. ".module.json")
- if scandependenciesflag and not target:policy("build.c++.msvc.fallbackscanner") then
+ if scandependenciesflag and not fallbackscanner then
local dependency_flags = {jsonfile, sourcefile, ifcoutputflag, outputdir, "-Fo" .. target:objectfile(sourcefile)}
local compflags = table.join(flags, common_flags, dependency_flags)
os.vrunv(compinst:program(), winos.cmdargv(compflags), {envs = msvc:runenvs()})
diff --git a/xmake/rules/c++/modules/msvc/support.lua b/xmake/rules/c++/modules/msvc/support.lua
index f2b07b999..0da24501f 100644
--- a/xmake/rules/c++/modules/msvc/support.lua
+++ b/xmake/rules/c++/modules/msvc/support.lua
@@ -94,7 +94,9 @@ function strip_flags(target, flags)
"analyze",
"?",
}
- if not target:policy("build.c++.modules.tryreuse.discriminate_on_defines") then
+ local strict = target:policy("build.c++.modules.reuse.strict") or
+ target:policy("build.c++.modules.tryreuse.discriminate_on_defines")
+ if not strict then
table.join2(strippable_flags, {"D", "U"})
end
local output = {}