From 7f83b4974ce59e71bb638dfb33341cb8c40ceb18 Mon Sep 17 00:00:00 2001 From: Ankit Aggarwal Date: Thu, 7 Mar 2019 19:19:52 -0800 Subject: [PATCH] [Build] Add linker search path to lib dir in the toolchain Add toolchain's usr/lib directory in the linker search path --- Sources/Build/BuildPlan.swift | 19 +++++++++++++++++-- Sources/Build/Toolchain.swift | 5 +++++ Tests/BuildTests/BuildPlanTests.swift | 3 ++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Sources/Build/BuildPlan.swift b/Sources/Build/BuildPlan.swift index e0ef02ab36d..5e0b96f8242 100644 --- a/Sources/Build/BuildPlan.swift +++ b/Sources/Build/BuildPlan.swift @@ -612,6 +612,9 @@ public final class ProductBuildDescription { /// The build parameters. let buildParameters: BuildParameters + /// The file system reference. + let fs: FileSystem + /// The path to the product binary produced. public var binary: AbsolutePath { return buildParameters.buildPath.appending(outname) @@ -670,10 +673,11 @@ public final class ProductBuildDescription { } /// Create a build description for a product. - init(product: ResolvedProduct, buildParameters: BuildParameters) { + init(product: ResolvedProduct, buildParameters: BuildParameters, fs: FileSystem) { assert(product.type != .library(.automatic), "Automatic type libraries should not be described.") self.product = product self.buildParameters = buildParameters + self.fs = fs } /// Strips the arguments which should *never* be passed to Swift compiler @@ -759,6 +763,15 @@ public final class ProductBuildDescription { // User arguments (from -Xlinker and -Xswiftc) should follow generated arguments to allow user overrides args += buildParameters.linkerFlags args += stripInvalidArguments(buildParameters.swiftCompilerFlags) + + // Add toolchain's libdir at the very end (even after the user -Xlinker arguments). + // + // This will allow linking to libraries shipped in the toolchain. + let toolchainLibDir = buildParameters.toolchain.toolchainLibDir + if fs.isDirectory(toolchainLibDir) { + args += ["-L", toolchainLibDir.pathString] + } + return args } @@ -921,7 +934,9 @@ public class BuildPlan { // for automatic libraries because they don't produce any output. for product in graph.allProducts where product.type != .library(.automatic) { productMap[product] = ProductBuildDescription( - product: product, buildParameters: buildParameters) + product: product, buildParameters: buildParameters, + fs: fileSystem + ) } self.productMap = productMap diff --git a/Sources/Build/Toolchain.swift b/Sources/Build/Toolchain.swift index 690281cefc7..4e1888685d3 100644 --- a/Sources/Build/Toolchain.swift +++ b/Sources/Build/Toolchain.swift @@ -44,4 +44,9 @@ extension Toolchain { public var macosSwiftStdlib: AbsolutePath { return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib/swift/macosx")) } + + var toolchainLibDir: AbsolutePath { + // FIXME: Not sure if it's better to base this off of Swift compiler or our own binary. + return resolveSymlinks(swiftCompiler).appending(RelativePath("../../lib")) + } } diff --git a/Tests/BuildTests/BuildPlanTests.swift b/Tests/BuildTests/BuildPlanTests.swift index e4a74714b4c..f141f899ae2 100644 --- a/Tests/BuildTests/BuildPlanTests.swift +++ b/Tests/BuildTests/BuildPlanTests.swift @@ -1360,6 +1360,7 @@ final class BuildPlanTests: XCTestCase { func testExtraBuildFlags() throws { let fs = InMemoryFileSystem(emptyFiles: "/A/Sources/exe/main.swift", + "/fake/path/lib/libSomething.dylib", "" ) @@ -1388,7 +1389,7 @@ final class BuildPlanTests: XCTestCase { ) let exe = try result.buildProduct(for: "exe").linkArguments() - XCTAssertMatch(exe, [.anySequence, "-L", "/path/to/foo", "-L/path/to/foo", "-Xlinker", "-rpath=foo", "-Xlinker", "-rpath", "-Xlinker", "foo", .anySequence]) + XCTAssertMatch(exe, [.anySequence, "-L", "/path/to/foo", "-L/path/to/foo", "-Xlinker", "-rpath=foo", "-Xlinker", "-rpath", "-Xlinker", "foo", "-L", "/fake/path/lib"]) } }