1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2024 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
//
//===----------------------------------------------------------------------===//
@_spi(SwiftPMInternal)
import Basics
import Dispatch
import class Foundation.JSONEncoder
import class Foundation.NSArray
import class Foundation.NSDictionary
import PackageGraph
import PackageModel
@_spi(SwiftPMInternal)
import SPMBuildCore
import func TSCBasic.memoize
import protocol TSCBasic.OutputByteStream
import class Basics.AsyncProcess
import func TSCBasic.withTemporaryFile
import enum TSCUtility.Diagnostics
public final class XcodeBuildSystem: SPMBuildCore.BuildSystem {
private let buildParameters: BuildParameters
private let packageGraphLoader: () throws -> ModulesGraph
private let logLevel: Basics.Diagnostic.Severity
private let xcbuildPath: AbsolutePath
private var packageGraph: ModulesGraph?
private var pifBuilder: PIFBuilder?
private let fileSystem: FileSystem
private let observabilityScope: ObservabilityScope
/// The output stream for the build delegate.
private let outputStream: OutputByteStream
/// The delegate used by the build system.
public weak var delegate: SPMBuildCore.BuildSystemDelegate?
public var builtTestProducts: [BuiltTestProduct] {
do {
let graph = try getPackageGraph()
var builtProducts: [BuiltTestProduct] = []
for package in graph.rootPackages {
for product in package.products where product.type == .test {
let binaryPath = try buildParameters.binaryPath(for: product)
builtProducts.append(
BuiltTestProduct(
productName: product.name,
binaryPath: binaryPath,
packagePath: package.path,
testEntryPointPath: product.underlying.testEntryPointPath
)
)
}
}
return builtProducts
} catch {
self.observabilityScope.emit(error)
return []
}
}
public var buildPlan: SPMBuildCore.BuildPlan {
get throws {
throw StringError("XCBuild does not provide a build plan")
}
}
public init(
buildParameters: BuildParameters,
packageGraphLoader: @escaping () throws -> ModulesGraph,
outputStream: OutputByteStream,
logLevel: Basics.Diagnostic.Severity,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) throws {
self.buildParameters = buildParameters
self.packageGraphLoader = packageGraphLoader
self.outputStream = outputStream
self.logLevel = logLevel
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope.makeChildScope(description: "Xcode Build System")
if let xcbuildTool = Environment.current["XCBUILD_TOOL"] {
xcbuildPath = try AbsolutePath(validating: xcbuildTool)
} else {
let xcodeSelectOutput = try AsyncProcess.popen(args: "xcode-select", "-p").utf8Output().spm_chomp()
let xcodeDirectory = try AbsolutePath(validating: xcodeSelectOutput)
xcbuildPath = try AbsolutePath(
validating: "../SharedFrameworks/XCBuild.framework/Versions/A/Support/xcbuild",
relativeTo: xcodeDirectory
)
}
guard fileSystem.exists(xcbuildPath) else {
throw StringError("xcbuild executable at '\(xcbuildPath)' does not exist or is not executable")
}
}
private func supportedSwiftVersions() throws -> [SwiftLanguageVersion] {
for path in [
"../../../../../Developer/Library/Xcode/Plug-ins/XCBSpecifications.ideplugin/Contents/Resources/Swift.xcspec",
"../PlugIns/XCBBuildService.bundle/Contents/PlugIns/XCBSpecifications.ideplugin/Contents/Resources/Swift.xcspec",
] {
let swiftSpecPath = try AbsolutePath(validating: path, relativeTo: xcbuildPath.parentDirectory)
if !fileSystem.exists(swiftSpecPath) {
continue
}
let swiftSpec = NSArray(contentsOfFile: swiftSpecPath.pathString)
let compilerSpec = swiftSpec?.compactMap { $0 as? NSDictionary }.first {
if let identifier = $0["Identifier"] as? String {
identifier == "com.apple.xcode.tools.swift.compiler"
} else {
false
}
}
let supportedSwiftVersions: [SwiftLanguageVersion] = if let versions =
compilerSpec?["SupportedLanguageVersions"] as? NSArray
{
versions.compactMap {
if let stringValue = $0 as? String {
SwiftLanguageVersion(string: stringValue)
} else {
nil
}
}
} else {
[]
}
return supportedSwiftVersions
}
return []
}
public func build(subset: BuildSubset) throws {
guard !buildParameters.shouldSkipBuilding else {
return
}
let pifBuilder = try getPIFBuilder()
let pif = try pifBuilder.generatePIF()
try self.fileSystem.writeIfChanged(path: buildParameters.pifManifest, string: pif)
var arguments = [
xcbuildPath.pathString,
"build",
buildParameters.pifManifest.pathString,
"--configuration",
buildParameters.configuration.xcbuildName,
"--derivedDataPath",
buildParameters.dataPath.pathString,
"--target",
subset.pifTargetName,
]
let buildParamsFile: AbsolutePath?
// Do not generate a build parameters file if a custom one has been passed.
if let flags = buildParameters.flags.xcbuildFlags, !flags.contains("--buildParametersFile") {
buildParamsFile = try createBuildParametersFile()
if let buildParamsFile {
arguments += ["--buildParametersFile", buildParamsFile.pathString]
}
} else {
buildParamsFile = nil
}
if let flags = buildParameters.flags.xcbuildFlags {
arguments += flags
}
let delegate = createBuildDelegate()
var hasStdout = false
var stdoutBuffer: [UInt8] = []
var stderrBuffer: [UInt8] = []
let redirection: AsyncProcess.OutputRedirection = .stream(stdout: { bytes in
hasStdout = hasStdout || !bytes.isEmpty
delegate.parse(bytes: bytes)
if !delegate.didParseAnyOutput {
stdoutBuffer.append(contentsOf: bytes)
}
}, stderr: { bytes in
stderrBuffer.append(contentsOf: bytes)
})
// We need to sanitize the environment we are passing to XCBuild because we could otherwise interfere with its
// linked dependencies e.g. when we have a custom swift-driver dynamic library in the path.
var sanitizedEnvironment = Environment.current
sanitizedEnvironment["DYLD_LIBRARY_PATH"] = nil
let process = AsyncProcess(
arguments: arguments,
environment: sanitizedEnvironment,
outputRedirection: redirection
)
try process.launch()
let result = try process.waitUntilExit()
if let buildParamsFile {
try? self.fileSystem.removeFileTree(buildParamsFile)
}
guard result.exitStatus == .terminated(code: 0) else {
if hasStdout {
if !delegate.didParseAnyOutput {
self.observabilityScope.emit(error: String(decoding: stdoutBuffer, as: UTF8.self))
}
} else {
if !stderrBuffer.isEmpty {
self.observabilityScope.emit(error: String(decoding: stderrBuffer, as: UTF8.self))
} else {
self.observabilityScope.emit(error: "Unknown error: stdout and stderr are empty")
}
}
throw Diagnostics.fatalError
}
}
func createBuildParametersFile() throws -> AbsolutePath {
// Generate the run destination parameters.
let runDestination = XCBBuildParameters.RunDestination(
platform: self.buildParameters.triple.osNameUnversioned,
sdk: self.buildParameters.triple.osNameUnversioned,
sdkVariant: nil,
targetArchitecture: buildParameters.triple.archName,
supportedArchitectures: [],
disableOnlyActiveArch: true
)
// Generate a table of any overriding build settings.
var settings: [String: String] = [:]
// An error with determining the override should not be fatal here.
settings["CC"] = try? buildParameters.toolchain.getClangCompiler().pathString
// Always specify the path of the effective Swift compiler, which was determined in the same way as for the
// native build system.
settings["SWIFT_EXEC"] = buildParameters.toolchain.swiftCompilerPath.pathString
settings["LIBRARY_SEARCH_PATHS"] = try "$(inherited) \(buildParameters.toolchain.toolchainLibDir.pathString)"
settings["OTHER_CFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.cCompilerFlags.map { $0.spm_shellEscaped() }
+ buildParameters.flags.cCompilerFlags.map { $0.spm_shellEscaped() }
).joined(separator: " ")
settings["OTHER_CPLUSPLUSFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.cxxCompilerFlags.map { $0.spm_shellEscaped() }
+ buildParameters.flags.cxxCompilerFlags.map { $0.spm_shellEscaped() }
).joined(separator: " ")
settings["OTHER_SWIFT_FLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.swiftCompilerFlags.map { $0.spm_shellEscaped() }
+ buildParameters.flags.swiftCompilerFlags.map { $0.spm_shellEscaped() }
).joined(separator: " ")
settings["OTHER_LDFLAGS"] = (
["$(inherited)"]
+ buildParameters.toolchain.extraFlags.linkerFlags.map { $0.spm_shellEscaped() }
+ buildParameters.flags.linkerFlags.map { $0.spm_shellEscaped() }
).joined(separator: " ")
// Optionally also set the list of architectures to build for.
if let architectures = buildParameters.architectures, !architectures.isEmpty {
settings["ARCHS"] = architectures.joined(separator: " ")
}
// Generate the build parameters.
let params = XCBBuildParameters(
configurationName: buildParameters.configuration.xcbuildName,
overrides: .init(synthesized: .init(table: settings)),
activeRunDestination: runDestination
)
// Write out the parameters as a JSON file, and return the path.
let encoder = JSONEncoder.makeWithDefaults()
let data = try encoder.encode(params)
let file = try withTemporaryFile(deleteOnClose: false) { AbsolutePath($0.path) }
try self.fileSystem.writeFileContents(file, data: data)
return file
}
public func cancel(deadline: DispatchTime) throws {}
/// Returns a new instance of `XCBuildDelegate` for a build operation.
private func createBuildDelegate() -> XCBuildDelegate {
let progressAnimation = ProgressAnimation.percent(
stream: self.outputStream,
verbose: self.logLevel.isVerbose,
header: ""
)
let delegate = XCBuildDelegate(
buildSystem: self,
outputStream: self.outputStream,
progressAnimation: progressAnimation,
logLevel: self.logLevel,
observabilityScope: self.observabilityScope
)
return delegate
}
private func getPIFBuilder() throws -> PIFBuilder {
try memoize(to: &pifBuilder) {
let graph = try getPackageGraph()
let pifBuilder = try PIFBuilder(
graph: graph,
parameters: .init(buildParameters, supportedSwiftVersions: supportedSwiftVersions()),
fileSystem: self.fileSystem,
observabilityScope: self.observabilityScope
)
return pifBuilder
}
}
/// Returns the package graph using the graph loader closure.
///
/// First access will cache the graph.
public func getPackageGraph() throws -> ModulesGraph {
try memoize(to: &packageGraph) {
try packageGraphLoader()
}
}
}
struct XCBBuildParameters: Encodable {
struct RunDestination: Encodable {
var platform: String
var sdk: String
var sdkVariant: String?
var targetArchitecture: String
var supportedArchitectures: [String]
var disableOnlyActiveArch: Bool
}
struct XCBSettingsTable: Encodable {
var table: [String: String]
}
struct SettingsOverride: Encodable {
var synthesized: XCBSettingsTable? = nil
}
var configurationName: String
var overrides: SettingsOverride
var activeRunDestination: RunDestination
}
extension BuildConfiguration {
public var xcbuildName: String {
switch self {
case .debug: "Debug"
case .release: "Release"
}
}
}
extension PIFBuilderParameters {
public init(_ buildParameters: BuildParameters, supportedSwiftVersions: [SwiftLanguageVersion]) {
self.init(
isPackageAccessModifierSupported: buildParameters.driverParameters.isPackageAccessModifierSupported,
enableTestability: buildParameters.testingParameters.enableTestability,
shouldCreateDylibForDynamicProducts: buildParameters.shouldCreateDylibForDynamicProducts,
toolchainLibDir: (try? buildParameters.toolchain.toolchainLibDir) ?? .root,
pkgConfigDirectories: buildParameters.pkgConfigDirectories,
sdkRootPath: buildParameters.toolchain.sdkRootPath,
supportedSwiftVersions: supportedSwiftVersions
)
}
}
extension BuildSubset {
var pifTargetName: String {
switch self {
case .product(let name, _):
PackagePIFProjectBuilder.targetName(for: name)
case .target(let name, _):
name
case .allExcludingTests:
PIFBuilder.allExcludingTestsTargetName
case .allIncludingTests:
PIFBuilder.allIncludingTestsTargetName
}
}
}
extension Basics.Diagnostic.Severity {
var isVerbose: Bool {
self <= .info
}
}
|