Skip to content
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

Keep separate build parameters for host and target #7164

Merged
merged 13 commits into from
Dec 7, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ public final class ClangTargetBuildDescription {

var args = [String]()
// Only enable ARC on macOS.
if buildParameters.targetTriple.isDarwin() {
if self.buildParameters.triple.isDarwin() {
args += ["-fobjc-arc"]
}
args += try buildParameters.targetTripleArgs(for: target)
@@ -225,32 +225,33 @@ public final class ClangTargetBuildDescription {
args += activeCompilationConditions
args += ["-fblocks"]

let buildTriple = self.buildParameters.triple
// Enable index store, if appropriate.
//
// This feature is not widely available in OSS clang. So, we only enable
// index store for Apple's clang or if explicitly asked to.
if ProcessEnv.vars.keys.contains("SWIFTPM_ENABLE_CLANG_INDEX_STORE") {
args += buildParameters.indexStoreArguments(for: target)
} else if buildParameters.targetTriple.isDarwin(),
(try? buildParameters.toolchain._isClangCompilerVendorApple()) == true
args += self.buildParameters.indexStoreArguments(for: target)
} else if buildTriple.isDarwin(),
(try? self.buildParameters.toolchain._isClangCompilerVendorApple()) == true
{
args += buildParameters.indexStoreArguments(for: target)
args += self.buildParameters.indexStoreArguments(for: target)
}

// Enable Clang module flags, if appropriate.
let enableModules: Bool
let triple = self.buildParameters.triple
if toolsVersion < .v5_8 {
// For version < 5.8, we enable them except in these cases:
// 1. on Darwin when compiling for C++, because C++ modules are disabled on Apple-built Clang releases
// 2. on Windows when compiling for any language, because of issues with the Windows SDK
// 3. on Android when compiling for any language, because of issues with the Android SDK
enableModules = !(buildParameters.targetTriple.isDarwin() && isCXX) && !buildParameters.targetTriple
.isWindows() && !buildParameters.targetTriple.isAndroid()
enableModules = !(triple.isDarwin() && isCXX) && !triple.isWindows() && !triple.isAndroid()
} else {
// For version >= 5.8, we disable them when compiling for C++ regardless of platforms, see:
// https://github.com/llvm/llvm-project/issues/55980 for clang frontend crash when module
// enabled for C++ on c++17 standard and above.
enableModules = !isCXX && !buildParameters.targetTriple.isWindows() && !buildParameters.targetTriple.isAndroid()
enableModules = !isCXX && !triple.isWindows() && !triple.isAndroid()
}

if enableModules {
24 changes: 13 additions & 11 deletions Sources/Build/BuildDescription/ProductBuildDescription.swift
Original file line number Diff line number Diff line change
@@ -110,15 +110,16 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
return []
}

let triple = self.buildParameters.triple
switch self.buildParameters.configuration {
case .debug:
return []
case .release:
if self.buildParameters.targetTriple.isApple() {
if triple.isApple() {
return ["-Xlinker", "-dead_strip"]
} else if self.buildParameters.targetTriple.isWindows() {
} else if triple.isWindows() {
return ["-Xlinker", "/OPT:REF"]
} else if self.buildParameters.targetTriple.arch == .wasm32 {
} else if triple.arch == .wasm32 {
// FIXME: wasm-ld strips data segments referenced through __start/__stop symbols
// during GC, and it removes Swift metadata sections like swift5_protocols
// We should add support of SHF_GNU_RETAIN-like flag for __attribute__((retain))
@@ -136,7 +137,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
/// The arguments to the librarian to create a static library.
public func archiveArguments() throws -> [String] {
let librarian = self.buildParameters.toolchain.librarianPath.pathString
let triple = self.buildParameters.targetTriple
let triple = self.buildParameters.triple
if triple.isWindows(), librarian.hasSuffix("link") || librarian.hasSuffix("link.exe") {
return try [librarian, "/LIB", "/OUT:\(binaryPath.pathString)", "@\(self.linkFileListPath.pathString)"]
}
@@ -187,6 +188,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
}

var isLinkingStaticStdlib = false
let triple = self.buildParameters.triple
switch derivedProductType {
case .macro:
throw InternalError("macro not supported") // should never be reached
@@ -206,7 +208,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
args += self.deadStripArguments
case .library(.dynamic):
args += ["-emit-library"]
if self.buildParameters.targetTriple.isDarwin() {
if triple.isDarwin() {
let relativePath = try "@rpath/\(buildParameters.binaryRelativePath(for: self.product).pathString)"
args += ["-Xlinker", "-install_name", "-Xlinker", relativePath]
}
@@ -215,9 +217,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// Link the Swift stdlib statically, if requested.
// TODO: unify this logic with SwiftTargetBuildDescription.stdlibArguments
if self.buildParameters.linkingParameters.shouldLinkStaticSwiftStdlib {
if self.buildParameters.targetTriple.isDarwin() {
if triple.isDarwin() {
self.observabilityScope.emit(.swiftBackDeployError)
} else if self.buildParameters.targetTriple.isSupportingStaticStdlib {
} else if triple.isSupportingStaticStdlib {
args += ["-static-stdlib"]
isLinkingStaticStdlib = true
}
@@ -260,9 +262,9 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
// Set rpath such that dynamic libraries are looked up
// adjacent to the product, unless overridden.
if !self.buildParameters.linkingParameters.shouldDisableLocalRpath {
if self.buildParameters.targetTriple.isLinux() {
if triple.isLinux() {
args += ["-Xlinker", "-rpath=$ORIGIN"]
} else if self.buildParameters.targetTriple.isDarwin() {
} else if triple.isDarwin() {
let rpath = self.product.type == .test ? "@loader_path/../../../" : "@loader_path"
args += ["-Xlinker", "-rpath", "-Xlinker", rpath]
}
@@ -283,7 +285,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription

// When deploying to macOS prior to macOS 12, add an rpath to the
// back-deployed concurrency libraries.
if useStdlibRpath, self.buildParameters.targetTriple.isMacOSX {
if useStdlibRpath, triple.isMacOSX {
let macOSSupportedPlatform = self.package.platforms.getDerived(for: .macOS, usingXCTest: product.isLinkingXCTest)
if macOSSupportedPlatform.version.major < 12 {
let backDeployedStdlib = try buildParameters.toolchain.macosSwiftStdlib
@@ -361,7 +363,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
flags += libraries.map { "-l" + $0 }

// Linked frameworks.
if self.buildParameters.targetTriple.supportsFrameworks {
if self.buildParameters.triple.supportsFrameworks {
let frameworks = OrderedSet(self.staticTargets.reduce([]) {
$0 + self.buildParameters.createScope(for: $1).evaluate(.LINK_FRAMEWORKS)
})
Original file line number Diff line number Diff line change
@@ -110,8 +110,8 @@ public final class SwiftTargetBuildDescription {
/// The path to the swiftmodule file after compilation.
public var moduleOutputPath: AbsolutePath { // note: needs to be public because of sourcekit-lsp
// If we're an executable and we're not allowing test targets to link against us, we hide the module.
let allowLinkingAgainstExecutables = (buildParameters.targetTriple.isDarwin() || self.buildParameters.targetTriple
.isLinux() || self.buildParameters.targetTriple.isWindows()) && self.toolsVersion >= .v5_5
let triple = buildParameters.triple
let allowLinkingAgainstExecutables = (triple.isDarwin() || triple.isLinux() || triple.isWindows()) && self.toolsVersion >= .v5_5
let dirPath = (target.type == .executable && !allowLinkingAgainstExecutables) ? self.tempsPath : self.modulesPath
return dirPath.appending(component: self.target.c99name + ".swiftmodule")
}
@@ -320,7 +320,7 @@ public final class SwiftTargetBuildDescription {
return
}

guard buildParameters.targetTriple.isDarwin(), buildParameters.testingParameters.experimentalTestOutput else {
guard self.buildParameters.triple.isDarwin(), self.buildParameters.testingParameters.experimentalTestOutput else {
return
}

@@ -364,7 +364,7 @@ public final class SwiftTargetBuildDescription {
guard let bundlePath else { return }

let mainPathSubstitution: String
if self.buildParameters.targetTriple.isWASI() {
if self.buildParameters.triple.isWASI() {
// We prefer compile-time evaluation of the bundle path here for WASI. There's no benefit in evaluating this
// at runtime, especially as `Bundle` support in WASI Foundation is partial. We expect all resource paths to
// evaluate to `/\(resourceBundleName)/\(resourcePath)`, which allows us to pass this path to JS APIs like
@@ -649,7 +649,7 @@ public final class SwiftTargetBuildDescription {

/// Returns true if ObjC compatibility header should be emitted.
private var shouldEmitObjCCompatibilityHeader: Bool {
self.buildParameters.targetTriple.isDarwin() && self.target.type == .library
self.buildParameters.triple.isDarwin() && self.target.type == .library
}

func writeOutputFileMap() throws -> AbsolutePath {
@@ -852,7 +852,7 @@ public final class SwiftTargetBuildDescription {
var arguments: [String] = []

let isLinkingStaticStdlib = self.buildParameters.linkingParameters.shouldLinkStaticSwiftStdlib
&& self.buildParameters.targetTriple.isSupportingStaticStdlib
&& self.buildParameters.triple.isSupportingStaticStdlib
if isLinkingStaticStdlib {
arguments += ["-static-stdlib"]
}
10 changes: 10 additions & 0 deletions Sources/Build/BuildDescription/TargetBuildDescription.swift
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import Basics
import struct SPMBuildCore.BuildParameters
import class PackageGraph.ResolvedTarget
import struct PackageModel.Resource
import struct SPMBuildCore.BuildToolPluginInvocationResult
@@ -91,4 +92,13 @@ public enum TargetBuildDescription {
return target.buildToolPluginInvocationResults
}
}

var buildParameters: BuildParameters {
switch self {
case .swift(let swiftTargetBuildDescription):
return swiftTargetBuildDescription.buildParameters
case .clang(let clangTargetBuildDescription):
return clangTargetBuildDescription.buildParameters
}
}
}
10 changes: 5 additions & 5 deletions Sources/Build/BuildManifest/LLBuildManifestBuilder+Clang.swift
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ extension LLBuildManifestBuilder {
}
}

for dependency in target.target.dependencies(satisfying: self.buildEnvironment) {
for dependency in target.target.dependencies(satisfying: target.buildEnvironment) {
switch dependency {
case .target(let target, _):
addStaticTargetInputs(target)
@@ -68,7 +68,7 @@ extension LLBuildManifestBuilder {
}

for binaryPath in target.libraryBinaryPaths {
let path = destinationPath(forBinaryAt: binaryPath)
let path = target.buildParameters.destinationPath(forBinaryAt: binaryPath)
if self.fileSystem.isDirectory(binaryPath) {
inputs.append(directory: path)
} else {
@@ -97,7 +97,7 @@ extension LLBuildManifestBuilder {

args += ["-c", path.source.pathString, "-o", path.object.pathString]

let clangCompiler = try buildParameters.toolchain.getClangCompiler().pathString
let clangCompiler = try target.buildParameters.toolchain.getClangCompiler().pathString
args.insert(clangCompiler, at: 0)

let objectFileNode: Node = .file(path.object)
@@ -116,7 +116,7 @@ extension LLBuildManifestBuilder {
try addBuildToolPlugins(.clang(target))

// Create a phony node to represent the entire target.
let targetName = target.target.getLLBuildTargetName(config: self.buildConfig)
let targetName = target.target.getLLBuildTargetName(config: target.buildParameters.buildConfig)
let output: Node = .virtual(targetName)

self.manifest.addNode(output, toTarget: targetName)
@@ -126,7 +126,7 @@ extension LLBuildManifestBuilder {
outputs: [output]
)

if self.plan.graph.isInRootPackages(target.target, satisfying: self.buildEnvironment) {
if self.plan.graph.isInRootPackages(target.target, satisfying: target.buildParameters.buildEnvironment) {
if !target.isTestTarget {
self.addNode(output, toTarget: .main)
}
Original file line number Diff line number Diff line change
@@ -15,12 +15,12 @@ import struct LLBuildManifest.Node

extension LLBuildManifestBuilder {
func createProductCommand(_ buildProduct: ProductBuildDescription) throws {
let cmdName = try buildProduct.product.getCommandName(config: self.buildConfig)
let cmdName = try buildProduct.product.getCommandName(config: buildProduct.buildParameters.buildConfig)

// Add dependency on Info.plist generation on Darwin platforms.
let testInputs: [AbsolutePath]
if buildProduct.product.type == .test
&& buildProduct.buildParameters.targetTriple.isDarwin()
&& buildProduct.buildParameters.triple.isDarwin()
&& buildProduct.buildParameters.testingParameters.experimentalTestOutput {
let testBundleInfoPlistPath = try buildProduct.binaryPath.parentDirectory.parentDirectory.appending(component: "Info.plist")
testInputs = [testBundleInfoPlistPath]
@@ -34,7 +34,7 @@ extension LLBuildManifestBuilder {
}

// Create a phony node to represent the entire target.
let targetName = try buildProduct.product.getLLBuildTargetName(config: self.buildConfig)
let targetName = try buildProduct.product.getLLBuildTargetName(config: buildProduct.buildParameters.buildConfig)
let output: Node = .virtual(targetName)

let finalProductNode: Node
@@ -59,8 +59,8 @@ extension LLBuildManifestBuilder {
let linkedBinaryNode: Node
let linkedBinaryPath = try buildProduct.binaryPath
if case .executable = buildProduct.product.type,
buildParameters.targetTriple.isMacOSX,
buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
buildProduct.buildParameters.triple.isMacOSX,
buildProduct.buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
shouldCodeSign = true
linkedBinaryNode = try .file(buildProduct.binaryPath, isMutated: true)
} else {
@@ -85,7 +85,7 @@ extension LLBuildManifestBuilder {
outputPath: plistPath
)

let cmdName = try buildProduct.product.getCommandName(config: self.buildConfig)
let cmdName = try buildProduct.product.getCommandName(config: buildProduct.buildParameters.buildConfig)
let codeSigningOutput = Node.virtual(targetName + "-CodeSigning")
try self.manifest.addShellCmd(
name: "\(cmdName)-entitlements",
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ extension LLBuildManifestBuilder {
outputs.append(output)
}

let cmdName = target.target.getLLBuildResourcesCmdName(config: self.buildConfig)
let cmdName = target.target.getLLBuildResourcesCmdName(config: target.buildParameters.buildConfig)
self.manifest.addPhonyCmd(name: cmdName, inputs: outputs, outputs: [.virtual(cmdName)])

return .virtual(cmdName)
Loading