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