blob: b9aba39f31a427fc514ebfc2adcdd8af6a533e00 [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 Dunbare99f9262009-01-11 22:03:55 +0000147 print >>sys.stderr, '"%s"' % '" "'.join(j.getArgv())
Daniel Dunbara5677512009-01-05 19:53:30 +0000148 elif isinstance(j, Jobs.PipedJob):
149 for c in j.commands:
Daniel Dunbare99f9262009-01-11 22:03:55 +0000150 print >>sys.stderr, '"%s" %c' % ('" "'.join(c.getArgv()),
151 "| "[c is j.commands[-1]])
Daniel Dunbara5677512009-01-05 19:53:30 +0000152 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.
Daniel Dunbare99f9262009-01-11 22:03:55 +0000311 #
312 # FIXME: Actually, this is just flat out broken, the
313 # tools expect inputs to be accessible by .getValue
314 # but that of course only yields the argument.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000315 inputs.append((Types.ObjectType, a))
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000316 elif a.opt is self.parser.xOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000317 self.claim(a)
318 inputTypeOpt = a
319 value = args.getValue(a)
320 if value in Types.kTypeSpecifierMap:
321 inputType = Types.kTypeSpecifierMap[value]
322 else:
323 # FIXME: How are we going to handle diagnostics.
324 self.warning("language %s not recognized" % value)
Daniel Dunbara5677512009-01-05 19:53:30 +0000325
Daniel Dunbar5039f212009-01-06 02:30:10 +0000326 # FIXME: Its not clear why we shouldn't just
327 # revert to unknown. I think this is more likely a
328 # bug / unintended behavior in gcc. Not very
329 # important though.
330 inputType = ObjectType
Daniel Dunbara5677512009-01-05 19:53:30 +0000331
332 # We claim things here so that options for which we silently allow
333 # override only ever claim the used option.
334 if hasCombine:
335 self.claim(hasCombine)
336
337 finalPhase = Phases.Phase.eOrderPostAssemble
338 finalPhaseOpt = None
339
340 # Determine what compilation mode we are in.
341 if hasDashE:
342 finalPhase = Phases.Phase.eOrderPreprocess
343 finalPhaseOpt = hasDashE
344 elif hasSyntaxOnly:
345 finalPhase = Phases.Phase.eOrderCompile
346 finalPhaseOpt = hasSyntaxOnly
347 elif hasDashS:
348 finalPhase = Phases.Phase.eOrderCompile
349 finalPhaseOpt = hasDashS
350 elif hasDashC:
351 finalPhase = Phases.Phase.eOrderAssemble
352 finalPhaseOpt = hasDashC
353
354 if finalPhaseOpt:
355 self.claim(finalPhaseOpt)
356
357 # FIXME: Support -combine.
358 if hasCombine:
359 raise NotImplementedError,"-combine is not yet supported."
360
361 actions = []
362 linkerInputs = []
363 # FIXME: This is gross.
364 linkPhase = Phases.LinkPhase()
365 for klass,input in inputs:
366 # Figure out what step to start at.
367
368 # FIXME: This should be part of the input class probably?
369 # Altough it doesn't quite fit there either, things like
370 # asm-with-preprocess don't easily fit into a linear scheme.
371
372 # FIXME: I think we are going to end up wanting to just build
373 # a simple FSA which we run the inputs down.
374 sequence = []
375 if klass.preprocess:
376 sequence.append(Phases.PreprocessPhase())
377 if klass == Types.ObjectType:
378 sequence.append(linkPhase)
379 elif klass.onlyAssemble:
380 sequence.extend([Phases.AssemblePhase(),
381 linkPhase])
382 elif klass.onlyPrecompile:
383 sequence.append(Phases.PrecompilePhase())
384 else:
385 sequence.extend([Phases.CompilePhase(),
386 Phases.AssemblePhase(),
387 linkPhase])
388
389 if sequence[0].order > finalPhase:
390 assert finalPhaseOpt and finalPhaseOpt.opt
391 # FIXME: Explain what type of input file is. Or just match
392 # gcc warning.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000393 self.warning("%s: %s input file unused when %s is present" % (args.getValue(input),
Daniel Dunbara5677512009-01-05 19:53:30 +0000394 sequence[0].name,
395 finalPhaseOpt.opt.name))
396 else:
397 # Build the pipeline for this file.
398
399 current = Phases.InputAction(input, klass)
400 for transition in sequence:
401 # If the current action produces no output, or we are
402 # past what the user requested, we are done.
403 if (current.type is Types.NothingType or
404 transition.order > finalPhase):
405 break
406 else:
407 if isinstance(transition, Phases.PreprocessPhase):
408 assert isinstance(klass.preprocess, Types.InputType)
409 current = Phases.JobAction(transition,
410 [current],
411 klass.preprocess)
412 elif isinstance(transition, Phases.PrecompilePhase):
413 current = Phases.JobAction(transition,
414 [current],
415 Types.PCHType)
416 elif isinstance(transition, Phases.CompilePhase):
417 if hasSyntaxOnly:
418 output = Types.NothingType
419 else:
420 output = Types.AsmTypeNoPP
421 current = Phases.JobAction(transition,
422 [current],
423 output)
424 elif isinstance(transition, Phases.AssemblePhase):
425 current = Phases.JobAction(transition,
426 [current],
427 Types.ObjectType)
428 elif transition is linkPhase:
429 linkerInputs.append(current)
430 current = None
431 break
432 else:
433 raise RuntimeError,'Unrecognized transition: %s.' % transition
434 pass
435
436 if current is not None:
437 assert not isinstance(current, Phases.InputAction)
438 actions.append(current)
439
440 if linkerInputs:
441 actions.append(Phases.JobAction(linkPhase,
442 linkerInputs,
443 Types.ImageType))
444
445 return actions
446
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000447 def buildPipeline(self, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000448 # FIXME: We need to handle canonicalization of the specified arch.
449
450 archs = []
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000451 hasDashM = None
452 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
453 args.getLastArg(self.parser.saveTempsOption2))
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000454 for arg in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000455 if arg.opt is self.parser.archOption:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000456 archs.append(arg)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000457 elif arg.opt.name.startswith('-M'):
458 hasDashM = arg
Daniel Dunbara5677512009-01-05 19:53:30 +0000459
460 if not archs:
Daniel Dunbar9066af82009-01-09 01:00:40 +0000461 archs.append(args.makeSeparateArg(self.hostInfo.getArchName(),
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000462 self.parser.archOption))
Daniel Dunbara5677512009-01-05 19:53:30 +0000463
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000464 actions = self.buildNormalPipeline(args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000465
466 # FIXME: Use custom exception for this.
467 #
468 # FIXME: We killed off some others but these aren't yet detected in
469 # a functional manner. If we added information to jobs about which
470 # "auxiliary" files they wrote then we could detect the conflict
471 # these cause downstream.
472 if len(archs) > 1:
473 if hasDashM:
474 raise ValueError,"Cannot use -M options with multiple arch flags."
475 elif hasSaveTemps:
476 raise ValueError,"Cannot use -save-temps with multiple arch flags."
477
478 # Execute once per arch.
479 finalActions = []
480 for p in actions:
481 # Make sure we can lipo this kind of output. If not (and it
482 # is an actual output) then we disallow, since we can't
483 # create an output file with the right name without
484 # overwriting it. We could remove this oddity by just
485 # changing the output names to include the arch, which would
486 # also fix -save-temps. Compatibility wins for now.
487 #
488 # FIXME: Is this error substantially less useful than
489 # gcc-dd's? The main problem is that "Cannot use compiler
490 # output with multiple arch flags" won't make sense to most
491 # developers.
492 if (len(archs) > 1 and
493 p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)):
494 raise ValueError,'Cannot use %s output with multiple arch flags.' % p.type.name
495
496 inputs = []
497 for arch in archs:
498 inputs.append(Phases.BindArchAction(p, arch))
499
500 # Lipo if necessary. We do it this way because we need to set
501 # the arch flag so that -Xarch_ gets rewritten.
502 if len(inputs) == 1 or p.type == Types.NothingType:
503 finalActions.extend(inputs)
504 else:
505 finalActions.append(Phases.JobAction(Phases.LipoPhase(),
506 inputs,
507 p.type))
508
509 # FIXME: We need to add -Wl,arch_multiple and -Wl,final_output in
510 # certain cases. This may be icky because we need to figure out the
511 # mode first. Current plan is to hack on the pipeline once it is built
512 # and we know what is being spit out. This avoids having to handling
513 # things like -c and -combine in multiple places.
514 #
515 # The annoying one of these is -Wl,final_output because it involves
516 # communication across different phases.
517 #
518 # Hopefully we can do this purely as part of the binding, but
519 # leaving comment here for now until it is clear this works.
520
521 return finalActions
522
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000523 def bindPhases(self, phases, args):
Daniel Dunbara5677512009-01-05 19:53:30 +0000524 jobs = Jobs.JobList()
525
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000526 finalOutput = args.getLastArg(self.parser.oOption)
527 hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or
528 args.getLastArg(self.parser.saveTempsOption2))
529 hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption)
530 hasPipe = args.getLastArg(self.parser.pipeOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000531 forward = []
532 for a in args:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000533 if a.opt is self.parser.inputOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000534 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000535
536 # FIXME: Needs to be part of option.
Daniel Dunbar5039f212009-01-06 02:30:10 +0000537 elif a.opt.name in ('-E', '-S', '-c',
538 '-arch', '-fsyntax-only', '-combine', '-x',
539 '-###'):
540 pass
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000541
Daniel Dunbara5677512009-01-05 19:53:30 +0000542 else:
543 forward.append(a)
544
545 # We claim things here so that options for which we silently allow
546 # override only ever claim the used option.
547 if hasPipe:
548 self.claim(hasPipe)
549 # FIXME: Hack, override -pipe till we support it.
550 hasPipe = None
551 # Claim these here. Its not completely accurate but any warnings
552 # about these being unused are likely to be noise anyway.
553 if hasSaveTemps:
554 self.claim(hasSaveTemps)
555 if hasNoIntegratedCPP:
556 self.claim(hasNoIntegratedCPP)
557
Daniel Dunbara5677512009-01-05 19:53:30 +0000558 class InputInfo:
559 def __init__(self, source, type, baseInput):
560 self.source = source
561 self.type = type
562 self.baseInput = baseInput
563
564 def __repr__(self):
565 return '%s(%r, %r, %r)' % (self.__class__.__name__,
566 self.source, self.type, self.baseInput)
567
568 def createJobs(phase, forwardArgs,
569 canAcceptPipe=False, atTopLevel=False, arch=None):
570 if isinstance(phase, Phases.InputAction):
571 return InputInfo(phase.filename, phase.type, phase.filename)
572 elif isinstance(phase, Phases.BindArchAction):
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000573 archName = args.getValue(phase.arch)
Daniel Dunbara5677512009-01-05 19:53:30 +0000574 filteredArgs = []
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000575 for arg in forwardArgs:
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000576 if arg.opt is self.parser.archOption:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000577 if arg is phase.arch:
578 filteredArgs.append(arg)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000579 elif arg.opt is self.parser.XarchOption:
Daniel Dunbara5677512009-01-05 19:53:30 +0000580 # FIXME: gcc-dd has another conditional for passing
581 # through, when the arch conditional array has an empty
582 # string. Why?
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000583 if args.getJoinedValue(arg) == archName:
Daniel Dunbara5677512009-01-05 19:53:30 +0000584 # FIXME: This is wrong, we don't want a
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000585 # unknown arg we want an actual parsed
586 # version of this arg.
587 filteredArgs.append(args.makeUnknownArg(args.getSeparateValue(arg)))
Daniel Dunbara5677512009-01-05 19:53:30 +0000588 else:
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000589 filteredArgs.append(arg)
Daniel Dunbara5677512009-01-05 19:53:30 +0000590
591 return createJobs(phase.inputs[0], filteredArgs,
592 canAcceptPipe, atTopLevel, phase.arch)
593
594 assert isinstance(phase, Phases.JobAction)
Daniel Dunbar43124722009-01-10 02:07:54 +0000595 tool = self.toolChain.selectTool(phase)
Daniel Dunbara5677512009-01-05 19:53:30 +0000596
597 # See if we should use an integrated CPP. We only use an
598 # integrated cpp when we have exactly one input, since this is
599 # the only use case we care about.
600 useIntegratedCPP = False
601 inputList = phase.inputs
602 if (not hasNoIntegratedCPP and
603 not hasSaveTemps and
604 tool.hasIntegratedCPP()):
605 if (len(phase.inputs) == 1 and
606 isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)):
607 useIntegratedCPP = True
608 inputList = phase.inputs[0].inputs
609
610 # Only try to use pipes when exactly one input.
611 canAcceptPipe = len(inputList) == 1 and tool.acceptsPipedInput()
612 inputs = [createJobs(p, forwardArgs, canAcceptPipe, False, arch) for p in inputList]
613
614 # Determine if we should output to a pipe.
615 canOutputToPipe = canAcceptPipe and tool.canPipeOutput()
616 outputToPipe = False
617 if canOutputToPipe:
618 # Some things default to writing to a pipe if the final
619 # phase and there was no user override.
620 #
621 # FIXME: What is the best way to handle this?
622 if (atTopLevel and
623 isinstance(phase, Phases.PreprocessPhase) and
624 not finalOutput):
625 outputToPipe = True
626 elif hasPipe:
627 outputToPipe = True
628
629 # Figure out where to put the job (pipes).
630 jobList = jobs
631 if canAcceptPipe and isinstance(inputs[0].source, Jobs.PipedJob):
632 jobList = inputs[0].source
633
634 # Figure out where to put the output.
635 baseInput = inputs[0].baseInput
636 if phase.type == Types.NothingType:
637 output = None
638 elif outputToPipe:
639 if isinstance(jobList, Jobs.PipedJob):
640 output = jobList
641 else:
642 jobList = output = Jobs.PipedJob([])
643 jobs.addJob(output)
644 else:
645 # Figure out what the derived output location would be.
646 #
647 # FIXME: gcc has some special case in here so that it doesn't
648 # create output files if they would conflict with an input.
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000649 inputName = args.getValue(baseInput)
Daniel Dunbara5677512009-01-05 19:53:30 +0000650 if phase.type is Types.ImageType:
651 namedOutput = "a.out"
652 else:
653 base,_ = os.path.splitext(inputName)
654 assert phase.type.tempSuffix is not None
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000655 namedOutput = base + '.' + phase.type.tempSuffix
Daniel Dunbara5677512009-01-05 19:53:30 +0000656
657 # Output to user requested destination?
658 if atTopLevel and finalOutput:
659 output = finalOutput
660 # Contruct a named destination?
661 elif atTopLevel or hasSaveTemps:
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000662 output = args.makeSeparateArg(namedOutput,
663 self.parser.oOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000664 else:
665 # Output to temp file...
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000666 fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix)
Daniel Dunbar39cbfaa2009-01-07 18:54:26 +0000667 output = args.makeSeparateArg(filename,
668 self.parser.oOption)
Daniel Dunbara5677512009-01-05 19:53:30 +0000669
Daniel Dunbardb439902009-01-07 18:40:45 +0000670 tool.constructJob(phase, arch, jobList, inputs, output, phase.type,
671 forwardArgs, args)
Daniel Dunbara5677512009-01-05 19:53:30 +0000672
673 return InputInfo(output, phase.type, baseInput)
674
675 # It is an error to provide a -o option if we are making multiple
676 # output files.
677 if finalOutput and len([a for a in phases if a.type is not Types.NothingType]) > 1:
678 # FIXME: Custom exception.
679 raise ValueError,"Cannot specify -o when generating multiple files."
680
681 for phase in phases:
682 createJobs(phase, forward, canAcceptPipe=True, atTopLevel=True)
683
684 return jobs