Skip to content

[CAS] Teach swift-cache-tool about plugin-cas #68123

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
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
6 changes: 6 additions & 0 deletions lib/DriverTool/SwiftCacheToolOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ def cache_tool_action: JoinedOrSeparate<["-"], "cache-tool-action">,

def cas_path: Separate<["-"], "cas-path">,
HelpText<"Path to CAS">, MetaVarName<"<path>">;

def cas_plugin_path: Separate<["-"], "cas-plugin-path">,
HelpText<"Path to CAS Plugin">, MetaVarName<"<path>">;

def cas_plugin_option: Separate<["-"], "cas-plugin-option">,
HelpText<"Option pass to CAS Plugin">, MetaVarName<"<name>=<option>">;
36 changes: 30 additions & 6 deletions lib/DriverTool/swift_cache_tool_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
#include "swift/Parse/ParseVersion.h"
#include "clang/CAS/CASOptions.h"
#include "llvm/CAS/ActionCache.h"
#include "llvm/CAS/BuiltinUnifiedCASDatabases.h"
#include "llvm/CAS/ObjectStore.h"
Expand Down Expand Up @@ -87,7 +88,7 @@ class SwiftCacheToolInvocation {
CompilerInvocation Invocation;
PrintingDiagnosticConsumer PDC;
std::string MainExecutablePath;
std::string CASPath;
clang::CASOptions CASOpts;
std::vector<std::string> Inputs;
std::vector<std::string> FrontendArgs;
SwiftCacheToolAction ActionKind = SwiftCacheToolAction::Invalid;
Expand Down Expand Up @@ -124,8 +125,19 @@ class SwiftCacheToolInvocation {
return 0;
}

CASPath =
ParsedArgs.getLastArgValue(OPT_cas_path, getDefaultOnDiskCASPath());
if (const Arg* PluginPath = ParsedArgs.getLastArg(OPT_cas_plugin_path))
CASOpts.PluginPath = PluginPath->getValue();
if (const Arg* OnDiskPath = ParsedArgs.getLastArg(OPT_cas_path))
CASOpts.CASPath = OnDiskPath->getValue();
for (StringRef Opt : ParsedArgs.getAllArgValues(OPT_cas_plugin_option)) {
StringRef Name, Value;
std::tie(Name, Value) = Opt.split('=');
CASOpts.PluginOptions.emplace_back(std::string(Name), std::string(Value));
}

// Fallback to default path if not set.
if (CASOpts.CASPath.empty() && CASOpts.PluginPath.empty())
CASOpts.CASPath = getDefaultOnDiskCASPath();

Inputs = ParsedArgs.getAllArgValues(OPT_INPUT);
FrontendArgs = ParsedArgs.getAllArgValues(OPT__DASH_DASH);
Expand Down Expand Up @@ -186,8 +198,20 @@ class SwiftCacheToolInvocation {

// Make sure CASPath is the same between invocation and cache-tool by
// appending the cas-path since the option doesn't affect cache key.
Args.emplace_back("-cas-path");
Args.emplace_back(CASPath.c_str());
if (!CASOpts.CASPath.empty()) {
Args.emplace_back("-cas-path");
Args.emplace_back(CASOpts.CASPath.c_str());
}
if (!CASOpts.PluginPath.empty()) {
Args.emplace_back("-cas-plugin-path");
Args.emplace_back(CASOpts.PluginPath.c_str());
}
std::vector<std::string> PluginJoinedOpts;
for (const auto& Opt: CASOpts.PluginOptions) {
PluginJoinedOpts.emplace_back(Opt.first + "=" + Opt.second);
Args.emplace_back("-cas-plugin-option");
Args.emplace_back(PluginJoinedOpts.back().c_str());
}

if (Invocation.parseArgs(Args, Instance.getDiags(),
&configurationFileBuffers, workingDirectory,
Expand Down Expand Up @@ -336,7 +360,7 @@ readOutputEntriesFromFile(StringRef Path) {
}

int SwiftCacheToolInvocation::validateOutputs() {
auto DB = llvm::cas::createOnDiskUnifiedCASDatabases(CASPath);
auto DB = CASOpts.getOrCreateDatabases();
if (!DB)
report_fatal_error(DB.takeError());

Expand Down
11 changes: 11 additions & 0 deletions test/CAS/cache_key_compute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,38 @@
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s

/// Test plugin CAS.
// RUN: %cache-tool -cas-path %t/cas -cas-plugin-path %llvm_libs_dir/libCASPluginTest%llvm_plugin_ext \
// RUN: -cas-plugin-option first-prefix=myfirst- -cache-tool-action print-output-keys -- \
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s --check-prefix=CHECK --check-prefix=PLUGIN

// CHECK: test.o
// CHECK-NEXT: "OutputKind": "object"
// CHECK-NEXT: "Input"
// CHECK-NEXT: "CacheKey"
// PLUGIN-SAME: myfirst-llvmcas://

// CHECK: test.swiftmodule
// CHECK-NEXT: "OutputKind": "swiftmodule"
// CHECK-NEXT: "Input"
// CHECK-NEXT: "CacheKey"
// PLUGIN-SAME: myfirst-llvmcas://

// CHECK: test.d
// CHECK-NEXT: "OutputKind": "dependencies"
// CHECK-NEXT: "Input"
// CHECK-NEXT: "CacheKey"
// PLUGIN-SAME: myfirst-llvmcas://

// CHECK: test.tbd
// CHECK-NEXT: "OutputKind": "tbd"
// CHECK-NEXT: "Input"
// CHECK-NEXT: "CacheKey"
// PLUGIN-SAME: myfirst-llvmcas://

// CHECK: <cached-diagnostics>
// CHECK-NEXT: "OutputKind": "cached-diagnostics"
// CHECK-NEXT: "Input": "<cached-diagnostics>"
// CHECK-NEXT: "CacheKey"
// PLUGIN-SAME: myfirst-llvmcas://