Skip to content

Commit 9364d75

Browse files
committed
Basics: introduce some native path representation helpers
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 73d1ddc commit 9364d75

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 String {
17+
public func _nativePathString(escaped: Bool) -> Self {
18+
return URL(fileURLWithPath: self).withUnsafeFileSystemRepresentation {
19+
let repr = String(cString: $0!)
20+
if escaped {
21+
return repr.replacingOccurrences(of: "\\", with: "\\\\")
22+
}
23+
return repr
24+
}
25+
}
26+
}
27+
28+
extension AbsolutePath {
29+
public func _nativePathString(escaped: Bool) -> String {
30+
return self.pathString._nativePathString(escaped: escaped)
31+
}
32+
}

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 {

Tests/WorkspaceTests/ManifestSourceGenerationTests.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ import TSCBasic
1919
import Workspace
2020
import XCTest
2121

22-
extension String {
23-
fileprivate func nativePathString(escaped: Bool) -> String {
24-
#if _runtime(_ObjC)
25-
return self
26-
#else
27-
let fsr = self.fileSystemRepresentation
28-
defer { fsr.deallocate() }
29-
if escaped {
30-
return String(cString: fsr).replacingOccurrences(of: "\\", with: "\\\\")
31-
}
32-
return String(cString: fsr)
33-
#endif
34-
}
35-
}
36-
3722
class ManifestSourceGenerationTests: XCTestCase {
3823

3924
/// Private function that writes the contents of a package manifest to a temporary package directory and then loads it, then serializes the loaded manifest back out again and loads it once again, after which it compares that no information was lost. Return the source of the newly generated manifest.
@@ -282,9 +267,9 @@ class ManifestSourceGenerationTests: XCTestCase {
282267
let newContents = try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
283268

284269
// Check some things about the contents of the manifest.
285-
XCTAssertTrue(newContents.contains("url: \"\("../MyPkg10".nativePathString(escaped: true))\""), newContents)
286-
XCTAssertTrue(newContents.contains("path: \"\("../MyPkg11".nativePathString(escaped: true))\""), newContents)
287-
XCTAssertTrue(newContents.contains("path: \"\("packages/path/to/MyPkg12".nativePathString(escaped: true))"), newContents)
270+
XCTAssertTrue(newContents.contains("url: \"\("../MyPkg10"._nativePathString(escaped: true))\""), newContents)
271+
XCTAssertTrue(newContents.contains("path: \"\("../MyPkg11"._nativePathString(escaped: true))\""), newContents)
272+
XCTAssertTrue(newContents.contains("path: \"\("packages/path/to/MyPkg12"._nativePathString(escaped: true))"), newContents)
288273
}
289274

290275
func testResources() throws {

0 commit comments

Comments
 (0)