1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
import os
import platform
import sys
import tempfile
from pprint import pprint
###
import Arguments
import Jobs
import HostInfo
import Phases
import Tools
import Types
import Util
# FIXME: Clean up naming of options and arguments. Decide whether to
# rename Option and be consistent about use of Option/Arg.
####
class Driver(object):
def __init__(self, driverName, driverDir):
self.driverName = driverName
self.driverDir = driverDir
self.hostInfo = None
self.parser = Arguments.OptionParser()
self.cccHostBits = self.cccHostMachine = None
self.cccHostSystem = self.cccHostRelease = None
self.cccCXX = False
self.cccEcho = False
self.cccFallback = False
self.cccNoClang = self.cccNoClangCXX = self.cccNoClangPreprocessor = False
self.cccClangArchs = None
# Certain options suppress the 'no input files' warning.
self.suppressMissingInputWarning = False
# Host queries which can be forcibly over-riden by the user for
# testing purposes.
#
# FIXME: We should make sure these are drawn from a fixed set so
# that nothing downstream ever plays a guessing game.
def getHostBits(self):
if self.cccHostBits:
return self.cccHostBits
return platform.architecture()[0].replace('bit','')
def getHostMachine(self):
if self.cccHostMachine:
return self.cccHostMachine
machine = platform.machine()
# Normalize names.
if machine == 'Power Macintosh':
return 'ppc'
if machine == 'x86_64':
return 'i386'
return machine
def getHostSystemName(self):
if self.cccHostSystem:
return self.cccHostSystem
return platform.system().lower()
def getHostReleaseName(self):
if self.cccHostRelease:
return self.cccHostRelease
return platform.release()
def getenvBool(self, name):
var = os.getenv(name)
if not var:
return False
try:
return bool(int(var))
except:
return False
###
def getFilePath(self, name, toolChain=None):
tc = toolChain or self.toolChain
for p in tc.filePathPrefixes:
path = os.path.join(p, name)
if os.path.exists(path):
return path
return name
def getProgramPath(self, name, toolChain=None):
tc = toolChain or self.toolChain
for p in tc.programPathPrefixes:
path = os.path.join(p, name)
if os.path.exists(path):
return path
return name
###
def run(self, argv):
# FIXME: Things to support from environment: GCC_EXEC_PREFIX,
# COMPILER_PATH, LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS,
# QA_OVERRIDE_GCC3_OPTIONS, ...?
# FIXME: -V and -b processing
# Handle some special -ccc- options used for testing which are
# only allowed at the beginning of the command line.
cccPrintOptions = False
cccPrintPhases = False
# FIXME: How to handle override of host? ccc specific options?
# Abuse -b?
arg = os.getenv('CCC_ADD_ARGS')
if arg:
args = filter(None, map(str.strip, arg.split(',')))
argv = args + argv
while argv and argv[0].startswith('-ccc-'):
fullOpt,argv = argv[0],argv[1:]
opt = fullOpt[5:]
if opt == 'print-options':
cccPrintOptions = True
elif opt == 'print-phases':
cccPrintPhases = True
elif opt == 'cxx':
self.cccCXX = True
elif opt == 'echo':
self.cccEcho = True
elif opt == 'fallback':
self.cccFallback = True
elif opt == 'no-clang':
self.cccNoClang = True
elif opt == 'no-clang-cxx':
self.cccNoClangCXX = True
elif opt == 'no-clang-cpp':
self.cccNoClangPreprocessor = True
elif opt == 'clang-archs':
self.cccClangArchs,argv = argv[0].split(','),argv[1:]
elif opt == 'host-bits':
self.cccHostBits,argv = argv[0],argv[1:]
elif opt == 'host-machine':
self.cccHostMachine,argv = argv[0],argv[1:]
elif opt == 'host-system':
self.cccHostSystem,argv = argv[0],argv[1:]
elif opt == 'host-release':
self.cccHostRelease,argv = argv[0],argv[1:]
else:
raise Arguments.InvalidArgumentsError("invalid option: %r" % fullOpt)
self.hostInfo = HostInfo.getHostInfo(self)
self.toolChain = self.hostInfo.getToolChain()
args = self.parser.parseArgs(argv)
# FIXME: Ho hum I have just realized -Xarch_ is broken. We really
# need to reparse the Arguments after they have been expanded by
# -Xarch. How is this going to work?
#
# Scratch that, we aren't going to do that; it really disrupts the
# organization, doesn't consistently work with gcc-dd, and is
# confusing. Instead we are going to enforce that -Xarch_ is only
# used with options which do not alter the driver behavior. Let's
# hope this is ok, because the current architecture is a little
# tied to it.
if cccPrintOptions:
self.printOptions(args)
sys.exit(0)
self.handleImmediateOptions(args)
if self.hostInfo.useDriverDriver():
phases = self.buildPipeline(args)
else:
phases = self.buildNormalPipeline(args)
if cccPrintPhases:
self.printPhases(phases, args)
sys.exit(0)
if 0:
print Util.pprint(phases)
jobs = self.bindPhases(phases, args)
# FIXME: We should provide some basic sanity checking of the
# pipeline as a "verification" sort of stage. For example, the
# pipeline should never end up writing to an output file in two
# places (I think). The pipeline should also never end up writing
# to an output file that is an input.
#
# This is intended to just be a "verify" step, not a functionality
# step. It should catch things like the driver driver not
# preventing -save-temps, but it shouldn't change behavior (so we
# can turn it off in Release-Asserts builds).
# Print in -### syntax.
hasHashHashHash = args.getLastArg(self.parser.hashHashHashOption)
if hasHashHashHash:
self.claim(hasHashHashHash)
for j in jobs.iterjobs():
if isinstance(j, Jobs.Command):
print >>sys.stderr, ' "%s"' % '" "'.join(j.getArgv())
elif isinstance(j, Jobs.PipedJob):
for c in j.commands:
print >>sys.stderr, ' "%s" %c' % ('" "'.join(c.getArgv()),
"| "[c is j.commands[-1]])
elif not isinstance(j, JobList):
raise ValueError,'Encountered unknown job.'
sys.exit(0)
vArg = args.getLastArg(self.parser.vOption)
for j in jobs.iterjobs():
if isinstance(j, Jobs.Command):
if vArg or self.cccEcho:
print >>sys.stderr, ' '.join(map(str,j.getArgv()))
sys.stderr.flush()
res = os.spawnvp(os.P_WAIT, j.executable, j.getArgv())
if res:
sys.exit(res)
elif isinstance(j, Jobs.PipedJob):
import subprocess
procs = []
for sj in j.commands:
if vArg or self.cccEcho:
print >> sys.stderr, ' '.join(map(str,sj.getArgv()))
sys.stdout.flush()
if not procs:
stdin = None
else:
stdin = procs[-1].stdout
if sj is j.commands[-1]:
stdout = None
else:
stdout = subprocess.PIPE
procs.append(subprocess.Popen(sj.getArgv(),
executable=sj.executable,
stdin=stdin,
stdout=stdout))
for proc in procs:
res = proc.wait()
if res:
sys.exit(res)
else:
raise ValueError,'Encountered unknown job.'
def claim(self, option):
# FIXME: Move to OptionList once introduced and implement.
pass
def warning(self, message):
print >>sys.stderr,'%s: %s' % (self.driverName, message)
def printOptions(self, args):
for i,arg in enumerate(args):
if isinstance(arg, Arguments.MultipleValuesArg):
values = list(args.getValues(arg))
elif isinstance(arg, Arguments.ValueArg):
values = [args.getValue(arg)]
elif isinstance(arg, Arguments.JoinedAndSeparateValuesArg):
values = [args.getJoinedValue(arg), args.getSeparateValue(arg)]
else:
values = []
print 'Option %d - Name: "%s", Values: {%s}' % (i, arg.opt.name,
', '.join(['"%s"' % v
for v in values]))
def printPhases(self, phases, args):
def printPhase(p, f, steps, arch=None):
if p in steps:
return steps[p]
elif isinstance(p, Phases.BindArchAction):
for kid in p.inputs:
printPhase(kid, f, steps, p.arch)
steps[p] = len(steps)
return
if isinstance(p, Phases.InputAction):
phaseName = 'input'
inputStr = '"%s"' % args.getValue(p.filename)
else:
phaseName = p.phase.name
inputs = [printPhase(i, f, steps, arch)
for i in p.inputs]
inputStr = '{%s}' % ', '.join(map(str, inputs))
if arch is not None:
phaseName += '-' + args.getValue(arch)
steps[p] = index = len(steps)
print "%d: %s, %s, %s" % (index,phaseName,inputStr,p.type.name)
return index
steps = {}
for phase in phases:
printPhase(phase, sys.stdout, steps)
def printVersion(self):
# FIXME: Print default target triple.
print >>sys.stderr,'ccc version 1.0'
def handleImmediateOptions(self, args):
# FIXME: Some driver Arguments are consumed right off the bat,
# like -dumpversion. Currently the gcc-dd handles these
# poorly, so we should be ok handling them upfront instead of
# after driver-driver level dispatching.
#
# FIXME: The actual order of these options in gcc is all over the
# place. The -dump ones seem to be first and in specification
# order, but there are other levels of precedence. For example,
# -print-search-dirs is evaluated before -print-prog-name=,
# regardless of order (and the last instance of -print-prog-name=
# wins verse itself).
#
# FIXME: Do we want to report "argument unused" type errors in the
# presence of things like -dumpmachine and -print-search-dirs?
# Probably not.
if (args.getLastArg(self.parser.vOption) or
args.getLastArg(self.parser.hashHashHashOption)):
self.printVersion()
self.suppressMissingInputWarning = True
arg = (args.getLastArg(self.parser.dumpmachineOption) or
args.getLastArg(self.parser.dumpversionOption) or
args.getLastArg(self.parser.printSearchDirsOption))
if arg:
raise NotImplementedError('%s unsupported' % arg.opt.name)
arg = (args.getLastArg(self.parser.dumpspecsOption) or
args.getLastArg(self.parser.printMultiDirectoryOption) or
args.getLastArg(self.parser.printMultiOsDirectoryOption) or
args.getLastArg(self.parser.printMultiLibOption))
if arg:
raise Arguments.InvalidArgumentsError('%s unsupported by this driver' % arg.opt.name)
arg = args.getLastArg(self.parser.printFileNameOption)
if arg:
print self.getFilePath(args.getValue(arg))
sys.exit(0)
arg = args.getLastArg(self.parser.printProgNameOption)
if arg:
print self.getProgramPath(args.getValue(arg))
sys.exit(0)
arg = args.getLastArg(self.parser.printLibgccFileNameOption)
if arg:
print self.getFilePath('libgcc.a')
sys.exit(0)
def buildNormalPipeline(self, args):
hasAnalyze = args.getLastArg(self.parser.analyzeOption)
hasCombine = args.getLastArg(self.parser.combineOption)
hasEmitLLVM = args.getLastArg(self.parser.emitLLVMOption)
hasSyntaxOnly = args.getLastArg(self.parser.syntaxOnlyOption)
hasDashC = args.getLastArg(self.parser.cOption)
hasDashE = args.getLastArg(self.parser.EOption)
hasDashS = args.getLastArg(self.parser.SOption)
hasDashM = args.getLastArg(self.parser.MOption)
hasDashMM = args.getLastArg(self.parser.MMOption)
inputType = None
inputTypeOpt = None
inputs = []
for a in args:
if a.opt is self.parser.inputOption:
inputValue = args.getValue(a)
if inputType is None:
base,ext = os.path.splitext(inputValue)
# stdin is handled specially.
if inputValue == '-':
if args.getLastArg(self.parser.EOption):
# Treat as a C input needing preprocessing
# (or Obj-C if over-ridden below).
klass = Types.CType
else:
raise Arguments.InvalidArgumentsError("-E or -x required when input is from standard input")
elif ext and ext in Types.kTypeSuffixMap:
klass = Types.kTypeSuffixMap[ext]
else:
# FIXME: Its not clear why we shouldn't just
# revert to unknown. I think this is more likely a
# bug / unintended behavior in gcc. Not very
# important though.
klass = Types.ObjectType
# -ObjC and -ObjC++ over-ride the default
# language, but only for "source files". We
# just treat everything that isn't a linker
# input as a source file.
#
# FIXME: Clean this up if we move the phase
# sequence into the type.
if klass is not Types.ObjectType:
if args.getLastArg(self.parser.ObjCOption):
klass = Types.ObjCType
elif args.getLastArg(self.parser.ObjCXXOption):
klass = Types.ObjCType
else:
assert inputTypeOpt is not None
self.claim(inputTypeOpt)
klass = inputType
# Check that the file exists. It isn't clear this is
# worth doing, since the tool presumably does this
# anyway, and this just adds an extra stat to the
# equation, but this is gcc compatible.
if inputValue != '-' and not os.path.exists(inputValue):
self.warning("%s: No such file or directory" % inputValue)
else:
inputs.append((klass, a))
elif a.opt.isLinkerInput:
# Treat as a linker input.
#
# FIXME: This might not be good enough. We may
# need to introduce another type for this case, so
# that other code which needs to know the inputs
# handles this properly. Best not to try and lipo
# this, for example.
inputs.append((Types.ObjectType, a))
elif a.opt is self.parser.xOption:
inputTypeOpt = a
value = args.getValue(a)
if value in Types.kTypeSpecifierMap:
inputType = Types.kTypeSpecifierMap[value]
else:
# FIXME: How are we going to handle diagnostics.
self.warning("language %s not recognized" % value)
# FIXME: Its not clear why we shouldn't just
# revert to unknown. I think this is more likely a
# bug / unintended behavior in gcc. Not very
# important though.
inputType = Types.ObjectType
# We claim things here so that options for which we silently allow
# override only ever claim the used option.
if hasCombine:
self.claim(hasCombine)
finalPhase = Phases.Phase.eOrderPostAssemble
finalPhaseOpt = None
# Determine what compilation mode we are in.
if hasDashE or hasDashM or hasDashMM:
finalPhase = Phases.Phase.eOrderPreprocess
finalPhaseOpt = hasDashE
elif (hasAnalyze or hasSyntaxOnly or
hasEmitLLVM or hasDashS):
finalPhase = Phases.Phase.eOrderCompile
finalPhaseOpt = (hasAnalyze or hasSyntaxOnly or
hasEmitLLVM or hasDashS)
elif hasDashC:
finalPhase = Phases.Phase.eOrderAssemble
finalPhaseOpt = hasDashC
if finalPhaseOpt:
self.claim(finalPhaseOpt)
# Reject -Z* at the top level for now.
arg = args.getLastArg(self.parser.ZOption)
if arg:
raise Arguments.InvalidArgumentsError("%s: unsupported use of internal gcc option" % ' '.join(args.render(arg)))
if not inputs and not self.suppressMissingInputWarning:
raise Arguments.InvalidArgumentsError("no input files")
actions = []
linkerInputs = []
# FIXME: This is gross.
linkPhase = Phases.LinkPhase()
for klass,input in inputs:
# Figure out what step to start at.
# FIXME: This should be part of the input class probably?
# Altough it doesn't quite fit there either, things like
# asm-with-preprocess don't easily fit into a linear scheme.
# FIXME: I think we are going to end up wanting to just build
# a simple FSA which we run the inputs down.
sequence = []
if klass.preprocess:
sequence.append(Phases.PreprocessPhase())
if klass == Types.ObjectType:
sequence.append(linkPhase)
elif klass.onlyAssemble:
sequence.extend([Phases.AssemblePhase(),
linkPhase])
elif klass.onlyPrecompile:
sequence.append(Phases.PrecompilePhase())
elif hasAnalyze:
sequence.append(Phases.AnalyzePhase())
elif hasSyntaxOnly:
sequence.append(Phases.SyntaxOnlyPhase())
elif hasEmitLLVM:
sequence.append(Phases.EmitLLVMPhase())
else:
sequence.extend([Phases.CompilePhase(),
Phases.AssemblePhase(),
linkPhase])
if sequence[0].order > finalPhase:
assert finalPhaseOpt and finalPhaseOpt.opt
# FIXME: Explain what type of input file is. Or just match
# gcc warning.
self.warning("%s: %s input file unused when %s is present" % (args.getValue(input),
sequence[0].name,
finalPhaseOpt.opt.name))
else:
# Build the pipeline for this file.
current = Phases.InputAction(input, klass)
for transition in sequence:
# If the current action produces no output, or we are
# past what the user requested, we are done.
if (current.type is Types.NothingType or
transition.order > finalPhase):
break
else:
if isinstance(transition, Phases.PreprocessPhase):
assert isinstance(klass.preprocess, Types.InputType)
current = Phases.JobAction(transition,
[current],
klass.preprocess)
elif isinstance(transition, Phases.PrecompilePhase):
current = Phases.JobAction(transition,
[current],
Types.PCHType)
elif isinstance(transition, Phases.AnalyzePhase):
output = Types.PlistType
current = Phases.JobAction(transition,
[current],
output)
elif isinstance(transition, Phases.SyntaxOnlyPhase):
output = Types.NothingType
current = Phases.JobAction(transition,
[current],
output)
elif isinstance(transition, Phases.EmitLLVMPhase):
if hasDashS:
output = Types.LLVMAsmType
else:
output = Types.LLVMBCType
current = Phases.JobAction(transition,
[current],
output)
elif isinstance(transition, Phases.CompilePhase):
output = Types.AsmTypeNoPP
current = Phases.JobAction(transition,
[current],
output)
elif isinstance(transition, Phases.AssemblePhase):
current = Phases.JobAction(transition,
[current],
Types.ObjectType)
elif transition is linkPhase:
linkerInputs.append(current)
current = None
break
else:
raise RuntimeError,'Unrecognized transition: %s.' % transition
pass
if current is not None:
assert not isinstance(current, Phases.InputAction)
actions.append(current)
if linkerInputs:
actions.append(Phases.JobAction(linkPhase,
linkerInputs,
Types.ImageType))
return actions
def buildPipeline(self, args):
# FIXME: We need to handle canonicalization of the specified arch.
archs = {}
hasDashM = args.getLastArg(self.parser.MGroup)
hasSaveTemps = args.getLastArg(self.parser.saveTempsOption)
for arg in args:
if arg.opt is self.parser.archOption:
# FIXME: Canonicalize this.
archName = args.getValue(arg)
archs[archName] = arg
archs = archs.values()
if not archs:
archs.append(args.makeSeparateArg(self.hostInfo.getArchName(args),
self.parser.archOption))
actions = self.buildNormalPipeline(args)
# FIXME: Use custom exception for this.
#
# FIXME: We killed off some others but these aren't yet detected in
# a functional manner. If we added information to jobs about which
# "auxiliary" files they wrote then we could detect the conflict
# these cause downstream.
if len(archs) > 1:
if hasDashM:
raise Arguments.InvalidArgumentsError("Cannot use -M options with multiple arch flags.")
elif hasSaveTemps:
raise Arguments.InvalidArgumentsError("Cannot use -save-temps with multiple arch flags.")
# Execute once per arch.
finalActions = []
for p in actions:
# Make sure we can lipo this kind of output. If not (and it
# is an actual output) then we disallow, since we can't
# create an output file with the right name without
# overwriting it. We could remove this oddity by just
# changing the output names to include the arch, which would
# also fix -save-temps. Compatibility wins for now.
#
# FIXME: Is this error substantially less useful than
# gcc-dd's? The main problem is that "Cannot use compiler
# output with multiple arch flags" won't make sense to most
# developers.
if (len(archs) > 1 and
p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)):
raise Arguments.InvalidArgumentsError('Cannot use %s output with multiple arch flags.' % p.type.name)
inputs = []
for arch in archs:
inputs.append(Phases.BindArchAction(p, arch))
# Lipo if necessary. We do it this way because we need to set
# the arch flag so that -Xarch_ gets rewritten.
if len(inputs) == 1 or p.type == Types.NothingType:
finalActions.extend(inputs)
else:
finalActions.append(Phases.JobAction(Phases.LipoPhase(),
inputs,
p.type))
return finalActions
def bindPhases(self, phases, args):
jobs = Jobs.JobList()
finalOutput = args.getLastArg(self.parser.oOption)
hasSaveTemps = args.getLastArg(self.parser.saveTempsOption)
hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption)
hasTraditionalCPP = args.getLastArg(self.parser.traditionalCPPOption)
hasPipe = args.getLastArg(self.parser.pipeOption)
# We claim things here so that options for which we silently allow
# override only ever claim the used option.
if hasPipe:
self.claim(hasPipe)
# FIXME: Hack, override -pipe till we support it.
if hasSaveTemps:
self.warning('-pipe ignored because -save-temps specified')
hasPipe = None
# Claim these here. Its not completely accurate but any warnings
# about these being unused are likely to be noise anyway.
if hasSaveTemps:
self.claim(hasSaveTemps)
if hasTraditionalCPP:
self.claim(hasTraditionalCPP)
elif hasNoIntegratedCPP:
self.claim(hasNoIntegratedCPP)
# FIXME: Move to... somewhere else.
class InputInfo:
def __init__(self, source, type, baseInput):
self.source = source
self.type = type
self.baseInput = baseInput
def __repr__(self):
return '%s(%r, %r, %r)' % (self.__class__.__name__,
self.source, self.type, self.baseInput)
def isOriginalInput(self):
return self.source is self.baseInput
def createJobs(tc, phase,
canAcceptPipe=False, atTopLevel=False, arch=None,
tcArgs=None, linkingOutput=None):
if isinstance(phase, Phases.InputAction):
return InputInfo(phase.filename, phase.type, phase.filename)
elif isinstance(phase, Phases.BindArchAction):
archName = args.getValue(phase.arch)
tc = self.hostInfo.getToolChainForArch(archName)
return createJobs(tc, phase.inputs[0],
canAcceptPipe, atTopLevel, phase.arch,
None, linkingOutput)
if tcArgs is None:
tcArgs = tc.translateArgs(args, arch)
assert isinstance(phase, Phases.JobAction)
tool = tc.selectTool(phase)
# See if we should use an integrated CPP. We only use an
# integrated cpp when we have exactly one input, since this is
# the only use case we care about.
useIntegratedCPP = False
inputList = phase.inputs
if (not hasNoIntegratedCPP and
not hasTraditionalCPP and
not hasSaveTemps and
tool.hasIntegratedCPP()):
if (len(phase.inputs) == 1 and
isinstance(phase.inputs[0], Phases.JobAction) and
isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)):
useIntegratedCPP = True
inputList = phase.inputs[0].inputs
# Only try to use pipes when exactly one input.
attemptToPipeInput = len(inputList) == 1 and tool.acceptsPipedInput()
inputs = [createJobs(tc, p, attemptToPipeInput, False,
arch, tcArgs, linkingOutput)
for p in inputList]
# Determine if we should output to a pipe.
canOutputToPipe = canAcceptPipe and tool.canPipeOutput()
outputToPipe = False
if canOutputToPipe:
# Some things default to writing to a pipe if the final
# phase and there was no user override.
#
# FIXME: What is the best way to handle this?
if atTopLevel:
if (isinstance(phase.phase, Phases.PreprocessPhase) and
not finalOutput):
outputToPipe = True
elif hasPipe:
outputToPipe = True
# Figure out where to put the job (pipes).
jobList = jobs
if isinstance(inputs[0].source, Jobs.PipedJob):
jobList = inputs[0].source
baseInput = inputs[0].baseInput
output,jobList = self.getOutputName(phase, outputToPipe, jobs, jobList, baseInput,
args, atTopLevel, hasSaveTemps, finalOutput)
tool.constructJob(phase, arch, jobList, inputs, output, phase.type,
tcArgs, linkingOutput)
return InputInfo(output, phase.type, baseInput)
# It is an error to provide a -o option if we are making multiple
# output files.
if finalOutput and len([a for a in phases if a.type is not Types.NothingType]) > 1:
raise Arguments.InvalidArgumentsError("cannot specify -o when generating multiple files")
for phase in phases:
# If we are linking an image for multiple archs then the
# linker wants -arch_multiple and -final_output <final image
# name>. Unfortunately this requires some gross contortions.
#
# FIXME: This is a hack; find a cleaner way to integrate this
# into the process.
linkingOutput = None
if (isinstance(phase, Phases.JobAction) and
isinstance(phase.phase, Phases.LipoPhase)):
finalOutput = args.getLastArg(self.parser.oOption)
if finalOutput:
linkingOutput = finalOutput
else:
linkingOutput = args.makeSeparateArg('a.out',
self.parser.oOption)
createJobs(self.toolChain, phase,
canAcceptPipe=True, atTopLevel=True,
linkingOutput=linkingOutput)
return jobs
def getOutputName(self, phase, outputToPipe, jobs, jobList, baseInput,
args, atTopLevel, hasSaveTemps, finalOutput):
# Figure out where to put the output.
if phase.type == Types.NothingType:
output = None
elif outputToPipe:
if isinstance(jobList, Jobs.PipedJob):
output = jobList
else:
jobList = output = Jobs.PipedJob([])
jobs.addJob(output)
else:
# Figure out what the derived output location would be.
#
# FIXME: gcc has some special case in here so that it doesn't
# create output files if they would conflict with an input.
if phase.type is Types.ImageType:
namedOutput = "a.out"
else:
inputName = args.getValue(baseInput)
base,_ = os.path.splitext(inputName)
assert phase.type.tempSuffix is not None
namedOutput = base + '.' + phase.type.tempSuffix
# Output to user requested destination?
if atTopLevel and finalOutput:
output = finalOutput
# Contruct a named destination?
elif atTopLevel or hasSaveTemps:
# As an annoying special case, pch generation
# doesn't strip the pathname.
if phase.type is Types.PCHType:
outputName = namedOutput
else:
outputName = os.path.basename(namedOutput)
output = args.makeSeparateArg(outputName,
self.parser.oOption)
else:
# Output to temp file...
fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix)
output = args.makeSeparateArg(filename,
self.parser.oOption)
return output,jobList
|