Skip to content

[Build] Add linker search path to lib dir in the toolchain #2035

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

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Sources/Build/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}
3 changes: 2 additions & 1 deletion Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"<end>"
)

Expand Down Expand Up @@ -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"])
}
}

Expand Down