@@ -249,6 +249,9 @@ public class SwiftTool<Options: ToolOptions> {
249
249
/// Path to the build directory.
250
250
let buildPath : AbsolutePath
251
251
252
+ /// Path to the repository cache.
253
+ let cachePath : AbsolutePath
254
+
252
255
/// Reference to the argument parser.
253
256
let parser : ArgumentParser
254
257
@@ -330,9 +333,15 @@ public class SwiftTool<Options: ToolOptions> {
330
333
binder. bind (
331
334
option: parser. add (
332
335
option: " --build-path " , kind: PathArgument . self,
333
- usage: " Specify build/cache directory [default: ./.build] " ) ,
336
+ usage: " Specify build directory [default: ./.build] " ) ,
334
337
to: { $0. buildPath = $1. path } )
335
338
339
+ binder. bind (
340
+ option: parser. add (
341
+ option: " --cache-path " , kind: PathArgument . self,
342
+ usage: " Specify the cache directory [default: \( Workspace . globalCachePath. prettyPath ( cwd: localFileSystem. homeDirectory) ) ] " ) ,
343
+ to: { $0. cachePath = $1. path } )
344
+
336
345
binder. bind (
337
346
option: parser. add (
338
347
option: " --chdir " , shortName: " -C " , kind: PathArgument . self) ,
@@ -537,12 +546,14 @@ public class SwiftTool<Options: ToolOptions> {
537
546
538
547
// Create local variables to use while finding build path to avoid capture self before init error.
539
548
let customBuildPath = options. buildPath
549
+ let customCachePath = options. cachePath
540
550
let packageRoot = findPackageRoot ( )
541
551
542
552
self . packageRoot = packageRoot
543
553
self . buildPath = getEnvBuildPath ( workingDir: cwd) ??
544
554
customBuildPath ??
545
555
( packageRoot ?? cwd) . appending ( component: " .build " )
556
+ self . cachePath = getEnvCachePath ( workingDir: cwd) ?? customCachePath ?? Workspace . globalCachePath
546
557
}
547
558
548
559
class func postprocessArgParserResult( result: ArgumentParser . Result , diagnostics: DiagnosticsEngine ) throws {
@@ -619,6 +630,7 @@ public class SwiftTool<Options: ToolOptions> {
619
630
let provider = GitRepositoryProvider ( processSet: processSet)
620
631
let workspace = Workspace (
621
632
dataPath: buildPath,
633
+ cachePath: cachePath,
622
634
editablesPath: try editablesPath ( ) ,
623
635
pinsFile: try resolvedFilePath ( ) ,
624
636
manifestLoader: try getManifestLoader ( ) ,
@@ -920,6 +932,14 @@ private func getEnvBuildPath(workingDir: AbsolutePath) -> AbsolutePath? {
920
932
return AbsolutePath ( env, relativeTo: workingDir)
921
933
}
922
934
935
+ /// Returns the cache path from the environment, if present.
936
+ private func getEnvCachePath( workingDir: AbsolutePath ) -> AbsolutePath ? {
937
+ // Don't rely on build path from env for SwiftPM's own tests.
938
+ guard ProcessEnv . vars [ " SWIFTPM_TESTS_MODULECACHE " ] == nil else { return nil }
939
+ guard let env = ProcessEnv . vars [ " SWIFTPM_CACHE_DIR " ] else { return nil }
940
+ return AbsolutePath ( env)
941
+ }
942
+
923
943
/// Returns the sandbox profile to be used when parsing manifest on macOS.
924
944
private func sandboxProfile( allowedDirectories: [ AbsolutePath ] ) -> String {
925
945
let stream = BufferedOutputByteStream ( )
0 commit comments