Skip to content

Commit b22458e

Browse files
authored
Merge pull request swiftlang#34189 from nkcsgexi/disable-building-interface
Frontend: add a frontend flag to disable building module from textual interface
2 parents 1af9977 + 00eb2e9 commit b22458e

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ class FrontendOptions {
272272
/// built and given to the compiler invocation.
273273
bool DisableImplicitModules = false;
274274

275+
/// Disable building Swift modules from textual interfaces. This should be
276+
/// for testing purposes only.
277+
bool DisableBuildingInterface = false;
278+
275279
/// When performing a dependency scanning action, only identify and output all imports
276280
/// of the main Swift module's source files.
277281
bool ImportPrescan = false;

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,13 @@ struct ModuleInterfaceLoaderOptions {
290290
bool remarkOnRebuildFromInterface = false;
291291
bool disableInterfaceLock = false;
292292
bool disableImplicitSwiftModule = false;
293+
bool disableBuildingInterface = false;
293294
std::string mainExecutablePath;
294295
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
295296
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
296297
disableInterfaceLock(Opts.DisableInterfaceFileLock),
297298
disableImplicitSwiftModule(Opts.DisableImplicitModules),
299+
disableBuildingInterface(Opts.DisableBuildingInterface),
298300
mainExecutablePath(Opts.MainExecutablePath)
299301
{
300302
switch (Opts.RequestedAction) {

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,4 +758,8 @@ def candidate_module_file
758758
def use_static_resource_dir
759759
: Flag<["-"], "use-static-resource-dir">,
760760
HelpText<"Use resources in the static resource directory">;
761+
762+
def disable_building_interface
763+
: Flag<["-"], "disable-building-interface">,
764+
HelpText<"Disallow building binary module from textual interface">;
761765
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ bool ArgsToFrontendOptionsConverter::convert(
212212
Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module);
213213
Opts.EnableIncrementalDependencyVerifier |= Args.hasArg(OPT_verify_incremental_dependencies);
214214
Opts.UseSharedResourceFolder = !Args.hasArg(OPT_use_static_resource_dir);
215+
Opts.DisableBuildingInterface = Args.hasArg(OPT_disable_building_interface);
215216

216217
computeImportObjCHeaderOptions();
217218
computeImplicitImportModuleNames();

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,13 +859,6 @@ class ModuleInterfaceLoaderImpl {
859859
prebuiltCacheDir,
860860
/*serializeDependencyHashes*/false,
861861
trackSystemDependencies);
862-
// Set up a builder if we need to build the module. It'll also set up
863-
// the genericSubInvocation we'll need to use to compute the cache paths.
864-
ModuleInterfaceBuilder builder(
865-
ctx.SourceMgr, ctx.Diags, astDelegate, interfacePath, moduleName, cacheDir,
866-
prebuiltCacheDir,
867-
Opts.disableInterfaceLock, diagnosticLoc,
868-
dependencyTracker);
869862

870863
// Compute the output path if we're loading or emitting a cached module.
871864
llvm::SmallString<256> cachedOutputPath;
@@ -908,6 +901,18 @@ class ModuleInterfaceLoaderImpl {
908901

909902
return std::move(module.moduleBuffer);
910903
}
904+
// If building from interface is disabled, return error.
905+
if (Opts.disableBuildingInterface) {
906+
return std::make_error_code(std::errc::not_supported);
907+
}
908+
909+
// Set up a builder if we need to build the module. It'll also set up
910+
// the genericSubInvocation we'll need to use to compute the cache paths.
911+
ModuleInterfaceBuilder builder(
912+
ctx.SourceMgr, ctx.Diags, astDelegate, interfacePath, moduleName, cacheDir,
913+
prebuiltCacheDir,
914+
Opts.disableInterfaceLock, diagnosticLoc,
915+
dependencyTracker);
911916

912917
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
913918

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 1. Create folders
2+
// RUN: %empty-directory(%t/PrebuiltModule.swiftmodule)
3+
// RUN: %empty-directory(%t/ModuleCache)
4+
5+
// 2. Define some public API
6+
// RUN: echo 'public struct InPrebuiltModule {}' > %t/PrebuiltModule.swift
7+
8+
// 3. Compile textual interface only into a directory
9+
// RUN: %target-swift-frontend -emit-module %t/PrebuiltModule.swift -module-name PrebuiltModule -emit-module-interface-path %t/PrebuiltModule.swiftmodule/%target-swiftinterface-name
10+
11+
// 4. Import this module with -disable-building-interface should fail
12+
// RUN: not %target-swift-frontend -typecheck -I %t %s -module-cache-path %t/ModuleCache -sdk %t -disable-building-interface
13+
14+
// 5. Import this module without -disable-building-interface should succeed
15+
// RUN: %target-swift-frontend -typecheck -I %t %s -module-cache-path %t/ModuleCache -sdk %t
16+
17+
import PrebuiltModule
18+
19+
func x<T>(_ x: T) {}
20+
21+
x(InPrebuiltModule.self)

0 commit comments

Comments
 (0)