blob: aa617c611eb2506f5ea93c6d720f6b9341b2d436 [file] [log] [blame]
Daniel Dunbara5677512009-01-05 19:53:30 +00001import os
2import sys
3import tempfile
4from pprint import pprint
5
6###
7
8import Arguments
9import Jobs
10import Phases
11import Tools
12import Types
13import Util
14
15# FIXME: Clean up naming of options and arguments. Decide whether to
16# rename Option and be consistent about use of Option/Arg.
17
18####
19
20class MissingArgumentError(ValueError):
21 """MissingArgumentError - An option required an argument but none
22 was given."""
23
24###
25
26class Driver(object):
27 def __init__(self):
Daniel Dunbarba6e3232009-01-06 06:12:13 +000028 self.parser = Arguments.OptionParser()
Daniel Dunbara5677512009-01-05 19:53:30 +000029
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000030 def run(self, argv):
Daniel Dunbara5677512009-01-05 19:53:30 +000031 # FIXME: Things to support from environment: GCC_EXEC_PREFIX,
32 # COMPILER_PATH, LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS,
33 # QA_OVERRIDE_GCC3_OPTIONS, ...?
34
35 # FIXME: -V and -b processing
36
37 # Handle some special -ccc- options used for testing which are
38 # only allowed at the beginning of the command line.
39 cccPrintOptions = False
40 cccPrintPhases = False
41 cccUseDriverDriver = True
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000042 while argv and argv[0].startswith('-ccc-'):
43 opt,argv = argv[0][5:],argv[1:]
Daniel Dunbara5677512009-01-05 19:53:30 +000044
45 if opt == 'print-options':
46 cccPrintOptions = True
47 elif opt == 'print-phases':
48 cccPrintPhases = True
49 elif opt == 'no-driver-driver':
50 # FIXME: Remove this once we have some way of being a
51 # cross compiler driver (cross driver compiler? compiler
52 # cross driver? etc.).
53 cccUseDriverDriver = False
54 else:
55 raise ValueError,"Invalid ccc option: %r" % cccPrintOptions
56
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000057 args = self.parser.parseArgs(argv)
Daniel Dunbara5677512009-01-05 19:53:30 +000058
59 # FIXME: Ho hum I have just realized -Xarch_ is broken. We really
60 # need to reparse the Arguments after they have been expanded by
61 # -Xarch. How is this going to work?
62 #
63 # Scratch that, we aren't going to do that; it really disrupts the
64 # organization, doesn't consistently work with gcc-dd, and is
65 # confusing. Instead we are going to enforce that -Xarch_ is only
66 # used with options which do not alter the driver behavior. Let's
67 # hope this is ok, because the current architecture is a little
68 # tied to it.
69
70 if cccPrintOptions:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000071 self.printOptions(args)
Daniel Dunbara5677512009-01-05 19:53:30 +000072 sys.exit(0)
73
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000074 self.handleImmediateOptions(args)
Daniel Dunbara5677512009-01-05 19:53:30 +000075
76 if cccUseDriverDriver:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000077 phases = self.buildPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +000078 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000079 phases = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +000080
81 if cccPrintPhases:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000082 self.printPhases(phases, args)
Daniel Dunbara5677512009-01-05 19:53:30 +000083 sys.exit(0)
84
85 if 0:
86 print Util.pprint(phases)
87
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000088 jobs = self.bindPhases(phases, args)
Daniel Dunbara5677512009-01-05 19:53:30 +000089
90 # FIXME: We should provide some basic sanity checking of the
91 # pipeline as a "verification" sort of stage. For example, the
92 # pipeline should never end up writing to an output file in two
93 # places (I think). The pipeline should also never end up writing
94 # to an output file that is an input.
95 #
96 # This is intended to just be a "verify" step, not a functionality
97 # step. It should catch things like the driver driver not
98 # preventing -save-temps, but it shouldn't change behavior (so we
99 # can turn it off in Release-Asserts builds).
100
101 # Print in -### syntax.
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000102 hasHashHashHash = args.getLastArg(self.parser.hashHashHashOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000103 if hasHashHashHash:
104 self.claim(hasHashHashHash)
105 for j in jobs.iterjobs():
106 if isinstance(j, Jobs.Command):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000107 print '"%s"' % '" "'.join(j.render(argv))
Daniel Dunbara5677512009-01-05 19:53:30 +0000108 elif isinstance(j, Jobs.PipedJob):
109 for c in j.commands:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000110 print '"%s" %c' % ('" "'.join(c.render(argv)),
Daniel Dunbara5677512009-01-05 19:53:30 +0000111 "| "[c is j.commands[-1]])
112 elif not isinstance(j, JobList):
113 raise ValueError,'Encountered unknown job.'
114 sys.exit(0)
115
116 for j in jobs.iterjobs():
117 if isinstance(j, Jobs.Command):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000118 cmd_args = j.render(argv)
Daniel Dunbara5677512009-01-05 19:53:30 +0000119 res = os.spawnvp(os.P_WAIT, cmd_args[0], cmd_args)
120 if res:
121 sys.exit(res)
122 elif isinstance(j, Jobs.PipedJob):
123 raise NotImplementedError,"Piped jobs aren't implemented yet."
124 else:
125 raise ValueError,'Encountered unknown job.'
126
127 def claim(self, option):
128 # FIXME: Move to OptionList once introduced and implement.
129 pass
130
131 def warning(self, message):
132 print >>sys.stderr,'%s: %s' % (sys.argv[0], message)
133
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000134 def printOptions(self, args):
135 for i,arg in enumerate(args):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000136 if isinstance(arg, Arguments.MultipleValuesArg):
137 values = list(args.getValues(arg))
138 elif isinstance(arg, Arguments.ValueArg):
139 values = [args.getValue(arg)]
140 elif isinstance(arg, Arguments.JoinedAndSeparateValuesArg):
141 values = [args.getJoinedValue(arg), args.getSeparateValue(arg)]
Daniel Dunbara5677512009-01-05 19:53:30 +0000142 else:
143 values = []
Daniel Dunbar5039f212009-01-06 02:30:10 +0000144 print 'Option %d - Name: "%s", Values: {%s}' % (i, arg.opt.name,
Daniel Dunbara5677512009-01-05 19:53:30 +0000145 ', '.join(['"%s"' % v
146 for v in values]))
147
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000148 def printPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000149 def printPhase(p, f, steps, arch=None):
150 if p in steps:
151 return steps[p]
152 elif isinstance(p, Phases.BindArchAction):
153 for kid in p.inputs:
154 printPhase(kid, f, steps, p.arch)
155 steps[p] = len(steps)
156 return
157
158 if isinstance(p, Phases.InputAction):
159 phaseName = 'input'
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000160 inputStr = '"%s"' % args.getValue(p.filename)
Daniel Dunbara5677512009-01-05 19:53:30 +0000161 else:
162 phaseName = p.phase.name
163 inputs = [printPhase(i, f, steps, arch)
164 for i in p.inputs]
165 inputStr = '{%s}' % ', '.join(map(str, inputs))
166 if arch is not None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000167 phaseName += '-' + args.getValue(arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000168 steps[p] = index = len(steps)
169 print "%d: %s, %s, %s" % (index,phaseName,inputStr,p.type.name)
170 return index
171 steps = {}
172 for phase in phases:
173 printPhase(phase, sys.stdout, steps)
174
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000175 def handleImmediateOptions(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000176 # FIXME: Some driver Arguments are consumed right off the bat,
177 # like -dumpversion. Currently the gcc-dd handles these
178 # poorly, so we should be ok handling them upfront instead of
179 # after driver-driver level dispatching.
180 #
181 # FIXME: The actual order of these options in gcc is all over the
182 # place. The -dump ones seem to be first and in specification
183 # order, but there are other levels of precedence. For example,
184 # -print-search-dirs is evaluated before -print-prog-name=,
185 # regardless of order (and the last instance of -print-prog-name=
186 # wins verse itself).
187 #
188 # FIXME: Do we want to report "argument unused" type errors in the
189 # presence of things like -dumpmachine and -print-search-dirs?
190 # Probably not.
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000191 arg = args.getLastArg(self.parser.dumpmachineOption)
192 if arg:
193 print 'FIXME: %s' % arg.opt.name
194 sys.exit(1)
195
196 arg = args.getLastArg(self.parser.dumpspecsOption)
197 if arg:
198 print 'FIXME: %s' % arg.opt.name
199 sys.exit(1)
200
201 arg = args.getLastArg(self.parser.dumpversionOption)
202 if arg:
203 print 'FIXME: %s' % arg.opt.name
204 sys.exit(1)
205
206 arg = args.getLastArg(self.parser.printFileNameOption)
207 if arg:
208 print 'FIXME: %s' % arg.opt.name
209 sys.exit(1)
210
211 arg = args.getLastArg(self.parser.printMultiDirectoryOption)
212 if arg:
213 print 'FIXME: %s' % arg.opt.name
214 sys.exit(1)
215
216 arg = args.getLastArg(self.parser.printMultiLibOption)
217 if arg:
218 print 'FIXME: %s' % arg.opt.name
219 sys.exit(1)
220
221 arg = args.getLastArg(self.parser.printProgNameOption)
222 if arg:
223 print 'FIXME: %s' % arg.opt.name
224 sys.exit(1)
225
226 arg = args.getLastArg(self.parser.printLibgccFilenameOption)
227 if arg:
228 print 'FIXME: %s' % arg.opt.name
229 sys.exit(1)
230
231 arg = args.getLastArg(self.parser.printSearchDirsOption)
232 if arg:
233 print 'FIXME: %s' % arg.opt.name
234 sys.exit(1)
Daniel Dunbara5677512009-01-05 19:53:30 +0000235
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000236 def buildNormalPipeline(self, args):
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000237 hasCombine = args.getLastArg(self.parser.combineOption)
238 hasSyntaxOnly = args.getLastArg(self.parser.syntaxOnlyOption)
239 hasDashC = args.getLastArg(self.parser.cOption)
240 hasDashE = args.getLastArg(self.parser.EOption)
241 hasDashS = args.getLastArg(self.parser.SOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000242
243 inputType = None
244 inputTypeOpt = None
245 inputs = []
246 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000247 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000248 if inputType is None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000249 base,ext = os.path.splitext(args.getValue(a))
Daniel Dunbara5677512009-01-05 19:53:30 +0000250 if ext and ext in Types.kTypeSuffixMap:
251 klass = Types.kTypeSuffixMap[ext]
252 else:
253 # FIXME: Its not clear why we shouldn't just
254 # revert to unknown. I think this is more likely a
255 # bug / unintended behavior in gcc. Not very
256 # important though.
257 klass = Types.ObjectType
258 else:
259 assert inputTypeOpt is not None
260 self.claim(inputTypeOpt)
261 klass = inputType
262 inputs.append((klass, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000263 elif a.opt is self.parser.filelistOption:
264 # Treat as a linker input. Investigate how gcc is
265 # handling this.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000266 #
267 # FIXME: This might not be good enough. We may
268 # need to introduce another type for this case, so
269 # that other code which needs to know the inputs
270 # handles this properly. Best not to try and lipo
271 # this, for example.
272 inputs.append((Types.ObjectType, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000273 elif a.opt is self.parser.xOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000274 self.claim(a)
275 inputTypeOpt = a
276 value = args.getValue(a)
277 if value in Types.kTypeSpecifierMap:
278 inputType = Types.kTypeSpecifierMap[value]
279 else:
280 # FIXME: How are we going to handle diagnostics.
281 self.warning("language %s not recognized" % value)
Daniel Dunbara5677512009-01-05 19:53:30 +0000282
Daniel Dunbar5039f212009-01-06 02:30:10 +0000283 # FIXME: Its not clear why we shouldn't just
284 # revert to unknown. I think this is more likely a
285 # bug / unintended behavior in gcc. Not very
286 # important though.
287 inputType = ObjectType
Daniel Dunbara5677512009-01-05 19:53:30 +0000288
289 # We claim things here so that options for which we silently allow
290 # override only ever claim the used option.
291 if hasCombine:
292 self.claim(hasCombine)
293
294 finalPhase = Phases.Phase.eOrderPostAssemble
295 finalPhaseOpt = None
296
297 # Determine what compilation mode we are in.
298 if hasDashE:
299 finalPhase = Phases.Phase.eOrderPreprocess
300 finalPhaseOpt = hasDashE
301 elif hasSyntaxOnly:
302 finalPhase = Phases.Phase.eOrderCompile
303 finalPhaseOpt = hasSyntaxOnly
304 elif hasDashS:
305 finalPhase = Phases.Phase.eOrderCompile
306 finalPhaseOpt = hasDashS
307 elif hasDashC:
308 finalPhase = Phases.Phase.eOrderAssemble
309 finalPhaseOpt = hasDashC
310
311 if finalPhaseOpt:
312 self.claim(finalPhaseOpt)
313
314 # FIXME: Support -combine.
315 if hasCombine:
316 raise NotImplementedError,"-combine is not yet supported."
317
318 actions = []
319 linkerInputs = []
320 # FIXME: This is gross.
321 linkPhase = Phases.LinkPhase()
322 for klass,input in inputs:
323 # Figure out what step to start at.
324
325 # FIXME: This should be part of the input class probably?
326 # Altough it doesn't quite fit there either, things like
327 # asm-with-preprocess don't easily fit into a linear scheme.
328
329 # FIXME: I think we are going to end up wanting to just build
330 # a simple FSA which we run the inputs down.
331 sequence = []
332 if klass.preprocess:
333 sequence.append(Phases.PreprocessPhase())
334 if klass == Types.ObjectType:
335 sequence.append(linkPhase)
336 elif klass.onlyAssemble:
337 sequence.extend([Phases.AssemblePhase(),
338 linkPhase])
339 elif klass.onlyPrecompile:
340 sequence.append(Phases.PrecompilePhase())
341 else:
342 sequence.extend([Phases.CompilePhase(),
343 Phases.AssemblePhase(),
344 linkPhase])
345
346 if sequence[0].order > finalPhase:
347 assert finalPhaseOpt and finalPhaseOpt.opt
348 # FIXME: Explain what type of input file is. Or just match
349 # gcc warning.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000350 self.warning("%s: %s input file unused when %s is present" % (args.getValue(input),
Daniel Dunbara5677512009-01-05 19:53:30 +0000351 sequence[0].name,
352 finalPhaseOpt.opt.name))
353 else:
354 # Build the pipeline for this file.
355
356 current = Phases.InputAction(input, klass)
357 for transition in sequence:
358 # If the current action produces no output, or we are
359 # past what the user requested, we are done.
360 if (current.type is Types.NothingType or
361 transition.order > finalPhase):
362 break
363 else:
364 if isinstance(transition, Phases.PreprocessPhase):
365 assert isinstance(klass.preprocess, Types.InputType)
366 current = Phases.JobAction(transition,
367 [current],
368 klass.preprocess)
369 elif isinstance(transition, Phases.PrecompilePhase):
370 current = Phases.JobAction(transition,
371 [current],
372 Types.PCHType)
373 elif isinstance(transition, Phases.CompilePhase):
374 if hasSyntaxOnly:
375 output = Types.NothingType
376 else:
377 output = Types.AsmTypeNoPP
378 current = Phases.JobAction(transition,
379 [current],
380 output)
381 elif isinstance(transition, Phases.AssemblePhase):
382 current = Phases.JobAction(transition,
383 [current],
384 Types.ObjectType)
385 elif transition is linkPhase:
386 linkerInputs.append(current)
387 current = None
388 break
389 else:
390 raise RuntimeError,'Unrecognized transition: %s.' % transition
391 pass
392
393 if current is not None:
394 assert not isinstance(current, Phases.InputAction)
395 actions.append(current)
396
397 if linkerInputs:
398 actions.append(Phases.JobAction(linkPhase,
399 linkerInputs,
400 Types.ImageType))
401
402 return actions
403
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000404 def buildPipeline(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000405 # FIXME: We need to handle canonicalization of the specified arch.
406
407 archs = []
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000408 hasDashM = None
409 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
410 args.getLastArg(self.parser.saveTempsOption2))
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000411 for arg in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000412 if arg.opt is self.parser.archOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000413 archs.append(arg)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000414 elif arg.opt.name.startswith('-M'):
415 hasDashM = arg
Daniel Dunbara5677512009-01-05 19:53:30 +0000416
417 if not archs:
418 # FIXME: Need to infer arch so that we sub -Xarch
419 # correctly.
420 archs.append(Arguments.DerivedArg('i386'))
421
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000422 actions = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000423
424 # FIXME: Use custom exception for this.
425 #
426 # FIXME: We killed off some others but these aren't yet detected in
427 # a functional manner. If we added information to jobs about which
428 # "auxiliary" files they wrote then we could detect the conflict
429 # these cause downstream.
430 if len(archs) > 1:
431 if hasDashM:
432 raise ValueError,"Cannot use -M options with multiple arch flags."
433 elif hasSaveTemps:
434 raise ValueError,"Cannot use -save-temps with multiple arch flags."
435
436 # Execute once per arch.
437 finalActions = []
438 for p in actions:
439 # Make sure we can lipo this kind of output. If not (and it
440 # is an actual output) then we disallow, since we can't
441 # create an output file with the right name without
442 # overwriting it. We could remove this oddity by just
443 # changing the output names to include the arch, which would
444 # also fix -save-temps. Compatibility wins for now.
445 #
446 # FIXME: Is this error substantially less useful than
447 # gcc-dd's? The main problem is that "Cannot use compiler
448 # output with multiple arch flags" won't make sense to most
449 # developers.
450 if (len(archs) > 1 and
451 p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)):
452 raise ValueError,'Cannot use %s output with multiple arch flags.' % p.type.name
453
454 inputs = []
455 for arch in archs:
456 inputs.append(Phases.BindArchAction(p, arch))
457
458 # Lipo if necessary. We do it this way because we need to set
459 # the arch flag so that -Xarch_ gets rewritten.
460 if len(inputs) == 1 or p.type == Types.NothingType:
461 finalActions.extend(inputs)
462 else:
463 finalActions.append(Phases.JobAction(Phases.LipoPhase(),
464 inputs,
465 p.type))
466
467 # FIXME: We need to add -Wl,arch_multiple and -Wl,final_output in
468 # certain cases. This may be icky because we need to figure out the
469 # mode first. Current plan is to hack on the pipeline once it is built
470 # and we know what is being spit out. This avoids having to handling
471 # things like -c and -combine in multiple places.
472 #
473 # The annoying one of these is -Wl,final_output because it involves
474 # communication across different phases.
475 #
476 # Hopefully we can do this purely as part of the binding, but
477 # leaving comment here for now until it is clear this works.
478
479 return finalActions
480
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000481 def bindPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000482 jobs = Jobs.JobList()
483
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000484 finalOutput = args.getLastArg(self.parser.oOption)
485 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
486 args.getLastArg(self.parser.saveTempsOption2))
487 hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption)
488 hasPipe = args.getLastArg(self.parser.pipeOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000489 forward = []
490 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000491 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000492 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000493
494 # FIXME: Needs to be part of option.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000495 elif a.opt.name in ('-E', '-S', '-c',
496 '-arch', '-fsyntax-only', '-combine', '-x',
497 '-###'):
498 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000499
Daniel Dunbara5677512009-01-05 19:53:30 +0000500 else:
501 forward.append(a)
502
503 # We claim things here so that options for which we silently allow
504 # override only ever claim the used option.
505 if hasPipe:
506 self.claim(hasPipe)
507 # FIXME: Hack, override -pipe till we support it.
508 hasPipe = None
509 # Claim these here. Its not completely accurate but any warnings
510 # about these being unused are likely to be noise anyway.
511 if hasSaveTemps:
512 self.claim(hasSaveTemps)
513 if hasNoIntegratedCPP:
514 self.claim(hasNoIntegratedCPP)
515
516 toolMap = {
517 Phases.PreprocessPhase : Tools.GCC_PreprocessTool(),
518 Phases.CompilePhase : Tools.GCC_CompileTool(),
519 Phases.PrecompilePhase : Tools.GCC_PrecompileTool(),
520 Phases.AssemblePhase : Tools.DarwinAssemblerTool(),
521 Phases.LinkPhase : Tools.Collect2Tool(),
522 Phases.LipoPhase : Tools.LipoTool(),
523 }
524
525 class InputInfo:
526 def __init__(self, source, type, baseInput):
527 self.source = source
528 self.type = type
529 self.baseInput = baseInput
530
531 def __repr__(self):
532 return '%s(%r, %r, %r)' % (self.__class__.__name__,
533 self.source, self.type, self.baseInput)
534
535 def createJobs(phase, forwardArgs,
536 canAcceptPipe=False, atTopLevel=False, arch=None):
537 if isinstance(phase, Phases.InputAction):
538 return InputInfo(phase.filename, phase.type, phase.filename)
539 elif isinstance(phase, Phases.BindArchAction):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000540 archName = args.getValue(phase.arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000541 filteredArgs = []
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000542 for arg in forwardArgs:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000543 if arg.opt is self.parser.archOption:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000544 if arg is phase.arch:
545 filteredArgs.append(arg)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000546 elif arg.opt is self.parser.XarchOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000547 # FIXME: gcc-dd has another conditional for passing
548 # through, when the arch conditional array has an empty
549 # string. Why?
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000550 if args.getJoinedValue(arg) == archName:
Daniel Dunbara5677512009-01-05 19:53:30 +0000551 # FIXME: This is wrong, we don't want a
552 # DerivedArg we want an actual parsed version
553 # of this arg.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000554 filteredArgs.append(Arguments.DerivedArg(args.getSeparateValue(arg)))
Daniel Dunbara5677512009-01-05 19:53:30 +0000555 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000556 filteredArgs.append(arg)
Daniel Dunbara5677512009-01-05 19:53:30 +0000557
558 return createJobs(phase.inputs[0], filteredArgs,
559 canAcceptPipe, atTopLevel, phase.arch)
560
561 assert isinstance(phase, Phases.JobAction)
562 tool = toolMap[phase.phase.__class__]
563
564 # See if we should use an integrated CPP. We only use an
565 # integrated cpp when we have exactly one input, since this is
566 # the only use case we care about.
567 useIntegratedCPP = False
568 inputList = phase.inputs
569 if (not hasNoIntegratedCPP and
570 not hasSaveTemps and
571 tool.hasIntegratedCPP()):
572 if (len(phase.inputs) == 1 and
573 isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)):
574 useIntegratedCPP = True
575 inputList = phase.inputs[0].inputs
576
577 # Only try to use pipes when exactly one input.
578 canAcceptPipe = len(inputList) == 1 and tool.acceptsPipedInput()
579 inputs = [createJobs(p, forwardArgs, canAcceptPipe, False, arch) for p in inputList]
580
581 # Determine if we should output to a pipe.
582 canOutputToPipe = canAcceptPipe and tool.canPipeOutput()
583 outputToPipe = False
584 if canOutputToPipe:
585 # Some things default to writing to a pipe if the final
586 # phase and there was no user override.
587 #
588 # FIXME: What is the best way to handle this?
589 if (atTopLevel and
590 isinstance(phase, Phases.PreprocessPhase) and
591 not finalOutput):
592 outputToPipe = True
593 elif hasPipe:
594 outputToPipe = True
595
596 # Figure out where to put the job (pipes).
597 jobList = jobs
598 if canAcceptPipe and isinstance(inputs[0].source, Jobs.PipedJob):
599 jobList = inputs[0].source
600
601 # Figure out where to put the output.
602 baseInput = inputs[0].baseInput
603 if phase.type == Types.NothingType:
604 output = None
605 elif outputToPipe:
606 if isinstance(jobList, Jobs.PipedJob):
607 output = jobList
608 else:
609 jobList = output = Jobs.PipedJob([])
610 jobs.addJob(output)
611 else:
612 # Figure out what the derived output location would be.
613 #
614 # FIXME: gcc has some special case in here so that it doesn't
615 # create output files if they would conflict with an input.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000616 inputName = args.getValue(baseInput)
Daniel Dunbara5677512009-01-05 19:53:30 +0000617 if phase.type is Types.ImageType:
618 namedOutput = "a.out"
619 else:
620 base,_ = os.path.splitext(inputName)
621 assert phase.type.tempSuffix is not None
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000622 namedOutput = base + '.' + phase.type.tempSuffix
Daniel Dunbara5677512009-01-05 19:53:30 +0000623
624 # Output to user requested destination?
625 if atTopLevel and finalOutput:
626 output = finalOutput
627 # Contruct a named destination?
628 elif atTopLevel or hasSaveTemps:
629 output = Arguments.DerivedArg(namedOutput)
630 else:
631 # Output to temp file...
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000632 fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix)
Daniel Dunbara5677512009-01-05 19:53:30 +0000633 output = Arguments.DerivedArg(filename)
634
635 tool.constructJob(phase, arch, jobList, inputs, output, phase.type, forwardArgs)
636
637 return InputInfo(output, phase.type, baseInput)
638
639 # It is an error to provide a -o option if we are making multiple
640 # output files.
641 if finalOutput and len([a for a in phases if a.type is not Types.NothingType]) > 1:
642 # FIXME: Custom exception.
643 raise ValueError,"Cannot specify -o when generating multiple files."
644
645 for phase in phases:
646 createJobs(phase, forward, canAcceptPipe=True, atTopLevel=True)
647
648 return jobs