Skip to content

Commit 4480d89

Browse files
authored
[Macros] Emit plugin dependencies to loaded module trace. (#63367)
Emit plugins loaded with `-load-plugin-library` to loaded module trace. Resolves rdar://102212316.
1 parent 5ad0331 commit 4480d89

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
386386
return;
387387

388388
DepTracker = std::make_unique<DependencyTracker>(*collectionMode);
389+
390+
// Collect compiler plugin dependencies.
391+
auto &searchPathOpts = Invocation.getSearchPathOptions();
392+
for (auto &path : searchPathOpts.getCompilerPluginLibraryPaths())
393+
DepTracker->addDependency(path, /*isSystem=*/false);
389394
}
390395

391396
bool CompilerInstance::setup(const CompilerInvocation &Invoke,

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 37 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,
@@ -569,6 +570,8 @@ static void computeSwiftModuleTraceInfo(
569570
SmallVector<std::string, 16> dependencies{deps.begin(), deps.end()};
570571
auto incrDeps = depTracker.getIncrementalDependencyPaths();
571572
dependencies.append(incrDeps.begin(), incrDeps.end());
573+
auto sharedLibraryExtRegex =
574+
llvm::Regex("dylib|so|dll", llvm::Regex::IgnoreCase);
572575
for (const auto &depPath : dependencies) {
573576

574577
// Decide if this is a swiftmodule based on the extension of the raw
@@ -580,8 +583,10 @@ static void computeSwiftModuleTraceInfo(
580583
auto isSwiftmodule = moduleFileType == file_types::TY_SwiftModuleFile;
581584
auto isSwiftinterface =
582585
moduleFileType == file_types::TY_SwiftModuleInterfaceFile;
586+
auto isSharedLibrary =
587+
sharedLibraryExtRegex.match(llvm::sys::path::extension(depPath));
583588

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

587592
auto dep = pathToModuleDecl.find(depPath);
@@ -637,6 +642,34 @@ 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+
/*IsImportedDirectly=*/
665+
false,
666+
/*SupportsLibraryEvolution=*/
667+
false});
668+
buffer.clear();
669+
670+
continue;
671+
}
672+
640673
// Skip cached modules in the prebuilt cache. We will add the corresponding
641674
// swiftinterface from the SDK directly, but this isn't checked. :-/
642675
//
@@ -731,8 +764,9 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
731764
}
732765

733766
std::vector<SwiftModuleTraceInfo> swiftModules;
734-
computeSwiftModuleTraceInfo(abiDependencies, pathToModuleDecl, *depTracker,
735-
opts.PrebuiltModuleCachePath, swiftModules);
767+
computeSwiftModuleTraceInfo(ctxt, abiDependencies, pathToModuleDecl,
768+
*depTracker, opts.PrebuiltModuleCachePath,
769+
swiftModules);
736770

737771
LoadedModuleTraceFormat trace = {
738772
/*version=*/LoadedModuleTraceFormat::CurrentVersion,
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|libPlugin.so|Plugin.dll}}","isImportedDirectly":false,"supportsLibraryEvolution":false}
2830
// CHECK: ]
2931
// CHECK: }
3032

0 commit comments

Comments
 (0)