Skip to content

Commit 1d11139

Browse files
[CAS] Teach swift-cache-tool about plugin-cas
1 parent 02c26e7 commit 1d11139

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/DriverTool/SwiftCacheToolOptions.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ def cache_tool_action: JoinedOrSeparate<["-"], "cache-tool-action">,
1515

1616
def cas_path: Separate<["-"], "cas-path">,
1717
HelpText<"Path to CAS">, MetaVarName<"<path>">;
18+
19+
def cas_plugin_path: Separate<["-"], "cas-plugin-path">,
20+
HelpText<"Path to CAS Plugin">, MetaVarName<"<path>">;
21+
22+
def cas_plugin_option: Separate<["-"], "cas-plugin-option">,
23+
HelpText<"Option pass to CAS Plugin">, MetaVarName<"<name>=<option>">;

lib/DriverTool/swift_cache_tool_main.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Frontend/Frontend.h"
2424
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
2525
#include "swift/Parse/ParseVersion.h"
26+
#include "clang/CAS/CASOptions.h"
2627
#include "llvm/CAS/ActionCache.h"
2728
#include "llvm/CAS/BuiltinUnifiedCASDatabases.h"
2829
#include "llvm/CAS/ObjectStore.h"
@@ -87,7 +88,7 @@ class SwiftCacheToolInvocation {
8788
CompilerInvocation Invocation;
8889
PrintingDiagnosticConsumer PDC;
8990
std::string MainExecutablePath;
90-
std::string CASPath;
91+
clang::CASOptions CASOpts;
9192
std::vector<std::string> Inputs;
9293
std::vector<std::string> FrontendArgs;
9394
SwiftCacheToolAction ActionKind = SwiftCacheToolAction::Invalid;
@@ -124,8 +125,19 @@ class SwiftCacheToolInvocation {
124125
return 0;
125126
}
126127

127-
CASPath =
128-
ParsedArgs.getLastArgValue(OPT_cas_path, getDefaultOnDiskCASPath());
128+
if (const Arg* PluginPath = ParsedArgs.getLastArg(OPT_cas_plugin_path))
129+
CASOpts.PluginPath = PluginPath->getValue();
130+
if (const Arg* OnDiskPath = ParsedArgs.getLastArg(OPT_cas_path))
131+
CASOpts.CASPath = OnDiskPath->getValue();
132+
for (StringRef Opt : ParsedArgs.getAllArgValues(OPT_cas_plugin_option)) {
133+
StringRef Name, Value;
134+
std::tie(Name, Value) = Opt.split('=');
135+
CASOpts.PluginOptions.emplace_back(std::string(Name), std::string(Value));
136+
}
137+
138+
// Fallback to default path if not set.
139+
if (CASOpts.CASPath.empty() && CASOpts.PluginPath.empty())
140+
CASOpts.CASPath = getDefaultOnDiskCASPath();
129141

130142
Inputs = ParsedArgs.getAllArgValues(OPT_INPUT);
131143
FrontendArgs = ParsedArgs.getAllArgValues(OPT__DASH_DASH);
@@ -186,8 +198,20 @@ class SwiftCacheToolInvocation {
186198

187199
// Make sure CASPath is the same between invocation and cache-tool by
188200
// appending the cas-path since the option doesn't affect cache key.
189-
Args.emplace_back("-cas-path");
190-
Args.emplace_back(CASPath.c_str());
201+
if (!CASOpts.CASPath.empty()) {
202+
Args.emplace_back("-cas-path");
203+
Args.emplace_back(CASOpts.CASPath.c_str());
204+
}
205+
if (!CASOpts.PluginPath.empty()) {
206+
Args.emplace_back("-cas-plugin-path");
207+
Args.emplace_back(CASOpts.PluginPath.c_str());
208+
}
209+
std::vector<std::string> PluginJoinedOpts;
210+
for (const auto& Opt: CASOpts.PluginOptions) {
211+
PluginJoinedOpts.emplace_back(Opt.first + "=" + Opt.second);
212+
Args.emplace_back("-cas-plugin-option");
213+
Args.emplace_back(PluginJoinedOpts.back().c_str());
214+
}
191215

192216
if (Invocation.parseArgs(Args, Instance.getDiags(),
193217
&configurationFileBuffers, workingDirectory,
@@ -336,7 +360,7 @@ readOutputEntriesFromFile(StringRef Path) {
336360
}
337361

338362
int SwiftCacheToolInvocation::validateOutputs() {
339-
auto DB = llvm::cas::createOnDiskUnifiedCASDatabases(CASPath);
363+
auto DB = CASOpts.getOrCreateDatabases();
340364
if (!DB)
341365
report_fatal_error(DB.takeError());
342366

test/CAS/cache_key_compute.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,38 @@
4444
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
4545
// RUN: -emit-tbd -emit-tbd-path %t/test.tbd -o %t/test.o -allow-unstable-cache-key-for-testing | %FileCheck %s
4646

47+
/// Test plugin CAS.
48+
// RUN: %cache-tool -cas-path %t/cas -cas-plugin-path %llvm_libs_dir/libCASPluginTest%llvm_plugin_ext \
49+
// RUN: -cas-plugin-option first-prefix=myfirst- -cache-tool-action print-output-keys -- \
50+
// RUN: %target-swift-frontend -cache-compile-job %s -emit-module -c -emit-dependencies \
51+
// 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
52+
4753
// CHECK: test.o
4854
// CHECK-NEXT: "OutputKind": "object"
4955
// CHECK-NEXT: "Input"
5056
// CHECK-NEXT: "CacheKey"
57+
// PLUGIN-SAME: myfirst-llvmcas://
5158

5259
// CHECK: test.swiftmodule
5360
// CHECK-NEXT: "OutputKind": "swiftmodule"
5461
// CHECK-NEXT: "Input"
5562
// CHECK-NEXT: "CacheKey"
63+
// PLUGIN-SAME: myfirst-llvmcas://
5664

5765
// CHECK: test.d
5866
// CHECK-NEXT: "OutputKind": "dependencies"
5967
// CHECK-NEXT: "Input"
6068
// CHECK-NEXT: "CacheKey"
69+
// PLUGIN-SAME: myfirst-llvmcas://
6170

6271
// CHECK: test.tbd
6372
// CHECK-NEXT: "OutputKind": "tbd"
6473
// CHECK-NEXT: "Input"
6574
// CHECK-NEXT: "CacheKey"
75+
// PLUGIN-SAME: myfirst-llvmcas://
6676

6777
// CHECK: <cached-diagnostics>
6878
// CHECK-NEXT: "OutputKind": "cached-diagnostics"
6979
// CHECK-NEXT: "Input": "<cached-diagnostics>"
7080
// CHECK-NEXT: "CacheKey"
81+
// PLUGIN-SAME: myfirst-llvmcas://

0 commit comments

Comments
 (0)