From 5e9ad18ac20b121f598b63a43891929cca487490 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Sat, 1 Feb 2025 06:46:05 -0800 Subject: [PATCH] Remove the dependency on libreadline It was only active on macOS and Linux but not other platforms due to existing dependency complexities, and just isn't that useful overall. This will help unblock swiftlang/swift-package-manager#8271 without having to add new dependencies to the Swift Docker images. --- Package.swift | 13 ----- Packages/.gitignore | 4 -- Packages/Sources/readline/module.modulemap | 5 -- Packages/Sources/readline/sdk_readline.h | 14 ----- Sources/SWBCSupport/SWBCSupport.h | 9 --- Sources/SWBUtil/GNUReadLine.swift | 66 ---------------------- Sources/SwiftBuild/SWBTerminal.swift | 20 ++----- 7 files changed, 4 insertions(+), 127 deletions(-) delete mode 100644 Packages/.gitignore delete mode 100644 Packages/Sources/readline/module.modulemap delete mode 100644 Packages/Sources/readline/sdk_readline.h delete mode 100644 Sources/SWBUtil/GNUReadLine.swift diff --git a/Package.swift b/Package.swift index 175cccf7..c54d2729 100644 --- a/Package.swift +++ b/Package.swift @@ -22,7 +22,6 @@ let appleOS = false let useLocalDependencies = Context.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil let useLLBuildFramework = Context.environment["SWIFTBUILD_LLBUILD_FWK"] != nil -let readlinePackage = !appleOS ? "readline" : nil let swiftSettings: [SwiftSetting] = [ // Upcoming Swift 6.0 features @@ -162,7 +161,6 @@ let package = Package( dependencies: [ "SWBCSupport", "SWBLibc", - .target(name: "readline", condition: .when(platforms: [.linux])), .product(name: "ArgumentParser", package: "swift-argument-parser"), .product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: [.linux, .android])), .product(name: "SystemPackage", package: "swift-system", condition: .when(platforms: [.linux, .android, .windows])), @@ -343,17 +341,6 @@ let package = Package( dependencies: ["SwiftBuild", "SWBTestSupport", "SwiftBuildTestSupport"], swiftSettings: swiftSettings), - // System libraries - .systemLibrary( - name: "readline", - path: "Packages/Sources/readline", - pkgConfig: readlinePackage, - providers: [ - .apt(["libreadline-dev"]), - .yum(["readline-devel"]), - ] - ), - // Commands .plugin( name: "launch-xcode", diff --git a/Packages/.gitignore b/Packages/.gitignore deleted file mode 100644 index e62ca52c..00000000 --- a/Packages/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/.build -/Packages -/*.xcodeproj -Package.resolved diff --git a/Packages/Sources/readline/module.modulemap b/Packages/Sources/readline/module.modulemap deleted file mode 100644 index 48d58dc9..00000000 --- a/Packages/Sources/readline/module.modulemap +++ /dev/null @@ -1,5 +0,0 @@ -module readline [system] { - header "sdk_readline.h" - link "readline" - export * -} diff --git a/Packages/Sources/readline/sdk_readline.h b/Packages/Sources/readline/sdk_readline.h deleted file mode 100644 index f4e77ed0..00000000 --- a/Packages/Sources/readline/sdk_readline.h +++ /dev/null @@ -1,14 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift open source project -// -// Copyright (c) 2025 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 the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#include -#include diff --git a/Sources/SWBCSupport/SWBCSupport.h b/Sources/SWBCSupport/SWBCSupport.h index a67b8257..c020472c 100644 --- a/Sources/SWBCSupport/SWBCSupport.h +++ b/Sources/SWBCSupport/SWBCSupport.h @@ -19,15 +19,6 @@ #include #endif -#ifndef __APPLE__ -// Re-exported from readline -char *readline(const char *); -int add_history(const char *); -int read_history(const char *); -int write_history(const char *); -int history_truncate_file(const char *, int); -#endif - #include "CLibclang.h" #include "CLibRemarksHelper.h" #include "PluginAPI.h" diff --git a/Sources/SWBUtil/GNUReadLine.swift b/Sources/SWBUtil/GNUReadLine.swift deleted file mode 100644 index 91916202..00000000 --- a/Sources/SWBUtil/GNUReadLine.swift +++ /dev/null @@ -1,66 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift open source project -// -// Copyright (c) 2025 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 the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -#if os(macOS) -private import EditLine -private import EditLine.readline -#elseif !canImport(Darwin) && !os(Windows) && !os(Android) -private import readline -#endif - -import SWBCSupport - -public func swb_readline(_ prompt: String?) -> String? { - #if os(macOS) || (!canImport(Darwin) && !os(Windows) && !os(Android)) - return prompt?.utf8CString.withUnsafeBufferPointer { bp -> String? in - if let ptr = readline(bp.baseAddress) { - defer { ptr.deallocate() } - return String(cString: ptr) - } - return nil - } - #else - return nil - #endif -} - -@discardableResult public func swb_add_history(_ string: String?) -> Int { - #if os(macOS) || (!canImport(Darwin) && !os(Windows) && !os(Android)) - return string?.utf8CString.withUnsafeBufferPointer { Int(add_history($0.baseAddress)) } ?? Int(add_history(nil)) - #else - return 0 - #endif -} - -@discardableResult public func swb_read_history(_ filename: String?) -> Int { - #if os(macOS) || (!canImport(Darwin) && !os(Windows) && !os(Android)) - return filename?.utf8CString.withUnsafeBufferPointer { Int(read_history($0.baseAddress)) } ?? Int(read_history(nil)) - #else - return 0 - #endif -} - -@discardableResult public func swb_write_history(_ filename: String?) -> Int { - #if os(macOS) || (!canImport(Darwin) && !os(Windows) && !os(Android)) - return filename?.utf8CString.withUnsafeBufferPointer { Int(write_history($0.baseAddress)) } ?? Int(write_history(nil)) - #else - return 0 - #endif -} - -@discardableResult public func swb_history_truncate_file(_ filename: String?, _ nlines: Int) -> Int { - #if os(macOS) || (!canImport(Darwin) && !os(Windows) && !os(Android)) - return filename?.utf8CString.withUnsafeBufferPointer { Int(history_truncate_file($0.baseAddress, Int32(nlines))) } ?? Int(history_truncate_file(nil, Int32(nlines))) - #else - return 0 - #endif -} diff --git a/Sources/SwiftBuild/SWBTerminal.swift b/Sources/SwiftBuild/SWBTerminal.swift index 57e9c1b8..101933f8 100644 --- a/Sources/SwiftBuild/SWBTerminal.swift +++ b/Sources/SwiftBuild/SWBTerminal.swift @@ -15,9 +15,6 @@ import SWBUtil import Foundation -let kHistoryFilePath = "~/.swbuild_history" -let kMaxHistoryLines = 100 - func swbuild_handle_command_result(result: SWBServiceConsoleResult) { print(result.output, terminator: "") } @@ -25,17 +22,12 @@ func swbuild_handle_command_result(result: SWBServiceConsoleResult) { /// Process a command line. /// /// - returns: True if the console should continue handling commands, otherwise the console should quit. -func swbuild_process_command(console: SWBBuildServiceConsole, command: String, historyPath: String) async -> (shouldContinue: Bool, success: Bool) { +func swbuild_process_command(console: SWBBuildServiceConsole, command: String) async -> (shouldContinue: Bool, success: Bool) { // Ignore empty commands. if command.isEmpty { return (true, true) } - // Add the line to the history. - swb_add_history(command) - swb_write_history(historyPath) - swb_history_truncate_file(historyPath, kMaxHistoryLines) - // Process the line. let (result, success) = await console.sendCommandString(command) swbuild_handle_command_result(result: result) @@ -71,24 +63,20 @@ func swbuild_repl() async throws -> Bool { // Save the terminal attributes (and restore them and exit). return try await withTerminalAttributes { terminalAttributes in return try await withServiceConsole { console in - let historyPath = (kHistoryFilePath as NSString).expandingTildeInPath - - // Read in the command history. - swb_read_history(historyPath) - // Disable echo, after all readline initialization is done. terminalAttributes.disableEcho() var ok = true var shouldContinue = true while shouldContinue { - guard let line = swb_readline("swbuild> ") else { + print("swbuild> ", terminator: "") + guard let line = readLine() else { // If we received the EOF then exit. print() break } - (shouldContinue, ok) = await swbuild_process_command(console: console, command: line, historyPath: historyPath) + (shouldContinue, ok) = await swbuild_process_command(console: console, command: line) } return ok