@@ -294,6 +294,15 @@ public class SwiftTool {
294294 /// Path to the build directory.
295295 let buildPath : AbsolutePath
296296
297+ /// Path to the shared security directory
298+ let sharedSecurityDirectory : AbsolutePath ?
299+
300+ /// Path to the shared cache directory
301+ let sharedCacheDirectory : AbsolutePath ?
302+
303+ /// Path to the shared configuration directory
304+ let sharedConfigurationDirectory : AbsolutePath ?
305+
297306 /// The process set to hold the launched processes. These will be terminated on any signal
298307 /// received by the swift tools.
299308 let processSet : ProcessSet
@@ -333,7 +342,7 @@ public class SwiftTool {
333342 self . observabilityScope. emit ( error: " couldn't determine the current working directory " )
334343 throw ExitCode . failure
335344 }
336- originalWorkingDirectory = cwd
345+ self . originalWorkingDirectory = cwd
337346
338347 do {
339348 try Self . postprocessArgParserResult ( options: options, observabilityScope: self . observabilityScope)
@@ -416,6 +425,11 @@ public class SwiftTool {
416425 customBuildPath ??
417426 ( packageRoot ?? cwd) . appending ( component: " .build " )
418427
428+ // make sure common directories are created
429+ self . sharedSecurityDirectory = try getSharedSecurityDirectory ( options: self . options, observabilityScope: self . observabilityScope)
430+ self . sharedConfigurationDirectory = try getSharedConfigurationDirectory ( options: self . options, observabilityScope: self . observabilityScope)
431+ self . sharedCacheDirectory = try getSharedCacheDirectory ( options: self . options, observabilityScope: self . observabilityScope)
432+
419433 // set verbosity globals.
420434 // TODO: get rid of this global settings in TSC
421435 switch self . logLevel {
@@ -492,7 +506,7 @@ public class SwiftTool {
492506 }
493507
494508 func getMirrorsConfig( sharedConfigurationDirectory: AbsolutePath ? = nil ) throws -> Workspace . Configuration . Mirrors {
495- let sharedConfigurationDirectory = try sharedConfigurationDirectory ?? self . getSharedConfigurationDirectory ( )
509+ let sharedConfigurationDirectory = sharedConfigurationDirectory ?? self . sharedConfigurationDirectory
496510 let sharedMirrorFile = sharedConfigurationDirectory. map { Workspace . DefaultLocations. mirrorsConfigurationFile ( at: $0) }
497511 return try . init(
498512 localMirrorFile: self . mirrorsConfigFile ( ) ,
@@ -537,7 +551,7 @@ public class SwiftTool {
537551 func getRegistriesConfig( sharedConfigurationDirectory: AbsolutePath ? = nil ) throws -> Workspace . Configuration . Registries {
538552 let localRegistriesFile = try Workspace . DefaultLocations. registriesConfigurationFile ( forRootPackage: self . getPackageRoot ( ) )
539553
540- let sharedConfigurationDirectory = try sharedConfigurationDirectory ?? self . getSharedConfigurationDirectory ( )
554+ let sharedConfigurationDirectory = sharedConfigurationDirectory ?? self . sharedConfigurationDirectory
541555 let sharedRegistriesFile = sharedConfigurationDirectory. map {
542556 Workspace . DefaultLocations. registriesConfigurationFile ( at: $0)
543557 }
@@ -607,56 +621,6 @@ public class SwiftTool {
607621 return providers
608622 }
609623
610- private func getSharedCacheDirectory( ) throws -> AbsolutePath ? {
611- if let explicitCachePath = options. cachePath {
612- // Create the explicit cache path if necessary
613- if !localFileSystem. exists ( explicitCachePath) {
614- try localFileSystem. createDirectory ( explicitCachePath, recursive: true )
615- }
616- return explicitCachePath
617- }
618-
619- do {
620- return try localFileSystem. getOrCreateSwiftPMCacheDirectory ( )
621- } catch {
622- self . observabilityScope. emit ( warning: " Failed creating default cache location, \( error) " )
623- return . none
624- }
625- }
626-
627- private func getSharedConfigurationDirectory( ) throws -> AbsolutePath ? {
628- if let explicitConfigPath = options. configPath {
629- // Create the explicit config path if necessary
630- if !localFileSystem. exists ( explicitConfigPath) {
631- try localFileSystem. createDirectory ( explicitConfigPath, recursive: true )
632- }
633- return explicitConfigPath
634- }
635-
636- do {
637- return try localFileSystem. getOrCreateSwiftPMConfigDirectory ( )
638- } catch {
639- self . observabilityScope. emit ( warning: " Failed creating default configuration location, \( error) " )
640- return . none
641- }
642- }
643-
644- private func getSharedSecurityDirectory( ) throws -> AbsolutePath ? {
645- do {
646- let fileSystem = localFileSystem
647- let sharedSecurityDirectory = fileSystem. swiftPMSecurityDirectory
648- if !fileSystem. exists ( sharedSecurityDirectory) {
649- try fileSystem. createDirectory ( sharedSecurityDirectory, recursive: true )
650- }
651- // And make sure we can write files (locking the directory writes a lock file)
652- try fileSystem. withLock ( on: sharedSecurityDirectory, type: . exclusive) { }
653- return sharedSecurityDirectory
654- } catch {
655- self . observabilityScope. emit ( warning: " Failed creating shared security directory: \( error) " )
656- return . none
657- }
658- }
659-
660624 /// Returns the currently active workspace.
661625 func getActiveWorkspace( ) throws -> Workspace {
662626 if let workspace = _workspace {
@@ -665,28 +629,25 @@ public class SwiftTool {
665629
666630 let delegate = ToolWorkspaceDelegate ( self . outputStream, logLevel: self . logLevel, observabilityScope: self . observabilityScope)
667631 let provider = GitRepositoryProvider ( processSet: processSet)
668- let sharedSecurityDirectory = try self . getSharedSecurityDirectory ( )
669- let sharedCacheDirectory = try self . getSharedCacheDirectory ( )
670- let sharedConfigurationDirectory = try self . getSharedConfigurationDirectory ( )
671632 let isXcodeBuildSystemEnabled = self . options. buildSystem == . xcode
672633 let workspace = try Workspace (
673634 fileSystem: localFileSystem,
674635 location: . init(
675- workingDirectory: buildPath,
636+ workingDirectory: self . buildPath,
676637 editsDirectory: self . editsDirectory ( ) ,
677638 resolvedVersionsFile: self . resolvedVersionsFile ( ) ,
678- sharedSecurityDirectory: sharedSecurityDirectory,
679- sharedCacheDirectory: sharedCacheDirectory,
680- sharedConfigurationDirectory: sharedConfigurationDirectory
639+ sharedSecurityDirectory: self . sharedSecurityDirectory,
640+ sharedCacheDirectory: self . sharedCacheDirectory,
641+ sharedConfigurationDirectory: self . sharedConfigurationDirectory
681642 ) ,
682- mirrors: self . getMirrorsConfig ( sharedConfigurationDirectory: sharedConfigurationDirectory) . mirrors,
683- registries: try self . getRegistriesConfig ( sharedConfigurationDirectory: sharedConfigurationDirectory) . configuration,
643+ mirrors: self . getMirrorsConfig ( sharedConfigurationDirectory: self . sharedConfigurationDirectory) . mirrors,
644+ registries: try self . getRegistriesConfig ( sharedConfigurationDirectory: self . sharedConfigurationDirectory) . configuration,
684645 authorizationProvider: self . getAuthorizationProvider ( ) ,
685646 customManifestLoader: self . getManifestLoader ( ) , // FIXME: doe we really need to customize it?
686647 customRepositoryProvider: provider, // FIXME: doe we really need to customize it?
687648 additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription . xcbuildFileTypes : FileRuleDescription . swiftpmFileTypes,
688- resolverUpdateEnabled: !options. skipDependencyUpdate,
689- resolverPrefetchingEnabled: options. shouldEnableResolverPrefetching,
649+ resolverUpdateEnabled: !self . options. skipDependencyUpdate,
650+ resolverPrefetchingEnabled: self . options. shouldEnableResolverPrefetching,
690651 resolverFingerprintCheckingMode: self . options. resolverFingerprintCheckingMode,
691652 sharedRepositoriesCacheEnabled: self . options. useRepositoriesCache,
692653 delegate: delegate
@@ -1011,7 +972,7 @@ public class SwiftTool {
1011972 case ( false , . local) :
1012973 cachePath = self . buildPath
1013974 case ( false , . shared) :
1014- cachePath = try self . getSharedCacheDirectory ( ) . map { Workspace . DefaultLocations. manifestsDirectory ( at: $0) }
975+ cachePath = self . sharedCacheDirectory . map { Workspace . DefaultLocations. manifestsDirectory ( at: $0) }
1015976 }
1016977
1017978 var extraManifestFlags = self . options. manifestFlags
@@ -1062,6 +1023,61 @@ private func getEnvBuildPath(workingDir: AbsolutePath) -> AbsolutePath? {
10621023 return AbsolutePath ( env, relativeTo: workingDir)
10631024}
10641025
1026+
1027+ private func getSharedSecurityDirectory( options: SwiftToolOptions , observabilityScope: ObservabilityScope ) throws -> AbsolutePath ? {
1028+ if let explicitSecurityPath = options. securityPath {
1029+ // Create the explicit security path if necessary
1030+ if !localFileSystem. exists ( explicitSecurityPath) {
1031+ try localFileSystem. createDirectory ( explicitSecurityPath, recursive: true )
1032+ }
1033+ return explicitSecurityPath
1034+ }
1035+
1036+ do {
1037+ let sharedSecurityDirectory = try localFileSystem. getOrCreateSwiftPMSecurityDirectory ( )
1038+ // And make sure we can write files (locking the directory writes a lock file)
1039+ try localFileSystem. withLock ( on: sharedSecurityDirectory, type: . exclusive) { }
1040+ return sharedSecurityDirectory
1041+ } catch {
1042+ observabilityScope. emit ( warning: " Failed creating default security location, \( error) " )
1043+ return . none
1044+ }
1045+ }
1046+
1047+ private func getSharedConfigurationDirectory( options: SwiftToolOptions , observabilityScope: ObservabilityScope ) throws -> AbsolutePath ? {
1048+ if let explicitConfigPath = options. configPath {
1049+ // Create the explicit config path if necessary
1050+ if !localFileSystem. exists ( explicitConfigPath) {
1051+ try localFileSystem. createDirectory ( explicitConfigPath, recursive: true )
1052+ }
1053+ return explicitConfigPath
1054+ }
1055+
1056+ do {
1057+ return try localFileSystem. getOrCreateSwiftPMConfigurationDirectory ( observabilityScope: observabilityScope)
1058+ } catch {
1059+ observabilityScope. emit ( warning: " Failed creating default configuration location, \( error) " )
1060+ return . none
1061+ }
1062+ }
1063+
1064+ private func getSharedCacheDirectory( options: SwiftToolOptions , observabilityScope: ObservabilityScope ) throws -> AbsolutePath ? {
1065+ if let explicitCachePath = options. cachePath {
1066+ // Create the explicit cache path if necessary
1067+ if !localFileSystem. exists ( explicitCachePath) {
1068+ try localFileSystem. createDirectory ( explicitCachePath, recursive: true )
1069+ }
1070+ return explicitCachePath
1071+ }
1072+
1073+ do {
1074+ return try localFileSystem. getOrCreateSwiftPMCacheDirectory ( )
1075+ } catch {
1076+ observabilityScope. emit ( warning: " Failed creating default cache location, \( error) " )
1077+ return . none
1078+ }
1079+ }
1080+
10651081/// A wrapper to hold the build system so we can use it inside
10661082/// the int. handler without requiring to initialize it.
10671083final class BuildSystemRef {
0 commit comments