blob: 83a814eb4815b77bdd2667099985b1a7e2871b87 [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 Dunbardb439902009-01-07 18:40:45 +0000107 print '"%s"' % '" "'.join(j.getArgv())
Daniel Dunbara5677512009-01-05 19:53:30 +0000108 elif isinstance(j, Jobs.PipedJob):
109 for c in j.commands:
Daniel Dunbardb439902009-01-07 18:40:45 +0000110 print '"%s" %c' % ('" "'.join(c.getArgv()),
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 Dunbardb439902009-01-07 18:40:45 +0000118 res = os.spawnvp(os.P_WAIT, j.executable, j.getArgv())
Daniel Dunbara5677512009-01-05 19:53:30 +0000119 if res:
120 sys.exit(res)
121 elif isinstance(j, Jobs.PipedJob):
122 raise NotImplementedError,"Piped jobs aren't implemented yet."
123 else:
124 raise ValueError,'Encountered unknown job.'
125
126 def claim(self, option):
127 # FIXME: Move to OptionList once introduced and implement.
128 pass
129
130 def warning(self, message):
131 print >>sys.stderr,'%s: %s' % (sys.argv[0], message)
132
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000133 def printOptions(self, args):
134 for i,arg in enumerate(args):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000135 if isinstance(arg, Arguments.MultipleValuesArg):
136 values = list(args.getValues(arg))
137 elif isinstance(arg, Arguments.ValueArg):
138 values = [args.getValue(arg)]
139 elif isinstance(arg, Arguments.JoinedAndSeparateValuesArg):
140 values = [args.getJoinedValue(arg), args.getSeparateValue(arg)]
Daniel Dunbara5677512009-01-05 19:53:30 +0000141 else:
142 values = []
Daniel Dunbar5039f212009-01-06 02:30:10 +0000143 print 'Option %d - Name: "%s", Values: {%s}' % (i, arg.opt.name,
Daniel Dunbara5677512009-01-05 19:53:30 +0000144 ', '.join(['"%s"' % v
145 for v in values]))
146
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000147 def printPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000148 def printPhase(p, f, steps, arch=None):
149 if p in steps:
150 return steps[p]
151 elif isinstance(p, Phases.BindArchAction):
152 for kid in p.inputs:
153 printPhase(kid, f, steps, p.arch)
154 steps[p] = len(steps)
155 return
156
157 if isinstance(p, Phases.InputAction):
158 phaseName = 'input'
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000159 inputStr = '"%s"' % args.getValue(p.filename)
Daniel Dunbara5677512009-01-05 19:53:30 +0000160 else:
161 phaseName = p.phase.name
162 inputs = [printPhase(i, f, steps, arch)
163 for i in p.inputs]
164 inputStr = '{%s}' % ', '.join(map(str, inputs))
165 if arch is not None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000166 phaseName += '-' + args.getValue(arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000167 steps[p] = index = len(steps)
168 print "%d: %s, %s, %s" % (index,phaseName,inputStr,p.type.name)
169 return index
170 steps = {}
171 for phase in phases:
172 printPhase(phase, sys.stdout, steps)
173
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000174 def handleImmediateOptions(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000175 # FIXME: Some driver Arguments are consumed right off the bat,
176 # like -dumpversion. Currently the gcc-dd handles these
177 # poorly, so we should be ok handling them upfront instead of
178 # after driver-driver level dispatching.
179 #
180 # FIXME: The actual order of these options in gcc is all over the
181 # place. The -dump ones seem to be first and in specification
182 # order, but there are other levels of precedence. For example,
183 # -print-search-dirs is evaluated before -print-prog-name=,
184 # regardless of order (and the last instance of -print-prog-name=
185 # wins verse itself).
186 #
187 # FIXME: Do we want to report "argument unused" type errors in the
188 # presence of things like -dumpmachine and -print-search-dirs?
189 # Probably not.
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000190 arg = args.getLastArg(self.parser.dumpmachineOption)
191 if arg:
192 print 'FIXME: %s' % arg.opt.name
193 sys.exit(1)
194
195 arg = args.getLastArg(self.parser.dumpspecsOption)
196 if arg:
197 print 'FIXME: %s' % arg.opt.name
198 sys.exit(1)
199
200 arg = args.getLastArg(self.parser.dumpversionOption)
201 if arg:
202 print 'FIXME: %s' % arg.opt.name
203 sys.exit(1)
204
205 arg = args.getLastArg(self.parser.printFileNameOption)
206 if arg:
207 print 'FIXME: %s' % arg.opt.name
208 sys.exit(1)
209
210 arg = args.getLastArg(self.parser.printMultiDirectoryOption)
211 if arg:
212 print 'FIXME: %s' % arg.opt.name
213 sys.exit(1)
214
215 arg = args.getLastArg(self.parser.printMultiLibOption)
216 if arg:
217 print 'FIXME: %s' % arg.opt.name
218 sys.exit(1)
219
220 arg = args.getLastArg(self.parser.printProgNameOption)
221 if arg:
222 print 'FIXME: %s' % arg.opt.name
223 sys.exit(1)
224
225 arg = args.getLastArg(self.parser.printLibgccFilenameOption)
226 if arg:
227 print 'FIXME: %s' % arg.opt.name
228 sys.exit(1)
229
230 arg = args.getLastArg(self.parser.printSearchDirsOption)
231 if arg:
232 print 'FIXME: %s' % arg.opt.name
233 sys.exit(1)
Daniel Dunbara5677512009-01-05 19:53:30 +0000234
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000235 def buildNormalPipeline(self, args):
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000236 hasCombine = args.getLastArg(self.parser.combineOption)
237 hasSyntaxOnly = args.getLastArg(self.parser.syntaxOnlyOption)
238 hasDashC = args.getLastArg(self.parser.cOption)
239 hasDashE = args.getLastArg(self.parser.EOption)
240 hasDashS = args.getLastArg(self.parser.SOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000241
242 inputType = None
243 inputTypeOpt = None
244 inputs = []
245 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000246 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000247 if inputType is None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000248 base,ext = os.path.splitext(args.getValue(a))
Daniel Dunbara5677512009-01-05 19:53:30 +0000249 if ext and ext in Types.kTypeSuffixMap:
250 klass = Types.kTypeSuffixMap[ext]
251 else:
252 # FIXME: Its not clear why we shouldn't just
253 # revert to unknown. I think this is more likely a
254 # bug / unintended behavior in gcc. Not very
255 # important though.
256 klass = Types.ObjectType
257 else:
258 assert inputTypeOpt is not None
259 self.claim(inputTypeOpt)
260 klass = inputType
261 inputs.append((klass, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000262 elif a.opt is self.parser.filelistOption:
263 # Treat as a linker input. Investigate how gcc is
264 # handling this.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000265 #
266 # FIXME: This might not be good enough. We may
267 # need to introduce another type for this case, so
268 # that other code which needs to know the inputs
269 # handles this properly. Best not to try and lipo
270 # this, for example.
271 inputs.append((Types.ObjectType, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000272 elif a.opt is self.parser.xOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000273 self.claim(a)
274 inputTypeOpt = a
275 value = args.getValue(a)
276 if value in Types.kTypeSpecifierMap:
277 inputType = Types.kTypeSpecifierMap[value]
278 else:
279 # FIXME: How are we going to handle diagnostics.
280 self.warning("language %s not recognized" % value)
Daniel Dunbara5677512009-01-05 19:53:30 +0000281
Daniel Dunbar5039f212009-01-06 02:30:10 +0000282 # FIXME: Its not clear why we shouldn't just
283 # revert to unknown. I think this is more likely a
284 # bug / unintended behavior in gcc. Not very
285 # important though.
286 inputType = ObjectType
Daniel Dunbara5677512009-01-05 19:53:30 +0000287
288 # We claim things here so that options for which we silently allow
289 # override only ever claim the used option.
290 if hasCombine:
291 self.claim(hasCombine)
292
293 finalPhase = Phases.Phase.eOrderPostAssemble
294 finalPhaseOpt = None
295
296 # Determine what compilation mode we are in.
297 if hasDashE:
298 finalPhase = Phases.Phase.eOrderPreprocess
299 finalPhaseOpt = hasDashE
300 elif hasSyntaxOnly:
301 finalPhase = Phases.Phase.eOrderCompile
302 finalPhaseOpt = hasSyntaxOnly
303 elif hasDashS:
304 finalPhase = Phases.Phase.eOrderCompile
305 finalPhaseOpt = hasDashS
306 elif hasDashC:
307 finalPhase = Phases.Phase.eOrderAssemble
308 finalPhaseOpt = hasDashC
309
310 if finalPhaseOpt:
311 self.claim(finalPhaseOpt)
312
313 # FIXME: Support -combine.
314 if hasCombine:
315 raise NotImplementedError,"-combine is not yet supported."
316
317 actions = []
318 linkerInputs = []
319 # FIXME: This is gross.
320 linkPhase = Phases.LinkPhase()
321 for klass,input in inputs:
322 # Figure out what step to start at.
323
324 # FIXME: This should be part of the input class probably?
325 # Altough it doesn't quite fit there either, things like
326 # asm-with-preprocess don't easily fit into a linear scheme.
327
328 # FIXME: I think we are going to end up wanting to just build
329 # a simple FSA which we run the inputs down.
330 sequence = []
331 if klass.preprocess:
332 sequence.append(Phases.PreprocessPhase())
333 if klass == Types.ObjectType:
334 sequence.append(linkPhase)
335 elif klass.onlyAssemble:
336 sequence.extend([Phases.AssemblePhase(),
337 linkPhase])
338 elif klass.onlyPrecompile:
339 sequence.append(Phases.PrecompilePhase())
340 else:
341 sequence.extend([Phases.CompilePhase(),
342 Phases.AssemblePhase(),
343 linkPhase])
344
345 if sequence[0].order > finalPhase:
346 assert finalPhaseOpt and finalPhaseOpt.opt
347 # FIXME: Explain what type of input file is. Or just match
348 # gcc warning.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000349 self.warning("%s: %s input file unused when %s is present" % (args.getValue(input),
Daniel Dunbara5677512009-01-05 19:53:30 +0000350 sequence[0].name,
351 finalPhaseOpt.opt.name))
352 else:
353 # Build the pipeline for this file.
354
355 current = Phases.InputAction(input, klass)
356 for transition in sequence:
357 # If the current action produces no output, or we are
358 # past what the user requested, we are done.
359 if (current.type is Types.NothingType or
360 transition.order > finalPhase):
361 break
362 else:
363 if isinstance(transition, Phases.PreprocessPhase):
364 assert isinstance(klass.preprocess, Types.InputType)
365 current = Phases.JobAction(transition,
366 [current],
367 klass.preprocess)
368 elif isinstance(transition, Phases.PrecompilePhase):
369 current = Phases.JobAction(transition,
370 [current],
371 Types.PCHType)
372 elif isinstance(transition, Phases.CompilePhase):
373 if hasSyntaxOnly:
374 output = Types.NothingType
375 else:
376 output = Types.AsmTypeNoPP
377 current = Phases.JobAction(transition,
378 [current],
379 output)
380 elif isinstance(transition, Phases.AssemblePhase):
381 current = Phases.JobAction(transition,
382 [current],
383 Types.ObjectType)
384 elif transition is linkPhase:
385 linkerInputs.append(current)
386 current = None
387 break
388 else:
389 raise RuntimeError,'Unrecognized transition: %s.' % transition
390 pass
391
392 if current is not None:
393 assert not isinstance(current, Phases.InputAction)
394 actions.append(current)
395
396 if linkerInputs:
397 actions.append(Phases.JobAction(linkPhase,
398 linkerInputs,
399 Types.ImageType))
400
401 return actions
402
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000403 def buildPipeline(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000404 # FIXME: We need to handle canonicalization of the specified arch.
405
406 archs = []
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000407 hasDashM = None
408 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
409 args.getLastArg(self.parser.saveTempsOption2))
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000410 for arg in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000411 if arg.opt is self.parser.archOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000412 archs.append(arg)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000413 elif arg.opt.name.startswith('-M'):
414 hasDashM = arg
Daniel Dunbara5677512009-01-05 19:53:30 +0000415
416 if not archs:
417 # FIXME: Need to infer arch so that we sub -Xarch
418 # correctly.
419 archs.append(Arguments.DerivedArg('i386'))
420
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000421 actions = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000422
423 # FIXME: Use custom exception for this.
424 #
425 # FIXME: We killed off some others but these aren't yet detected in
426 # a functional manner. If we added information to jobs about which
427 # "auxiliary" files they wrote then we could detect the conflict
428 # these cause downstream.
429 if len(archs) > 1:
430 if hasDashM:
431 raise ValueError,"Cannot use -M options with multiple arch flags."
432 elif hasSaveTemps:
433 raise ValueError,"Cannot use -save-temps with multiple arch flags."
434
435 # Execute once per arch.
436 finalActions = []
437 for p in actions:
438 # Make sure we can lipo this kind of output. If not (and it
439 # is an actual output) then we disallow, since we can't
440 # create an output file with the right name without
441 # overwriting it. We could remove this oddity by just
442 # changing the output names to include the arch, which would
443 # also fix -save-temps. Compatibility wins for now.
444 #
445 # FIXME: Is this error substantially less useful than
446 # gcc-dd's? The main problem is that "Cannot use compiler
447 # output with multiple arch flags" won't make sense to most
448 # developers.
449 if (len(archs) > 1 and
450 p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)):
451 raise ValueError,'Cannot use %s output with multiple arch flags.' % p.type.name
452
453 inputs = []
454 for arch in archs:
455 inputs.append(Phases.BindArchAction(p, arch))
456
457 # Lipo if necessary. We do it this way because we need to set
458 # the arch flag so that -Xarch_ gets rewritten.
459 if len(inputs) == 1 or p.type == Types.NothingType:
460 finalActions.extend(inputs)
461 else:
462 finalActions.append(Phases.JobAction(Phases.LipoPhase(),
463 inputs,
464 p.type))
465
466 # FIXME: We need to add -Wl,arch_multiple and -Wl,final_output in
467 # certain cases. This may be icky because we need to figure out the
468 # mode first. Current plan is to hack on the pipeline once it is built
469 # and we know what is being spit out. This avoids having to handling
470 # things like -c and -combine in multiple places.
471 #
472 # The annoying one of these is -Wl,final_output because it involves
473 # communication across different phases.
474 #
475 # Hopefully we can do this purely as part of the binding, but
476 # leaving comment here for now until it is clear this works.
477
478 return finalActions
479
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000480 def bindPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000481 jobs = Jobs.JobList()
482
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000483 finalOutput = args.getLastArg(self.parser.oOption)
484 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
485 args.getLastArg(self.parser.saveTempsOption2))
486 hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption)
487 hasPipe = args.getLastArg(self.parser.pipeOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000488 forward = []
489 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000490 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000491 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000492
493 # FIXME: Needs to be part of option.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000494 elif a.opt.name in ('-E', '-S', '-c',
495 '-arch', '-fsyntax-only', '-combine', '-x',
496 '-###'):
497 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000498
Daniel Dunbara5677512009-01-05 19:53:30 +0000499 else:
500 forward.append(a)
501
502 # We claim things here so that options for which we silently allow
503 # override only ever claim the used option.
504 if hasPipe:
505 self.claim(hasPipe)
506 # FIXME: Hack, override -pipe till we support it.
507 hasPipe = None
508 # Claim these here. Its not completely accurate but any warnings
509 # about these being unused are likely to be noise anyway.
510 if hasSaveTemps:
511 self.claim(hasSaveTemps)
512 if hasNoIntegratedCPP:
513 self.claim(hasNoIntegratedCPP)
514
515 toolMap = {
516 Phases.PreprocessPhase : Tools.GCC_PreprocessTool(),
517 Phases.CompilePhase : Tools.GCC_CompileTool(),
518 Phases.PrecompilePhase : Tools.GCC_PrecompileTool(),
519 Phases.AssemblePhase : Tools.DarwinAssemblerTool(),
520 Phases.LinkPhase : Tools.Collect2Tool(),
521 Phases.LipoPhase : Tools.LipoTool(),
522 }
523
524 class InputInfo:
525 def __init__(self, source, type, baseInput):
526 self.source = source
527 self.type = type
528 self.baseInput = baseInput
529
530 def __repr__(self):
531 return '%s(%r, %r, %r)' % (self.__class__.__name__,
532 self.source, self.type, self.baseInput)
533
534 def createJobs(phase, forwardArgs,
535 canAcceptPipe=False, atTopLevel=False, arch=None):
536 if isinstance(phase, Phases.InputAction):
537 return InputInfo(phase.filename, phase.type, phase.filename)
538 elif isinstance(phase, Phases.BindArchAction):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000539 archName = args.getValue(phase.arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000540 filteredArgs = []
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000541 for arg in forwardArgs:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000542 if arg.opt is self.parser.archOption:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000543 if arg is phase.arch:
544 filteredArgs.append(arg)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000545 elif arg.opt is self.parser.XarchOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000546 # FIXME: gcc-dd has another conditional for passing
547 # through, when the arch conditional array has an empty
548 # string. Why?
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000549 if args.getJoinedValue(arg) == archName:
Daniel Dunbara5677512009-01-05 19:53:30 +0000550 # FIXME: This is wrong, we don't want a
551 # DerivedArg we want an actual parsed version
552 # of this arg.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000553 filteredArgs.append(Arguments.DerivedArg(args.getSeparateValue(arg)))
Daniel Dunbara5677512009-01-05 19:53:30 +0000554 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000555 filteredArgs.append(arg)
Daniel Dunbara5677512009-01-05 19:53:30 +0000556
557 return createJobs(phase.inputs[0], filteredArgs,
558 canAcceptPipe, atTopLevel, phase.arch)
559
560 assert isinstance(phase, Phases.JobAction)
561 tool = toolMap[phase.phase.__class__]
562
563 # See if we should use an integrated CPP. We only use an
564 # integrated cpp when we have exactly one input, since this is
565 # the only use case we care about.
566 useIntegratedCPP = False
567 inputList = phase.inputs
568 if (not hasNoIntegratedCPP and
569 not hasSaveTemps and
570 tool.hasIntegratedCPP()):
571 if (len(phase.inputs) == 1 and
572 isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)):
573 useIntegratedCPP = True
574 inputList = phase.inputs[0].inputs
575
576 # Only try to use pipes when exactly one input.
577 canAcceptPipe = len(inputList) == 1 and tool.acceptsPipedInput()
578 inputs = [createJobs(p, forwardArgs, canAcceptPipe, False, arch) for p in inputList]
579
580 # Determine if we should output to a pipe.
581 canOutputToPipe = canAcceptPipe and tool.canPipeOutput()
582 outputToPipe = False
583 if canOutputToPipe:
584 # Some things default to writing to a pipe if the final
585 # phase and there was no user override.
586 #
587 # FIXME: What is the best way to handle this?
588 if (atTopLevel and
589 isinstance(phase, Phases.PreprocessPhase) and
590 not finalOutput):
591 outputToPipe = True
592 elif hasPipe:
593 outputToPipe = True
594
595 # Figure out where to put the job (pipes).
596 jobList = jobs
597 if canAcceptPipe and isinstance(inputs[0].source, Jobs.PipedJob):
598 jobList = inputs[0].source
599
600 # Figure out where to put the output.
601 baseInput = inputs[0].baseInput
602 if phase.type == Types.NothingType:
603 output = None
604 elif outputToPipe:
605 if isinstance(jobList, Jobs.PipedJob):
606 output = jobList
607 else:
608 jobList = output = Jobs.PipedJob([])
609 jobs.addJob(output)
610 else:
611 # Figure out what the derived output location would be.
612 #
613 # FIXME: gcc has some special case in here so that it doesn't
614 # create output files if they would conflict with an input.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000615 inputName = args.getValue(baseInput)
Daniel Dunbara5677512009-01-05 19:53:30 +0000616 if phase.type is Types.ImageType:
617 namedOutput = "a.out"
618 else:
619 base,_ = os.path.splitext(inputName)
620 assert phase.type.tempSuffix is not None
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000621 namedOutput = base + '.' + phase.type.tempSuffix
Daniel Dunbara5677512009-01-05 19:53:30 +0000622
623 # Output to user requested destination?
624 if atTopLevel and finalOutput:
625 output = finalOutput
626 # Contruct a named destination?
627 elif atTopLevel or hasSaveTemps:
628 output = Arguments.DerivedArg(namedOutput)
629 else:
630 # Output to temp file...
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000631 fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix)
Daniel Dunbara5677512009-01-05 19:53:30 +0000632 output = Arguments.DerivedArg(filename)
633
Daniel Dunbardb439902009-01-07 18:40:45 +0000634 tool.constructJob(phase, arch, jobList, inputs, output, phase.type,
635 forwardArgs, args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000636
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