@@ -24,7 +24,7 @@ struct SwiftTargets {
24
24
25
25
init ( for buildDir: RepoBuildDir ) throws {
26
26
log. debug ( " [*] Reading Swift targets from build.ninja " )
27
- for rule in try buildDir. ninjaFile. buildRules {
27
+ for rule in try buildDir. ninjaFile. buildEdges {
28
28
try tryAddTarget ( for: rule, buildDir: buildDir)
29
29
}
30
30
targets. sort ( by: { $0. name < $1. name } )
@@ -65,33 +65,17 @@ struct SwiftTargets {
65
65
}
66
66
67
67
private mutating func computeBuildArgs(
68
- for rule: NinjaBuildFile . BuildRule
68
+ for edge: NinjaBuildFile . BuildEdge ,
69
+ in ninja: NinjaBuildFile
69
70
) throws -> BuildArgs ? {
70
- var buildArgs = BuildArgs ( for: . swiftc)
71
- if let commandAttr = rule. attributes [ . command] {
72
- // We have a custom command, parse it looking for a swiftc invocation.
73
- let command = try CommandParser . parseKnownCommandOnly ( commandAttr. value)
74
- guard let command, command. executable. knownCommand == . swiftc else {
75
- return nil
76
- }
77
- buildArgs += command. args
78
- } else if rule. attributes [ . flags] != nil {
79
- // Ninja separates out other arguments we need, splice them back in.
80
- for key : NinjaBuildFile . Attribute . Key in [ . flags, . includes, . defines] {
81
- guard let attr = rule. attributes [ key] else { continue }
82
- buildArgs. append ( attr. value)
83
- }
84
- // Add a module name argument if one is specified, validating to
85
- // ensure it's correct since we currently have some targets with
86
- // invalid module names, e.g swift-plugin-server.
87
- if let moduleName = rule. attributes [ . swiftModuleName] ? . value,
88
- moduleName. isValidSwiftIdentifier {
89
- buildArgs. append ( " -module-name \( moduleName) " )
90
- }
91
- } else {
71
+ let commandLine = try ninja. commandLine ( for: edge)
72
+ let command = try CommandParser . parseKnownCommandOnly ( commandLine)
73
+ guard let command, command. executable. knownCommand == . swiftc else {
92
74
return nil
93
75
}
94
76
77
+ var buildArgs = BuildArgs ( for: . swiftc, args: command. args)
78
+
95
79
// Only include known flags for now.
96
80
buildArgs = buildArgs. filter { arg in
97
81
if arg. flag != nil {
@@ -125,17 +109,9 @@ struct SwiftTargets {
125
109
}
126
110
127
111
func getSources(
128
- from rule : NinjaBuildFile . BuildRule , buildDir: RepoBuildDir
112
+ from edge : NinjaBuildFile . BuildEdge , buildDir: RepoBuildDir
129
113
) throws -> SwiftTarget . Sources {
130
- // If we have SWIFT_SOURCES defined, use it, otherwise check the rule
131
- // inputs.
132
- let files : [ AnyPath ]
133
- if let sourcesStr = rule. attributes [ . swiftSources] ? . value {
134
- files = try CommandParser . parseArguments ( sourcesStr, for: . swiftc)
135
- . compactMap ( \. value) . map ( AnyPath . init)
136
- } else {
137
- files = rule. inputs. map ( AnyPath . init)
138
- }
114
+ let files : [ AnyPath ] = edge. inputs. map ( AnyPath . init)
139
115
140
116
// Split the files into repo sources and external sources. Repo sources
141
117
// are those under the repo path, external sources are outside that path,
@@ -166,29 +142,29 @@ struct SwiftTargets {
166
142
}
167
143
168
144
private mutating func tryAddTarget(
169
- for rule : NinjaBuildFile . BuildRule ,
145
+ for edge : NinjaBuildFile . BuildEdge ,
170
146
buildDir: RepoBuildDir
171
147
) throws {
172
148
// Phonies are only used to track aliases.
173
- if rule . isPhony {
174
- for output in rule . outputs {
175
- outputAliases [ output, default: [ ] ] += rule . inputs
149
+ if edge . isPhony {
150
+ for output in edge . outputs {
151
+ outputAliases [ output, default: [ ] ] += edge . inputs
176
152
}
177
153
return
178
154
}
179
155
180
156
// Ignore build rules that don't have object file or swiftmodule outputs.
181
- let forBuild = rule . outputs. contains (
157
+ let forBuild = edge . outputs. contains (
182
158
where: { $0. hasExtension ( . o) }
183
159
)
184
- let forModule = rule . outputs. contains (
160
+ let forModule = edge . outputs. contains (
185
161
where: { $0. hasExtension ( . swiftmodule) }
186
162
)
187
163
guard forBuild || forModule else {
188
164
return
189
165
}
190
- let primaryOutput = rule . outputs. first!
191
- let sources = try getSources ( from: rule , buildDir: buildDir)
166
+ let primaryOutput = edge . outputs. first!
167
+ let sources = try getSources ( from: edge , buildDir: buildDir)
192
168
let repoSources = sources. repoSources
193
169
let externalSources = sources. externalSources
194
170
@@ -198,32 +174,30 @@ struct SwiftTargets {
198
174
return
199
175
}
200
176
201
- guard let buildArgs = try computeBuildArgs ( for: rule ) else { return }
177
+ guard let buildArgs = try computeBuildArgs ( for: edge , in : buildDir . ninjaFile ) else { return }
202
178
203
179
// Pick up the module name from the arguments, or use an explicitly
204
180
// specified module name if we have one. The latter might be invalid so
205
181
// may not be part of the build args (e.g 'swift-plugin-server'), but is
206
182
// fine for generation.
207
- let moduleName = buildArgs. lastValue ( for: . moduleName) ??
208
- rule. attributes [ . swiftModuleName] ? . value
183
+ let moduleName = buildArgs. lastValue ( for: . moduleName) ?? edge. bindings [ . swiftModuleName]
209
184
guard let moduleName else {
210
185
log. debug ( " ! Skipping Swift target with output \( primaryOutput) ; no module name " )
211
186
return
212
187
}
213
- let moduleLinkName = rule. attributes [ . swiftLibraryName] ? . value ??
214
- buildArgs. lastValue ( for: . moduleLinkName)
188
+ let moduleLinkName = buildArgs. lastValue ( for: . moduleLinkName) ?? edge. bindings [ . swiftLibraryName]
215
189
let name = moduleLinkName ?? moduleName
216
190
217
191
// Add the dependencies. We track dependencies for any input files, along
218
192
// with any recorded swiftmodule dependencies.
219
193
dependenciesByTargetName. withValue ( for: name, default: [ ] ) { deps in
220
194
deps. formUnion (
221
- rule . inputs. filter {
195
+ edge . inputs. filter {
222
196
$0. hasExtension ( . swiftmodule) || $0. hasExtension ( . o)
223
197
}
224
198
)
225
199
deps. formUnion (
226
- rule . dependencies. filter { $0. hasExtension ( . swiftmodule) }
200
+ edge . dependencies. filter { $0. hasExtension ( . swiftmodule) }
227
201
)
228
202
}
229
203
@@ -258,7 +232,7 @@ struct SwiftTargets {
258
232
targets. append ( target)
259
233
return target
260
234
} ( )
261
- for output in rule . outputs {
235
+ for output in edge . outputs {
262
236
targetsByOutput [ output] = target
263
237
}
264
238
if buildRule == nil || target. buildRule == nil {
0 commit comments