Skip to content

Commit 8ffb5b9

Browse files
authored
Merge pull request #6446 from xmake-io/unitybuild
Fix unitybuild with pch/modules
2 parents 941d60a + 2f8decd commit 8ffb5b9

File tree

8 files changed

+126
-95
lines changed

8 files changed

+126
-95
lines changed

xmake/core/project/target.lua

Lines changed: 10 additions & 6 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
@@ -2375,11 +2374,16 @@ function _instance:sourcebatches()
23752374
sourcebatch.sourcekind = sourcekind
23762375

23772376
-- insert object files to source batches
2378-
sourcebatch.objectfiles = sourcebatch.objectfiles or {}
2379-
sourcebatch.dependfiles = sourcebatch.dependfiles or {}
2380-
local objectfile = self:objectfile(sourcefile, sourcekind)
2381-
table.insert(sourcebatch.objectfiles, objectfile)
2382-
table.insert(sourcebatch.dependfiles, self:dependfile(objectfile))
2377+
-- and we need to avoid duplication with object files, which may cause some conflicts.
2378+
-- e.g. c++.build, c++ module and unity_build rules
2379+
-- @see https://github.com/xmake-io/xmake/issues/6420
2380+
if filerule:extraconf("sourcekinds", sourcekind, "objectfiles") ~= false then
2381+
sourcebatch.objectfiles = sourcebatch.objectfiles or {}
2382+
sourcebatch.dependfiles = sourcebatch.dependfiles or {}
2383+
local objectfile = self:objectfile(sourcefile)
2384+
table.insert(sourcebatch.objectfiles, objectfile)
2385+
table.insert(sourcebatch.dependfiles, self:dependfile(objectfile))
2386+
end
23832387
end
23842388
end
23852389
end

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
199199
local jobdeps = {}
200200
local buildfilejobs = {}
201201

202-
-- if two phase supported only build interface and implementation named modules bmi
202+
-- get named modules
203203
local _built_modules = {}
204204
for _, sourcefile in ipairs(built_modules) do
205205
local module = mapper.get(target, sourcefile)
206-
if module.interface or module.implementation then
206+
if module.name then
207207
table.insert(_built_modules, sourcefile)
208208
else
209209
if not support.is_bmionly(target, sourcefile) then
@@ -217,7 +217,6 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
217217
end
218218
end
219219
end
220-
221220
-- add module jobs
222221
local moduletype = has_two_phase_compilation_support and "bmi" or "onephase"
223222
local buildgroup = _get_module_buildgroup_for(target, moduletype)
@@ -229,7 +228,7 @@ function build_modules_for_jobgraph(target, jobgraph, built_modules)
229228
table.insert(buildfilejobs, buildfilejob)
230229
jobgraph:add(buildfilejob, function(_, _, jobopt)
231230
-- build bmi if named job
232-
jobopt.bmi = module.interface or module.implementation
231+
jobopt.bmi = module.name
233232
-- build objectfile here if two phase compilation is not supported
234233
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
235234
builder.make_module_job(target, module, jobopt)
@@ -271,7 +270,7 @@ function build_objectfiles_for_jobgraph(target, jobgraph, built_modules)
271270
else
272271
for _, sourcefile in ipairs(built_modules) do
273272
local module = mapper.get(target, sourcefile)
274-
if not module.interface and not module.implementation then
273+
if not module.name then
275274
table.insert(_built_modules, sourcefile)
276275
end
277276
end
@@ -306,11 +305,11 @@ function build_modules_for_batchjobs(target, batchjobs, built_modules, opt)
306305
local builder = _builder(target)
307306
local has_two_phase_compilation_support = support.has_two_phase_compilation_support(target)
308307

309-
-- if two phase supported only build interface and implementation named modules bmi
308+
-- if two phase supported only build named modules bmi
310309
local _built_modules = {}
311310
for _, sourcefile in ipairs(built_modules) do
312311
local module = mapper.get(target, sourcefile)
313-
if module.interface or module.implementation then
312+
if module.name then
314313
table.insert(_built_modules, sourcefile)
315314
end
316315
end
@@ -350,7 +349,7 @@ function build_modules_for_batchjobs(target, batchjobs, built_modules, opt)
350349
sourcefile = module.sourcefile,
351350
job = batchjobs:newjob(buildfilejob, function(_, _, jobopt)
352351
-- build bmi if named job
353-
jobopt.bmi = module.interface or module.implementation
352+
jobopt.bmi = module.name
354353
-- build objectfile here if two phase compilation is not supported
355354
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
356355
builder.make_module_job(target, module, jobopt)
@@ -373,7 +372,7 @@ function build_objectfiles_for_batchjobs(target, batchjobs, built_modules, opt)
373372
else
374373
for _, sourcefile in ipairs(built_modules) do
375374
local module = mapper.get(target, sourcefile)
376-
if not module.interface and not module.implementation then
375+
if not module.name then
377376
table.insert(_built_modules, sourcefile)
378377
end
379378
end
@@ -410,7 +409,7 @@ function build_modules_for_batchcmds(target, batchcmds, built_modules, opt)
410409
local _built_modules = {}
411410
for _, sourcefile in ipairs(built_modules) do
412411
local module = mapper.get(target, sourcefile)
413-
if module.interface or module.implementation then
412+
if module.name then
414413
table.insert(_built_modules, sourcefile)
415414
end
416415
end
@@ -419,7 +418,7 @@ function build_modules_for_batchcmds(target, batchcmds, built_modules, opt)
419418
local bmionly = support.is_bmionly(target, sourcefile)
420419
local module = mapper.get(target, sourcefile)
421420
local jobopt = {}
422-
jobopt.bmi = module.interface or module.implementation
421+
jobopt.bmi = module.name
423422
jobopt.objectfile = not has_two_phase_compilation_support and not bmionly
424423
jobopt.progress = opt.progress
425424
depmtime = math.max(depmtime, builder.make_module_buildcmds(target, batchcmds, module, jobopt))
@@ -440,8 +439,8 @@ function build_objectfiles_for_batchcmds(target, batchcmds, built_modules, opt)
440439
else
441440
for _, sourcefile in ipairs(built_modules) do
442441
local module = mapper.get(target, sourcefile)
443-
if not module.interface and not module.implementation then
444-
table.insert(_built_modules, sourcefile)
442+
if not module.name then
443+
table.insert(_built_modules, sourcefile)
445444
end
446445
end
447446
end
@@ -659,7 +658,7 @@ end
659658
function build_bmis(target, jobgraph, _, opt)
660659
opt = opt or {}
661660
if target:data("cxx.has_modules") then
662-
if target:is_moduleonly() and not target:data("cxx.modules.reused") then
661+
if target:is_moduleonly() and not target:data("cxx.modules.reused") or target:is_phony() then
663662
return
664663
end
665664
profiler.enter(target:fullname(), "c++ modules", "builder", "bmis")
@@ -706,7 +705,7 @@ end
706705
function build_objectfiles(target, jobgraph, _, opt)
707706

708707
if target:data("cxx.has_modules") then
709-
if target:is_moduleonly() and not target:data("cxx.modules.reused") then
708+
if target:is_moduleonly() and not target:data("cxx.modules.reused") or target:is_phony() then
710709
return
711710
end
712711
profiler.enter(target:fullname(), "c++ modules", "builder", "objectfiles")

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)