Skip to content

Commit f857dbf

Browse files
authored
Merge pull request #686 from compnerd/utility
SourceKit: sever dependency on `TSCUtility`
2 parents 1011c6e + 4bbd74f commit f857dbf

File tree

14 files changed

+67
-67
lines changed

14 files changed

+67
-67
lines changed

Sources/SKCore/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ target_link_libraries(SKCore PRIVATE
2424
LanguageServerProtocolJSONRPC
2525
PackageModel
2626
SKSupport
27-
SourceKitD
28-
TSCUtility)
27+
SourceKitD)

Sources/SKCore/FallbackBuildSystem.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
import BuildServerProtocol
1414
import LanguageServerProtocol
15+
import SKSupport
16+
import Foundation
1517
import Dispatch
1618

1719
import class TSCBasic.Process
1820
import struct TSCBasic.AbsolutePath
19-
import enum TSCUtility.Platform
2021

2122
/// A simple BuildSystem suitable as a fallback when accurate settings are unknown.
2223
public final class FallbackBuildSystem: BuildSystem {
@@ -29,14 +30,8 @@ public final class FallbackBuildSystem: BuildSystem {
2930

3031
/// The path to the SDK.
3132
public lazy var sdkpath: AbsolutePath? = {
32-
if case .darwin? = Platform.currentPlatform,
33-
let str = try? Process.checkNonZeroExit(
34-
args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"),
35-
let path = try? AbsolutePath(validating: str.spm_chomp())
36-
{
37-
return path
38-
}
39-
return nil
33+
guard Platform.current == .darwin else { return nil }
34+
return try? AbsolutePath(validating: Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx").trimmingCharacters(in: .whitespacesAndNewlines))
4035
}()
4136

4237
/// Delegate to handle any build system events.

Sources/SKCore/Toolchain.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import SKSupport
1717
import struct TSCBasic.AbsolutePath
1818
import protocol TSCBasic.FileSystem
1919
import var TSCBasic.localFileSystem
20-
import enum TSCUtility.Platform
2120

2221
/// A Toolchain is a collection of related compilers and libraries meant to be used together to
2322
/// build and edit source code.
@@ -135,7 +134,7 @@ extension Toolchain {
135134

136135
var foundAny = false
137136

138-
let execExt = Platform.currentPlatform?.executableExtension ?? ""
137+
let execExt = Platform.current?.executableExtension ?? ""
139138

140139
let clangPath = binPath.appending(component: "clang\(execExt)")
141140
if fs.isExecutableFile(clangPath) {
@@ -162,7 +161,7 @@ extension Toolchain {
162161

163162
// If 'currentPlatform' is nil it's most likely an unknown linux flavor.
164163
let dylibExt: String
165-
if let dynamicLibraryExtension = Platform.currentPlatform?.dynamicLibraryExtension {
164+
if let dynamicLibraryExtension = Platform.current?.dynamicLibraryExtension {
166165
dylibExt = dynamicLibraryExtension
167166
} else {
168167
log("Could not determine host OS. Falling back to using '.so' as dynamic library extension", level: .error)

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ extension ToolchainRegistry {
351351

352352
/// The path of the current Xcode.app/Contents/Developer.
353353
public static var currentXcodeDeveloperPath: AbsolutePath? {
354-
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p"),
355-
let path = try? AbsolutePath(validating: str.spm_chomp())
356-
{
357-
return path
358-
}
359-
return nil
354+
guard let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p") else { return nil }
355+
return try? AbsolutePath(validating: str.trimmingCharacters(in: .whitespacesAndNewlines))
360356
}
361357
}

Sources/SKSupport/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ add_library(SKSupport STATIC
1010
dlopen.swift)
1111
set_target_properties(SKSupport PROPERTIES
1212
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
13-
target_link_libraries(SKSupport PRIVATE
14-
TSCUtility)

Sources/SKSupport/Platform.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,46 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import enum TSCUtility.Platform
13+
import Foundation
14+
15+
import class TSCBasic.Process
16+
import struct TSCBasic.AbsolutePath
17+
import var TSCBasic.localFileSystem
18+
19+
private func isAndroid() -> Bool {
20+
return (try? localFileSystem.isFile(AbsolutePath(validating: "/system/bin/toolchain"))) ?? false ||
21+
(try? localFileSystem.isFile(AbsolutePath(validating: "/system/bin/toybox"))) ?? false
22+
}
23+
24+
public enum Platform: Equatable {
25+
case android
26+
case darwin
27+
case linux
28+
case windows
29+
}
1430

1531
extension Platform {
32+
// This is not just a computed property because the ToolchainRegistryTests
33+
// change the value.
34+
public static var current: Platform? = {
35+
#if os(Windows)
36+
return .windows
37+
#else
38+
switch try? Process.checkNonZeroExit(args: "uname")
39+
.trimmingCharacters(in: .whitespacesAndNewlines)
40+
.lowercased() {
41+
case "darwin"?:
42+
return .darwin
43+
case "linux"?:
44+
return isAndroid() ? .android : .linux
45+
default:
46+
return nil
47+
}
48+
#endif
49+
}()
50+
}
1651

52+
extension Platform {
1753
/// The file extension used for a dynamic library on this platform.
1854
public var dynamicLibraryExtension: String {
1955
switch self {

Sources/SKTestSupport/HostPlatform.swift

Lines changed: 0 additions & 18 deletions
This file was deleted.

Sources/SKTestSupport/SKTibsTestWorkspace.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import SourceKitLSP
1414
import LanguageServerProtocol
1515
import SKCore
16+
import SKSupport
1617
import IndexStoreDB
1718
import ISDBTibs
1819
import ISDBTestSupport
@@ -21,7 +22,6 @@ import Foundation
2122
import LSPTestSupport
2223

2324
import struct TSCBasic.AbsolutePath
24-
import enum TSCUtility.Platform
2525
import struct PackageModel.BuildFlags
2626

2727
public typealias URL = Foundation.URL
@@ -237,7 +237,7 @@ extension Location {
237237

238238
extension TibsToolchain {
239239
public convenience init(_ sktc: Toolchain) {
240-
let execExt: String = Platform.currentPlatform?.executableExtension ?? ""
240+
let execExt: String = Platform.current?.executableExtension ?? ""
241241

242242
let ninja: URL?
243243
if let ninjaPath = ProcessInfo.processInfo.environment["NINJA_BIN"] {

Sources/SourceKitD/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ set_target_properties(SourceKitD PROPERTIES
1515
target_link_libraries(SourceKitD PRIVATE
1616
Csourcekitd
1717
LSPLogging
18-
SKSupport
19-
TSCUtility)
18+
SKSupport)

Sources/SourceKitLSP/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ target_link_libraries(SourceKitLSP PUBLIC
4646
SwiftSyntax::SwiftParser
4747
SwiftSyntax::SwiftDiagnostics
4848
SwiftSyntax::SwiftSyntax
49-
SwiftSyntax::IDEUtils
50-
TSCUtility)
49+
SwiftSyntax::IDEUtils)
5150
target_link_libraries(SourceKitLSP PRIVATE
5251
$<$<NOT:$<PLATFORM_ID:Darwin>>:FoundationXML>)
5352

Sources/sourcekit-lsp/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ target_link_libraries(sourcekit-lsp PRIVATE
55
ArgumentParser
66
LanguageServerProtocolJSONRPC
77
SKCore
8-
SourceKitLSP
9-
TSCUtility)
8+
SourceKitLSP)
109
target_link_libraries(sourcekit-lsp PRIVATE
1110
$<$<NOT:$<PLATFORM_ID:Darwin>>:FoundationXML>)
1211

Tests/SKCoreTests/ToolchainRegistryTests.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SKCore
14+
import SKSupport
1415
import TSCBasic
1516
import XCTest
1617

17-
import enum TSCUtility.Platform
18-
1918
final class ToolchainRegistryTests: XCTestCase {
2019
func testDefaultBasic() {
2120
let tr = ToolchainRegistry()
@@ -33,9 +32,9 @@ final class ToolchainRegistryTests: XCTestCase {
3332
}
3433

3534
func testDefaultDarwin() {
36-
let prevPlatform = Platform.currentPlatform
37-
defer { Platform.currentPlatform = prevPlatform }
38-
Platform.currentPlatform = .darwin
35+
let prevPlatform = Platform.current
36+
defer { Platform.current = prevPlatform }
37+
Platform.current = .darwin
3938

4039
let tr = ToolchainRegistry()
4140
tr.darwinToolchainOverride = nil
@@ -51,9 +50,9 @@ final class ToolchainRegistryTests: XCTestCase {
5150
}
5251

5352
func testUnknownPlatform() {
54-
let prevPlatform = Platform.currentPlatform
55-
defer { Platform.currentPlatform = prevPlatform }
56-
Platform.currentPlatform = nil
53+
let prevPlatform = Platform.current
54+
defer { Platform.current = prevPlatform }
55+
Platform.current = nil
5756

5857
let fs = InMemoryFileSystem()
5958
let binPath = AbsolutePath("/foo/bar/my_toolchain/bin")
@@ -548,7 +547,7 @@ private func makeToolchain(
548547
#endif
549548
}
550549

551-
let execExt = Platform.currentPlatform?.executableExtension ?? ""
550+
let execExt = Platform.current?.executableExtension ?? ""
552551

553552
if clang {
554553
makeExec(binPath.appending(component: "clang\(execExt)"))
@@ -560,7 +559,7 @@ private func makeToolchain(
560559
makeExec(binPath.appending(component: "swiftc\(execExt)"))
561560
}
562561

563-
let dylibSuffix = Platform.currentPlatform?.dynamicLibraryExtension ?? ".so"
562+
let dylibSuffix = Platform.current?.dynamicLibraryExtension ?? ".so"
564563

565564
if sourcekitd {
566565
try! fs.createDirectory(libPath.appending(component: "sourcekitd.framework"))

Tests/SourceKitDTests/CrashRecoveryTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import LSPTestSupport
1616
import LSPLogging
1717
import SourceKitLSP
1818
import SourceKitD
19+
import SKSupport
1920
import SKTestSupport
2021
import XCTest
2122

@@ -42,7 +43,7 @@ fileprivate extension HoverResponse {
4243

4344
final class CrashRecoveryTests: XCTestCase {
4445
func testSourcekitdCrashRecovery() throws {
45-
try XCTSkipUnless(isDarwinHost, "Linux and Windows use in-process sourcekitd")
46+
try XCTSkipUnless(Platform.current == .darwin, "Linux and Windows use in-process sourcekitd")
4647
try XCTSkipUnless(longTestsEnabled)
4748

4849
let ws = try! staticSourceKitTibsWorkspace(name: "sourcekitdCrashRecovery")!

Tests/SourceKitDTests/SourceKitDTests.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@
1313
import LSPTestSupport
1414
import SourceKitD
1515
import SKCore
16+
import SKSupport
1617
import TSCBasic
1718
import ISDBTibs
1819
import ISDBTestSupport
20+
import Foundation
1921
import XCTest
2022

21-
import enum TSCUtility.Platform
23+
import class TSCBasic.Process
2224

2325
final class SourceKitDTests: XCTestCase {
2426
static var sourcekitdPath: AbsolutePath! = nil
2527
static var sdkpath: String? = nil
2628

2729
override class func setUp() {
2830
sourcekitdPath = ToolchainRegistry.shared.default!.sourcekitd!
29-
if case .darwin? = Platform.currentPlatform,
30-
let str = try? Process.checkNonZeroExit(
31-
args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx")
32-
{
33-
sdkpath = str.spm_chomp()
34-
}
31+
guard case .darwin? = Platform.current else { return }
32+
sdkpath = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx").trimmingCharacters(in: .whitespacesAndNewlines)
3533
}
3634

3735
func testMultipleNotificationHandlers() {

0 commit comments

Comments
 (0)