|
8 | 8 | See http://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 | 9 | */
|
10 | 10 |
|
| 11 | +import ArgumentParser |
11 | 12 | import TSCUtility
|
12 | 13 | import TSCBasic
|
13 | 14 | import PackageGraph
|
14 | 15 | import SPMBuildCore
|
15 | 16 | import Build
|
16 | 17 |
|
17 |
| -/// swift-build tool namespace |
18 |
| -public class SwiftBuildTool: SwiftTool<BuildToolOptions> { |
| 18 | +public struct BuildToolOptions: ParsableArguments { |
| 19 | + enum BuildToolMode { |
| 20 | + /// Build the package. |
| 21 | + case build |
19 | 22 |
|
20 |
| - public convenience init(args: [String]) { |
21 |
| - self.init( |
22 |
| - toolName: "build", |
23 |
| - usage: "[options]", |
24 |
| - overview: "Build sources into binary products", |
25 |
| - args: args, |
26 |
| - seeAlso: type(of: self).otherToolNames() |
27 |
| - ) |
| 23 | + /// Print the binary output path. |
| 24 | + case binPath |
28 | 25 | }
|
29 |
| - |
30 |
| - override func runImpl() throws { |
31 |
| - switch try options.mode() { |
32 |
| - case .build: |
33 |
| - #if os(Linux) |
34 |
| - // Emit warning if clang is older than version 3.6 on Linux. |
35 |
| - // See: <rdar://problem/28108951> SR-2299 Swift isn't using Gold by default on stock 14.04. |
36 |
| - checkClangVersion() |
37 |
| - #endif |
38 |
| - |
39 |
| - guard let subset = options.buildSubset(diagnostics: diagnostics) else { return } |
40 |
| - let buildSystem = try createBuildSystem() |
41 |
| - try buildSystem.build(subset: subset) |
42 |
| - |
43 |
| - case .binPath: |
44 |
| - try print(buildParameters().buildPath.description) |
45 |
| - |
46 |
| - case .version: |
47 |
| - print(Versioning.currentVersion.completeDisplayString) |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - override class func defineArguments(parser: ArgumentParser, binder: ArgumentBinder<BuildToolOptions>) { |
52 |
| - binder.bind( |
53 |
| - option: parser.add(option: buildTestsOptionName, kind: Bool.self, |
54 |
| - usage: "Build both source and test targets"), |
55 |
| - to: { $0.buildTests = $1 }) |
56 |
| - |
57 |
| - binder.bind( |
58 |
| - option: parser.add(option: productOptionName, kind: String.self, |
59 |
| - usage: "Build the specified product"), |
60 |
| - to: { $0.product = $1 }) |
61 |
| - |
62 |
| - binder.bind( |
63 |
| - option: parser.add(option: targetOptionName, kind: String.self, |
64 |
| - usage: "Build the specified target"), |
65 |
| - to: { $0.target = $1 }) |
66 |
| - |
67 |
| - binder.bind( |
68 |
| - option: parser.add(option: "--show-bin-path", kind: Bool.self, |
69 |
| - usage: "Print the binary output path"), |
70 |
| - to: { $0.shouldPrintBinPath = $1 }) |
71 |
| - } |
72 |
| - |
73 |
| - private func checkClangVersion() { |
74 |
| - // We only care about this on Ubuntu 14.04 |
75 |
| - guard let uname = try? Process.checkNonZeroExit(args: "lsb_release", "-r").spm_chomp(), |
76 |
| - uname.hasSuffix("14.04"), |
77 |
| - let clangVersionOutput = try? Process.checkNonZeroExit(args: "clang", "--version").spm_chomp(), |
78 |
| - let clang = getClangVersion(versionOutput: clangVersionOutput) else { |
79 |
| - return |
80 |
| - } |
81 |
| - |
82 |
| - if clang < Version(3, 6, 0) { |
83 |
| - print("warning: minimum recommended clang is version 3.6, otherwise you may encounter linker errors.") |
84 |
| - } |
85 |
| - } |
86 |
| -} |
87 |
| - |
88 |
| -public class BuildToolOptions: ToolOptions { |
| 26 | + |
89 | 27 | /// Returns the mode in which the build tool should run.
|
90 | 28 | func mode() throws -> BuildToolMode {
|
91 |
| - if shouldPrintVersion { |
92 |
| - return .version |
93 |
| - } |
94 | 29 | if shouldPrintBinPath {
|
95 | 30 | return .binPath
|
96 | 31 | }
|
@@ -122,52 +57,67 @@ public class BuildToolOptions: ToolOptions {
|
122 | 57 | return allSubsets.first ?? .allExcludingTests
|
123 | 58 | }
|
124 | 59 |
|
| 60 | + @OptionGroup() |
| 61 | + var swiftOptions: SwiftToolOptions |
| 62 | + |
125 | 63 | /// If the test should be built.
|
126 |
| - var buildTests = false |
| 64 | + @Flag(help: "Build both source and test targets") |
| 65 | + var buildTests: Bool |
127 | 66 |
|
128 | 67 | /// If the binary output path should be printed.
|
129 |
| - var shouldPrintBinPath = false |
| 68 | + @Flag(name: .customLong("show-bin-path"), help: "Print the binary output path") |
| 69 | + var shouldPrintBinPath: Bool |
130 | 70 |
|
131 | 71 | /// Specific target to build.
|
| 72 | + @Option(help: "Build the specified target") |
132 | 73 | var target: String?
|
133 | 74 |
|
134 | 75 | /// Specific product to build.
|
| 76 | + @Option(help: "Build the specified product) |
135 | 77 | var product: String?
|
136 | 78 | }
|
137 | 79 |
|
138 |
| -public enum BuildToolMode { |
139 |
| - /// Build the package. |
140 |
| - case build |
| 80 | +/// swift-build tool namespace |
| 81 | +public class SwiftBuildTool: SwiftTool<BuildToolOptions> { |
| 82 | + static let configuration = CommandConfiguration( |
| 83 | + commandName: "build", |
| 84 | + abstract: "Build sources into binary products") |
141 | 85 |
|
142 |
| - /// Print the binary output path. |
143 |
| - case binPath |
| 86 | + @OptionGroup() |
| 87 | + var options: BuildToolOptions |
144 | 88 |
|
145 |
| - /// Print the version. |
146 |
| - case version |
147 |
| -} |
| 89 | + func runImpl() throws { |
| 90 | + let swiftTool = try SwiftTool(options: options.swiftOptions) |
| 91 | + |
| 92 | + switch try options.mode() { |
| 93 | + case .build: |
| 94 | + #if os(Linux) |
| 95 | + // Emit warning if clang is older than version 3.6 on Linux. |
| 96 | + // See: <rdar://problem/28108951> SR-2299 Swift isn't using Gold by default on stock 14.04. |
| 97 | + checkClangVersion() |
| 98 | + #endif |
| 99 | + |
| 100 | + guard let subset = options.buildSubset(diagnostics: diagnostics) else { return } |
| 101 | + let buildSystem = try swiftTool.createBuildSystem() |
| 102 | + try buildSystem.build(subset: subset) |
148 | 103 |
|
149 |
| -fileprivate let buildTestsOptionName = "--build-tests" |
150 |
| -fileprivate let productOptionName = "--product" |
151 |
| -fileprivate let targetOptionName = "--target" |
152 |
| - |
153 |
| -fileprivate extension BuildSubset { |
154 |
| - var argumentName: String { |
155 |
| - switch self { |
156 |
| - case .allExcludingTests: |
157 |
| - fatalError("no corresponding argument") |
158 |
| - case .allIncludingTests: |
159 |
| - return buildTestsOptionName |
160 |
| - case .product: |
161 |
| - return productOptionName |
162 |
| - case .target: |
163 |
| - return targetOptionName |
| 104 | + case .binPath: |
| 105 | + try print(swiftTool.buildParameters().buildPath.description) |
164 | 106 | }
|
165 | 107 | }
|
166 |
| -} |
167 | 108 |
|
168 |
| -extension SwiftBuildTool: ToolName { |
169 |
| - static var toolName: String { |
170 |
| - return "swift build" |
| 109 | + private func checkClangVersion() { |
| 110 | + // We only care about this on Ubuntu 14.04 |
| 111 | + guard let uname = try? Process.checkNonZeroExit(args: "lsb_release", "-r").spm_chomp(), |
| 112 | + uname.hasSuffix("14.04"), |
| 113 | + let clangVersionOutput = try? Process.checkNonZeroExit(args: "clang", "--version").spm_chomp(), |
| 114 | + let clang = getClangVersion(versionOutput: clangVersionOutput) else { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + if clang < Version(3, 6, 0) { |
| 119 | + print("warning: minimum recommended clang is version 3.6, otherwise you may encounter linker errors.") |
| 120 | + } |
171 | 121 | }
|
172 | 122 | }
|
173 | 123 |
|
|
0 commit comments