-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathCommandLineAdditions.swift
More file actions
84 lines (79 loc) · 2.81 KB
/
Copy pathCommandLineAdditions.swift
File metadata and controls
84 lines (79 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
private import _TestingInternals
#if hasFeature(Embedded)
/// A minimal interface-compatible implementation of the `CommandLine` type from
/// the Swift standard library.
///
/// This type is declared for Embedded Swift targets to simplify calling code.
enum CommandLine {
/// An array that provides access to this program's command line arguments.
///
/// In Embedded Swift, this array contains one string standing in for the name
/// of the current program (as required by the C language standard).
static var arguments: [String] {
["swift-test"]
}
}
#endif
extension CommandLine {
#if !hasFeature(Embedded) && !os(WASI) && !SWT_TARGET_OS_APPLE
#if os(Windows)
private typealias FPEncoding = UTF16
#else
private typealias FPEncoding = UTF8
#endif
private static var executablePathCString: ContiguousArray<FPEncoding.CodeUnit>? {
@_silgen_name("_swift_stdlib_executablePathCString") get
}
#endif
/// The path to the current process' executable.
static var executablePath: String {
get throws {
#if hasFeature(Embedded) || os(WASI)
// Embedded Swift and WASI do not currently support getting the executable
// path via the standard library.
throw SystemError(description: "The current executable path is not available on this platform.")
#elseif SWT_TARGET_OS_APPLE
var result: String?
#if DEBUG
var bufferCount = UInt32(1) // force looping
#else
var bufferCount = UInt32(PATH_MAX)
#endif
while result == nil {
withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(bufferCount)) { buffer in
// _NSGetExecutablePath returns 0 on success and -1 if bufferCount is
// too small. If that occurs, we'll return nil here and loop with the
// new value of bufferCount.
if 0 == _NSGetExecutablePath(buffer.baseAddress, &bufferCount) {
result = String(cString: buffer.baseAddress!)
}
}
}
return result!
#else
guard let executablePathCString else {
#if os(Windows)
throw Win32Error(rawValue: GetLastError())
#else
throw CError(rawValue: swt_errno())
#endif
}
return try executablePathCString.withUnsafeBufferPointer { executablePathCString in
guard let result = String.decodeCString(executablePathCString.baseAddress!, as: FPEncoding.self)?.result else {
throw SystemError(description: "Could not decode the current executable's path as \(FPEncoding.self).")
}
return result
}
#endif
}
}
}