Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generateResourceAccessor WASI regression #3819

Merged
merged 3 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,21 @@ public final class SwiftTargetBuildDescription {
// Do nothing if we're not generating a bundle.
guard let bundlePath = self.bundlePath else { return }

let mainPathSubstitution: String
if buildParameters.triple.isWASI() {
let mainPath = AbsolutePath(Bundle.main.bundlePath).appending(component: bundlePath.basename).pathString
mainPathSubstitution = #""\#(mainPath.asSwiftStringLiteralConstant)""#
} else {
mainPathSubstitution = #"Bundle.main.bundleURL.appendingPathComponent("\#(bundlePath.basename.asSwiftStringLiteralConstant)").path"#
}

let stream = BufferedOutputByteStream()
stream <<< """
import class Foundation.Bundle

extension Foundation.Bundle {
static var module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("\(bundlePath.basename.asSwiftStringLiteralConstant)").path
let mainPath = \(mainPathSubstitution)
let buildPath = "\(bundlePath.pathString.asSwiftStringLiteralConstant)"

let preferredBundle = Bundle(path: mainPath)
Expand Down
64 changes: 64 additions & 0 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,70 @@ final class BuildPlanTests: XCTestCase {
])
}

func testSwiftWASIBundleAccessor() throws {
// This has a Swift and ObjC target in the same package.
let fs = InMemoryFileSystem(emptyFiles:
"/PkgA/Sources/Foo/Foo.swift",
"/PkgA/Sources/Foo/foo.txt",
"/PkgA/Sources/Foo/bar.txt",
"/PkgA/Sources/Bar/Bar.swift"
)

let observability = ObservabilitySystem.makeForTesting()

let graph = try loadPackageGraph(
fs: fs,
manifests: [
Manifest.createRootManifest(
name: "PkgA",
path: .init("/PkgA"),
toolsVersion: .v5_2,
targets: [
TargetDescription(
name: "Foo",
resources: [
.init(rule: .copy, path: "foo.txt"),
.init(rule: .process, path: "bar.txt"),
]
),
TargetDescription(
name: "Bar"
),
]
)
],
observabilityScope: observability.topScope
)

XCTAssertNoDiagnostics(observability.diagnostics)

let plan = try BuildPlan(
buildParameters: mockBuildParameters(destinationTriple: .wasi),
graph: graph,
fileSystem: fs,
observabilityScope: observability.topScope
)
let result = BuildPlanResult(plan: plan)

let fooTarget = try result.target(for: "Foo").swiftTarget()
XCTAssertEqual(fooTarget.objects.map{ $0.pathString }, [
"/path/to/build/debug/Foo.build/Foo.swift.o",
"/path/to/build/debug/Foo.build/resource_bundle_accessor.swift.o"
])

let resourceAccessor = fooTarget.sources.first{ $0.basename == "resource_bundle_accessor.swift" }!
let contents = try fs.readFileContents(resourceAccessor).cString
XCTAssertMatch(contents, .contains("extension Foundation.Bundle"))
// Assert that `Bundle.main` is executed in the compiled binary (and not during compilation)
// See https://bugs.swift.org/browse/SR-14555 and https://github.com/apple/swift-package-manager/pull/2972/files#r623861646
XCTAssertMatch(contents, .contains("let mainPath = \""))

let barTarget = try result.target(for: "Bar").swiftTarget()
XCTAssertEqual(barTarget.objects.map{ $0.pathString }, [
"/path/to/build/debug/Bar.build/Bar.swift.o",
])
}

func testShouldLinkStaticSwiftStdlib() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Pkg/Sources/exe/main.swift",
Expand Down