Skip to content

SourceKit: sever dependency on TSCUtility #686

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
Jan 3, 2023
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
3 changes: 1 addition & 2 deletions Sources/SKCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ target_link_libraries(SKCore PRIVATE
LanguageServerProtocolJSONRPC
PackageModel
SKSupport
SourceKitD
TSCUtility)
SourceKitD)
13 changes: 4 additions & 9 deletions Sources/SKCore/FallbackBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

import BuildServerProtocol
import LanguageServerProtocol
import SKSupport
import Foundation
import Dispatch

import class TSCBasic.Process
import struct TSCBasic.AbsolutePath
import enum TSCUtility.Platform

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

/// The path to the SDK.
public lazy var sdkpath: AbsolutePath? = {
if case .darwin? = Platform.currentPlatform,
let str = try? Process.checkNonZeroExit(
args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"),
let path = try? AbsolutePath(validating: str.spm_chomp())
{
return path
}
return nil
guard Platform.current == .darwin else { return nil }
return try? AbsolutePath(validating: Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx").trimmingCharacters(in: .whitespacesAndNewlines))
}()

/// Delegate to handle any build system events.
Expand Down
5 changes: 2 additions & 3 deletions Sources/SKCore/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import SKSupport
import struct TSCBasic.AbsolutePath
import protocol TSCBasic.FileSystem
import var TSCBasic.localFileSystem
import enum TSCUtility.Platform

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

var foundAny = false

let execExt = Platform.currentPlatform?.executableExtension ?? ""
let execExt = Platform.current?.executableExtension ?? ""

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

// If 'currentPlatform' is nil it's most likely an unknown linux flavor.
let dylibExt: String
if let dynamicLibraryExtension = Platform.currentPlatform?.dynamicLibraryExtension {
if let dynamicLibraryExtension = Platform.current?.dynamicLibraryExtension {
dylibExt = dynamicLibraryExtension
} else {
log("Could not determine host OS. Falling back to using '.so' as dynamic library extension", level: .error)
Expand Down
8 changes: 2 additions & 6 deletions Sources/SKCore/ToolchainRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,7 @@ extension ToolchainRegistry {

/// The path of the current Xcode.app/Contents/Developer.
public static var currentXcodeDeveloperPath: AbsolutePath? {
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p"),
let path = try? AbsolutePath(validating: str.spm_chomp())
{
return path
}
return nil
guard let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p") else { return nil }
return try? AbsolutePath(validating: str.trimmingCharacters(in: .whitespacesAndNewlines))
}
}
2 changes: 0 additions & 2 deletions Sources/SKSupport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,3 @@ add_library(SKSupport STATIC
dlopen.swift)
set_target_properties(SKSupport PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
target_link_libraries(SKSupport PRIVATE
TSCUtility)
38 changes: 37 additions & 1 deletion Sources/SKSupport/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,46 @@
//
//===----------------------------------------------------------------------===//

import enum TSCUtility.Platform
import Foundation

import class TSCBasic.Process
import struct TSCBasic.AbsolutePath
import var TSCBasic.localFileSystem

private func isAndroid() -> Bool {
return (try? localFileSystem.isFile(AbsolutePath(validating: "/system/bin/toolchain"))) ?? false ||
(try? localFileSystem.isFile(AbsolutePath(validating: "/system/bin/toybox"))) ?? false
}

public enum Platform: Equatable {
case android
case darwin
case linux
case windows
}

extension Platform {
// This is not just a computed property because the ToolchainRegistryTests
// change the value.
public static var current: Platform? = {
#if os(Windows)
return .windows
#else
switch try? Process.checkNonZeroExit(args: "uname")
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased() {
case "darwin"?:
return .darwin
case "linux"?:
return isAndroid() ? .android : .linux
default:
return nil
}
#endif
}()
}

extension Platform {
/// The file extension used for a dynamic library on this platform.
public var dynamicLibraryExtension: String {
switch self {
Expand Down
18 changes: 0 additions & 18 deletions Sources/SKTestSupport/HostPlatform.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/SKTestSupport/SKTibsTestWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import SourceKitLSP
import LanguageServerProtocol
import SKCore
import SKSupport
import IndexStoreDB
import ISDBTibs
import ISDBTestSupport
Expand All @@ -21,7 +22,6 @@ import Foundation
import LSPTestSupport

import struct TSCBasic.AbsolutePath
import enum TSCUtility.Platform
import struct PackageModel.BuildFlags

public typealias URL = Foundation.URL
Expand Down Expand Up @@ -237,7 +237,7 @@ extension Location {

extension TibsToolchain {
public convenience init(_ sktc: Toolchain) {
let execExt: String = Platform.currentPlatform?.executableExtension ?? ""
let execExt: String = Platform.current?.executableExtension ?? ""

let ninja: URL?
if let ninjaPath = ProcessInfo.processInfo.environment["NINJA_BIN"] {
Expand Down
3 changes: 1 addition & 2 deletions Sources/SourceKitD/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ set_target_properties(SourceKitD PROPERTIES
target_link_libraries(SourceKitD PRIVATE
Csourcekitd
LSPLogging
SKSupport
TSCUtility)
SKSupport)
3 changes: 1 addition & 2 deletions Sources/SourceKitLSP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ target_link_libraries(SourceKitLSP PUBLIC
SwiftSyntax::SwiftParser
SwiftSyntax::SwiftDiagnostics
SwiftSyntax::SwiftSyntax
SwiftSyntax::IDEUtils
TSCUtility)
SwiftSyntax::IDEUtils)
target_link_libraries(SourceKitLSP PRIVATE
$<$<NOT:$<PLATFORM_ID:Darwin>>:FoundationXML>)

3 changes: 1 addition & 2 deletions Sources/sourcekit-lsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ target_link_libraries(sourcekit-lsp PRIVATE
ArgumentParser
LanguageServerProtocolJSONRPC
SKCore
SourceKitLSP
TSCUtility)
SourceKitLSP)
target_link_libraries(sourcekit-lsp PRIVATE
$<$<NOT:$<PLATFORM_ID:Darwin>>:FoundationXML>)

Expand Down
19 changes: 9 additions & 10 deletions Tests/SKCoreTests/ToolchainRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
//===----------------------------------------------------------------------===//

import SKCore
import SKSupport
import TSCBasic
import XCTest

import enum TSCUtility.Platform

final class ToolchainRegistryTests: XCTestCase {
func testDefaultBasic() {
let tr = ToolchainRegistry()
Expand All @@ -33,9 +32,9 @@ final class ToolchainRegistryTests: XCTestCase {
}

func testDefaultDarwin() {
let prevPlatform = Platform.currentPlatform
defer { Platform.currentPlatform = prevPlatform }
Platform.currentPlatform = .darwin
let prevPlatform = Platform.current
defer { Platform.current = prevPlatform }
Platform.current = .darwin

let tr = ToolchainRegistry()
tr.darwinToolchainOverride = nil
Expand All @@ -51,9 +50,9 @@ final class ToolchainRegistryTests: XCTestCase {
}

func testUnknownPlatform() {
let prevPlatform = Platform.currentPlatform
defer { Platform.currentPlatform = prevPlatform }
Platform.currentPlatform = nil
let prevPlatform = Platform.current
defer { Platform.current = prevPlatform }
Platform.current = nil

let fs = InMemoryFileSystem()
let binPath = AbsolutePath("/foo/bar/my_toolchain/bin")
Expand Down Expand Up @@ -548,7 +547,7 @@ private func makeToolchain(
#endif
}

let execExt = Platform.currentPlatform?.executableExtension ?? ""
let execExt = Platform.current?.executableExtension ?? ""

if clang {
makeExec(binPath.appending(component: "clang\(execExt)"))
Expand All @@ -560,7 +559,7 @@ private func makeToolchain(
makeExec(binPath.appending(component: "swiftc\(execExt)"))
}

let dylibSuffix = Platform.currentPlatform?.dynamicLibraryExtension ?? ".so"
let dylibSuffix = Platform.current?.dynamicLibraryExtension ?? ".so"

if sourcekitd {
try! fs.createDirectory(libPath.appending(component: "sourcekitd.framework"))
Expand Down
3 changes: 2 additions & 1 deletion Tests/SourceKitDTests/CrashRecoveryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import LSPTestSupport
import LSPLogging
import SourceKitLSP
import SourceKitD
import SKSupport
import SKTestSupport
import XCTest

Expand All @@ -42,7 +43,7 @@ fileprivate extension HoverResponse {

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

let ws = try! staticSourceKitTibsWorkspace(name: "sourcekitdCrashRecovery")!
Expand Down
12 changes: 5 additions & 7 deletions Tests/SourceKitDTests/SourceKitDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,23 @@
import LSPTestSupport
import SourceKitD
import SKCore
import SKSupport
import TSCBasic
import ISDBTibs
import ISDBTestSupport
import Foundation
import XCTest

import enum TSCUtility.Platform
import class TSCBasic.Process
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we need to import TSCBasic.Process where we didn’t need to import it before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because TSCUtility dragged it along for us. Something something @_implementationOnly all the things and explicit imports.


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

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

func testMultipleNotificationHandlers() {
Expand Down