Skip to content

[5.8] PackageModel: partially address #5719 #6117

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
Feb 4, 2023
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
28 changes: 20 additions & 8 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ public final class UserToolchain: Toolchain {

// MARK: - public API

public static func determineLibrarian(triple: Triple, binDir: AbsolutePath,
useXcrun: Bool,
environment: EnvironmentVariables,
searchPaths: [AbsolutePath]) throws
-> AbsolutePath {
public static func determineLibrarian(
triple: Triple, binDir: AbsolutePath,
useXcrun: Bool,
environment: EnvironmentVariables,
searchPaths: [AbsolutePath],
extraSwiftFlags: [String]
) throws
-> AbsolutePath
{
let variable: String = triple.isDarwin() ? "LIBTOOL" : "AR"
let tool: String = {
if triple.isDarwin() { return "libtool" }
Expand All @@ -128,7 +132,12 @@ public final class UserToolchain: Toolchain {
environment: environment) {
return librarian.basename
}
// TODO(5719) use `lld-link` if the build requests lld.
// TODO(5719) handle `-Xmanifest` vs `-Xswiftc`
// `-use-ld=` is always joined in Swift.
if let ld = extraSwiftFlags.first(where: { $0.starts(with: "-use-ld=") }) {
let linker = String(ld.split(separator: "=").last!)
return linker == "lld" ? "lld-link" : linker
}
return "link"
}
// TODO(compnerd) consider defaulting to `llvm-ar` universally with
Expand Down Expand Up @@ -375,8 +384,6 @@ public final class UserToolchain: Toolchain {
// Use the triple from destination or compute the host triple using swiftc.
var triple = destination.targetTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)

self.librarianPath = try UserToolchain.determineLibrarian(triple: triple, binDir: binDir, useXcrun: useXcrun, environment: environment, searchPaths: envSearchPaths)

// Change the triple to the specified arch if there's exactly one of them.
// The Triple property is only looked at by the native build system currently.
if archs.count == 1 {
Expand All @@ -389,6 +396,11 @@ public final class UserToolchain: Toolchain {

self.extraFlags.swiftCompilerFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)

self.librarianPath = try UserToolchain.determineLibrarian(
triple: triple, binDir: binDir, useXcrun: useXcrun, environment: environment,
searchPaths: envSearchPaths, extraSwiftFlags: self.extraFlags.swiftCompilerFlags
)

if let sdkDir = destination.sdkRootDir {
self.extraFlags.cCompilerFlags = [
triple.isDarwin() ? "-isysroot" : "--sysroot", sdkDir.pathString
Expand Down
78 changes: 73 additions & 5 deletions Tests/PackageModelTests/PackageModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PackageModelTests: XCTestCase {
checkCodable(.executable)
checkCodable(.test)
}

func testProductFilterCodable() throws {
// Test ProductFilter.everything
try {
Expand All @@ -66,9 +66,77 @@ class PackageModelTests: XCTestCase {
toolchainBinDir: toolchainPath.appending(components: "usr", "bin")
)

XCTAssertEqual(try UserToolchain.deriveSwiftCFlags(triple: triple, destination: destination, environment: .process()), [
// Needed when cross‐compiling for Android. 2020‐03‐01
"-sdk", sdkDir.pathString,
])
XCTAssertEqual(
try UserToolchain.deriveSwiftCFlags(triple: triple, destination: destination, environment: .process()),
[
// Needed when cross‐compiling for Android. 2020‐03‐01
"-sdk", sdkDir.pathString,
]
)
}

func testWindowsLibrarianSelection() throws {
// tiny PE binary from: https://archive.is/w01DO
let contents: [UInt8] = [
0x4D, 0x5A, 0x00, 0x00, 0x50, 0x45, 0x00, 0x00, 0x4C, 0x01, 0x01, 0x00,
0x6A, 0x2A, 0x58, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x03, 0x01, 0x0B, 0x01, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x68, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02,
]

#if os(Windows)
let suffix = ".exe"
#else
let suffix = ""
#endif

let triple = try Triple("x86_64-unknown-windows-msvc")
let fs = TSCBasic.localFileSystem

try withTemporaryFile { [contents] _ in
try withTemporaryDirectory(removeTreeOnDeinit: true) { [contents] tmp in
let bin = tmp.appending(component: "bin")
try fs.createDirectory(bin)

let lld = bin.appending(component: "lld-link\(suffix)")
try fs.writeFileContents(lld, bytes: ByteString(contents))

let not = bin.appending(component: "not-link\(suffix)")
try fs.writeFileContents(not, bytes: ByteString(contents))

#if !os(Windows)
try fs.chmod(.executable, path: lld, options: [])
try fs.chmod(.executable, path: not, options: [])
#endif

try XCTAssertEqual(
UserToolchain.determineLibrarian(
triple: triple, binDir: bin, useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: ["-Xswiftc", "-use-ld=lld"]
),
lld
)

try XCTAssertEqual(
UserToolchain.determineLibrarian(
triple: triple, binDir: bin, useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: ["-Xswiftc", "-use-ld=not-link\(suffix)"]
),
not
)

try XCTAssertThrowsError(
UserToolchain.determineLibrarian(
triple: triple, binDir: bin, useXcrun: false, environment: [:], searchPaths: [],
extraSwiftFlags: []
)
)
}
}
}
}