Skip to content

begin to integrate SwiftSystem #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ xcuserdata/
.swiftpm
build
.vscode
Package.resolved
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
37 changes: 27 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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",
Expand All @@ -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"],
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Sources/TSCBasic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"$<$<PLATFORM_ID:Android>:SHELL:-Xcc -U_GNU_SOURCE>"
# Ignore secure function warnings on Windows.
"$<$<PLATFORM_ID:Windows>:SHELL:-Xcc -D_CRT_SECURE_NO_WARNINGS>")
target_link_libraries(TSCBasic PUBLIC
SwiftSystem::SystemPackage
TSCLibc)
target_link_libraries(TSCBasic PRIVATE
TSCclibc)
Expand Down
21 changes: 11 additions & 10 deletions Sources/TSCBasic/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import TSCLibc
import Foundation
import Dispatch
import SystemPackage

public struct FileSystemError: Error, Equatable {
public enum Kind: Equatable {
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -551,7 +552,7 @@ public class InMemoryFileSystem: FileSystem {
}
}
}

/// Private internal representation the contents of a directory.
/// Not threadsafe.
private class DirectoryContents {
Expand All @@ -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
Expand All @@ -583,7 +584,7 @@ public class InMemoryFileSystem: FileSystem {
private var lockFiles = Dictionary<AbsolutePath, WeakReference<DispatchQueue>>()
/// 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<Value: AnyObject> {
Expand Down Expand Up @@ -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")
}
Expand All @@ -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.
Expand Down Expand Up @@ -882,7 +883,7 @@ public class InMemoryFileSystem: FileSystem {
public func chmod(_ mode: FileMode, path: AbsolutePath, options: Set<FileMode.Option>) 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 {
Expand Down Expand Up @@ -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.")
}
Expand Down