Skip to content

Commit 86b3678

Browse files
committed
[Build] Add linker search path to lib dir in the toolchain
<rdar://problem/48696675> Add toolchain's usr/lib directory in the linker search path
1 parent 0b2d3ea commit 86b3678

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ public final class ProductBuildDescription {
612612
/// The build parameters.
613613
let buildParameters: BuildParameters
614614

615+
/// The file system reference.
616+
let fs: FileSystem
617+
615618
/// The path to the product binary produced.
616619
public var binary: AbsolutePath {
617620
return buildParameters.buildPath.appending(outname)
@@ -670,10 +673,11 @@ public final class ProductBuildDescription {
670673
}
671674

672675
/// Create a build description for a product.
673-
init(product: ResolvedProduct, buildParameters: BuildParameters) {
676+
init(product: ResolvedProduct, buildParameters: BuildParameters, fs: FileSystem) {
674677
assert(product.type != .library(.automatic), "Automatic type libraries should not be described.")
675678
self.product = product
676679
self.buildParameters = buildParameters
680+
self.fs = fs
677681
}
678682

679683
/// Strips the arguments which should *never* be passed to Swift compiler
@@ -759,6 +763,15 @@ public final class ProductBuildDescription {
759763
// User arguments (from -Xlinker and -Xswiftc) should follow generated arguments to allow user overrides
760764
args += buildParameters.linkerFlags
761765
args += stripInvalidArguments(buildParameters.swiftCompilerFlags)
766+
767+
// Add toolchain's libdir at the very end (even after the user -Xlinker arguments).
768+
//
769+
// This will allow linking to libraries shipped in the toolchain.
770+
let toolchainLibDir = buildParameters.toolchain.toolchainLibDir
771+
if fs.isDirectory(toolchainLibDir) {
772+
args += ["-L", toolchainLibDir.pathString]
773+
}
774+
762775
return args
763776
}
764777

@@ -921,7 +934,9 @@ public class BuildPlan {
921934
// for automatic libraries because they don't produce any output.
922935
for product in graph.allProducts where product.type != .library(.automatic) {
923936
productMap[product] = ProductBuildDescription(
924-
product: product, buildParameters: buildParameters)
937+
product: product, buildParameters: buildParameters,
938+
fs: fileSystem
939+
)
925940
}
926941

927942
self.productMap = productMap

Sources/Build/Toolchain.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ extension Toolchain {
4444
public var macosSwiftStdlib: AbsolutePath {
4545
return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib/swift/macosx"))
4646
}
47+
48+
var toolchainLibDir: AbsolutePath {
49+
// FIXME: Not sure if it's better to base this off of Swift compiler or our own binary.
50+
return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib"))
51+
}
4752
}

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,7 @@ final class BuildPlanTests: XCTestCase {
13601360
func testExtraBuildFlags() throws {
13611361
let fs = InMemoryFileSystem(emptyFiles:
13621362
"/A/Sources/exe/main.swift",
1363+
"/fake/path/lib/libSomething.dylib",
13631364
"<end>"
13641365
)
13651366

@@ -1388,7 +1389,7 @@ final class BuildPlanTests: XCTestCase {
13881389
)
13891390

13901391
let exe = try result.buildProduct(for: "exe").linkArguments()
1391-
XCTAssertMatch(exe, [.anySequence, "-L", "/path/to/foo", "-L/path/to/foo", "-Xlinker", "-rpath=foo", "-Xlinker", "-rpath", "-Xlinker", "foo", .anySequence])
1392+
XCTAssertMatch(exe, [.anySequence, "-L", "/path/to/foo", "-L/path/to/foo", "-Xlinker", "-rpath=foo", "-Xlinker", "-rpath", "-Xlinker", "foo", "-L", "/fake/path/lib"])
13921393
}
13931394
}
13941395

0 commit comments

Comments
 (0)