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