[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

@@ -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,