Skip to content

Commit 32846eb

Browse files
authored
begin to integrate SwiftSystem (#262)
motivation: SwiftSystem provides infra that can replace some bespoke implementations in TSC, improving the cross-platform support changes: add SwiftPM and CMake setup for including SwiftSystem as a dependency
1 parent 8e324ae commit 32846eb

File tree

5 files changed

+42
-21
lines changed

5 files changed

+42
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ xcuserdata/
66
.swiftpm
77
build
88
.vscode
9+
Package.resolved

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ find_package(dispatch QUIET)
2929
find_package(Foundation QUIET)
3030
find_package(Threads QUIET)
3131
find_package(SQLite3 REQUIRED)
32+
find_package(SwiftSystem CONFIG REQUIRED)
3233

3334
add_subdirectory(Sources)
3435
add_subdirectory(cmake/modules)

Package.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
/*
44
This source file is part of the Swift.org open source project
5-
5+
66
Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors
77
Licensed under Apache License v2.0 with Runtime Library Exception
8-
8+
99
See http://swift.org/LICENSE.txt for license information
1010
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
1111
*/
@@ -46,9 +46,9 @@ let package = Package(
4646
],
4747
dependencies: [],
4848
targets: [
49-
49+
5050
// MARK: Tools support core targets
51-
51+
5252
.target(
5353
/** Shim target to import missing C headers in Darwin and Glibc modulemap. */
5454
name: "TSCclibc",
@@ -62,24 +62,28 @@ let package = Package(
6262
.target(
6363
/** TSCBasic support library */
6464
name: "TSCBasic",
65-
dependencies: ["TSCLibc", "TSCclibc"],
65+
dependencies: [
66+
"TSCLibc",
67+
"TSCclibc",
68+
.product(name: "SystemPackage", package: "swift-system"),
69+
],
6670
exclude: CMakeFiles + ["README.md"]),
6771
.target(
6872
/** Abstractions for common operations, should migrate to TSCBasic */
6973
name: "TSCUtility",
7074
dependencies: ["TSCBasic", "TSCclibc"],
7175
exclude: CMakeFiles),
72-
76+
7377
// MARK: Additional Test Dependencies
74-
78+
7579
.target(
7680
/** Generic test support library */
7781
name: "TSCTestSupport",
7882
dependencies: ["TSCBasic", "TSCUtility"]),
79-
80-
83+
84+
8185
// MARK: Tools support core tests
82-
86+
8387
.testTarget(
8488
name: "TSCBasicTests",
8589
dependencies: ["TSCTestSupport", "TSCclibc"],
@@ -97,6 +101,19 @@ let package = Package(
97101
]
98102
)
99103

104+
/// When not using local dependencies, the branch to use for llbuild and TSC repositories.
105+
let relatedDependenciesBranch = "main"
106+
107+
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
108+
package.dependencies += [
109+
.package(url: "https://github.com/apple/swift-system.git", from: "1.0.0"),
110+
]
111+
} else {
112+
package.dependencies += [
113+
.package(path: "../swift-system"),
114+
]
115+
}
116+
100117
// FIXME: conditionalise these flags since SwiftPM 5.3 and earlier will crash
101118
// for platforms they don't know about.
102119
#if os(Windows)

Sources/TSCBasic/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ add_library(TSCBasic
5050
Thread.swift
5151
Tuple.swift
5252
misc.swift)
53-
53+
5454
target_compile_options(TSCBasic PUBLIC
5555
# Don't use GNU strerror_r on Android.
5656
"$<$<PLATFORM_ID:Android>:SHELL:-Xcc -U_GNU_SOURCE>"
5757
# Ignore secure function warnings on Windows.
5858
"$<$<PLATFORM_ID:Windows>:SHELL:-Xcc -D_CRT_SECURE_NO_WARNINGS>")
5959
target_link_libraries(TSCBasic PUBLIC
60+
SwiftSystem::SystemPackage
6061
TSCLibc)
6162
target_link_libraries(TSCBasic PRIVATE
6263
TSCclibc)

Sources/TSCBasic/FileSystem.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import TSCLibc
1212
import Foundation
1313
import Dispatch
14+
import SystemPackage
1415

1516
public struct FileSystemError: Error, Equatable {
1617
public enum Kind: Equatable {
@@ -175,7 +176,7 @@ public protocol FileSystem: AnyObject {
175176

176177
/// Get the home directory of current user
177178
var homeDirectory: AbsolutePath { get }
178-
179+
179180
/// Get the caches directory of current user
180181
var cachesDirectory: AbsolutePath? { get }
181182

@@ -346,7 +347,7 @@ private class LocalFileSystem: FileSystem {
346347
var homeDirectory: AbsolutePath {
347348
return AbsolutePath(NSHomeDirectory())
348349
}
349-
350+
350351
var cachesDirectory: AbsolutePath? {
351352
return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first.flatMap { AbsolutePath($0.path) }
352353
}
@@ -515,7 +516,7 @@ private class LocalFileSystem: FileSystem {
515516
//
516517
/// Concrete FileSystem implementation which simulates an empty disk.
517518
public class InMemoryFileSystem: FileSystem {
518-
519+
519520
/// Private internal representation of a file system node.
520521
/// Not threadsafe.
521522
private class Node {
@@ -551,7 +552,7 @@ public class InMemoryFileSystem: FileSystem {
551552
}
552553
}
553554
}
554-
555+
555556
/// Private internal representation the contents of a directory.
556557
/// Not threadsafe.
557558
private class DirectoryContents {
@@ -573,7 +574,7 @@ public class InMemoryFileSystem: FileSystem {
573574

574575
/// The root node of the filesytem.
575576
private var root: Node
576-
577+
577578
/// Protects `root` and everything underneath it.
578579
/// FIXME: Using a single lock for this is a performance problem, but in
579580
/// reality, the only practical use for InMemoryFileSystem is for unit
@@ -583,7 +584,7 @@ public class InMemoryFileSystem: FileSystem {
583584
private var lockFiles = Dictionary<AbsolutePath, WeakReference<DispatchQueue>>()
584585
/// Used to access lockFiles in a thread safe manner.
585586
private let lockFilesLock = Lock()
586-
587+
587588
/// Exclusive file system lock vended to clients through `withLock()`.
588589
// Used to ensure that DispatchQueues are releassed when they are no longer in use.
589590
private struct WeakReference<Value: AnyObject> {
@@ -717,7 +718,7 @@ public class InMemoryFileSystem: FileSystem {
717718
// FIXME: Maybe we should allow setting this when creating the fs.
718719
return AbsolutePath("/home/user")
719720
}
720-
721+
721722
public var cachesDirectory: AbsolutePath? {
722723
return self.homeDirectory.appending(component: "caches")
723724
}
@@ -735,7 +736,7 @@ public class InMemoryFileSystem: FileSystem {
735736
return [String](contents.entries.keys)
736737
}
737738
}
738-
739+
739740
/// Not threadsafe.
740741
private func _createDirectory(_ path: AbsolutePath, recursive: Bool) throws {
741742
// Ignore if client passes root.
@@ -882,7 +883,7 @@ public class InMemoryFileSystem: FileSystem {
882883
public func chmod(_ mode: FileMode, path: AbsolutePath, options: Set<FileMode.Option>) throws {
883884
// FIXME: We don't have these semantics in InMemoryFileSystem.
884885
}
885-
886+
886887
/// Private implementation of core copying function.
887888
/// Not threadsafe.
888889
private func _copy(from sourcePath: AbsolutePath, to destinationPath: AbsolutePath) throws {
@@ -1023,7 +1024,7 @@ public class RerootedFileSystemView: FileSystem {
10231024
public var homeDirectory: AbsolutePath {
10241025
fatalError("homeDirectory on RerootedFileSystemView is not supported.")
10251026
}
1026-
1027+
10271028
public var cachesDirectory: AbsolutePath? {
10281029
fatalError("cachesDirectory on RerootedFileSystemView is not supported.")
10291030
}

0 commit comments

Comments
 (0)