Skip to content

Commit d8ce869

Browse files
steipeteYuxin-Qiao
andauthored
build: add musl CLI source compatibility (#1620)
Co-authored-by: Yuxin Qiao <[email protected]>
1 parent b9a3cef commit d8ce869

24 files changed

Lines changed: 177 additions & 30 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Codex agents: add a read-only `codexbar` skill for bounded, redacted provider usage JSON. Thanks @coygeek!
77

88
### Changed
9+
- Linux CLI: add musl source compatibility for static Linux SDK builds. Thanks @Yuxin-Qiao!
910
- Cost history: resize the chart details to the hovered day's model breakdown instead of reserving the tallest day. Thanks @elijahfriedman!
1011
- Antigravity: use current backend quota labels in menus and widgets while preferring a usable quota lane over an exhausted one. Thanks @Yuxin-Qiao!
1112
- Pi: cache session filename and timestamp parsers to reduce cost-history refresh overhead. Thanks @ProspectOre!

Sources/CodexBarCLI/CLIEntry.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import CodexBarCore
22
import Commander
33
#if canImport(Darwin)
44
import Darwin
5-
#else
5+
#elseif canImport(Glibc)
66
import Glibc
7+
#elseif canImport(Musl)
8+
import Musl
79
#endif
810
import Foundation
911
#if canImport(FoundationNetworking)

Sources/CodexBarCLI/CLIHelpers.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import CodexBarCore
22
import Commander
33
#if canImport(Darwin)
44
import Darwin
5-
#else
5+
#elseif canImport(Glibc)
66
import Glibc
7+
#elseif canImport(Musl)
8+
import Musl
79
#endif
810
import Foundation
911

Sources/CodexBarCLI/CLIIO.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#if canImport(Darwin)
22
import Darwin
3-
#else
3+
#elseif canImport(Glibc)
44
import Glibc
5+
#elseif canImport(Musl)
6+
import Musl
57
#endif
68
import Foundation
79

@@ -112,8 +114,10 @@ extension CodexBarCLI {
112114
static func platformExit(_ code: Int32) -> Never {
113115
#if canImport(Darwin)
114116
Darwin.exit(code)
115-
#else
117+
#elseif canImport(Glibc)
116118
Glibc.exit(code)
119+
#elseif canImport(Musl)
120+
Musl.exit(code)
117121
#endif
118122
}
119123
}

Sources/CodexBarCLI/CLILocalHTTPServer.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Foundation
22
#if canImport(Darwin)
33
import Darwin
4-
#else
4+
#elseif canImport(Glibc)
55
import Glibc
6+
#elseif canImport(Musl)
7+
import Musl
68
#endif
79

810
private let requestReadTimeoutMilliseconds: Int32 = 5000
@@ -218,8 +220,10 @@ final class CLILocalHTTPServer: @unchecked Sendable {
218220

219221
#if canImport(Darwin)
220222
let streamType = SOCK_STREAM
221-
#else
223+
#elseif canImport(Glibc)
222224
let streamType = Int32(SOCK_STREAM.rawValue)
225+
#elseif canImport(Musl)
226+
let streamType = Int32(SOCK_STREAM)
223227
#endif
224228

225229
let serverFD = socket(AF_INET, streamType, 0)
@@ -411,15 +415,19 @@ private func sendNoSignalFlags() -> Int32 {
411415
private func ignoreSIGPIPE() {
412416
#if canImport(Darwin)
413417
_ = Darwin.signal(SIGPIPE, SIG_IGN)
414-
#else
418+
#elseif canImport(Glibc)
415419
_ = Glibc.signal(SIGPIPE, SIG_IGN)
420+
#elseif canImport(Musl)
421+
_ = Musl.signal(SIGPIPE, SIG_IGN)
416422
#endif
417423
}
418424

419425
private func closeSocket(_ fd: Int32) {
420426
#if canImport(Darwin)
421427
Darwin.close(fd)
422-
#else
428+
#elseif canImport(Glibc)
423429
Glibc.close(fd)
430+
#elseif canImport(Musl)
431+
Musl.close(fd)
424432
#endif
425433
}

Sources/CodexBarCLI/CLITerminationSignalMonitor.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import Dispatch
33
import Foundation
44
#if canImport(Darwin)
55
import Darwin
6-
#else
6+
#elseif canImport(Glibc)
77
import Glibc
8+
#elseif canImport(Musl)
9+
import Musl
810
#endif
911

1012
private func handleCLITerminationSignal(_: Int32) {}
@@ -58,16 +60,20 @@ final class CLITerminationSignalMonitor: @unchecked Sendable {
5860
private static func installCaptureHandler(for signalNumber: Int32) {
5961
#if canImport(Darwin)
6062
_ = Darwin.signal(signalNumber, handleCLITerminationSignal)
61-
#else
63+
#elseif canImport(Glibc)
6264
_ = Glibc.signal(signalNumber, handleCLITerminationSignal)
65+
#elseif canImport(Musl)
66+
_ = Musl.signal(signalNumber, handleCLITerminationSignal)
6367
#endif
6468
}
6569

6670
private static func restoreDefaultHandler(for signalNumber: Int32) {
6771
#if canImport(Darwin)
6872
_ = Darwin.signal(signalNumber, SIG_DFL)
69-
#else
73+
#elseif canImport(Glibc)
7074
_ = Glibc.signal(signalNumber, SIG_DFL)
75+
#elseif canImport(Musl)
76+
_ = Musl.signal(signalNumber, SIG_DFL)
7177
#endif
7278
}
7379
}

Sources/CodexBarCore/CookieHeaderCache.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Foundation
33
import Darwin
44
#elseif canImport(Glibc)
55
import Glibc
6+
#elseif canImport(Musl)
7+
import Musl
68
#endif
79

810
public enum CookieHeaderCache {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand.
22

33
enum CodexParserHash {
4-
static let value = "3b61cd1408bffc6a"
4+
static let value = "800a06dead603ea7"
55
}

Sources/CodexBarCore/Host/PTY/TTYCommandRunner.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#if canImport(Darwin)
22
import Darwin
3-
#else
3+
#elseif canImport(Glibc)
44
import Glibc
5+
#elseif canImport(Musl)
6+
import Musl
57
#endif
68
import Foundation
79

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#if canImport(Darwin)
2+
import Darwin
3+
#elseif canImport(Glibc)
4+
import Glibc
5+
#elseif canImport(Musl)
6+
import Musl
7+
#endif
8+
import Foundation
9+
10+
enum PosixSpawnFileActionsCloseFrom {
11+
enum CloseFromError: LocalizedError {
12+
case descriptorEnumerationFailed(String)
13+
case actionFailed(Int32)
14+
15+
var errorDescription: String? {
16+
switch self {
17+
case let .descriptorEnumerationFailed(details):
18+
"Could not enumerate /proc/self/fd: \(details)"
19+
case let .actionFailed(code):
20+
"Could not configure descriptor cleanup: \(String(cString: strerror(code))) (\(code))"
21+
}
22+
}
23+
}
24+
25+
static func descriptorsToClose(
26+
startingAt minimumFileDescriptor: Int32,
27+
contentsOfDirectory: (String) throws -> [String] = FileManager.default.contentsOfDirectory(atPath:)) throws
28+
-> [Int32]
29+
{
30+
let entries: [String]
31+
do {
32+
entries = try contentsOfDirectory("/proc/self/fd")
33+
} catch {
34+
throw CloseFromError.descriptorEnumerationFailed(error.localizedDescription)
35+
}
36+
return entries.compactMap(Int32.init)
37+
.filter { $0 >= minimumFileDescriptor }
38+
.sorted()
39+
}
40+
41+
#if canImport(Glibc) || canImport(Musl)
42+
static func addCloseFrom(
43+
_ fileActions: inout posix_spawn_file_actions_t,
44+
startingAt minimumFileDescriptor: Int32) throws
45+
{
46+
#if canImport(Glibc)
47+
try self.check(posix_spawn_file_actions_addclosefrom_np(&fileActions, minimumFileDescriptor))
48+
#else
49+
for descriptor in try self.descriptorsToClose(startingAt: minimumFileDescriptor) {
50+
try self.check(posix_spawn_file_actions_addclose(&fileActions, descriptor))
51+
}
52+
#endif
53+
}
54+
55+
private static func check(_ result: Int32) throws {
56+
guard result == 0 else {
57+
throw CloseFromError.actionFailed(result)
58+
}
59+
}
60+
#endif
61+
}

0 commit comments

Comments
 (0)