Skip to content

Commit afa8d32

Browse files
committed
(C++ modules support) fix unitybuild, pch and c++ modules objectfiles conflicts
1 parent 8fe7c16 commit afa8d32

File tree

8 files changed

+113
-89
lines changed

8 files changed

+113
-89
lines changed

xmake/core/project/target.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,6 @@ function _instance:objectfiles()
20992099
-- get object files from source batches
21002100
local objectfiles = {}
21012101
local batchcount = 0
2102-
local sourcebatches = self:sourcebatches()
21032102
local orderkeys = table.keys(sourcebatches)
21042103
table.sort(orderkeys) -- @note we need to guarantee the order of objectfiles for depend.is_changed() and etc.
21052104
for _, k in ipairs(orderkeys) do
@@ -2381,7 +2380,7 @@ function _instance:sourcebatches()
23812380
if filerule:extraconf("sourcekinds", sourcekind, "objectfiles") ~= false then
23822381
sourcebatch.objectfiles = sourcebatch.objectfiles or {}
23832382
sourcebatch.dependfiles = sourcebatch.dependfiles or {}
2384-
local objectfile = self:objectfile(sourcefile, sourcekind)
2383+
local objectfile = self:objectfile(sourcefile)
23852384
table.insert(sourcebatch.objectfiles, objectfile)
23862385
table.insert(sourcebatch.dependfiles, self:dependfile(objectfile))
23872386
end

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
191191
local jobdeps = {}
192192
local buildfilejobs = {}
193193

