Skip to content

Commit f3816e0

Browse files
committed
[Explicit Module Builds] Only specify '-fmodule-map-file' for bridging header Clang module dependencies
Relying on the corresponding field in the '-explicit-swift-module-map-file' provided by the driver. Only bridging headers require a module map because that's what aids header include resolution. With lazy module loading today, '.modulemap' parsing which happens when instantiating Clang is responsible for associating headers with modules. Then upon encountering a header include inside the bridging header the compiler knows which module corresponds to said header and is then able to load explicitly-provided PCM for that module. For all other module dependencies, they are only ever queried by-name from Swift, so '.modulemap' parsing is not necessary.
1 parent 3947fa9 commit f3816e0

File tree

7 files changed

+73
-9
lines changed

7 files changed

+73
-9
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class SearchPathOptions {
476476
std::vector<std::string> CandidateCompiledModules;
477477

478478
/// A map of explicit Swift module information.
479-
std::string ExplicitSwiftModuleMap;
479+
std::string ExplicitSwiftModuleMapPath;
480480

481481
/// Module inputs specified with -swift-module-input,
482482
/// <ModuleName, Path to .swiftmodule file>

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ struct ExplicitClangModuleInputInfo {
269269
ExplicitClangModuleInputInfo(
270270
std::string moduleMapPath, std::string modulePath,
271271
bool isFramework = false, bool isSystem = false,
272+
bool isBridgingHeaderDependency = true,
272273
std::optional<std::string> moduleCacheKey = std::nullopt)
273274
: moduleMapPath(moduleMapPath), modulePath(modulePath),
274275
isFramework(isFramework), isSystem(isSystem),
276+
isBridgingHeaderDependency(isBridgingHeaderDependency),
275277
moduleCacheKey(moduleCacheKey) {}
276278
// Path of the Clang module map file.
277279
std::string moduleMapPath;
@@ -281,6 +283,8 @@ struct ExplicitClangModuleInputInfo {
281283
bool isFramework = false;
282284
// A flag that indicates whether this module is a system module
283285
bool isSystem = false;
286+
// A flag that indicates whether this is a module dependency of a textual header input
287+
bool isBridgingHeaderDependency = true;
284288
// The cache key for clang module.
285289
std::optional<std::string> moduleCacheKey;
286290
};
@@ -367,7 +371,12 @@ class ExplicitModuleMapParser {
367371
swiftModuleSourceInfoPath, swiftModuleCacheKey, clangModuleCacheKey;
368372
std::optional<std::vector<std::string>> headerDependencyPaths;
369373
std::string clangModuleMapPath = "", clangModulePath = "";
370-
bool isFramework = false, isSystem = false;
374+
bool isFramework = false, isSystem = false,
375+
// The default value is 'true' in case the build system does not yet
376+
// support emitting this field, in which case we must be conservative and
377+
// ensure all dependencies get '-fmodule-map-file', instead of strictly
378+
// module dependencies of textual header inputs.
379+
isBridgingHeaderDependency = true;
371380
for (auto &entry : *mapNode) {
372381
auto key = getScalaNodeText(entry.getKey());
373382
if (key == "prebuiltHeaderDependencyPaths") {
@@ -394,6 +403,8 @@ class ExplicitModuleMapParser {
394403
swiftModuleCacheKey = val.str();
395404
} else if (key == "clangModuleCacheKey") {
396405
clangModuleCacheKey = val.str();
406+
} else if (key == "isBridgingHeaderDependency") {
407+
isBridgingHeaderDependency = parseBoolValue(val);
397408
} else {
398409
// Being forgiving for future fields.
399410
continue;
@@ -423,6 +434,7 @@ class ExplicitModuleMapParser {
423434
clangModulePath,
424435
isFramework,
425436
isSystem,
437+
isBridgingHeaderDependency,
426438
clangModuleCacheKey);
427439
clangModuleMap.try_emplace(moduleName, std::move(entry));
428440
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts,
19811981
Args.hasArg(OPT_disable_modules_validate_system_headers);
19821982

19831983
if (const Arg *A = Args.getLastArg(OPT_explicit_swift_module_map))
1984-
Opts.ExplicitSwiftModuleMap = A->getValue();
1984+
Opts.ExplicitSwiftModuleMapPath = A->getValue();
19851985
for (auto A : Args.getAllArgValues(options::OPT_swift_module_file)) {
19861986
if (validateSwiftModuleFileArgumentAndAdd(A, Diags,
19871987
Opts.ExplicitSwiftModuleInputs))

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,18 +747,18 @@ bool CompilerInstance::setUpModuleLoaders() {
747747
bool ExplicitModuleBuild =
748748
Invocation.getFrontendOptions().DisableImplicitModules;
749749
if (ExplicitModuleBuild ||
750-
!Invocation.getSearchPathOptions().ExplicitSwiftModuleMap.empty() ||
750+
!Invocation.getSearchPathOptions().ExplicitSwiftModuleMapPath.empty() ||
751751
!Invocation.getSearchPathOptions().ExplicitSwiftModuleInputs.empty()) {
752752
if (Invocation.getCASOptions().EnableCaching)
753753
ESML = ExplicitCASModuleLoader::create(
754754
*Context, getObjectStore(), getActionCache(), getDependencyTracker(),
755-
MLM, Invocation.getSearchPathOptions().ExplicitSwiftModuleMap,
755+
MLM, Invocation.getSearchPathOptions().ExplicitSwiftModuleMapPath,
756756
Invocation.getSearchPathOptions().ExplicitSwiftModuleInputs,
757757
IgnoreSourceInfoFile);
758758
else
759759
ESML = ExplicitSwiftModuleLoader::create(
760760
*Context, getDependencyTracker(), MLM,
761-
Invocation.getSearchPathOptions().ExplicitSwiftModuleMap,
761+
Invocation.getSearchPathOptions().ExplicitSwiftModuleMapPath,
762762
Invocation.getSearchPathOptions().ExplicitSwiftModuleInputs,
763763
IgnoreSourceInfoFile);
764764
}

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,8 +1847,8 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
18471847
}
18481848

18491849
// Pass down -explicit-swift-module-map-file
1850-
StringRef explicitSwiftModuleMap = searchPathOpts.ExplicitSwiftModuleMap;
1851-
genericSubInvocation.getSearchPathOptions().ExplicitSwiftModuleMap =
1850+
StringRef explicitSwiftModuleMap = searchPathOpts.ExplicitSwiftModuleMapPath;
1851+
genericSubInvocation.getSearchPathOptions().ExplicitSwiftModuleMapPath =
18521852
explicitSwiftModuleMap.str();
18531853

18541854
// Pass down VFSOverlay flags (do not need to inherit the options because
@@ -2188,6 +2188,7 @@ struct ExplicitSwiftModuleLoader::Implementation {
21882188
for (auto &entry : ExplicitClangModuleMap) {
21892189
const auto &moduleMapPath = entry.getValue().moduleMapPath;
21902190
if (!moduleMapPath.empty() &&
2191+
entry.getValue().isBridgingHeaderDependency &&
21912192
moduleMapsSeen.find(moduleMapPath) == moduleMapsSeen.end()) {
21922193
moduleMapsSeen.insert(moduleMapPath);
21932194
extraClangArgs.push_back(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/TestInputs)
4+
// RUN: split-file %s %t
5+
6+
// - Fixup the input module file map
7+
// RUN: sed -e "s|INPUTSDIR|%/t/TestInputs|g" %t/map.json.template > %t/map.json.template1
8+
// RUN: sed -e "s|STDLIBMOD|%/stdlib_module|g" %t/map.json.template1 > %t/map.json.template2
9+
// RUN: sed -e "s|ONONEMOD|%/ononesupport_module|g" %t/map.json.template2 > %t/map.json.template3
10+
// RUN: sed -e "s|CHEADERSDIR|%/S/Inputs/CHeaders|g" %t/map.json.template3 > %t/map.json.template4
11+
// RUN: sed -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/map.json.template4 > %t/map.json
12+
13+
// - Pre-compile explicit module dependency inputs
14+
// RUN: %target-swift-emit-pcm -module-name A -o %t/TestInputs/A.pcm %S/Inputs/CHeaders/module.modulemap
15+
// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/TestInputs/SwiftShims.pcm
16+
17+
// RUN: %target-swift-frontend -c -disable-implicit-swift-modules -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -explicit-swift-module-map-file %t/map.json -primary-file %t/bridging_header_modulemap_only.swift -o %t/bridging_header_modulemap_only.o -dump-clang-diagnostics 2>&1 | %FileCheck %s --check-prefix=CHECK-CLANG-COMMAND
18+
19+
//--- map.json.template
20+
[
21+
{
22+
"moduleName": "Swift",
23+
"modulePath": "STDLIBMOD",
24+
"isFramework": false
25+
},
26+
{
27+
"moduleName": "SwiftOnoneSupport",
28+
"modulePath": "ONONEMOD",
29+
"isFramework": false
30+
},
31+
{
32+
"moduleName": "SwiftShims",
33+
"isFramework": false,
34+
"isBridgingHeaderDependency": false,
35+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
36+
"clangModulePath": "INPUTSDIR/SwiftShims.pcm"
37+
},
38+
{
39+
"moduleName": "A",
40+
"isFramework": false,
41+
"isBridgingHeaderDependency": true,
42+
"clangModulePath": "INPUTSDIR/A.pcm",
43+
"clangModuleMapPath": "CHEADERSDIR/module.modulemap"
44+
}
45+
]
46+
47+
//--- bridging_header_modulemap_only.swift
48+
import A
49+
50+
// CHECK-CLANG-COMMAND: -fmodule-map-file={{.*}}{{/|\\}}CHeaders{{/|\\}}module.modulemap
51+
// CHECK-CLANG-COMMAND-NOT: -fmodule-map-file={{.*}}{{/|\\}}swift{{/|\\}}shims{{/|\\}}module.modulemap

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4519,7 +4519,7 @@ int main(int argc, char *argv[]) {
45194519
InitInvok.getLangOptions().addCustomConditionalCompilationFlag(ConfigName);
45204520

45214521
if (!options::ExplicitSwiftModuleMap.empty()) {
4522-
InitInvok.getSearchPathOptions().ExplicitSwiftModuleMap =
4522+
InitInvok.getSearchPathOptions().ExplicitSwiftModuleMapPath =
45234523
options::ExplicitSwiftModuleMap;
45244524
InitInvok.getFrontendOptions().DisableImplicitModules = true;
45254525
}

0 commit comments

Comments
 (0)