blob: 53a4c22dc262b514bddf24000ae347501e552217 [file] [log] [blame]
Daniel Dunbara5677512009-01-05 19:53:30 +00001import os
Daniel Dunbar9066af82009-01-09 01:00:40 +00002import platform
Daniel Dunbara5677512009-01-05 19:53:30 +00003import sys
4import tempfile
5from pprint import pprint
6
7###
8
9import Arguments
10import Jobs
Daniel Dunbar9066af82009-01-09 01:00:40 +000011import HostInfo
Daniel Dunbara5677512009-01-05 19:53:30 +000012import Phases
13import Tools
14import Types
15import Util
16
17# FIXME: Clean up naming of options and arguments. Decide whether to
18# rename Option and be consistent about use of Option/Arg.
19
20####
21
22class MissingArgumentError(ValueError):
23 """MissingArgumentError - An option required an argument but none
24 was given."""
25
26###
27
28class Driver(object):
29 def __init__(self):
Daniel Dunbar9066af82009-01-09 01:00:40 +000030 self.hostInfo = None
Daniel Dunbarba6e3232009-01-06 06:12:13 +000031 self.parser = Arguments.OptionParser()
Daniel Dunbara5677512009-01-05 19:53:30 +000032
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000033 # Host queries which can be forcibly over-riden by the user for
34 # testing purposes.
35 #
36 # FIXME: We should make sure these are drawn from a fixed set so
37 # that nothing downstream ever plays a guessing game.
38
39 def getHostBits(self):
40 if self.cccHostBits:
41 return self.cccHostBits
42
43 return platform.architecture()[0].replace('bit','')
44
45 def getHostMachine(self):
46 if self.cccHostMachine:
47 return self.cccHostMachine
48
49 machine = platform.machine()
50 # Normalize names.
51 if machine == 'Power Macintosh':
52 return 'ppc'
53 return machine
54
55 def getHostSystemName(self):
56 if self.cccHostSystem:
57 return self.cccHostSystem
58
59 return platform.system().lower()
60
61 ###
62
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000063 def run(self, argv):
Daniel Dunbara5677512009-01-05 19:53:30 +000064 # FIXME: Things to support from environment: GCC_EXEC_PREFIX,
65 # COMPILER_PATH, LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS,
66 # QA_OVERRIDE_GCC3_OPTIONS, ...?
67
68 # FIXME: -V and -b processing
69
70 # Handle some special -ccc- options used for testing which are
71 # only allowed at the beginning of the command line.
72 cccPrintOptions = False
73 cccPrintPhases = False
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000074
75 # FIXME: How to handle override of host? ccc specific options?
76 # Abuse -b?
77 self.cccHostBits = self.cccHostMachine = self.cccHostSystem = None
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000078 while argv and argv[0].startswith('-ccc-'):
79 opt,argv = argv[0][5:],argv[1:]
Daniel Dunbara5677512009-01-05 19:53:30 +000080
81 if opt == 'print-options':
82 cccPrintOptions = True
83 elif opt == 'print-phases':
84 cccPrintPhases = True
Daniel Dunbar9066af82009-01-09 01:00:40 +000085 elif opt == 'host-bits':
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000086 self.cccHostBits,argv = argv[0],argv[1:]
Daniel Dunbar9066af82009-01-09 01:00:40 +000087 elif opt == 'host-machine':
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000088 self.cccHostMachine,argv = argv[0],argv[1:]
Daniel Dunbar9066af82009-01-09 01:00:40 +000089 elif opt == 'host-system':
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000090 self.cccHostSystem,argv = argv[0],argv[1:]
Daniel Dunbara5677512009-01-05 19:53:30 +000091 else:
92 raise ValueError,"Invalid ccc option: %r" % cccPrintOptions
93
Daniel Dunbara75ea3d2009-01-09 22:21:24 +000094 self.hostInfo = HostInfo.getHostInfo(self)
Daniel Dunbar43124722009-01-10 02:07:54 +000095 self.toolChain = self.hostInfo.getToolChain()
Daniel Dunbar9066af82009-01-09 01:00:40 +000096
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +000097 args = self.parser.parseArgs(argv)
Daniel Dunbara5677512009-01-05 19:53:30 +000098
99 # FIXME: Ho hum I have just realized -Xarch_ is broken. We really
100 # need to reparse the Arguments after they have been expanded by
101 # -Xarch. How is this going to work?
102 #
103 # Scratch that, we aren't going to do that; it really disrupts the
104 # organization, doesn't consistently work with gcc-dd, and is
105 # confusing. Instead we are going to enforce that -Xarch_ is only
106 # used with options which do not alter the driver behavior. Let's
107 # hope this is ok, because the current architecture is a little
108 # tied to it.
109
110 if cccPrintOptions:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000111 self.printOptions(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000112 sys.exit(0)
113
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000114 self.handleImmediateOptions(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000115
Daniel Dunbar9066af82009-01-09 01:00:40 +0000116 if self.hostInfo.useDriverDriver():
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000117 phases = self.buildPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000118 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000119 phases = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000120
121 if cccPrintPhases:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000122 self.printPhases(phases, args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000123 sys.exit(0)
Daniel Dunbar9066af82009-01-09 01:00:40 +0000124
Daniel Dunbara5677512009-01-05 19:53:30 +0000125 if 0:
126 print Util.pprint(phases)
127
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000128 jobs = self.bindPhases(phases, args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000129
130 # FIXME: We should provide some basic sanity checking of the
131 # pipeline as a "verification" sort of stage. For example, the
132 # pipeline should never end up writing to an output file in two
133 # places (I think). The pipeline should also never end up writing
134 # to an output file that is an input.
135 #
136 # This is intended to just be a "verify" step, not a functionality
137 # step. It should catch things like the driver driver not
138 # preventing -save-temps, but it shouldn't change behavior (so we
139 # can turn it off in Release-Asserts builds).
140
141 # Print in -### syntax.
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000142 hasHashHashHash = args.getLastArg(self.parser.hashHashHashOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000143 if hasHashHashHash:
144 self.claim(hasHashHashHash)
145 for j in jobs.iterjobs():
146 if isinstance(j, Jobs.Command):
Daniel Dunbardb439902009-01-07 18:40:45 +0000147 print '"%s"' % '" "'.join(j.getArgv())
Daniel Dunbara5677512009-01-05 19:53:30 +0000148 elif isinstance(j, Jobs.PipedJob):
149 for c in j.commands:
Daniel Dunbardb439902009-01-07 18:40:45 +0000150 print '"%s" %c' % ('" "'.join(c.getArgv()),
Daniel Dunbara5677512009-01-05 19:53:30 +0000151 "| "[c is j.commands[-1]])
152 elif not isinstance(j, JobList):
153 raise ValueError,'Encountered unknown job.'
154 sys.exit(0)
155
156 for j in jobs.iterjobs():
157 if isinstance(j, Jobs.Command):
Daniel Dunbardb439902009-01-07 18:40:45 +0000158 res = os.spawnvp(os.P_WAIT, j.executable, j.getArgv())
Daniel Dunbara5677512009-01-05 19:53:30 +0000159 if res:
160 sys.exit(res)
161 elif isinstance(j, Jobs.PipedJob):
162 raise NotImplementedError,"Piped jobs aren't implemented yet."
163 else:
164 raise ValueError,'Encountered unknown job.'
165
166 def claim(self, option):
167 # FIXME: Move to OptionList once introduced and implement.
168 pass
169
170 def warning(self, message):
171 print >>sys.stderr,'%s: %s' % (sys.argv[0], message)
172
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000173 def printOptions(self, args):
174 for i,arg in enumerate(args):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000175 if isinstance(arg, Arguments.MultipleValuesArg):
176 values = list(args.getValues(arg))
177 elif isinstance(arg, Arguments.ValueArg):
178 values = [args.getValue(arg)]
179 elif isinstance(arg, Arguments.JoinedAndSeparateValuesArg):
180 values = [args.getJoinedValue(arg), args.getSeparateValue(arg)]
Daniel Dunbara5677512009-01-05 19:53:30 +0000181 else:
182 values = []
Daniel Dunbar5039f212009-01-06 02:30:10 +0000183 print 'Option %d - Name: "%s", Values: {%s}' % (i, arg.opt.name,
Daniel Dunbara5677512009-01-05 19:53:30 +0000184 ', '.join(['"%s"' % v
185 for v in values]))
186
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000187 def printPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000188 def printPhase(p, f, steps, arch=None):
189 if p in steps:
190 return steps[p]
191 elif isinstance(p, Phases.BindArchAction):
192 for kid in p.inputs:
193 printPhase(kid, f, steps, p.arch)
194 steps[p] = len(steps)
195 return
196
197 if isinstance(p, Phases.InputAction):
198 phaseName = 'input'
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000199 inputStr = '"%s"' % args.getValue(p.filename)
Daniel Dunbara5677512009-01-05 19:53:30 +0000200 else:
201 phaseName = p.phase.name
202 inputs = [printPhase(i, f, steps, arch)
203 for i in p.inputs]
204 inputStr = '{%s}' % ', '.join(map(str, inputs))
205 if arch is not None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000206 phaseName += '-' + args.getValue(arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000207 steps[p] = index = len(steps)
208 print "%d: %s, %s, %s" % (index,phaseName,inputStr,p.type.name)
209 return index
210 steps = {}
211 for phase in phases:
212 printPhase(phase, sys.stdout, steps)
213
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000214 def handleImmediateOptions(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000215 # FIXME: Some driver Arguments are consumed right off the bat,
216 # like -dumpversion. Currently the gcc-dd handles these
217 # poorly, so we should be ok handling them upfront instead of
218 # after driver-driver level dispatching.
219 #
220 # FIXME: The actual order of these options in gcc is all over the
221 # place. The -dump ones seem to be first and in specification
222 # order, but there are other levels of precedence. For example,
223 # -print-search-dirs is evaluated before -print-prog-name=,
224 # regardless of order (and the last instance of -print-prog-name=
225 # wins verse itself).
226 #
227 # FIXME: Do we want to report "argument unused" type errors in the
228 # presence of things like -dumpmachine and -print-search-dirs?
229 # Probably not.
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000230 arg = args.getLastArg(self.parser.dumpmachineOption)
231 if arg:
232 print 'FIXME: %s' % arg.opt.name
233 sys.exit(1)
234
235 arg = args.getLastArg(self.parser.dumpspecsOption)
236 if arg:
237 print 'FIXME: %s' % arg.opt.name
238 sys.exit(1)
239
240 arg = args.getLastArg(self.parser.dumpversionOption)
241 if arg:
242 print 'FIXME: %s' % arg.opt.name
243 sys.exit(1)
244
245 arg = args.getLastArg(self.parser.printFileNameOption)
246 if arg:
247 print 'FIXME: %s' % arg.opt.name
248 sys.exit(1)
249
250 arg = args.getLastArg(self.parser.printMultiDirectoryOption)
251 if arg:
252 print 'FIXME: %s' % arg.opt.name
253 sys.exit(1)
254
255 arg = args.getLastArg(self.parser.printMultiLibOption)
256 if arg:
257 print 'FIXME: %s' % arg.opt.name
258 sys.exit(1)
259
260 arg = args.getLastArg(self.parser.printProgNameOption)
261 if arg:
262 print 'FIXME: %s' % arg.opt.name
263 sys.exit(1)
264
265 arg = args.getLastArg(self.parser.printLibgccFilenameOption)
266 if arg:
267 print 'FIXME: %s' % arg.opt.name
268 sys.exit(1)
269
270 arg = args.getLastArg(self.parser.printSearchDirsOption)
271 if arg:
272 print 'FIXME: %s' % arg.opt.name
273 sys.exit(1)
Daniel Dunbara5677512009-01-05 19:53:30 +0000274
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000275 def buildNormalPipeline(self, args):
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000276 hasCombine = args.getLastArg(self.parser.combineOption)
277 hasSyntaxOnly = args.getLastArg(self.parser.syntaxOnlyOption)
278 hasDashC = args.getLastArg(self.parser.cOption)
279 hasDashE = args.getLastArg(self.parser.EOption)
280 hasDashS = args.getLastArg(self.parser.SOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000281
282 inputType = None
283 inputTypeOpt = None
284 inputs = []
285 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000286 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000287 if inputType is None:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000288 base,ext = os.path.splitext(args.getValue(a))
Daniel Dunbara5677512009-01-05 19:53:30 +0000289 if ext and ext in Types.kTypeSuffixMap:
290 klass = Types.kTypeSuffixMap[ext]
291 else:
292 # FIXME: Its not clear why we shouldn't just
293 # revert to unknown. I think this is more likely a
294 # bug / unintended behavior in gcc. Not very
295 # important though.
296 klass = Types.ObjectType
297 else:
298 assert inputTypeOpt is not None
299 self.claim(inputTypeOpt)
300 klass = inputType
301 inputs.append((klass, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000302 elif a.opt is self.parser.filelistOption:
303 # Treat as a linker input. Investigate how gcc is
304 # handling this.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000305 #
306 # FIXME: This might not be good enough. We may
307 # need to introduce another type for this case, so
308 # that other code which needs to know the inputs
309 # handles this properly. Best not to try and lipo
310 # this, for example.
311 inputs.append((Types.ObjectType, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000312 elif a.opt is self.parser.xOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000313 self.claim(a)
314 inputTypeOpt = a
315 value = args.getValue(a)
316 if value in Types.kTypeSpecifierMap:
317 inputType = Types.kTypeSpecifierMap[value]
318 else:
319 # FIXME: How are we going to handle diagnostics.
320 self.warning("language %s not recognized" % value)
Daniel Dunbara5677512009-01-05 19:53:30 +0000321
Daniel Dunbar5039f212009-01-06 02:30:10 +0000322 # FIXME: Its not clear why we shouldn't just
323 # revert to unknown. I think this is more likely a
324 # bug / unintended behavior in gcc. Not very
325 # important though.
326 inputType = ObjectType
Daniel Dunbara5677512009-01-05 19:53:30 +0000327
328 # We claim things here so that options for which we silently allow
329 # override only ever claim the used option.
330 if hasCombine:
331 self.claim(hasCombine)
332
333 finalPhase = Phases.Phase.eOrderPostAssemble
334 finalPhaseOpt = None
335
336 # Determine what compilation mode we are in.
337 if hasDashE:
338 finalPhase = Phases.Phase.eOrderPreprocess
339 finalPhaseOpt = hasDashE
340 elif hasSyntaxOnly:
341 finalPhase = Phases.Phase.eOrderCompile
342 finalPhaseOpt = hasSyntaxOnly
343 elif hasDashS:
344 finalPhase = Phases.Phase.eOrderCompile
345 finalPhaseOpt = hasDashS
346 elif hasDashC:
347 finalPhase = Phases.Phase.eOrderAssemble
348 finalPhaseOpt = hasDashC
349
350 if finalPhaseOpt:
351 self.claim(finalPhaseOpt)
352
353 # FIXME: Support -combine.
354 if hasCombine:
355 raise NotImplementedError,"-combine is not yet supported."
356
357 actions = []
358 linkerInputs = []
359 # FIXME: This is gross.
360 linkPhase = Phases.LinkPhase()
361 for klass,input in inputs:
362 # Figure out what step to start at.
363
364 # FIXME: This should be part of the input class probably?
365 # Altough it doesn't quite fit there either, things like
366 # asm-with-preprocess don't easily fit into a linear scheme.
367
368 # FIXME: I think we are going to end up wanting to just build
369 # a simple FSA which we run the inputs down.
370 sequence = []
371 if klass.preprocess:
372 sequence.append(Phases.PreprocessPhase())
373 if klass == Types.ObjectType:
374 sequence.append(linkPhase)
375 elif klass.onlyAssemble:
376 sequence.extend([Phases.AssemblePhase(),
377 linkPhase])
378 elif klass.onlyPrecompile:
379 sequence.append(Phases.PrecompilePhase())
380 else:
381 sequence.extend([Phases.CompilePhase(),
382 Phases.AssemblePhase(),
383 linkPhase])
384
385 if sequence[0].order > finalPhase:
386 assert finalPhaseOpt and finalPhaseOpt.opt
387 # FIXME: Explain what type of input file is. Or just match
388 # gcc warning.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000389 self.warning("%s: %s input file unused when %s is present" % (args.getValue(input),
Daniel Dunbara5677512009-01-05 19:53:30 +0000390 sequence[0].name,
391 finalPhaseOpt.opt.name))
392 else:
393 # Build the pipeline for this file.
394
395 current = Phases.InputAction(input, klass)
396 for transition in sequence:
397 # If the current action produces no output, or we are
398 # past what the user requested, we are done.
399 if (current.type is Types.NothingType or
400 transition.order > finalPhase):
401 break
402 else:
403 if isinstance(transition, Phases.PreprocessPhase):
404 assert isinstance(klass.preprocess, Types.InputType)
405 current = Phases.JobAction(transition,
406 [current],
407 klass.preprocess)
408 elif isinstance(transition, Phases.PrecompilePhase):
409 current = Phases.JobAction(transition,
410 [current],
411 Types.PCHType)
412 elif isinstance(transition, Phases.CompilePhase):
413 if hasSyntaxOnly:
414 output = Types.NothingType
415 else:
416 output = Types.AsmTypeNoPP
417 current = Phases.JobAction(transition,
418 [current],
419 output)
420 elif isinstance(transition, Phases.AssemblePhase):
421 current = Phases.JobAction(transition,
422 [current],
423 Types.ObjectType)
424 elif transition is linkPhase:
425 linkerInputs.append(current)
426 current = None
427 break
428 else:
429 raise RuntimeError,'Unrecognized transition: %s.' % transition
430 pass
431
432 if current is not None:
433 assert not isinstance(current, Phases.InputAction)
434 actions.append(current)
435
436 if linkerInputs:
437 actions.append(Phases.JobAction(linkPhase,
438 linkerInputs,
439 Types.ImageType))
440
441 return actions
442
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000443 def buildPipeline(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000444 # FIXME: We need to handle canonicalization of the specified arch.
445
446 archs = []
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000447 hasDashM = None
448 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
449 args.getLastArg(self.parser.saveTempsOption2))
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000450 for arg in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000451 if arg.opt is self.parser.archOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000452 archs.append(arg)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000453 elif arg.opt.name.startswith('-M'):
454 hasDashM = arg
Daniel Dunbara5677512009-01-05 19:53:30 +0000455
456 if not archs:
Daniel Dunbar9066af82009-01-09 01:00:40 +0000457 archs.append(args.makeSeparateArg(self.hostInfo.getArchName(),
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000458 self.parser.archOption))
Daniel Dunbara5677512009-01-05 19:53:30 +0000459
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000460 actions = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000461
462 # FIXME: Use custom exception for this.
463 #
464 # FIXME: We killed off some others but these aren't yet detected in
465 # a functional manner. If we added information to jobs about which
466 # "auxiliary" files they wrote then we could detect the conflict
467 # these cause downstream.
468 if len(archs) > 1:
469 if hasDashM:
470 raise ValueError,"Cannot use -M options with multiple arch flags."
471 elif hasSaveTemps:
472 raise ValueError,"Cannot use -save-temps with multiple arch flags."
473
474 # Execute once per arch.
475 finalActions = []
476 for p in actions:
477 # Make sure we can lipo this kind of output. If not (and it
478 # is an actual output) then we disallow, since we can't
479 # create an output file with the right name without
480 # overwriting it. We could remove this oddity by just
481 # changing the output names to include the arch, which would
482 # also fix -save-temps. Compatibility wins for now.
483 #
484 # FIXME: Is this error substantially less useful than
485 # gcc-dd's? The main problem is that "Cannot use compiler
486 # output with multiple arch flags" won't make sense to most
487 # developers.
488 if (len(archs) > 1 and
489 p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)):
490 raise ValueError,'Cannot use %s output with multiple arch flags.' % p.type.name
491
492 inputs = []
493 for arch in archs:
494 inputs.append(Phases.BindArchAction(p, arch))
495
496 # Lipo if necessary. We do it this way because we need to set
497 # the arch flag so that -Xarch_ gets rewritten.
498 if len(inputs) == 1 or p.type == Types.NothingType:
499 finalActions.extend(inputs)
500 else:
501 finalActions.append(Phases.JobAction(Phases.LipoPhase(),
502 inputs,
503 p.type))
504
505 # FIXME: We need to add -Wl,arch_multiple and -Wl,final_output in
506 # certain cases. This may be icky because we need to figure out the
507 # mode first. Current plan is to hack on the pipeline once it is built
508 # and we know what is being spit out. This avoids having to handling
509 # things like -c and -combine in multiple places.
510 #
511 # The annoying one of these is -Wl,final_output because it involves
512 # communication across different phases.
513 #
514 # Hopefully we can do this purely as part of the binding, but
515 # leaving comment here for now until it is clear this works.
516
517 return finalActions
518
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000519 def bindPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000520 jobs = Jobs.JobList()
521
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000522 finalOutput = args.getLastArg(self.parser.oOption)
523 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
524 args.getLastArg(self.parser.saveTempsOption2))
525 hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption)
526 hasPipe = args.getLastArg(self.parser.pipeOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000527 forward = []
528 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000529 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000530 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000531
532 # FIXME: Needs to be part of option.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000533 elif a.opt.name in ('-E', '-S', '-c',
534 '-arch', '-fsyntax-only', '-combine', '-x',
535 '-###'):
536 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000537
Daniel Dunbara5677512009-01-05 19:53:30 +0000538 else:
539 forward.append(a)
540
541 # We claim things here so that options for which we silently allow
542 # override only ever claim the used option.
543 if hasPipe:
544 self.claim(hasPipe)
545 # FIXME: Hack, override -pipe till we support it.
546 hasPipe = None
547 # Claim these here. Its not completely accurate but any warnings
548 # about these being unused are likely to be noise anyway.
549 if hasSaveTemps:
550 self.claim(hasSaveTemps)
551 if hasNoIntegratedCPP:
552 self.claim(hasNoIntegratedCPP)
553
Daniel Dunbara5677512009-01-05 19:53:30 +0000554 class InputInfo:
555 def __init__(self, source, type, baseInput):
556 self.source = source
557 self.type = type
558 self.baseInput = baseInput
559
560 def __repr__(self):
561 return '%s(%r, %r, %r)' % (self.__class__.__name__,
562 self.source, self.type, self.baseInput)
563
564 def createJobs(phase, forwardArgs,
565 canAcceptPipe=False, atTopLevel=False, arch=None):
566 if isinstance(phase, Phases.InputAction):
567 return InputInfo(phase.filename, phase.type, phase.filename)
568 elif isinstance(phase, Phases.BindArchAction):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000569 archName = args.getValue(phase.arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000570 filteredArgs = []
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000571 for arg in forwardArgs:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000572 if arg.opt is self.parser.archOption:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000573 if arg is phase.arch:
574 filteredArgs.append(arg)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000575 elif arg.opt is self.parser.XarchOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000576 # FIXME: gcc-dd has another conditional for passing
577 # through, when the arch conditional array has an empty
578 # string. Why?
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000579 if args.getJoinedValue(arg) == archName:
Daniel Dunbara5677512009-01-05 19:53:30 +0000580 # FIXME: This is wrong, we don't want a
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000581 # unknown arg we want an actual parsed
582 # version of this arg.
583 filteredArgs.append(args.makeUnknownArg(args.getSeparateValue(arg)))
Daniel Dunbara5677512009-01-05 19:53:30 +0000584 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000585 filteredArgs.append(arg)
Daniel Dunbara5677512009-01-05 19:53:30 +0000586
587 return createJobs(phase.inputs[0], filteredArgs,
588 canAcceptPipe, atTopLevel, phase.arch)
589
590 assert isinstance(phase, Phases.JobAction)
Daniel Dunbar43124722009-01-10 02:07:54 +0000591 tool = self.toolChain.selectTool(phase)
Daniel Dunbara5677512009-01-05 19:53:30 +0000592
593 # See if we should use an integrated CPP. We only use an
594 # integrated cpp when we have exactly one input, since this is
595 # the only use case we care about.
596 useIntegratedCPP = False
597 inputList = phase.inputs
598 if (not hasNoIntegratedCPP and
599 not hasSaveTemps and
600 tool.hasIntegratedCPP()):
601 if (len(phase.inputs) == 1 and
602 isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)):
603 useIntegratedCPP = True
604 inputList = phase.inputs[0].inputs
605
606 # Only try to use pipes when exactly one input.
607 canAcceptPipe = len(inputList) == 1 and tool.acceptsPipedInput()
608 inputs = [createJobs(p, forwardArgs, canAcceptPipe, False, arch) for p in inputList]
609
610 # Determine if we should output to a pipe.
611 canOutputToPipe = canAcceptPipe and tool.canPipeOutput()
612 outputToPipe = False
613 if canOutputToPipe:
614 # Some things default to writing to a pipe if the final
615 # phase and there was no user override.
616 #
617 # FIXME: What is the best way to handle this?
618 if (atTopLevel and
619 isinstance(phase, Phases.PreprocessPhase) and
620 not finalOutput):
621 outputToPipe = True
622 elif hasPipe:
623 outputToPipe = True
624
625 # Figure out where to put the job (pipes).
626 jobList = jobs
627 if canAcceptPipe and isinstance(inputs[0].source, Jobs.PipedJob):
628 jobList = inputs[0].source
629
630 # Figure out where to put the output.
631 baseInput = inputs[0].baseInput
632 if phase.type == Types.NothingType:
633 output = None
634 elif outputToPipe:
635 if isinstance(jobList, Jobs.PipedJob):
636 output = jobList
637 else:
638 jobList = output = Jobs.PipedJob([])
639 jobs.addJob(output)
640 else:
641 # Figure out what the derived output location would be.
642 #
643 # FIXME: gcc has some special case in here so that it doesn't
644 # create output files if they would conflict with an input.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000645 inputName = args.getValue(baseInput)
Daniel Dunbara5677512009-01-05 19:53:30 +0000646 if phase.type is Types.ImageType:
647 namedOutput = "a.out"
648 else:
649 base,_ = os.path.splitext(inputName)
650 assert phase.type.tempSuffix is not None
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000651 namedOutput = base + '.' + phase.type.tempSuffix
Daniel Dunbara5677512009-01-05 19:53:30 +0000652
653 # Output to user requested destination?
654 if atTopLevel and finalOutput:
655 output = finalOutput
656 # Contruct a named destination?
657 elif atTopLevel or hasSaveTemps:
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000658 output = args.makeSeparateArg(namedOutput,
659 self.parser.oOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000660 else:
661 # Output to temp file...
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000662 fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix)
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000663 output = args.makeSeparateArg(filename,
664 self.parser.oOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000665
Daniel Dunbardb439902009-01-07 18:40:45 +0000666 tool.constructJob(phase, arch, jobList, inputs, output, phase.type,
667 forwardArgs, args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000668
669 return InputInfo(output, phase.type, baseInput)
670
671 # It is an error to provide a -o option if we are making multiple
672 # output files.
673 if finalOutput and len([a for a in phases if a.type is not Types.NothingType]) > 1:
674 # FIXME: Custom exception.
675 raise ValueError,"Cannot specify -o when generating multiple files."
676
677 for phase in phases:
678 createJobs(phase, forward, canAcceptPipe=True, atTopLevel=True)
679
680 return jobs