Skip to content

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

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
merged 5 commits into from
Feb 3, 2023
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: 5 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 37 additions & 3 deletions lib/FrontendTool/LoadedModuleTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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. :-/
//
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Don't need anything here, just for the module to exist.
4 changes: 3 additions & 1 deletion test/Driver/loaded_module_trace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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: }

Expand Down