Skip to content

Commit 1bbe6b7

Browse files
authored
Merge pull request #60356 from artemcm/StandaloneInterfaceFlagIgnore
Add flag that allows ignoring compiler flags specified in an interface file when running a '-compile-module-from-interface' frontend action.
2 parents 3f0df6a + 7bdec99 commit 1bbe6b7

12 files changed

+92
-14
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ WARNING(warning_cannot_find_locale_file,none,
9999
"cannot find translations for '%0' at '%1': no such file", (StringRef, StringRef))
100100
WARNING(warning_cannot_multithread_batch_mode,none,
101101
"ignoring -num-threads argument; cannot multithread batch mode", ())
102+
ERROR(error_cannot_ignore_interface_options_in_mode,none,
103+
"'-ignore-interface-provided-options' only supported when building a module from interface ('-compile-module-from-interface')'", ())
102104
ERROR(error_unsupported_option_argument,none,
103105
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
104106
ERROR(error_immediate_mode_missing_stdlib,none,

include/swift/AST/ModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,15 @@ struct InterfaceSubContextDelegate {
165165
StringRef interfacePath,
166166
StringRef outputPath,
167167
SourceLoc diagLoc,
168+
bool ignoreInterfaceProvidedOptions,
168169
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
169170
ArrayRef<StringRef>,
170171
ArrayRef<StringRef>, StringRef)> action) = 0;
171172
virtual std::error_code runInSubCompilerInstance(StringRef moduleName,
172173
StringRef interfacePath,
173174
StringRef outputPath,
174175
SourceLoc diagLoc,
176+
bool ignoreInterfaceProvidedOptions,
175177
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) = 0;
176178

177179
virtual ~InterfaceSubContextDelegate() = default;

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class FrontendOptions {
108108
/// Include local definitions/references in the index data.
109109
bool IndexIncludeLocals = false;
110110

111+
/// If building a module from interface, ignore compiler flags
112+
/// specified in the swiftinterface.
113+
bool IgnoreInterfaceProvidedOptions = false;
114+
111115
/// The module for which we should verify all of the generic signatures.
112116
std::string VerifyGenericSignaturesInModule;
113117

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,15 @@ struct ModuleInterfaceLoaderOptions {
300300
bool disableImplicitSwiftModule = false;
301301
bool disableBuildingInterface = false;
302302
bool downgradeInterfaceVerificationError = false;
303+
bool ignoreInterfaceProvidedOptions = false;
303304
std::string mainExecutablePath;
304305
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
305306
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
306307
disableInterfaceLock(Opts.DisableInterfaceFileLock),
307308
disableImplicitSwiftModule(Opts.DisableImplicitModules),
308309
disableBuildingInterface(Opts.DisableBuildingInterface),
309310
downgradeInterfaceVerificationError(Opts.DowngradeInterfaceVerificationError),
311+
ignoreInterfaceProvidedOptions(Opts.IgnoreInterfaceProvidedOptions),
310312
mainExecutablePath(Opts.MainExecutablePath)
311313
{
312314
switch (Opts.RequestedAction) {
@@ -452,6 +454,7 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
452454
llvm::StringSaver ArgSaver;
453455
std::vector<StringRef> GenericArgs;
454456
CompilerInvocation genericSubInvocation;
457+
llvm::Triple ParentInvocationTarget;
455458

456459
template<typename ...ArgTypes>
457460
InFlightDiagnostic diagnose(StringRef interfacePath,
@@ -473,7 +476,8 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
473476
SmallVectorImpl<const char *> &SubArgs,
474477
std::string &CompilerVersion,
475478
StringRef interfacePath,
476-
SourceLoc diagnosticLoc);
479+
SourceLoc diagnosticLoc,
480+
bool ignoreInterfaceProvidedOptions);
477481
public:
478482
InterfaceSubContextDelegateImpl(
479483
SourceManager &SM, DiagnosticEngine *Diags,
@@ -488,13 +492,15 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
488492
StringRef interfacePath,
489493
StringRef outputPath,
490494
SourceLoc diagLoc,
495+
bool ignoreInterfaceProvidedOptions,
491496
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*,
492497
ArrayRef<StringRef>, ArrayRef<StringRef>,
493498
StringRef)> action) override;
494499
std::error_code runInSubCompilerInstance(StringRef moduleName,
495500
StringRef interfacePath,
496501
StringRef outputPath,
497502
SourceLoc diagLoc,
503+
bool ignoreInterfaceProvidedOptions,
498504
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) override;
499505

500506
~InterfaceSubContextDelegateImpl() = default;

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,10 @@ def compile_module_from_interface :
877877
HelpText<"Treat the (single) input as a swiftinterface and produce a module">,
878878
ModeOpt;
879879

880+
def ignore_interface_provided_options :
881+
Flag<["-"], "ignore-interface-provided-options">,
882+
HelpText<"Ignore all module flags specified in the swiftinterface being built">;
883+
880884
def build_module_from_parseable_interface :
881885
Flag<["-"], "build-module-from-parseable-interface">,
882886
Alias<compile_module_from_interface>,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ bool ArgsToFrontendOptionsConverter::convert(
8383
Opts.EnablePrivateImports |= Args.hasArg(OPT_enable_private_imports);
8484
Opts.EnableLibraryEvolution |= Args.hasArg(OPT_enable_library_evolution);
8585
Opts.FrontendParseableOutput |= Args.hasArg(OPT_frontend_parseable_output);
86+
Opts.IgnoreInterfaceProvidedOptions |= Args.hasArg(OPT_ignore_interface_provided_options);
8687

8788
// FIXME: Remove this flag
8889
Opts.EnableLibraryEvolution |= Args.hasArg(OPT_enable_resilience);
@@ -233,6 +234,9 @@ bool ArgsToFrontendOptionsConverter::convert(
233234
if (checkUnusedSupplementaryOutputPaths())
234235
return true;
235236

237+
if (checkBuildFromInterfaceOnlyOptions())
238+
return true;
239+
236240
if (FrontendOptions::doesActionGenerateIR(Opts.RequestedAction)) {
237241
if (Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies) ||
238242
Args.hasArg(OPT_experimental_skip_all_function_bodies) ||
@@ -615,6 +619,17 @@ bool ArgsToFrontendOptionsConverter::
615619
return false;
616620
}
617621

622+
bool ArgsToFrontendOptionsConverter::checkBuildFromInterfaceOnlyOptions()
623+
const {
624+
if (Opts.RequestedAction != FrontendOptions::ActionType::CompileModuleFromInterface &&
625+
Opts.IgnoreInterfaceProvidedOptions) {
626+
Diags.diagnose(SourceLoc(),
627+
diag::error_cannot_ignore_interface_options_in_mode);
628+
return true;
629+
}
630+
return false;
631+
}
632+
618633
bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
619634
const {
620635
if (!FrontendOptions::canActionEmitDependencies(Opts.RequestedAction) &&

lib/Frontend/ArgsToFrontendOptionsConverter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class ArgsToFrontendOptionsConverter {
5555

5656
bool checkForUnusedOutputPaths() const;
5757

58+
bool checkBuildFromInterfaceOnlyOptions() const;
59+
5860
public:
5961
ArgsToFrontendOptionsConverter(DiagnosticEngine &Diags,
6062
const llvm::opt::ArgList &Args,

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ struct ErrorDowngradeConsumerRAII: DiagnosticConsumer {
180180
};
181181

182182
bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
183-
StringRef OutPath, bool ShouldSerializeDeps,
183+
StringRef OutPath,
184+
bool ShouldSerializeDeps,
185+
bool IgnoreInterfaceProvidedOptions,
184186
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
185187
ArrayRef<std::string> CompiledCandidates) {
186188

@@ -200,6 +202,7 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
200202
interfacePath,
201203
OutPath,
202204
diagnosticLoc,
205+
IgnoreInterfaceProvidedOptions,
203206
[&](SubCompilerInstanceInfo &info) {
204207
auto &SubInstance = *info.Instance;
205208
auto subInvocation = SubInstance.getInvocation();
@@ -320,15 +323,16 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
320323

321324
bool ModuleInterfaceBuilder::buildSwiftModule(StringRef OutPath,
322325
bool ShouldSerializeDeps,
326+
bool IgnoreInterfaceProvidedOptions,
323327
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
324328
llvm::function_ref<void()> RemarkRebuild,
325329
ArrayRef<std::string> CompiledCandidates) {
326330
auto build = [&]() {
327331
if (RemarkRebuild) {
328332
RemarkRebuild();
329333
}
330-
return buildSwiftModuleInternal(OutPath, ShouldSerializeDeps, ModuleBuffer,
331-
CompiledCandidates);
334+
return buildSwiftModuleInternal(OutPath, ShouldSerializeDeps, IgnoreInterfaceProvidedOptions,
335+
ModuleBuffer, CompiledCandidates);
332336
};
333337
if (disableInterfaceFileLock) {
334338
return build();

lib/Frontend/ModuleInterfaceBuilder.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class ModuleInterfaceBuilder {
8686
SmallVectorImpl<SerializationOptions::FileDependency> &Deps,
8787
bool IsHashBased);
8888

89-
bool buildSwiftModuleInternal(StringRef OutPath, bool ShouldSerializeDeps,
89+
bool buildSwiftModuleInternal(StringRef OutPath,
90+
bool ShouldSerializeDeps,
91+
bool IgnoreInterfaceProvidedOptions,
9092
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
9193
ArrayRef<std::string> CandidateModules);
9294
public:
@@ -116,7 +118,9 @@ class ModuleInterfaceBuilder {
116118
extraDependencies.push_back(path);
117119
}
118120

119-
bool buildSwiftModule(StringRef OutPath, bool ShouldSerializeDeps,
121+
bool buildSwiftModule(StringRef OutPath,
122+
bool ShouldSerializeDeps,
123+
bool ignoreInterfaceProvidedOptions,
120124
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
121125
llvm::function_ref<void()> RemarkRebuild = nullptr,
122126
ArrayRef<std::string> CandidateModules = {});

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ class ModuleInterfaceLoaderImpl {
10181018
builder.addExtraDependency(modulePath);
10191019
failed = builder.buildSwiftModule(cachedOutputPath,
10201020
/*shouldSerializeDeps*/true,
1021+
Opts.ignoreInterfaceProvidedOptions,
10211022
&moduleBuffer, remarkRebuild);
10221023
}
10231024
if (!failed) {
@@ -1051,7 +1052,10 @@ class ModuleInterfaceLoaderImpl {
10511052
// calculated using the canonical interface file path to make sure we
10521053
// can find it from the canonical interface file.
10531054
auto failedAgain = fallbackBuilder.buildSwiftModule(cachedOutputPath,
1054-
/*shouldSerializeDeps*/true, &moduleBuffer, remarkRebuild);
1055+
/*shouldSerializeDeps*/true,
1056+
Opts.ignoreInterfaceProvidedOptions,
1057+
&moduleBuffer,
1058+
remarkRebuild);
10551059
if (failedAgain)
10561060
return std::make_error_code(std::errc::invalid_argument);
10571061
assert(moduleBuffer);
@@ -1233,6 +1237,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
12331237
// FIXME: We really only want to serialize 'important' dependencies here, if
12341238
// we want to ship the built swiftmodules to another machine.
12351239
auto failed = builder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
1240+
LoaderOpts.ignoreInterfaceProvidedOptions,
12361241
/*ModuleBuffer*/nullptr, nullptr,
12371242
SearchPathOpts.CandidateCompiledModules);
12381243
if (!failed)
@@ -1253,6 +1258,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
12531258
// FIXME: We really only want to serialize 'important' dependencies here, if
12541259
// we want to ship the built swiftmodules to another machine.
12551260
return backupBuilder.buildSwiftModule(OutPath, /*shouldSerializeDeps*/true,
1261+
LoaderOpts.ignoreInterfaceProvidedOptions,
12561262
/*ModuleBuffer*/nullptr, nullptr,
12571263
SearchPathOpts.CandidateCompiledModules);
12581264
}
@@ -1359,7 +1365,8 @@ bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
13591365
SmallVectorImpl<const char *> &SubArgs,
13601366
std::string &CompilerVersion,
13611367
StringRef interfacePath,
1362-
SourceLoc diagnosticLoc) {
1368+
SourceLoc diagnosticLoc,
1369+
bool ignoreInterfaceProvidedOptions) {
13631370
llvm::vfs::FileSystem &fs = *SM.getFileSystem();
13641371
auto FileOrError = swift::vfs::getFileOrSTDIN(fs, interfacePath);
13651372
if (!FileOrError) {
@@ -1379,10 +1386,13 @@ bool InterfaceSubContextDelegateImpl::extractSwiftInterfaceVersionAndArgs(
13791386
diag::error_extracting_version_from_module_interface);
13801387
return true;
13811388
}
1382-
if (extractCompilerFlagsFromInterface(interfacePath, SB, ArgSaver, SubArgs)) {
1383-
diagnose(interfacePath, diagnosticLoc,
1384-
diag::error_extracting_version_from_module_interface);
1385-
return true;
1389+
1390+
if (!ignoreInterfaceProvidedOptions) {
1391+
if (extractCompilerFlagsFromInterface(interfacePath, SB, ArgSaver, SubArgs)) {
1392+
diagnose(interfacePath, diagnosticLoc,
1393+
diag::error_extracting_version_from_module_interface);
1394+
return true;
1395+
}
13861396
}
13871397
assert(VersMatches.size() == 2);
13881398
// FIXME We should diagnose this at a location that makes sense:
@@ -1463,6 +1473,9 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
14631473
genericSubInvocation.getFrontendOptions().DisableImplicitModules = true;
14641474
GenericArgs.push_back("-disable-implicit-swift-modules");
14651475
}
1476+
// Save the parent invocation's Target Triple
1477+
ParentInvocationTarget = langOpts.Target;
1478+
14661479
// Pass down -explicit-swift-module-map-file
14671480
// FIXME: we shouldn't need this. Remove it?
14681481
StringRef explicitSwiftModuleMap = searchPathOpts.ExplicitSwiftModuleMap;
@@ -1600,9 +1613,11 @@ InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
16001613
StringRef interfacePath,
16011614
StringRef outputPath,
16021615
SourceLoc diagLoc,
1616+
bool ignoreInterfaceProvidedOptions,
16031617
llvm::function_ref<std::error_code(ASTContext&, ModuleDecl*, ArrayRef<StringRef>,
16041618
ArrayRef<StringRef>, StringRef)> action) {
16051619
return runInSubCompilerInstance(moduleName, interfacePath, outputPath, diagLoc,
1620+
ignoreInterfaceProvidedOptions,
16061621
[&](SubCompilerInstanceInfo &info){
16071622
return action(info.Instance->getASTContext(),
16081623
info.Instance->getMainModule(),
@@ -1617,6 +1632,7 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
16171632
StringRef interfacePath,
16181633
StringRef outputPath,
16191634
SourceLoc diagLoc,
1635+
bool ignoreInterfaceProvidedOptions,
16201636
llvm::function_ref<std::error_code(SubCompilerInstanceInfo&)> action) {
16211637
// We are about to mess up the compiler invocation by using the compiler
16221638
// arguments in the textual interface file. So copy to use a new compiler
@@ -1674,9 +1690,11 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
16741690
SubArgs,
16751691
CompilerVersion,
16761692
interfacePath,
1677-
diagLoc)) {
1693+
diagLoc,
1694+
ignoreInterfaceProvidedOptions)) {
16781695
return std::make_error_code(std::errc::not_supported);
16791696
}
1697+
16801698
// Insert arguments collected from the interface file.
16811699
BuildArgs.insert(BuildArgs.end(), SubArgs.begin(), SubArgs.end());
16821700
if (subInvocation.parseArgs(SubArgs, *Diags)) {
@@ -1693,9 +1711,13 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
16931711
parsedTargetTriple.getVendor() == originalTargetTriple.getVendor() &&
16941712
parsedTargetTriple.getOS() == originalTargetTriple.getOS() &&
16951713
parsedTargetTriple.getEnvironment()
1696-
== originalTargetTriple.getEnvironment()) {
1714+
== originalTargetTriple.getEnvironment()) {
16971715
parsedTargetTriple.setArchName(originalTargetTriple.getArchName());
16981716
subInvocation.setTargetTriple(parsedTargetTriple.str());
1717+
1718+
// Overload the target in the BuildArgs as well
1719+
BuildArgs.push_back("-target");
1720+
BuildArgs.push_back(parsedTargetTriple.str());
16991721
}
17001722

17011723
CompilerInstance subInstance;

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
111111
moduleInterfacePath.str(),
112112
StringRef(),
113113
SourceLoc(),
114+
false,
114115
[&](ASTContext &Ctx, ModuleDecl *mainMod,
115116
ArrayRef<StringRef> Args,
116117
ArrayRef<StringRef> PCMArgs, StringRef Hash) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -module-name DoesNotIgnoreFlags -module-interface-preserve-types-as-written
3+
4+
// REQUIRES: OS=macosx
5+
// RUN: %empty-directory(%t)
6+
// Without '-ignore-interface-provided-options' this job fails because of the mismatch in 'module-name'
7+
// RUN: %target-swift-frontend -compile-module-from-interface -module-name IgnoresFlags -ignore-interface-provided-options -o %/t/IgnoresFlags.swiftmodule %s -verify
8+
9+
import Swift
10+
extension Int {
11+
public static var fortytwo: Int = 42
12+
}

0 commit comments

Comments
 (0)