Skip to content

Commit d58d857

Browse files
committed
[POSIX] Deprecate getenv and adopt Process.env
<rdar://problem/49197626> [POSIX] Kill getenv and adopt Process.env
1 parent 9c7c8fa commit d58d857

File tree

13 files changed

+28
-37
lines changed

13 files changed

+28
-37
lines changed

Package.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,10 @@ let package = Package(
251251
// package dependency like this but there is no other good way of expressing
252252
// this right now.
253253

254-
#if os(Linux)
255-
import Glibc
256-
#else
257-
import Darwin.C
258-
#endif
254+
import class Foundation.ProcessInfo
259255

260-
if getenv("SWIFTPM_BOOTSTRAP") == nil {
261-
if getenv("SWIFTCI_USE_LOCAL_DEPS") == nil {
256+
if ProcessInfo.processInfo.environment["SWIFTPM_BOOTSTRAP"] == nil {
257+
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
262258
package.dependencies += [
263259
.package(url: "https://github.com/apple/swift-llbuild.git", .branch("master")),
264260
]
@@ -272,7 +268,7 @@ if getenv("SWIFTPM_BOOTSTRAP") == nil {
272268
package.targets.first(where: { $0.name == "SPMLLBuild" })!.dependencies += ["llbuildSwift"]
273269
}
274270

275-
if getenv("SWIFTPM_BUILD_PACKAGE_EDITOR") != nil {
271+
if ProcessInfo.processInfo.environment["SWIFTPM_BUILD_PACKAGE_EDITOR"] != nil {
276272
package.targets += [
277273
.target(name: "SPMPackageEditor", dependencies: ["Workspace", "SwiftSyntax"]),
278274
.target(name: "swiftpm-manifest-tool", dependencies: ["SPMPackageEditor"]),

Sources/Basic/Process.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import class Foundation.ProcessInfo
1212

1313
import enum POSIX.SystemError
14-
import func POSIX.getenv
1514
import SPMLibc
1615
import Dispatch
1716

@@ -239,7 +238,7 @@ public final class Process: ObjectIdentifierProtocol {
239238
}
240239
// FIXME: This can be cached.
241240
let envSearchPaths = getEnvSearchPaths(
242-
pathString: getenv("PATH"),
241+
pathString: Process.env["PATH"],
243242
currentWorkingDirectory: localFileSystem.currentWorkingDirectory
244243
)
245244
// Lookup and cache the executable path.

Sources/Basic/TemporaryFile.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import SPMLibc
12-
import func POSIX.getenv
1312
import class Foundation.FileHandle
1413
import class Foundation.FileManager
1514

@@ -56,7 +55,7 @@ public func determineTempDirectory(_ dir: AbsolutePath? = nil) throws -> Absolut
5655
/// Returns temporary directory location by searching relevant env variables.
5756
/// Evaluates once per execution.
5857
private var cachedTempDirectory: AbsolutePath = {
59-
return AbsolutePath(getenv("TMPDIR") ?? getenv("TEMP") ?? getenv("TMP") ?? "/tmp/")
58+
return AbsolutePath(Process.env["TMPDIR"] ?? Process.env["TEMP"] ?? Process.env["TMP"] ?? "/tmp/")
6059
}()
6160

6261
/// This class is basically a wrapper over posix's mkstemps() function to creates disposable files.

Sources/Basic/TerminalController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import SPMLibc
12-
import func POSIX.getenv
1312

1413
/// A class to have better control on tty output streams: standard output and standard error.
1514
/// Allows operations like cursor movement and colored text output on tty.
@@ -95,7 +94,7 @@ public final class TerminalController {
9594

9695
/// Computes the terminal type of the stream.
9796
public static func terminalType(_ stream: LocalFileOutputByteStream) -> TerminalType {
98-
if POSIX.getenv("TERM") == "dumb" {
97+
if Process.env["TERM"] == "dumb" {
9998
return .dumb
10099
}
101100
let isTTY = isatty(fileno(stream.filePointer)) != 0
@@ -108,7 +107,7 @@ public final class TerminalController {
108107
/// - Returns: Current width of terminal if it was determinable.
109108
public static func terminalWidth() -> Int? {
110109
// Try to get from environment.
111-
if let columns = POSIX.getenv("COLUMNS"), let width = Int(columns) {
110+
if let columns = Process.env["COLUMNS"], let width = Int(columns) {
112111
return width
113112
}
114113

Sources/Build/BuildPlan.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import SPMUtility
1313
import PackageModel
1414
import PackageGraph
1515
import PackageLoading
16-
import func POSIX.getenv
1716

1817
public struct BuildParameters {
1918

@@ -37,7 +36,7 @@ public struct BuildParameters {
3736
let base: AbsolutePath
3837
// FIXME: We use this hack to let swiftpm's functional test use shared
3938
// cache so it doesn't become painfully slow.
40-
if getenv("IS_SWIFTPM_TEST") != nil {
39+
if Process.env["IS_SWIFTPM_TEST"] != nil {
4140
base = BuildParameters.swiftpmTestCache
4241
} else {
4342
base = buildPath

Sources/Commands/SwiftTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,8 +825,8 @@ private func findPackageRoot() -> AbsolutePath? {
825825
/// Returns the build path from the environment, if present.
826826
private func getEnvBuildPath(workingDir: AbsolutePath) -> AbsolutePath? {
827827
// Don't rely on build path from env for SwiftPM's own tests.
828-
guard POSIX.getenv("IS_SWIFTPM_TEST") == nil else { return nil }
829-
guard let env = POSIX.getenv("SWIFTPM_BUILD_DIR") else { return nil }
828+
guard Process.env["IS_SWIFTPM_TEST"] == nil else { return nil }
829+
guard let env = Process.env["SWIFTPM_BUILD_DIR"] else { return nil }
830830
return AbsolutePath(env, relativeTo: workingDir)
831831
}
832832

Sources/POSIX/env.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import func SPMLibc.setenv
1313
import func SPMLibc.unsetenv
1414
import var SPMLibc.errno
1515

16+
@available(*, deprecated, message: "Use Foundation.ProcessInfo instead.")
1617
public func getenv(_ key: String) -> String? {
1718
let out = SPMLibc.getenv(key)
1819
return out == nil ? nil : String(validatingUTF8: out!) //FIXME locale may not be UTF8

Sources/SPMUtility/PkgConfig.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import Basic
1212
import Foundation
13-
import func POSIX.getenv
1413

1514
public enum PkgConfigError: Swift.Error, CustomStringConvertible {
1615
case couldNotFindConfigFile
@@ -173,7 +172,7 @@ public struct PkgConfig {
173172
}
174173

175174
private static var envSearchPaths: [AbsolutePath] {
176-
if let configPath = POSIX.getenv("PKG_CONFIG_PATH") {
175+
if let configPath = Process.env["PKG_CONFIG_PATH"] {
177176
return configPath.split(separator: ":").map({ AbsolutePath(String($0)) })
178177
}
179178
return []

Sources/TestSupport/misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public func loadPackageGraph(
243243
/// - throws: errors thrown in `body`, POSIX.SystemError.setenv and
244244
/// POSIX.SystemError.unsetenv
245245
public func withCustomEnv(_ env: [String: String], body: () throws -> Void) throws {
246-
let state = Array(env.keys).map({ ($0, getenv($0)) })
246+
let state = Array(env.keys).map({ ($0, Process.env[$0]) })
247247
let restore = {
248248
for (key, value) in state {
249249
if let value = value {

Sources/Workspace/Destination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct Destination {
9191
#if os(macOS)
9292
// Get the SDK.
9393
let sdkPath: AbsolutePath
94-
if let value = lookupExecutablePath(filename: getenv("SDKROOT")) {
94+
if let value = lookupExecutablePath(filename: Process.env["SDKROOT"]) {
9595
sdkPath = value
9696
} else {
9797
// No value in env, so search for it.

0 commit comments

Comments
 (0)