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