Skip to content

Commit

Permalink
Build lld when no prebuilt binaries are available (#19)
Browse files Browse the repository at this point in the history
Resolves #9.
Resolves rdar://115890915.
  • Loading branch information
MaxDesiatov authored Sep 28, 2023
1 parent 0ac6671 commit 321e22a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import struct Foundation.URL
import struct SystemPackage.FilePath

/// Information about the OS for which the artifact is built, if it's downloaded as prebuilt.
private enum ArtifactOS: Hashable {
enum ArtifactOS: Hashable {
init(_ tripleOS: Triple.OS, _ versions: VersionsConfiguration) {
switch tripleOS {
case .linux:
Expand All @@ -24,7 +24,7 @@ private enum ArtifactOS: Hashable {
case .wasi:
self = .wasi
case .win32:
self = .windows
self = .windows
}
}

Expand Down
15 changes: 15 additions & 0 deletions Sources/SwiftSDKGenerator/Generator/LocalSwiftSDKGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ public final class LocalSwiftSDKGenerator: SwiftSDKGenerator {
}
}

public func buildCMakeProject(_ projectPath: FilePath, options: String) async throws -> FilePath {
try await Shell.run(
"""
PATH='/bin:/usr/bin:\(Self.homebrewPrefix)/bin' \
cmake -B build -G Ninja -S llvm -DCMAKE_BUILD_TYPE=Release \(options)
""",
currentDirectory: projectPath
)

let buildDirectory = projectPath.appending("build")
try await Shell.run("PATH='/bin:/usr/bin:\(Self.homebrewPrefix)/bin' ninja", currentDirectory: buildDirectory)

return buildDirectory
}

public func inTemporaryDirectory<T>(
_ closure: @Sendable (LocalSwiftSDKGenerator, FilePath) async throws -> T
) async throws -> T {
Expand Down
24 changes: 24 additions & 0 deletions Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SystemPackage

extension SwiftSDKGenerator {
func buildLLD(llvmSourcesDirectory: FilePath) async throws -> FilePath {
let buildDirectory = try await self.buildCMakeProject(
llvmSourcesDirectory,
options: "-DLLVM_ENABLE_PROJECTS=lld -DLLVM_TARGETS_TO_BUILD=\(self.targetTriple.cpu.llvmTargetConventionName)"
)

return buildDirectory.appending("bin").appending("lld")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ extension SwiftSDKGenerator {

print("Using these URLs for downloads:")

for artifact in downloadableArtifacts.allItems {
print(artifact.remoteURL)
}

// FIXME: some code duplication is necessary due to https://github.com/apple/swift-async-algorithms/issues/226
if shouldUseDocker {
for artifact in [downloadableArtifacts.hostSwift, downloadableArtifacts.hostLLVM] {
print(artifact.remoteURL)
}

let stream = combineLatest(hostSwiftProgressStream, hostLLVMProgressStream)
.throttle(for: .seconds(1))

Expand All @@ -56,13 +56,6 @@ extension SwiftSDKGenerator {
report(progress: llvmProgress, for: downloadableArtifacts.hostLLVM)
}
} else {
for artifact in [
downloadableArtifacts.hostSwift,
downloadableArtifacts.hostLLVM,
downloadableArtifacts.targetSwift,
] {
print(artifact.remoteURL)
}
let targetSwiftProgressStream = client.streamDownloadProgress(for: downloadableArtifacts.targetSwift)
.removeDuplicates(by: didProgressChangeSignificantly)

Expand Down
24 changes: 21 additions & 3 deletions Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,34 @@ extension SwiftSDKGenerator {
logGenerationStep("Unpacking and copying `lld` linker...")
let downloadableArtifacts = self.downloadableArtifacts
let pathsConfiguration = self.pathsConfiguration
let targetOS = self.targetTriple.os

try await inTemporaryDirectory { fileSystem, tmpDir in
let llvmArtifact = downloadableArtifacts.hostLLVM
try await fileSystem.untar(
file: downloadableArtifacts.hostLLVM.localPath,
file: llvmArtifact.localPath,
into: tmpDir,
stripComponents: 1
)

let unpackedLLDPath = if llvmArtifact.isPrebuilt {
tmpDir.appending("bin/lld")
} else {
try await self.buildLLD(llvmSourcesDirectory: tmpDir)
}

let toolchainLLDPath = switch targetOS {
case .linux:
pathsConfiguration.toolchainBinDirPath.appending("ld.lld")
case .wasi:
pathsConfiguration.toolchainBinDirPath.appending("wasm-ld")
default:
fatalError()
}

try fileSystem.copy(
from: tmpDir.appending("bin/lld"),
to: pathsConfiguration.toolchainBinDirPath.appending("ld.lld")
from: unpackedLLDPath,
to: toolchainLLDPath
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public protocol SwiftSDKGenerator: AnyObject {
func untar(file: FilePath, into directoryPath: FilePath, stripComponents: Int?) async throws
func unpack(file: FilePath, into directoryPath: FilePath) async throws
func rsync(from source: FilePath, to destination: FilePath) async throws
func buildCMakeProject(_ projectPath: FilePath, options: String) async throws -> FilePath

static func isChecksumValid(artifact: DownloadableArtifacts.Item, isVerbose: Bool) async throws -> Bool

Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftSDKGenerator/PlatformModels/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public struct Triple: CustomStringConvertible {
case .x86_64: "x86_64"
}
}

/// Returns the value of `cpu` converted to a convention used by `LLVM_TARGETS_TO_BUILD` CMake setting.
var llvmTargetConventionName: String {
switch self {
case .x86_64: "X86"
case .arm64: "AArch64"
}
}
}

enum Vendor: String {
Expand Down

0 comments on commit 321e22a

Please sign in to comment.