Skip to content

Commit 905b196

Browse files
committed
[Macros] Emit plugin dependencies to loaded module trace.
Emit plugins loaded with `-load-plugin-library` to loaded module trace. Resolves rdar://102212316.
1 parent 3fd895a commit 905b196

File tree

8 files changed

+74
-4
lines changed

8 files changed

+74
-4
lines changed

include/swift/Basic/FileTypes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ TYPE("external-swift-dependencies", ExternalSwiftDeps, "swiftdeps.external", "
6565
TYPE("remap", Remapping, "remap", "")
6666
TYPE("imported-modules", ImportedModules, "importedmodules", "")
6767
TYPE("tbd", TBD, "tbd", "")
68+
TYPE("dylib", SharedLibraryDylib, "dylib", "")
69+
TYPE("so", SharedLibrarySO, "so", "")
70+
TYPE("dll", SharedLibraryDLL, "dll", "")
6871

6972
// Module traces are used by Apple's internal build infrastructure. Apple
7073
// engineers can see more details on the "Swift module traces" page in the

lib/Basic/FileTypes.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ bool file_types::isTextual(ID Id) {
109109
case file_types::TY_IndexData:
110110
case file_types::TY_BitstreamOptRecord:
111111
case file_types::TY_IndexUnitOutputPath:
112+
case file_types::TY_SharedLibraryDylib:
113+
case file_types::TY_SharedLibrarySO:
114+
case file_types::TY_SharedLibraryDLL:
112115
return false;
113116
case file_types::TY_INVALID:
114117
llvm_unreachable("Invalid type ID.");
@@ -124,6 +127,9 @@ bool file_types::isAfterLLVM(ID Id) {
124127
case file_types::TY_LLVM_IR:
125128
case file_types::TY_LLVM_BC:
126129
case file_types::TY_Object:
130+
case file_types::TY_SharedLibraryDylib:
131+
case file_types::TY_SharedLibrarySO:
132+
case file_types::TY_SharedLibraryDLL:
127133
return true;
128134
case file_types::TY_Swift:
129135
case file_types::TY_PCH:
@@ -215,6 +221,9 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
215221
case file_types::TY_IndexUnitOutputPath:
216222
case file_types::TY_SwiftABIDescriptor:
217223
case file_types::TY_ConstValues:
224+
case file_types::TY_SharedLibraryDylib:
225+
case file_types::TY_SharedLibraryDLL:
226+
case file_types::TY_SharedLibrarySO:
218227
return false;
219228
case file_types::TY_INVALID:
220229
llvm_unreachable("Invalid type ID.");

lib/Driver/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,9 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
20822082
case file_types::TY_JSONFeatures:
20832083
case file_types::TY_SwiftABIDescriptor:
20842084
case file_types::TY_ConstValues:
2085+
case file_types::TY_SharedLibraryDylib:
2086+
case file_types::TY_SharedLibraryDLL:
2087+
case file_types::TY_SharedLibrarySO:
20852088
// We could in theory handle assembly or LLVM input, but let's not.
20862089
// FIXME: What about LTO?
20872090
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,

lib/Driver/ToolChains.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,9 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
704704
case file_types::TY_IndexUnitOutputPath:
705705
case file_types::TY_SwiftABIDescriptor:
706706
case file_types::TY_ConstValues:
707+
case file_types::TY_SharedLibraryDylib:
708+
case file_types::TY_SharedLibrarySO:
709+
case file_types::TY_SharedLibraryDLL:
707710
llvm_unreachable("Output type can never be primary output.");
708711
case file_types::TY_INVALID:
709712
llvm_unreachable("Invalid type ID");
@@ -965,6 +968,9 @@ ToolChain::constructInvocation(const BackendJobAction &job,
965968
case file_types::TY_IndexUnitOutputPath:
966969
case file_types::TY_SwiftABIDescriptor:
967970
case file_types::TY_ConstValues:
971+
case file_types::TY_SharedLibraryDylib:
972+
case file_types::TY_SharedLibraryDLL:
973+
case file_types::TY_SharedLibrarySO:
968974
llvm_unreachable("Output type can never be primary output.");
969975
case file_types::TY_INVALID:
970976
llvm_unreachable("Invalid type ID");

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ void ABIDependencyEvaluator::printABIExportMap(llvm::raw_ostream &os) const {
552552
// FIXME: Use the VFS instead of handling paths directly. We are particularly
553553
// sloppy about handling relative paths in the dependency tracker.
554554
static void computeSwiftModuleTraceInfo(
555+
ASTContext &ctx,
555556
const SmallPtrSetImpl<ModuleDecl *> &abiDependencies,
556557
const llvm::DenseMap<StringRef, ModuleDecl *> &pathToModuleDecl,
557558
const DependencyTracker &depTracker, StringRef prebuiltCachePath,
@@ -580,8 +581,12 @@ static void computeSwiftModuleTraceInfo(
580581
auto isSwiftmodule = moduleFileType == file_types::TY_SwiftModuleFile;
581582
auto isSwiftinterface =
582583
moduleFileType == file_types::TY_SwiftModuleInterfaceFile;
584+
auto isSharedLibrary =
585+
moduleFileType == file_types::TY_SharedLibraryDylib ||
586+
moduleFileType == file_types::TY_SharedLibrarySO ||
587+
moduleFileType == file_types::TY_SharedLibraryDLL;
583588

584-
if (!(isSwiftmodule || isSwiftinterface))
589+
if (!(isSwiftmodule || isSwiftinterface || isSharedLibrary))
585590
continue;
586591

587592
auto dep = pathToModuleDecl.find(depPath);
@@ -637,6 +642,41 @@ static void computeSwiftModuleTraceInfo(
637642
continue;
638643
}
639644

645+
// If we found a shared library, it must be a compiler plugin dependency.
646+
if (isSharedLibrary) {
647+
// Infer the module name by dropping the library prefix and extension.
648+
// e.g "/path/to/lib/libPlugin.dylib" -> "Plugin"
649+
auto moduleName = llvm::sys::path::stem(depPath);
650+
#if !defined(_WIN32)
651+
moduleName.consume_front("lib");
652+
#endif
653+
654+
StringRef realDepPath =
655+
fs::real_path(depPath, buffer, /*expand_tile*/ true)
656+
? StringRef(depPath)
657+
: buffer.str();
658+
659+
traceInfo.push_back(
660+
{/*Name=*/
661+
ctx.getIdentifier(moduleName),
662+
/*Path=*/
663+
realDepPath.str(),
664+
// TODO: There is an edge case which is not handled here.
665+
// When we build a framework using -import-underlying-module, or an
666+
// app/test using -import-objc-header, we should look at the direct
667+
// imports of the bridging modules, and mark those as our direct
668+
// imports.
669+
// TODO: Add negative test cases for the comment above.
670+
// TODO: Describe precise semantics of "isImportedDirectly".
671+
/*IsImportedDirectly=*/
672+
false,
673+
/*SupportsLibraryEvolution=*/
674+
false});
675+
buffer.clear();
676+
677+
continue;
678+
}
679+
640680
// Skip cached modules in the prebuilt cache. We will add the corresponding
641681
// swiftinterface from the SDK directly, but this isn't checked. :-/
642682
//
@@ -731,8 +771,9 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
731771
}
732772

733773
std::vector<SwiftModuleTraceInfo> swiftModules;
734-
computeSwiftModuleTraceInfo(abiDependencies, pathToModuleDecl, *depTracker,
735-
opts.PrebuiltModuleCachePath, swiftModules);
774+
computeSwiftModuleTraceInfo(ctxt, abiDependencies, pathToModuleDecl,
775+
*depTracker, opts.PrebuiltModuleCachePath,
776+
swiftModules);
736777

737778
LoadedModuleTraceFormat trace = {
738779
/*version=*/LoadedModuleTraceFormat::CurrentVersion,

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,11 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
12681268
}
12691269
}
12701270
}
1271+
1272+
for (auto &path : Ctx.SearchPathOpts.getCompilerPluginLibraryPaths()) {
1273+
dependencyTracker->addDependency(path, /*isSystem=*/false);
1274+
}
1275+
12711276
return M;
12721277
}
12731278

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Don't need anything here, just for the module to exist.

test/Driver/loaded_module_trace.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// RUN: %empty-directory(%t/cache)
33
// RUN: %target-build-swift -emit-module -module-name Module %S/Inputs/loaded_module_trace_empty.swift -o %t/Module.swiftmodule -module-cache-path %t/cache
44
// RUN: %target-build-swift -emit-module -module-name Module2 %S/Inputs/loaded_module_trace_imports_module.swift -o %t/Module2.swiftmodule -I %t -module-cache-path %t/cache
5-
// RUN: %target-build-swift %s -emit-loaded-module-trace -o %t/loaded_module_trace -I %t -module-cache-path %t/cache
5+
// RUN: %target-build-swift -emit-library -module-name Plugin %S/Inputs/loaded_module_trace_compiler_plugin.swift -o %t/%target-library-name(Plugin) -module-cache-path %t/cache
6+
// RUN: %target-build-swift %s -emit-loaded-module-trace -o %t/loaded_module_trace -I %t -module-cache-path %t/cache -load-plugin-library %t/%target-library-name(Plugin)
67
// RUN: %FileCheck -check-prefix=CHECK %s < %t/loaded_module_trace.trace.json
78
// RUN: %FileCheck -check-prefix=CHECK-CONFIRM-ONELINE %s < %t/loaded_module_trace.trace.json
89

@@ -25,6 +26,7 @@
2526
// CHECK-DAG: {"name":"Swift","path":"{{[^"]*\\[/\\]}}Swift.swiftmodule{{(\\[/\\][^"]+[.]swiftmodule)?}}","isImportedDirectly":true,"supportsLibraryEvolution":true}
2627
// CHECK-DAG: {"name":"SwiftOnoneSupport","path":"{{[^"]*\\[/\\]}}SwiftOnoneSupport.swiftmodule{{(\\[/\\][^"]+[.]swiftmodule)?}}","isImportedDirectly":true,"supportsLibraryEvolution":true}
2728
// CHECK-DAG: {"name":"Module","path":"{{[^"]*\\[/\\]}}Module.swiftmodule","isImportedDirectly":false,"supportsLibraryEvolution":false}
29+
// CHECK-DAG: {"name":"Plugin","path":"{{[^"]*\\[/\\]}}libPlugin.dylib","isImportedDirectly":false,"supportsLibraryEvolution":false}
2830
// CHECK: ]
2931
// CHECK: }
3032

0 commit comments

Comments
 (0)