Skip to content

[6.0][ScanDependency] Pass crossimport overlay file to swift-frontend #73753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,9 @@ class ModuleDependencyInfo {
/// Collect a map from a secondary module name to a list of cross-import
/// overlays, when this current module serves as the primary module.
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>
collectCrossImportOverlayNames(ASTContext &ctx, StringRef moduleName,
std::vector<std::string> &overlayFiles) const;
collectCrossImportOverlayNames(
ASTContext &ctx, StringRef moduleName,
std::vector<std::pair<std::string, std::string>> &overlayFiles) const;
};

using ModuleDependencyVector = llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>;
Expand Down
8 changes: 8 additions & 0 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ class SearchPathOptions {
/// version inheritance.
std::optional<std::string> PlatformAvailabilityInheritanceMapPath;

/// Cross import module information. Map from module name to the list of cross
/// import overlay files that associate with that module.
using CrossImportMap = llvm::StringMap<std::vector<std::string>>;
CrossImportMap CrossImportInfo;

/// Whether to search for cross import overlay on file system.
bool DisableCrossImportOverlaySearch = false;

/// Debug path mappings to apply to serialized search paths. These are
/// specified in LLDB from the target.source-map entries.
PathRemapper SearchPathRemapper;
Expand Down
7 changes: 7 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ def swift_module_file: Joined<["-"], "swift-module-file=">,
MetaVarName<"<name>=<path>">,
HelpText<"Specify Swift module input explicitly built from textual interface">;

def swift_module_cross_import: MultiArg<["-"], "swift-module-cross-import", 2>,
MetaVarName<"<moduleName> <crossImport.swiftoverlay>">,
HelpText<"Specify the cross import module">;

def disable_cross_import_overlay_search: Flag<["-"], "disable-cross-import-overlay-search">,
HelpText<"Disable searching for cross import overlay file">;

def explicit_swift_module_map
: Separate<["-"], "explicit-swift-module-map-file">, MetaVarName<"<path>">,
HelpText<"Specify a JSON file containing information of explicit Swift modules">;
Expand Down
17 changes: 15 additions & 2 deletions lib/AST/ModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ void ModuleLoader::findOverlayFiles(SourceLoc diagLoc, ModuleDecl *module,
using namespace llvm::sys;
using namespace file_types;

// If cross import information is passed on command-line, prefer use that.
auto &crossImports = module->getASTContext().SearchPathOpts.CrossImportInfo;
auto overlays = crossImports.find(module->getNameStr());
if (overlays != crossImports.end()) {
for (auto entry : overlays->second) {
module->addCrossImportOverlayFile(entry);
if (dependencyTracker)
dependencyTracker->addDependency(entry, module->isSystemModule());
}
}
if (module->getASTContext().SearchPathOpts.DisableCrossImportOverlaySearch)
return;

if (file->getModuleDefiningPath().empty())
return;
findOverlayFilesInternal(module->getASTContext(),
Expand All @@ -188,7 +201,7 @@ void ModuleLoader::findOverlayFiles(SourceLoc diagLoc, ModuleDecl *module,
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>
ModuleDependencyInfo::collectCrossImportOverlayNames(
ASTContext &ctx, StringRef moduleName,
std::vector<std::string> &overlayFiles) const {
std::vector<std::pair<std::string, std::string>> &overlayFiles) const {
using namespace llvm::sys;
using namespace file_types;
std::optional<std::string> modulePath;
Expand Down Expand Up @@ -240,7 +253,7 @@ ModuleDependencyInfo::collectCrossImportOverlayNames(
ModuleDecl::collectCrossImportOverlay(ctx, file, moduleName,
bystandingModule);
result[bystandingModule] = std::move(overlayNames);
overlayFiles.push_back(file.str());
overlayFiles.push_back({moduleName.str(), file.str()});
});
return result;
}
Expand Down
14 changes: 10 additions & 4 deletions lib/DependencyScan/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ void ModuleDependencyScanner::discoverCrossImportOverlayDependencies(
llvm::function_ref<void(ModuleDependencyID)> action) {
// Modules explicitly imported. Only these can be secondary module.
llvm::SetVector<Identifier> newOverlays;
std::vector<std::string> overlayFiles;
std::vector<std::pair<std::string, std::string>> overlayFiles;
for (auto dep : allDependencies) {
auto moduleName = dep.ModuleName;
// Do not look for overlays of main module under scan
Expand Down Expand Up @@ -876,9 +876,15 @@ void ModuleDependencyScanner::discoverCrossImportOverlayDependencies(
mainDep.addModuleDependency(crossImportOverlayModID);
});

llvm::for_each(overlayFiles, [&mainDep](const std::string &file) {
mainDep.addAuxiliaryFile(file);
});
auto cmdCopy = mainDep.getCommandline();
cmdCopy.push_back("-disable-cross-import-overlay-search");
for (auto &entry : overlayFiles) {
mainDep.addAuxiliaryFile(entry.second);
cmdCopy.push_back("-swift-module-cross-import");
cmdCopy.push_back(entry.first);
cmdCopy.push_back(entry.second);
}
mainDep.updateCommandLine(cmdCopy);

cache.updateDependency(
{mainModuleName.str(), ModuleDependencyKind::SwiftSource}, mainDep);
Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,12 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
*forceModuleLoadingMode);
}

for (auto *A : Args.filtered(OPT_swift_module_cross_import))
Opts.CrossImportInfo[A->getValue(0)].push_back(A->getValue(1));

Opts.DisableCrossImportOverlaySearch |=
Args.hasArg(OPT_disable_cross_import_overlay_search);

// Opts.RuntimeIncludePath is set by calls to
// setRuntimeIncludePath() or setMainExecutablePath().
// Opts.RuntimeImportPath is set by calls to
Expand Down
65 changes: 48 additions & 17 deletions test/CAS/cross_import.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
// RUN: -emit-module-interface-path %t/A.swiftinterface -enable-library-evolution -I %t %t/A.swift

// RUN: %target-swift-frontend -emit-module -module-name B -o %t/A.swiftmodule -swift-version 5 \
// RUN: %target-swift-frontend -emit-module -module-name B -o %t/B.swiftmodule -swift-version 5 \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
// RUN: -emit-module-interface-path %t/B.swiftinterface -enable-library-evolution -I %t %t/B.swift

// RUN: %target-swift-frontend -emit-module -module-name _Cross -o %t/A.swiftmodule -swift-version 5 \
// RUN: %target-swift-frontend -emit-module -module-name _B_A -o %t/_B_A.swiftmodule -swift-version 5 \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
// RUN: -emit-module-interface-path %t/_Cross.swiftinterface -enable-library-evolution -I %t %t/cross.swift
// RUN: -emit-module-interface-path %t/_B_A.swiftinterface -enable-library-evolution -I %t %t/b_a.swift

// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache %t/main.swift \
// RUN: %target-swift-frontend -emit-module -module-name _C_A -o %t/_C_A.swiftmodule -swift-version 5 \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
// RUN: -o %t/deps.json -I %t -cache-compile-job -cas-path %t/cas -swift-version 5 -enable-cross-import-overlays -module-load-mode prefer-interface
// RUN: -emit-module-interface-path %t/_C_A.swiftinterface -enable-library-evolution -I %t %t/c_a.swift

// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json A > %t/A.cmd
// RUN: %swift_frontend_plain @%t/A.cmd
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json B > %t/B.cmd
// RUN: %swift_frontend_plain @%t/B.cmd
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json _Cross > %t/Cross.cmd
// RUN: %swift_frontend_plain @%t/Cross.cmd
// RUN: %target-swift-frontend -scan-dependencies -module-name Test -module-cache-path %t/clang-module-cache %t/main.swift -F %t \
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -O \
// RUN: -o %t/deps.json -I %t -cache-compile-job -cas-path %t/cas -swift-version 5 -enable-cross-import-overlays -module-load-mode prefer-serialized

// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps.json > %t/map.json
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:SwiftShims > %t/shim.cmd
// RUN: %swift_frontend_plain @%t/shim.cmd
// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json clang:C > %t/C.cmd
// RUN: %swift_frontend_plain @%t/C.cmd

// RUN: %{python} %S/Inputs/GenerateExplicitModuleMap.py %t/deps.json %t > %t/map.json
// RUN: llvm-cas --cas %t/cas --make-blob --data %t/map.json > %t/map.casid

// RUN: %{python} %S/Inputs/BuildCommandExtractor.py %t/deps.json Test > %t/MyApp.cmd
// RUN: %FileCheck %s --input-file=%t/MyApp.cmd --check-prefix CMD
// CMD: -swift-module-cross-import
// CMD-NEXT: B
// CMD-NEXT: A.swiftoverlay
// CMD-NEXT: -swift-module-cross-import
// CMD-NEXT: C
// CMD-NEXT: A.swiftoverlay

// RUN: %target-swift-frontend -emit-module -o %t/Test.swiftmodule \
// RUN: -emit-module-interface-path %t/Test.swiftinterface \
Expand All @@ -40,28 +49,50 @@
// RUN: %FileCheck %s --input-file=%t/Test.swiftinterface

/// Check to make sure the implicit cross import turned into explicit import in the interface file.
// CHECK: import _Cross
// CHECK: import _B_A
// CHECK: import _C_A

//--- A.swift
public func a() {}

//--- B.swift
public func b() {}

//--- cross.swift
public func cross() {}
//--- b_a.swift
public func b_a() {}

//--- c_a.swift
public func c_a() {}

//--- C.framework/Modules/module.modulemap
framework module C {
umbrella header "C.h"
export *
}

//--- C.framework/Headers/C.h
void c(void);

//--- C.framework/Modules/C.swiftcrossimport/A.swiftoverlay
%YAML 1.2
---
version: 1
modules:
- name: _C_A

//--- main.swift
import A
import B
import C

func test () {
cross()
b_a()
c_a()
}

//--- B.swiftcrossimport/A.swiftoverlay
%YAML 1.2
---
version: 1
modules:
- name: _Cross
- name: _B_A
14 changes: 14 additions & 0 deletions test/CrossImport/explicit-overlay-file.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This file tests that the -Rcross-import option causes an appropriate remark to be emitted
// RUN: %empty-directory(%t)
// RUN: cp -r %S/Inputs/lib-templates/* %t/
// RUN: %target-swift-frontend -typecheck %s -enable-cross-import-overlays -Rcross-import -I %t/include -I %t/lib/swift -F %t/Frameworks 2>&1 | %FileCheck %s -check-prefix IMPORT
// RUN: %target-swift-frontend -typecheck %s -disable-cross-import-overlay-search -enable-cross-import-overlays -Rcross-import -I %t/include -I %t/lib/swift -F %t/Frameworks 2>&1 \
// RUN: | %FileCheck %s -check-prefix NO-IMPORT -allow-empty
// RUN: %target-swift-frontend -typecheck %s -disable-cross-import-overlay-search -enable-cross-import-overlays -Rcross-import -I %t/include -I %t/lib/swift -F %t/Frameworks 2>&1 \
// RUN: -swift-module-cross-import DeclaringLibrary %t/lib/swift/DeclaringLibrary.swiftcrossimport/BystandingLibrary.swiftoverlay | %FileCheck %s -check-prefix IMPORT

import DeclaringLibrary
import BystandingLibrary

// IMPORT: import of 'DeclaringLibrary' and 'BystandingLibrary' triggered a cross-import of '_OverlayLibrary'
// NO-IMPORT-NOT: import of 'DeclaringLibrary' and 'BystandingLibrary' triggered a cross-import of '_OverlayLibrary'
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ import SubEWrapper
// CHECK-NOT: "clang": "X"
// CHECK: ],

// CHECK: "swift": {
// CHECK-NEXT: "commandLine": [
// CHECK-NEXT: "-disable-cross-import-overlay-search",
// CHECK-NEXT: "-swift-module-cross-import",
// CHECK-NEXT: "E",
// CHECK-NEXT: SubE.swiftoverlay

// Ensure a transitive dependency via "_cross_import_E" is recorded in the graph still
// CHECK: "clang": "X"