[Macros] Emit plugin dependencies to loaded module trace. (#63367)

Emit plugins loaded with `-load-plugin-library` to loaded module trace.

Resolves rdar://102212316.
This commit is contained in:
Richard Wei
2023-02-03 09:16:14 +08:00
committed by GitHub
parent 5ad0331e55
commit 4480d89536
4 changed files with 46 additions and 4 deletions

View File

@@ -386,6 +386,11 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
return;
DepTracker = std::make_unique<DependencyTracker>(*collectionMode);
// Collect compiler plugin dependencies.
auto &searchPathOpts = Invocation.getSearchPathOptions();
for (auto &path : searchPathOpts.getCompilerPluginLibraryPaths())
DepTracker->addDependency(path, /*isSystem=*/false);
}
bool CompilerInstance::setup(const CompilerInvocation &Invoke,

View File

@@ -552,6 +552,7 @@ void ABIDependencyEvaluator::printABIExportMap(llvm::raw_ostream &os) const {
// FIXME: Use the VFS instead of handling paths directly. We are particularly
// sloppy about handling relative paths in the dependency tracker.
static void computeSwiftModuleTraceInfo(
ASTContext &ctx,
const SmallPtrSetImpl<ModuleDecl *> &abiDependencies,
const llvm::DenseMap<StringRef, ModuleDecl *> &pathToModuleDecl,
const DependencyTracker &depTracker, StringRef prebuiltCachePath,
@@ -569,6 +570,8 @@ static void computeSwiftModuleTraceInfo(
SmallVector<std::string, 16> dependencies{deps.begin(), deps.end()};
auto incrDeps = depTracker.getIncrementalDependencyPaths();
dependencies.append(incrDeps.begin(), incrDeps.end());
auto sharedLibraryExtRegex =
llvm::Regex("dylib|so|dll", llvm::Regex::IgnoreCase);
for (const auto &depPath : dependencies) {
// Decide if this is a swiftmodule based on the extension of the raw
@@ -580,8 +583,10 @@ static void computeSwiftModuleTraceInfo(
auto isSwiftmodule = moduleFileType == file_types::TY_SwiftModuleFile;
auto isSwiftinterface =
moduleFileType == file_types::TY_SwiftModuleInterfaceFile;
auto isSharedLibrary =
sharedLibraryExtRegex.match(llvm::sys::path::extension(depPath));
if (!(isSwiftmodule || isSwiftinterface))
if (!(isSwiftmodule || isSwiftinterface || isSharedLibrary))
continue;
auto dep = pathToModuleDecl.find(depPath);
@@ -637,6 +642,34 @@ static void computeSwiftModuleTraceInfo(
continue;
}
// If we found a shared library, it must be a compiler plugin dependency.
if (isSharedLibrary) {
// Infer the module name by dropping the library prefix and extension.
// e.g "/path/to/lib/libPlugin.dylib" -> "Plugin"
auto moduleName = llvm::sys::path::stem(depPath);
#if !defined(_WIN32)
moduleName.consume_front("lib");
#endif
StringRef realDepPath =
fs::real_path(depPath, buffer, /*expand_tile*/ true)
? StringRef(depPath)
: buffer.str();
traceInfo.push_back(
{/*Name=*/
ctx.getIdentifier(moduleName),
/*Path=*/
realDepPath.str(),
/*IsImportedDirectly=*/
false,
/*SupportsLibraryEvolution=*/
false});
buffer.clear();
continue;
}
// Skip cached modules in the prebuilt cache. We will add the corresponding
// swiftinterface from the SDK directly, but this isn't checked. :-/
//
@@ -731,8 +764,9 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
}
std::vector<SwiftModuleTraceInfo> swiftModules;
computeSwiftModuleTraceInfo(abiDependencies, pathToModuleDecl, *depTracker,
opts.PrebuiltModuleCachePath, swiftModules);
computeSwiftModuleTraceInfo(ctxt, abiDependencies, pathToModuleDecl,
*depTracker, opts.PrebuiltModuleCachePath,
swiftModules);
LoadedModuleTraceFormat trace = {
/*version=*/LoadedModuleTraceFormat::CurrentVersion,

View File

@@ -0,0 +1 @@
// Don't need anything here, just for the module to exist.

View File

@@ -2,7 +2,8 @@
// RUN: %empty-directory(%t/cache)
// 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
// 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
// RUN: %target-build-swift %s -emit-loaded-module-trace -o %t/loaded_module_trace -I %t -module-cache-path %t/cache
// 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
// 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)
// RUN: %FileCheck -check-prefix=CHECK %s < %t/loaded_module_trace.trace.json
// RUN: %FileCheck -check-prefix=CHECK-CONFIRM-ONELINE %s < %t/loaded_module_trace.trace.json
@@ -25,6 +26,7 @@
// CHECK-DAG: {"name":"Swift","path":"{{[^"]*\\[/\\]}}Swift.swiftmodule{{(\\[/\\][^"]+[.]swiftmodule)?}}","isImportedDirectly":true,"supportsLibraryEvolution":true}
// CHECK-DAG: {"name":"SwiftOnoneSupport","path":"{{[^"]*\\[/\\]}}SwiftOnoneSupport.swiftmodule{{(\\[/\\][^"]+[.]swiftmodule)?}}","isImportedDirectly":true,"supportsLibraryEvolution":true}
// CHECK-DAG: {"name":"Module","path":"{{[^"]*\\[/\\]}}Module.swiftmodule","isImportedDirectly":false,"supportsLibraryEvolution":false}
// CHECK-DAG: {"name":"Plugin","path":"{{[^"]*\\[/\\]}}{{libPlugin.dylib|libPlugin.so|Plugin.dll}}","isImportedDirectly":false,"supportsLibraryEvolution":false}
// CHECK: ]
// CHECK: }