Skip to content

Commit 97e099b

Browse files
authored
Basics: introduce some native path representation helpers (#6454)
We end up needing these in a few places, and while we could make it `internal` and use `@testable` imports in the tests, this ends up being needed in `Build` and `PackageLoading`. Ideally, we would give it package visibility, but that functionality would not be backwards compatible. This is a reluctant approach at solving that problem by using `public` visibility.
1 parent 8af71a9 commit 97e099b

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

Sources/Basics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_library(Basics
4242
ImportScanning.swift
4343
JSON+Extensions.swift
4444
JSONDecoder+Extensions.swift
45+
NativePathExtensions.swift
4546
Netrc.swift
4647
Observability.swift
4748
SQLite.swift
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import struct TSCBasic.AbsolutePath
15+
16+
extension AbsolutePath {
17+
public func _nativePathString(escaped: Bool) -> String {
18+
return URL(fileURLWithPath: self.pathString).withUnsafeFileSystemRepresentation {
19+
let repr = String(cString: $0!)
20+
if escaped {
21+
return repr.replacingOccurrences(of: "\\", with: "\\\\")
22+
}
23+
return repr
24+
}
25+
}
26+
}

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public final class SwiftTargetBuildDescription {
681681
content +=
682682
#"""
683683
"dependencies": "\#(
684-
self.tempsPath.appending(component: moduleName + ".d").nativePathString(escaped: true)
684+
self.tempsPath.appending(component: moduleName + ".d")._nativePathString(escaped: true)
685685
)",
686686
687687
"""#
@@ -690,15 +690,15 @@ public final class SwiftTargetBuildDescription {
690690
content +=
691691
#"""
692692
"object": "\#(
693-
self.tempsPath.appending(component: moduleName + ".o").nativePathString(escaped: true)
693+
self.tempsPath.appending(component: moduleName + ".o")._nativePathString(escaped: true)
694694
)",
695695
696696
"""#
697697

698698
}
699699
content +=
700700
#"""
701-
"swift-dependencies": "\#(masterDepsPath.nativePathString(escaped: true))"
701+
"swift-dependencies": "\#(masterDepsPath._nativePathString(escaped: true))"
702702
},
703703
704704
"""#
@@ -716,15 +716,15 @@ public final class SwiftTargetBuildDescription {
716716

717717
content +=
718718
#"""
719-
"\#(source.nativePathString(escaped: true))": {
719+
"\#(source._nativePathString(escaped: true))": {
720720
721721
"""#
722722

723723
if !self.buildParameters.useWholeModuleOptimization {
724724
let depsPath = objectDir.appending(component: sourceFileName + ".d")
725725
content +=
726726
#"""
727-
"dependencies": "\#(depsPath.nativePathString(escaped: true))",
727+
"dependencies": "\#(depsPath._nativePathString(escaped: true))",
728728
729729
"""#
730730
// FIXME: Need to record this deps file for processing it later.
@@ -735,9 +735,9 @@ public final class SwiftTargetBuildDescription {
735735

736736
content +=
737737
#"""
738-
"object": "\#(object.nativePathString(escaped: true))",
739-
"swiftmodule": "\#(partialModulePath.nativePathString(escaped: true))",
740-
"swift-dependencies": "\#(swiftDepsPath.nativePathString(escaped: true))"
738+
"object": "\#(object._nativePathString(escaped: true))",
739+
"swiftmodule": "\#(partialModulePath._nativePathString(escaped: true))",
740+
"swift-dependencies": "\#(swiftDepsPath._nativePathString(escaped: true))"
741741
}\#((idx + 1) < sources.count ? "," : "")
742742

743743
"""#

Sources/Build/BuildPlan.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ extension String {
3030
}
3131
}
3232

33-
extension AbsolutePath {
34-
internal func nativePathString(escaped: Bool) -> String {
35-
return URL(fileURLWithPath: self.pathString).withUnsafeFileSystemRepresentation {
36-
let repr = String(cString: $0!)
37-
if escaped {
38-
return repr.replacingOccurrences(of: "\\", with: "\\\\")
39-
}
40-
return repr
41-
}
42-
}
43-
}
44-
4533
extension BuildParameters {
4634
/// Returns the directory to be used for module cache.
4735
public var moduleCache: AbsolutePath {

0 commit comments

Comments
 (0)