Skip to content

Commit 1561e81

Browse files
committed
BuildDescription: propagate swiftResourcesPath
BuildDescription: propagate resources to clang too Doc comments feedback
1 parent 0e50b63 commit 1561e81

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
187187
derivedProductType = self.product.type
188188
}
189189

190+
var hasStaticStdlib = false
190191
switch derivedProductType {
191192
case .macro:
192193
throw InternalError("macro not supported") // should never be reached
@@ -218,6 +219,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
218219
self.observabilityScope.emit(.swiftBackDeployError)
219220
} else if self.buildParameters.targetTriple.isSupportingStaticStdlib {
220221
args += ["-static-stdlib"]
222+
hasStaticStdlib = true
221223
}
222224
}
223225
args += ["-emit-executable"]
@@ -243,6 +245,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
243245
throw InternalError("unexpectedly asked to generate linker arguments for a plugin product")
244246
}
245247

248+
if let resourcesPath = self.buildParameters.toolchain.swiftResourcesPath(isStatic: hasStaticStdlib) {
249+
args += ["-resource-dir", resourcesPath.pathString]
250+
}
251+
252+
// clang resources are always in lib/swift/
253+
if let dynamicResourcesPath = self.buildParameters.toolchain.swiftResourcesPath {
254+
let clangResourcesPath = dynamicResourcesPath.appending("clang")
255+
args += ["-Xclang-linker", "-resource-dir", "-Xclang-linker", clangResourcesPath.pathString]
256+
}
257+
246258
// Set rpath such that dynamic libraries are looked up
247259
// adjacent to the product.
248260
if self.buildParameters.targetTriple.isLinux() {

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,12 +931,18 @@ public final class SwiftTargetBuildDescription {
931931
}
932932

933933
private var stdlibArguments: [String] {
934-
if self.buildParameters.shouldLinkStaticSwiftStdlib,
935-
self.buildParameters.targetTriple.isSupportingStaticStdlib
936-
{
937-
return ["-static-stdlib"]
938-
} else {
939-
return []
934+
var arguments: [String] = []
935+
936+
let isStatic = self.buildParameters.shouldLinkStaticSwiftStdlib
937+
&& self.buildParameters.targetTriple.isSupportingStaticStdlib
938+
if isStatic {
939+
arguments += ["-static-stdlib"]
940940
}
941+
942+
if let resourcesPath = self.buildParameters.toolchain.swiftResourcesPath(isStatic: isStatic) {
943+
arguments += ["-resource-dir", resourcesPath.pathString]
944+
}
945+
946+
return arguments
941947
}
942948
}

Sources/PackageModel/Toolchain.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public protocol Toolchain {
1919
/// Path of the `swiftc` compiler.
2020
var swiftCompilerPath: AbsolutePath { get }
2121

22+
/// Path to `lib/swift`
23+
var swiftResourcesPath: AbsolutePath? { get }
24+
25+
/// Path to `lib/swift_static`
26+
var swiftStaticResourcesPath: AbsolutePath? { get }
27+
2228
/// Whether the used compiler is from a open source development toolchain.
2329
var isSwiftDevelopmentToolchain: Bool { get }
2430

@@ -81,7 +87,15 @@ extension Toolchain {
8187
return try AbsolutePath(validating: "../../lib", relativeTo: resolveSymlinks(swiftCompilerPath))
8288
}
8389
}
84-
90+
91+
/// Returns the appropriate Swift resources directory path.
92+
///
93+
/// - Parameter static: Controls whether to use the static or dynamic
94+
/// resources directory.
95+
public func swiftResourcesPath(isStatic: Bool) -> AbsolutePath? {
96+
isStatic ? swiftStaticResourcesPath : swiftResourcesPath
97+
}
98+
8599
public var extraCCFlags: [String] {
86100
extraFlags.cCompilerFlags
87101
}

Sources/PackageModel/UserToolchain.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public final class UserToolchain: Toolchain {
4040
/// An array of paths to search for libraries at link time.
4141
public let librarySearchPaths: [AbsolutePath]
4242

43+
/// Path containing Swift resources for dynamic linking.
44+
public var swiftResourcesPath: AbsolutePath? {
45+
destination.pathsConfiguration.swiftResourcesPath
46+
}
47+
48+
/// Path containing Swift resources for static linking.
49+
public var swiftStaticResourcesPath: AbsolutePath? {
50+
destination.pathsConfiguration.swiftStaticResourcesPath
51+
}
52+
4353
/// Additional flags to be passed to the build tools.
4454
public var extraFlags: BuildFlags
4555

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,6 +3523,9 @@ final class BuildPlanTests: XCTestCase {
35233523
Manifest.createRootManifest(
35243524
displayName: "Pkg",
35253525
path: "/Pkg",
3526+
products: [
3527+
ProductDescription(name: "exe", type: .executable, targets: ["exe"]),
3528+
],
35263529
targets: [
35273530
TargetDescription(name: "exe", dependencies: ["lib"]),
35283531
TargetDescription(name: "lib", dependencies: []),
@@ -3540,7 +3543,11 @@ final class BuildPlanTests: XCTestCase {
35403543
],
35413544
rootPaths: try UserToolchain.default.swiftSDK.toolset.rootPaths
35423545
),
3543-
pathsConfiguration: .init(sdkRootPath: "/fake/sdk")
3546+
pathsConfiguration: .init(
3547+
sdkRootPath: "/fake/sdk",
3548+
swiftResourcesPath: "/fake/lib/swift",
3549+
swiftStaticResourcesPath: "/fake/lib/swift_static"
3550+
)
35443551
)
35453552
let mockToolchain = try UserToolchain(swiftSDK: userSwiftSDK)
35463553
let extraBuildParameters = mockBuildParameters(toolchain: mockToolchain,
@@ -3567,7 +3574,30 @@ final class BuildPlanTests: XCTestCase {
35673574
XCTAssertMatch(try lib.basicArguments(isCXX: false), args)
35683575

35693576
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
3570-
XCTAssertMatch(exe, ["-module-cache-path", "\(buildPath.appending(components: "ModuleCache"))", .anySequence, "-swift-flag-from-json", "-g", "-swift-command-line-flag", .anySequence, "-Xcc", "-clang-flag-from-json", "-Xcc", "-g", "-Xcc", "-clang-command-line-flag"])
3577+
XCTAssertMatch(exe, ["-module-cache-path", "\(buildPath.appending(components: "ModuleCache"))", "-resource-dir", "/fake/lib/swift", .anySequence, "-swift-flag-from-json", "-g", "-swift-command-line-flag", .anySequence, "-Xcc", "-clang-flag-from-json", "-Xcc", "-g", "-Xcc", "-clang-command-line-flag"])
3578+
3579+
let exeProduct = try result.buildProduct(for: "exe").linkArguments()
3580+
XCTAssertMatch(exeProduct, [.anySequence, "-resource-dir", "/fake/lib/swift", "-Xclang-linker", "-resource-dir", "-Xclang-linker", "/fake/lib/swift/clang", .anySequence])
3581+
3582+
let staticBuildParameters = {
3583+
var copy = extraBuildParameters
3584+
copy.shouldLinkStaticSwiftStdlib = true
3585+
// pick a triple with support for static linking
3586+
copy.triple = .x86_64Linux
3587+
return copy
3588+
}()
3589+
let staticResult = try BuildPlanResult(plan: BuildPlan(
3590+
buildParameters: staticBuildParameters,
3591+
graph: graph,
3592+
fileSystem: fs,
3593+
observabilityScope: observability.topScope
3594+
))
3595+
3596+
let staticExe = try staticResult.target(for: "exe").swiftTarget().compileArguments()
3597+
XCTAssertMatch(staticExe, [.anySequence, "-resource-dir", "/fake/lib/swift_static", .anySequence])
3598+
3599+
let staticExeProduct = try staticResult.buildProduct(for: "exe").linkArguments()
3600+
XCTAssertMatch(staticExeProduct, [.anySequence, "-resource-dir", "/fake/lib/swift_static", "-Xclang-linker", "-resource-dir", "-Xclang-linker", "/fake/lib/swift/clang", .anySequence])
35713601
}
35723602

35733603
func testUserToolchainWithToolsetCompileFlags() throws {

Tests/BuildTests/MockBuildTestHelper.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct MockToolchain: PackageModel.Toolchain {
3030
let swiftCompilerPath = AbsolutePath("/fake/path/to/swiftc")
3131
let includeSearchPaths = [AbsolutePath]()
3232
let librarySearchPaths = [AbsolutePath]()
33+
let swiftResourcesPath: AbsolutePath? = nil
34+
let swiftStaticResourcesPath: AbsolutePath? = nil
3335
let isSwiftDevelopmentToolchain = false
3436
let swiftPluginServerPath: AbsolutePath? = nil
3537
let extraFlags = PackageModel.BuildFlags()

0 commit comments

Comments
 (0)