194-
-- if two phase supported only build interface and implementation named modules bmi
194+
-- get named modules
195195
local _built_modules = {}
196196
for _, sourcefile in ipairs(built_modules) do
197197
local module = mapper.get(target, sourcefile)
198-
if module.interface or module.implementation then
198+
if module.name then
199199
table.insert(_built_modules, sourcefile)
200200
else
201201
if not support.is_bmionly(target, sourcefile) then
@@ -209,7 +209,6 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
209209
end
210210
end
211211
end
212-
213212
-- add module jobs
214213
local moduletype = has_two_phase_compilation_support and "bmi" or "onephase"
215214
local buildgroup = _get_module_buildgroup_for(target, moduletype)
@@ -221,7 +220,7 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
221220
table.insert(buildfilejobs, buildfilejob)
222221
jobgraph:add(buildfilejob, function(_, _, jobopt)
223222
-- build bmi if named job
224-
jobopt.bmi = module.interface or module.implementation
223+
jobopt.bmi = module.name
225224
-- build objectfile here if two phase compilation is not supported
226225
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
227226
builder.make_module_job(target, module, jobopt)
@@ -261,7 +260,7 @@ function build_objectfiles_for_jobgraph(target, jobgraph, built_modules)
261260
else
262261
for _, sourcefile in ipairs(built_modules) do
263262
local module = mapper.get(target, sourcefile)
264-
if not module.interface and not module.implementation then
263+
if not module.name then
265264
table.insert(_built_modules, sourcefile)
266265
end
267266
end
@@ -295,11 +294,11 @@ function build_modules_for_batchjobs(target, batchjobs, built_modules, opt)
295294
local builder = _builder(target)
296295
local has_two_phase_compilation_support = support.has_two_phase_compilation_support(target)
297296

298-
-- if two phase supported only build interface and implementation named modules bmi
297+
-- if two phase supported only build named modules bmi
299298
local _built_modules = {}
300299
for _, sourcefile in ipairs(built_modules) do
301300
local module = mapper.get(target, sourcefile)
302-
if module.interface or module.implementation then
301+
if module.name then
303302
table.insert(_built_modules, sourcefile)
304303
end
305304
end
@@ -339,7 +338,7 @@ function build_modules_for_batchjobs(target, batchjobs, built_modules, opt)
339338
sourcefile = module.sourcefile,
340339
job = batchjobs:newjob(buildfilejob, function(_, _, jobopt)
341340
-- build bmi if named job
342-
jobopt.bmi = module.interface or module.implementation
341+
jobopt.bmi = module.name
343342
-- build objectfile here if two phase compilation is not supported
344343
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
345344
builder.make_module_job(target, module, jobopt)
@@ -362,7 +361,7 @@ function build_objectfiles_for_batchjobs(target, batchjobs, built_modules, opt)
362361
else
363362
for _, sourcefile in ipairs(built_modules) do
364363
local module = mapper.get(target, sourcefile)
365-
if not module.interface and not module.implementation then
364+
if not module.name then
366365
table.insert(_built_modules, sourcefile)
367366
end
368367
end
@@ -399,7 +398,7 @@ function build_modules_for_batchcmds(target, batchcmds, built_modules, opt)
399398
local _built_modules = {}
400399
for _, sourcefile in ipairs(built_modules) do
401400
local module = mapper.get(target, sourcefile)
402-
if module.interface or module.implementation then
401+
if module.name then
403402
table.insert(_built_modules, sourcefile)
404403
end
405404
end
@@ -408,7 +407,7 @@ function build_modules_for_batchcmds(target, batchcmds, built_modules, opt)
408407
local bmionly = support.is_bmionly(target, sourcefile)
409408
local module = mapper.get(target, sourcefile)
410409
local jobopt = {}
411-
jobopt.bmi = module.interface or module.implementation
410+
jobopt.bmi = module.name
412411
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
413412
jobopt.progress = opt.progress
414413
depmtime = math.max(depmtime, builder.make_module_buildcmds(target, batchcmds, module, jobopt))
@@ -429,8 +428,8 @@ function build_objectfiles_for_batchcmds(target, batchcmds, built_modules, opt)
429428
else
430429
for _, sourcefile in ipairs(built_modules) do
431430
local module = mapper.get(target, sourcefile)
432-
if not module.interface and not module.implementation then
433-
table.insert(_built_modules, sourcefile)
431+
if not module.name then
432+
table.insert(_built_modules, sourcefile)
434433
end
435434
end
436435
end
@@ -641,7 +640,7 @@ end
641640
function build_bmis(target, jobgraph, _, opt)
642641
opt = opt or {}
643642
if target:data("cxx.has_modules") then
644-
if target:is_moduleonly() and not target:data("cxx.modules.reused") then
643+
if target:is_moduleonly() and not target:data("cxx.modules.reused") or target:is_phony() then
645644
return
646645
end
647646
local modules = scanner.get_modules(target)
@@ -686,7 +685,7 @@ end
686685
function build_objectfiles(target, jobgraph, _, opt)
687686

688687
if target:data("cxx.has_modules") then
689-
if target:is_moduleonly() and not target:data("cxx.modules.reused") then
688+
if target:is_moduleonly() and not target:data("cxx.modules.reused") or target:is_phony() then
690689
return
691690
end
692691
local modules = scanner.get_modules(target)

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ function make_module_job(target, module, opt)
218218

219219
local dryrun = option.get("dry-run")
220220

221-
-- append requires flags
222-
-- if module.deps then
223-
-- _append_requires_flags(target, module)
224-
-- end
225-
226221
local build = should_build(target, module)
227222
local bmi = opt and opt.bmi
228223
local objectfile = opt and opt.objectfile
@@ -246,7 +241,7 @@ function make_module_job(target, module, opt)
246241
elseif bmi then
247242
_compile_bmi_step(target, module, opt)
248243
else
249-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
244+
if support.has_module_extension(module.sourcefile) or module.name then
250245
_compile_objectfile_step(target, module, opt)
251246
else
252247
os.tryrm(module.objectfile) -- force rebuild for .cpp files
@@ -277,7 +272,7 @@ function make_module_buildcmds(target, batchcmds, module, opt)
277272
elseif bmi then
278273
_compile_bmi_step(target, module, table.join(opt, {batchcmds = batchcmds}))
279274
else
280-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
275+
if support.has_module_extension(module.sourcefile) or module.name then
281276
_compile_objectfile_step(target, module, table.join(opt, {batchcmds = batchcmds}))
282277
else
283278
batchcmds:rm(module.objectfile) -- force rebuild for .cpp files

xmake/rules/c++/modules/config.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,20 @@ function main(target)
6767
wprint("build.c++.modules.tryreuse.discriminate_on_defines is deprecated, please use build.c++.modules.reuse.strict")
6868
end
6969

70+
-- if containes modules, enable objectfiles output of c++.build.modules.builder
71+
local rule = target:rule("c++.build.modules.builder")
72+
rule = rule:clone()
73+
if rule then
74+
rule:extraconf_set("sourcekinds", "cxx", "objectfiles", true)
75+
target:rule_add(rule)
76+
target:add("files")
77+
end
78+
7079
-- moduleonly modules are implicitly public
7180
if target:is_moduleonly() then
72-
local sourcebatch = target:sourcebatches()["c++.build.modules.builder"]
73-
if sourcebatch then
81+
local sourcebatches = target:sourcebatches()
82+
if sourcebatches and sourcebatches["c++.build.modules.scanner"] then
83+
local sourcebatch = sourcebatches["c++.build.modules.scanner"]
7484
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
7585
target:fileconfig_add(sourcefile, {public = true})
7686
end
@@ -81,11 +91,15 @@ end
8191

8292
function insert_stdmodules(target)
8393
if target:data("cxx.has_modules") then
84-
-- add std modules to sourcebatch
85-
local stdmodules = support.get_stdmodules(target)
86-
for _, sourcefile in ipairs(stdmodules) do
87-
table.insert(target:sourcebatches()["c++.build.modules.scanner"].sourcefiles, sourcefile)
88-
table.insert(target:sourcebatches()["c++.build.modules.builder"].sourcefiles, sourcefile)
94+
local sourcebatches = target:sourcebatches()
95+
if sourcebatches and sourcebatches["c++.build.modules.scanner"] then
96+
local sourcebatch = sourcebatches["c++.build.modules.scanner"]
97+
sourcebatch.sourcefiles = sourcebatch.sourcefiles or {}
98+
-- add std modules to sourcebatch
99+
local stdmodules = support.get_stdmodules(target)
100+
for _, sourcefile in ipairs(stdmodules) do
101+
table.insert(sourcebatch.sourcefiles, sourcefile)
102+
end
89103
end
90104
end
91105
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ end
129129
function _get_maplines(target, module)
130130

131131
local maplines = {}
132-
if module.interface or module.implementation then
132+
if module.name then
133133
table.insert(maplines, module.name .. " " .. path.absolute(module.bmifile))
134134
end
135135
for dep_name, dep_module in table.orderpairs(module.deps) do
@@ -223,7 +223,7 @@ function make_module_job(target, module, opt)
223223
end
224224

225225
local flags = {"-x", "c++"}
226-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
226+
if support.has_module_extension(module.sourcefile) or module.name then
227227
if bmi then
228228
if objectfile then
229229
table.insert(flags, module_flag)
@@ -270,7 +270,7 @@ function make_module_buildcmds(target, batchcmds, module, opt)
270270
end
271271

272272
local flags = {"-x", "c++"}
273-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
273+
if support.has_module_extension(module.sourcefile) or module.name then
274274
if bmi then
275275
if objectfile then
276276
table.insert(flags, module_flag)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function _make_modulebuildflags(target, module, opt)
4343
local bmionly = opt.bmi and not opt.objectfile
4444

4545
local flags
46-
if module.interface or module.implementation then -- named module
46+
if module.name then -- named module
4747
flags = table.join("-TP", module.interface and interfaceflag or internalpartitionflag, bmionly and ifconlyflag or {}, ifcoutputflag, path(module.bmifile))
4848
else
4949
flags = {"-TP"}
@@ -258,7 +258,7 @@ function make_module_job(target, module, opt)
258258
elseif bmi then
259259
_compile_bmi_step(target, module, opt)
260260
else
261-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
261+
if support.has_module_extension(module.sourcefile) or module.name then
262262
_compile_objectfile_step(target, module, opt)
263263
else
264264
os.tryrm(module.objectfile) -- force rebuild for .cpp files
@@ -287,7 +287,7 @@ function make_module_buildcmds(target, batchcmds, module, opt)
287287
elseif bmi then
288288
_compile_bmi_step(target, module, table.join(opt, {batchcmds = batchcmds}))
289289
else
290-
if support.has_module_extension(module.sourcefile) or module.interface or module.implementation then
290+
if support.has_module_extension(module.sourcefile) or module.name then
291291
_compile_objectfile_step(target, module, table.join(opt, {batchcmds = batchcmds}))
292292
else
293293
batchcmds:rm(module.objectfile) -- force rebuild for .cpp files

0 commit comments

Comments
 (0)