Skip to content

Commit

Permalink
Use the Xcode build system in swift-run
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hart committed Feb 20, 2020
1 parent caaffd0 commit bf7d01d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
9 changes: 0 additions & 9 deletions Sources/Build/BuildDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ public struct BuildDescription: Codable {
/// The built test products.
public let builtTestProducts: [BuiltTestProduct]

/// The list of executable products in the package graph.
public let allExecutables: [String]

/// The list of executable products in the root package.
public let rootExecutables: [String]

public init(
plan: BuildPlan,
testDiscoveryCommands: [BuildManifest.CmdName: LLBuildManifest.TestDiscoveryTool],
Expand All @@ -216,9 +210,6 @@ public struct BuildDescription: Codable {
binaryPath: desc.binary
)
}

self.allExecutables = plan.graph.allProducts.filter{ $0.type == .executable }.map{ $0.name }
self.rootExecutables = plan.graph.rootPackages.flatMap{ $0.products }.filter{ $0.type == .executable }.map{ $0.name }
}

public func write(to path: AbsolutePath) throws {
Expand Down
13 changes: 5 additions & 8 deletions Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
/// The build description resulting from planing.
private var buildDescription: BuildDescription?

/// The loaded package graph.
private var packageGraph: PackageGraph?

/// The stdout stream for the build delegate.
let stdoutStream: OutputByteStream

Expand All @@ -64,17 +67,11 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
self.stdoutStream = stdoutStream
}

/// Returns the package graph using the graph loader closure.
///
/// First access will cache the graph.
public func getPackageGraph() throws -> PackageGraph {
if let packageGraph = _packageGraph {
return packageGraph
try memoize(to: &packageGraph) {
try self.packageGraphLoader()
}
_packageGraph = try packageGraphLoader()
return _packageGraph!
}
private var _packageGraph: PackageGraph?

/// Compute and return the latest build descroption.
///
Expand Down
19 changes: 11 additions & 8 deletions Sources/Commands/SwiftRunTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,13 @@ public class SwiftRunTool: SwiftTool<RunToolOptions> {
return
}

let buildOp = try createBuildOperation()
let buildDescription = try buildOp.getBuildDescription()
let productName = try findProductName(in: buildDescription)
let buildSystem = try createBuildSystem()
let productName = try findProductName(in: buildSystem.getPackageGraph())

if options.shouldBuildTests {
try buildOp.build(subset: .allIncludingTests)
try buildSystem.build(subset: .allIncludingTests)
} else if options.shouldBuild {
try buildOp.build(subset: .product(productName))
try buildSystem.build(subset: .product(productName))
}

let executablePath = try self.buildParameters().buildPath.appending(component: productName)
Expand All @@ -156,16 +155,20 @@ public class SwiftRunTool: SwiftTool<RunToolOptions> {
}

/// Returns the path to the correct executable based on options.
private func findProductName(in buildDescription: BuildDescription) throws -> String {
private func findProductName(in graph: PackageGraph) throws -> String {
if let executable = options.executable {
guard buildDescription.allExecutables.contains(executable) else {
let executableExists = graph.allProducts.contains { $0.type == .executable && $0.name == executable }
guard executableExists else {
throw RunError.executableNotFound(executable)
}
return executable
}

// If the executable is implicit, search through root products.
let rootExecutables = buildDescription.rootExecutables
let rootExecutables = graph.rootPackages
.flatMap { $0.products }
.filter { $0.type == .executable }
.map { $0.name }

// Error out if the package contains no executables.
guard rootExecutables.count > 0 else {
Expand Down
5 changes: 5 additions & 0 deletions Sources/SPMBuildCore/BuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import PackageGraph

/// An enum representing what subset of the package to build.
public enum BuildSubset {
/// Represents the subset of all products and non-test targets.
Expand All @@ -30,6 +32,9 @@ public protocol BuildSystem {
/// The test products that this build system will build.
var builtTestProducts: [BuiltTestProduct] { get }

/// Returns the package graph used by the build system.
func getPackageGraph() throws -> PackageGraph

/// Builds a subset of the package graph.
/// - Parameters:
/// - subset: The subset of the package graph to build.
Expand Down
15 changes: 6 additions & 9 deletions Sources/XCBuildSupport/XcodeBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public final class XcodeBuildSystem: BuildSystem {
}
}

public func getPackageGraph() throws -> PackageGraph {
try memoize(to: &packageGraph) {
try packageGraphLoader()
}
}

public func build(subset: BuildSubset) throws {
let pifBuilder = try getPIFBuilder()
let pif = try pifBuilder.generatePIF()
Expand Down Expand Up @@ -123,15 +129,6 @@ public final class XcodeBuildSystem: BuildSystem {
return pifBuilder
}
}

/// Returns the package graph using the graph loader closure.
///
/// First access will cache the graph.
private func getPackageGraph() throws -> PackageGraph {
try memoize(to: &packageGraph) {
try packageGraphLoader()
}
}
}

extension BuildConfiguration {
Expand Down

0 comments on commit bf7d01d

Please sign in to comment.