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