Skip to content

Commit c1599fe

Browse files
committed
Add -swift-module-file frontend flag for explicit Swift module dependencies
1 parent 7a89642 commit c1599fe

File tree

7 files changed

+55
-2
lines changed

7 files changed

+55
-2
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ ERROR(error_cannot_direct_cc1_pcm_build_in_mode,none,
110110
"'-direct-clang-cc1-module-build' only supported when building a PCM ('-emit-pcm')'", ())
111111
ERROR(error_unsupported_option_argument,none,
112112
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
113+
ERROR(error_swift_module_file_requires_delimeter,none,
114+
"-swift-module-file= argument requires format <name>=<path>, got: '%0'", (StringRef))
113115
ERROR(error_immediate_mode_missing_stdlib,none,
114116
"could not load the swift standard library", ())
115117
ERROR(error_immediate_mode_missing_library,none,

include/swift/AST/SearchPathOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ class SearchPathOptions {
355355
/// A map of explicit Swift module information.
356356
std::string ExplicitSwiftModuleMap;
357357

358+
/// Module inputs specified with -swift-module-input,
359+
/// <ModuleName, Path to .swiftmodule file>
360+
std::vector<std::pair<std::string, std::string>> ExplicitSwiftModuleInputs;
361+
358362
/// A map of placeholder Swift module dependency information.
359363
std::string PlaceholderDependencyModuleMap;
360364

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class SearchPathOptions;
129129
class CompilerInvocation;
130130

131131
/// A ModuleLoader that loads explicitly built Swift modules specified via
132-
/// -swift-module-file
132+
/// -swift-module-file or modules found in
133133
class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
134134
explicit ExplicitSwiftModuleLoader(ASTContext &ctx, DependencyTracker *tracker,
135135
ModuleLoadingMode loadMode,
@@ -164,6 +164,7 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
164164
create(ASTContext &ctx,
165165
DependencyTracker *tracker, ModuleLoadingMode loadMode,
166166
StringRef ExplicitSwiftModuleMap,
167+
const std::vector<std::pair<std::string, std::string>> &ExplicitSwiftModuleInputs,
167168
bool IgnoreSwiftSourceInfoFile);
168169

169170
/// Append visible module names to \p names. Note that names are possibly

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def disable_typo_correction : Flag<["-"], "disable-typo-correction">,
185185
def disable_implicit_swift_modules: Flag<["-"], "disable-implicit-swift-modules">,
186186
HelpText<"Disable building Swift modules implicitly by the compiler">;
187187

188+
def swift_module_file: Joined<["-"], "swift-module-file=">,
189+
MetaVarName<"<name>=<path>">,
190+
HelpText<"Specify Swift module input explicitly built from textual interface">;
191+
188192
def explicit_swift_module_map
189193
: Separate<["-"], "explicit-swift-module-map-file">, MetaVarName<"<path>">,
190194
HelpText<"Specify a JSON file containing information of explicit Swift modules">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,25 @@ static void ParseSymbolGraphArgs(symbolgraphgen::SymbolGraphOptions &Opts,
13631363
Opts.IncludeClangDocs = false;
13641364
}
13651365

1366+
static bool validateSwiftModuleFileArgumentAndAdd(const std::string &swiftModuleArgument,
1367+
DiagnosticEngine &Diags,
1368+
std::vector<std::pair<std::string, std::string>> &ExplicitSwiftModuleInputs) {
1369+
std::size_t foundDelimeterPos = swiftModuleArgument.find_first_of("=");
1370+
if (foundDelimeterPos == std::string::npos) {
1371+
Diags.diagnose(SourceLoc(), diag::error_swift_module_file_requires_delimeter,
1372+
swiftModuleArgument);
1373+
return true;
1374+
}
1375+
std::string moduleName = swiftModuleArgument.substr(0, foundDelimeterPos),
1376+
modulePath = swiftModuleArgument.substr(foundDelimeterPos+1);
1377+
if (!Lexer::isIdentifier(moduleName)) {
1378+
Diags.diagnose(SourceLoc(), diag::error_bad_module_name, moduleName, false);
1379+
return true;
1380+
}
1381+
ExplicitSwiftModuleInputs.emplace_back(std::make_pair(moduleName, modulePath));
1382+
return false;
1383+
}
1384+
13661385
static bool ParseSearchPathArgs(SearchPathOptions &Opts,
13671386
ArgList &Args,
13681387
DiagnosticEngine &Diags,
@@ -1415,6 +1434,11 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts,
14151434

14161435
if (const Arg *A = Args.getLastArg(OPT_explicit_swift_module_map))
14171436
Opts.ExplicitSwiftModuleMap = A->getValue();
1437+
for (auto A : Args.getAllArgValues(options::OPT_swift_module_file)) {
1438+
if (validateSwiftModuleFileArgumentAndAdd(A, Diags,
1439+
Opts.ExplicitSwiftModuleInputs))
1440+
return true;
1441+
}
14181442
for (auto A: Args.filtered(OPT_candidate_module_file)) {
14191443
Opts.CandidateCompiledModules.push_back(resolveSearchPath(A->getValue()));
14201444
}

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,12 @@ bool CompilerInstance::setUpModuleLoaders() {
566566
bool ExplicitModuleBuild =
567567
Invocation.getFrontendOptions().DisableImplicitModules;
568568
if (ExplicitModuleBuild ||
569-
!Invocation.getSearchPathOptions().ExplicitSwiftModuleMap.empty()) {
569+
!Invocation.getSearchPathOptions().ExplicitSwiftModuleMap.empty() ||
570+
!Invocation.getSearchPathOptions().ExplicitSwiftModuleInputs.empty()) {
570571
ESML = ExplicitSwiftModuleLoader::create(
571572
*Context, getDependencyTracker(), MLM,
572573
Invocation.getSearchPathOptions().ExplicitSwiftModuleMap,
574+
Invocation.getSearchPathOptions().ExplicitSwiftModuleInputs,
573575
IgnoreSourceInfoFile);
574576
}
575577

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,16 @@ struct ExplicitSwiftModuleLoader::Implementation {
18971897
}
18981898
}
18991899
}
1900+
1901+
void addCommandLineExplicitInputs(
1902+
const std::vector<std::pair<std::string, std::string>>
1903+
&commandLineExplicitInputs) {
1904+
for (const auto &moduleInput : commandLineExplicitInputs) {
1905+
ExplicitModuleInfo entry;
1906+
entry.modulePath = moduleInput.second;
1907+
ExplicitModuleMap.try_emplace(moduleInput.first, std::move(entry));
1908+
}
1909+
}
19001910
};
19011911

19021912
ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader(
@@ -2057,6 +2067,7 @@ std::unique_ptr<ExplicitSwiftModuleLoader>
20572067
ExplicitSwiftModuleLoader::create(ASTContext &ctx,
20582068
DependencyTracker *tracker, ModuleLoadingMode loadMode,
20592069
StringRef ExplicitSwiftModuleMap,
2070+
const std::vector<std::pair<std::string, std::string>> &ExplicitSwiftModuleInputs,
20602071
bool IgnoreSwiftSourceInfoFile) {
20612072
auto result = std::unique_ptr<ExplicitSwiftModuleLoader>(
20622073
new ExplicitSwiftModuleLoader(ctx, tracker, loadMode,
@@ -2067,6 +2078,11 @@ ExplicitSwiftModuleLoader::create(ASTContext &ctx,
20672078
// Parse a JSON file to collect explicitly built modules.
20682079
Impl.parseSwiftExplicitModuleMap(ExplicitSwiftModuleMap);
20692080
}
2081+
// If some modules are provided with explicit
2082+
// '-swift-module-file' options, add those as well.
2083+
if (!ExplicitSwiftModuleInputs.empty()) {
2084+
Impl.addCommandLineExplicitInputs(ExplicitSwiftModuleInputs);
2085+
}
20702086

20712087
return result;
20722088
}

0 commit comments

Comments
 (0)