Skip to content

Commit 865af27

Browse files
committed
feat(C++ modules) add support of reduced bmi for clang
1 parent 750f981 commit 865af27

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

xmake/rules/c++/modules/clang/builder.lua

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,30 @@ import("support")
3131
import(".mapper")
3232
import(".builder", {inherit = true})
3333

34+
function _get_bmifile(target, module)
35+
local has_reduced_bmi = support.get_modulesreducedbmiflag(target)
36+
local has_two_phases = target:policy("build.c++.modules.two_phases")
37+
local add_reduced_flag = not has_two_phases and has_reduced_bmi
38+
local bmifile = module.bmifile
39+
40+
if has_two_phases and add_reduced_flag then
41+
bmifile = path.join(path.directory(module.bmifile), "reduced." .. path.filename(module.bmifile))
42+
end
43+
44+
return bmifile, add_reduced_flag
45+
end
46+
3447
function _make_modulebuildflags(target, module, opt)
3548
assert(not module.headerunit)
49+
50+
local modules_reduced_bmi_flag = support.get_modulesreducedbmiflag(target)
51+
local has_two_phases = target:policy("build.c++.modules.two_phases")
3652
local flags
37-
local two_phases = target:policy("build.c++.modules.two_phases")
3853
if opt.bmi then
3954
local module_outputflag = support.get_moduleoutputflag(target)
4055

4156
flags = {"-x", "c++-module"}
57+
4258
if not opt.objectfile then
4359
table.insert(flags, "--precompile")
4460
if target:has_tool("cxx", "clang_cl") then
@@ -49,10 +65,18 @@ function _make_modulebuildflags(target, module, opt)
4965
if std then
5066
table.join2(flags, {"-Wno-include-angled-in-module-purview", "-Wno-reserved-module-identifier", "-Wno-deprecated-declarations"})
5167
end
52-
table.insert(flags, module_outputflag .. module.bmifile)
68+
69+
local bmifile, add_reduced_flag = _get_bmifile(target, module)
70+
if add_reduced_flag then
71+
table.insert(flags, modules_reduced_bmi_flag)
72+
end
73+
74+
if not has_two_phases or add_reduced_flag then
75+
table.insert(flags, module_outputflag .. bmifile)
76+
end
5377
else
5478
flags = {}
55-
if not two_phases or not module.bmifile then
79+
if not has_two_phases or not module.bmifile then
5680
flags = {"-x", "c++"}
5781
end
5882
local std = (module.name == "std" or module.name == "std.compat")
@@ -200,7 +224,8 @@ function _get_requiresflags(target, module)
200224
local dep_module = mapper.get(target, required)
201225
assert(dep_module, "module dependency %s required for %s not found", required, name)
202226

203-
local mapflag = dep_module.headerunit and modulefileflag .. dep_module.bmifile or format("%s%s=%s", modulefileflag, required, dep_module.bmifile)
227+
local dep_bmifile, _ = dep.headerunit and dep_module.bmifile or _get_bmifile(target, dep_module)
228+
local mapflag = dep_module.headerunit and modulefileflag .. dep_bmifile or format("%s%s=%s", modulefileflag, required, dep_bmifile)
204229
table.insert(requiresflags, mapflag)
205230

206231
-- append deps
@@ -219,6 +244,7 @@ end
219244
function _append_requires_flags(target, module)
220245
local cxxflags = {}
221246
local requiresflags = _get_requiresflags(target, module)
247+
local has_two_phases = target:policy("build.c++.modules.two_phases")
222248
local hide_dependencies = target:policy("build.c++.modules.hide_dependencies")
223249
if #requiresflags> 0 then
224250
for _, flag in ipairs(requiresflags) do

xmake/rules/c++/modules/clang/support.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,20 @@ function get_moduleheaderflag(target)
347347
return moduleheaderflag or nil
348348
end
349349

350+
function get_modulesreducedbmiflag(target)
351+
local modulesreducedbmiflag = _g.modulesreducedbmiflag
352+
if modulesreducedbmiflag == nil then
353+
local compinst = target:compiler("cxx")
354+
if compinst:has_flags("-fmodules-reduced-bmi", "cxxflags", {flagskey = "clang_modules_reduced_bmi"}) then
355+
modulesreducedbmiflag = "-fmodules-reduced-bmi"
356+
elseif compinst:has_flags("-fexperimental-modules-reduced-bmi", "cxxflags", {flagskey = "clang_modules_reduced_bmi"}) then
357+
modulesreducedbmiflag = "-fexperimental-modules-reduced-bmi"
358+
end
359+
_g.modulesreducedbmiflag = modulesreducedbmiflag or false
360+
end
361+
return modulesreducedbmiflag or nil
362+
end
363+
350364
function has_clangscandepssupport(target)
351365
local support_clangscandeps = _g.support_clangscandeps
352366
if support_clangscandeps == nil then

0 commit comments

Comments
 (0)