mirror of
https://github.com/apple/swift.git
synced 2026-11-04 00:12:11 +01:00
This PR implements the C++ back end of `CommandLine.executablePath` as approved in [SE-0513](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0513-commandline-executablepath.md). The front end that exposes a `FilePath`-based API is blocked on the existence of `FilePath` in the standard library, but some toolchain components like Swift Testing need to back-deploy and can't link to the System package directly, so they need to directly use the C++ implementation. Darwin doesn't need this implementation as it has `_NSGetExecutablePath()` that will be used by the back-deployable Darwin implementation instead of this helper function.
49 lines
1.4 KiB
Swift
49 lines
1.4 KiB
Swift
// RUN: %empty-directory(%t)
|
|
// RUN: %target-build-swift %s -o %t/this-is-my-executable-path.out
|
|
// RUN: %target-codesign %t/this-is-my-executable-path.out
|
|
// RUN: %target-run %t/this-is-my-executable-path.out
|
|
// REQUIRES: executable_test
|
|
// UNSUPPORTED: OS=macosx, OS=ios, OS=tvos, OS=watchos, OS=xros
|
|
// UNSUPPORTED: OS=wasip1, OS=emscripten
|
|
|
|
import StdlibUnittest
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
#elseif os(Windows)
|
|
import WinSDK
|
|
#endif
|
|
|
|
var CommandLineExecutablePath = TestSuite("CommandLineExecutablePath")
|
|
|
|
extension CommandLine {
|
|
#if os(Windows)
|
|
internal static var executablePathCString: ContiguousArray<CWideChar> {
|
|
@_silgen_name("_swift_stdlib_executablePathCString") get
|
|
}
|
|
#else
|
|
internal static var executablePathCString: ContiguousArray<CChar> {
|
|
@_silgen_name("_swift_stdlib_executablePathCString") get
|
|
}
|
|
#endif
|
|
}
|
|
|
|
CommandLineExecutablePath.test("executablePathCapturedAndCorrect") {
|
|
#if os(Windows)
|
|
CommandLine.executablePathCString.withUnsafeBufferPointer { path in
|
|
#"\this-is-my-executable-path.out"#.withCString(encodedAs: UTF16.self) { subpath in
|
|
expectNotEqual(nil, wcsstr(path.baseAddress!, subpath))
|
|
}
|
|
}
|
|
#else
|
|
CommandLine.executablePathCString.withUnsafeBufferPointer { path in
|
|
expectNotEqual(nil, strstr(path.baseAddress!, "/this-is-my-executable-path.out"))
|
|
}
|
|
#endif
|
|
}
|
|
|
|
runAllTests()
|
|
|