From 6b64b2b1a8eeb3f2a6a76d4b7c188276a4738416 Mon Sep 17 00:00:00 2001 From: tom doron Date: Fri, 19 Nov 2021 16:09:56 -0800 Subject: [PATCH] begin to integrate SwiftSystem 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 --- .gitignore | 1 + CMakeLists.txt | 1 + Package.swift | 37 ++++++++++++++++++++++--------- Sources/TSCBasic/CMakeLists.txt | 3 ++- Sources/TSCBasic/FileSystem.swift | 21 +++++++++--------- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 602922f3..4b488575 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ xcuserdata/ .swiftpm build .vscode +Package.resolved diff --git a/CMakeLists.txt b/CMakeLists.txt index e4abb990..d5b24e48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ find_package(dispatch QUIET) find_package(Foundation QUIET) find_package(Threads QUIET) find_package(SQLite3 REQUIRED) +find_package(SwiftSystem CONFIG REQUIRED) add_subdirectory(Sources) add_subdirectory(cmake/modules) diff --git a/Package.swift b/Package.swift index deb7fbbf..64ceb5c9 100644 --- a/Package.swift +++ b/Package.swift @@ -2,10 +2,10 @@ /* This source file is part of the Swift.org open source project - + Copyright (c) 2019 - 2021 Apple Inc. and the Swift project authors Licensed under Apache License v2.0 with Runtime Library Exception - + See http://swift.org/LICENSE.txt for license information See http://swift.org/CONTRIBUTORS.txt for Swift project authors */ @@ -46,9 +46,9 @@ let package = Package( ], dependencies: [], targets: [ - + // MARK: Tools support core targets - + .target( /** Shim target to import missing C headers in Darwin and Glibc modulemap. */ name: "TSCclibc", @@ -62,24 +62,28 @@ let package = Package( .target( /** TSCBasic support library */ name: "TSCBasic", - dependencies: ["TSCLibc", "TSCclibc"], + dependencies: [ + "TSCLibc", + "TSCclibc", + .product(name: "SystemPackage", package: "swift-system"), + ], exclude: CMakeFiles + ["README.md"]), .target( /** Abstractions for common operations, should migrate to TSCBasic */ name: "TSCUtility", dependencies: ["TSCBasic", "TSCclibc"], exclude: CMakeFiles), - + // MARK: Additional Test Dependencies - + .target( /** Generic test support library */ name: "TSCTestSupport", dependencies: ["TSCBasic", "TSCUtility"]), - - + + // MARK: Tools support core tests - + .testTarget( name: "TSCBasicTests", dependencies: ["TSCTestSupport", "TSCclibc"], @@ -97,6 +101,19 @@ let package = Package( ] ) +/// When not using local dependencies, the branch to use for llbuild and TSC repositories. + let relatedDependenciesBranch = "main" + + if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil { + package.dependencies += [ + .package(url: "https://github.com/apple/swift-system.git", from: "1.0.0"), + ] + } else { + package.dependencies += [ + .package(path: "../swift-system"), + ] + } + // FIXME: conditionalise these flags since SwiftPM 5.3 and earlier will crash // for platforms they don't know about. #if os(Windows) diff --git a/Sources/TSCBasic/CMakeLists.txt b/Sources/TSCBasic/CMakeLists.txt index c5efd819..73517a6a 100644 --- a/Sources/TSCBasic/CMakeLists.txt +++ b/Sources/TSCBasic/CMakeLists.txt @@ -50,13 +50,14 @@ add_library(TSCBasic Thread.swift Tuple.swift misc.swift) - + target_compile_options(TSCBasic PUBLIC # Don't use GNU strerror_r on Android. "$<$:SHELL:-Xcc -U_GNU_SOURCE>" # Ignore secure function warnings on Windows. "$<$:SHELL:-Xcc -D_CRT_SECURE_NO_WARNINGS>") target_link_libraries(TSCBasic PUBLIC + SwiftSystem::SystemPackage TSCLibc) target_link_libraries(TSCBasic PRIVATE TSCclibc) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index b15a2d6c..a0b698f8 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -11,6 +11,7 @@ import TSCLibc import Foundation import Dispatch +import SystemPackage public struct FileSystemError: Error, Equatable { public enum Kind: Equatable { @@ -175,7 +176,7 @@ public protocol FileSystem: AnyObject { /// Get the home directory of current user var homeDirectory: AbsolutePath { get } - + /// Get the caches directory of current user var cachesDirectory: AbsolutePath? { get } @@ -346,7 +347,7 @@ private class LocalFileSystem: FileSystem { var homeDirectory: AbsolutePath { return AbsolutePath(NSHomeDirectory()) } - + var cachesDirectory: AbsolutePath? { return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first.flatMap { AbsolutePath($0.path) } } @@ -515,7 +516,7 @@ private class LocalFileSystem: FileSystem { // /// Concrete FileSystem implementation which simulates an empty disk. public class InMemoryFileSystem: FileSystem { - + /// Private internal representation of a file system node. /// Not threadsafe. private class Node { @@ -551,7 +552,7 @@ public class InMemoryFileSystem: FileSystem { } } } - + /// Private internal representation the contents of a directory. /// Not threadsafe. private class DirectoryContents { @@ -573,7 +574,7 @@ public class InMemoryFileSystem: FileSystem { /// The root node of the filesytem. private var root: Node - + /// Protects `root` and everything underneath it. /// FIXME: Using a single lock for this is a performance problem, but in /// reality, the only practical use for InMemoryFileSystem is for unit @@ -583,7 +584,7 @@ public class InMemoryFileSystem: FileSystem { private var lockFiles = Dictionary>() /// Used to access lockFiles in a thread safe manner. private let lockFilesLock = Lock() - + /// Exclusive file system lock vended to clients through `withLock()`. // Used to ensure that DispatchQueues are releassed when they are no longer in use. private struct WeakReference { @@ -717,7 +718,7 @@ public class InMemoryFileSystem: FileSystem { // FIXME: Maybe we should allow setting this when creating the fs. return AbsolutePath("/home/user") } - + public var cachesDirectory: AbsolutePath? { return self.homeDirectory.appending(component: "caches") } @@ -735,7 +736,7 @@ public class InMemoryFileSystem: FileSystem { return [String](contents.entries.keys) } } - + /// Not threadsafe. private func _createDirectory(_ path: AbsolutePath, recursive: Bool) throws { // Ignore if client passes root. @@ -882,7 +883,7 @@ public class InMemoryFileSystem: FileSystem { public func chmod(_ mode: FileMode, path: AbsolutePath, options: Set) throws { // FIXME: We don't have these semantics in InMemoryFileSystem. } - + /// Private implementation of core copying function. /// Not threadsafe. private func _copy(from sourcePath: AbsolutePath, to destinationPath: AbsolutePath) throws { @@ -1023,7 +1024,7 @@ public class RerootedFileSystemView: FileSystem { public var homeDirectory: AbsolutePath { fatalError("homeDirectory on RerootedFileSystemView is not supported.") } - + public var cachesDirectory: AbsolutePath? { fatalError("cachesDirectory on RerootedFileSystemView is not supported.") }