Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 1 | import os |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 2 | import platform |
Daniel Dunbar | a567751 | 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 | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 11 | import HostInfo |
Daniel Dunbar | a567751 | 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 | |
| 22 | class MissingArgumentError(ValueError): |
| 23 | """MissingArgumentError - An option required an argument but none |
| 24 | was given.""" |
| 25 | |
| 26 | ### |
| 27 | |
| 28 | class Driver(object): |
| 29 | def __init__(self): |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 30 | self.hostInfo = None |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 31 | self.parser = Arguments.OptionParser() |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 33 | # Host queries which can be forcibly over-riden by the user for |
| 34 | # testing purposes. |
| 35 | # |
| 36 | # FIXME: We should make sure these are drawn from a fixed set so |
| 37 | # that nothing downstream ever plays a guessing game. |
| 38 | |
| 39 | def getHostBits(self): |
| 40 | if self.cccHostBits: |
| 41 | return self.cccHostBits |
| 42 | |
| 43 | return platform.architecture()[0].replace('bit','') |
| 44 | |
| 45 | def getHostMachine(self): |
| 46 | if self.cccHostMachine: |
| 47 | return self.cccHostMachine |
| 48 | |
| 49 | machine = platform.machine() |
| 50 | # Normalize names. |
| 51 | if machine == 'Power Macintosh': |
| 52 | return 'ppc' |
| 53 | return machine |
| 54 | |
| 55 | def getHostSystemName(self): |
| 56 | if self.cccHostSystem: |
| 57 | return self.cccHostSystem |
| 58 | |
| 59 | return platform.system().lower() |
| 60 | |
| 61 | ### |
| 62 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 63 | def run(self, argv): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 64 | # FIXME: Things to support from environment: GCC_EXEC_PREFIX, |
| 65 | # COMPILER_PATH, LIBRARY_PATH, LPATH, CC_PRINT_OPTIONS, |
| 66 | # QA_OVERRIDE_GCC3_OPTIONS, ...? |
| 67 | |
| 68 | # FIXME: -V and -b processing |
| 69 | |
| 70 | # Handle some special -ccc- options used for testing which are |
| 71 | # only allowed at the beginning of the command line. |
| 72 | cccPrintOptions = False |
| 73 | cccPrintPhases = False |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 74 | |
| 75 | # FIXME: How to handle override of host? ccc specific options? |
| 76 | # Abuse -b? |
| 77 | self.cccHostBits = self.cccHostMachine = self.cccHostSystem = None |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 78 | while argv and argv[0].startswith('-ccc-'): |
| 79 | opt,argv = argv[0][5:],argv[1:] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 80 | |
| 81 | if opt == 'print-options': |
| 82 | cccPrintOptions = True |
| 83 | elif opt == 'print-phases': |
| 84 | cccPrintPhases = True |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 85 | elif opt == 'host-bits': |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 86 | self.cccHostBits,argv = argv[0],argv[1:] |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 87 | elif opt == 'host-machine': |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 88 | self.cccHostMachine,argv = argv[0],argv[1:] |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 89 | elif opt == 'host-system': |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 90 | self.cccHostSystem,argv = argv[0],argv[1:] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 91 | else: |
| 92 | raise ValueError,"Invalid ccc option: %r" % cccPrintOptions |
| 93 | |
Daniel Dunbar | a75ea3d | 2009-01-09 22:21:24 +0000 | [diff] [blame] | 94 | self.hostInfo = HostInfo.getHostInfo(self) |
Daniel Dunbar | 4312472 | 2009-01-10 02:07:54 +0000 | [diff] [blame] | 95 | self.toolChain = self.hostInfo.getToolChain() |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 97 | args = self.parser.parseArgs(argv) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 98 | |
| 99 | # FIXME: Ho hum I have just realized -Xarch_ is broken. We really |
| 100 | # need to reparse the Arguments after they have been expanded by |
| 101 | # -Xarch. How is this going to work? |
| 102 | # |
| 103 | # Scratch that, we aren't going to do that; it really disrupts the |
| 104 | # organization, doesn't consistently work with gcc-dd, and is |
| 105 | # confusing. Instead we are going to enforce that -Xarch_ is only |
| 106 | # used with options which do not alter the driver behavior. Let's |
| 107 | # hope this is ok, because the current architecture is a little |
| 108 | # tied to it. |
| 109 | |
| 110 | if cccPrintOptions: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 111 | self.printOptions(args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 112 | sys.exit(0) |
| 113 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 114 | self.handleImmediateOptions(args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 116 | if self.hostInfo.useDriverDriver(): |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 117 | phases = self.buildPipeline(args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 118 | else: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 119 | phases = self.buildNormalPipeline(args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 120 | |
| 121 | if cccPrintPhases: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 122 | self.printPhases(phases, args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 123 | sys.exit(0) |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 124 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 125 | if 0: |
| 126 | print Util.pprint(phases) |
| 127 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 128 | jobs = self.bindPhases(phases, args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 129 | |
| 130 | # FIXME: We should provide some basic sanity checking of the |
| 131 | # pipeline as a "verification" sort of stage. For example, the |
| 132 | # pipeline should never end up writing to an output file in two |
| 133 | # places (I think). The pipeline should also never end up writing |
| 134 | # to an output file that is an input. |
| 135 | # |
| 136 | # This is intended to just be a "verify" step, not a functionality |
| 137 | # step. It should catch things like the driver driver not |
| 138 | # preventing -save-temps, but it shouldn't change behavior (so we |
| 139 | # can turn it off in Release-Asserts builds). |
| 140 | |
| 141 | # Print in -### syntax. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 142 | hasHashHashHash = args.getLastArg(self.parser.hashHashHashOption) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 143 | if hasHashHashHash: |
| 144 | self.claim(hasHashHashHash) |
| 145 | for j in jobs.iterjobs(): |
| 146 | if isinstance(j, Jobs.Command): |
Daniel Dunbar | e99f926 | 2009-01-11 22:03:55 +0000 | [diff] [blame^] | 147 | print >>sys.stderr, '"%s"' % '" "'.join(j.getArgv()) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 148 | elif isinstance(j, Jobs.PipedJob): |
| 149 | for c in j.commands: |
Daniel Dunbar | e99f926 | 2009-01-11 22:03:55 +0000 | [diff] [blame^] | 150 | print >>sys.stderr, '"%s" %c' % ('" "'.join(c.getArgv()), |
| 151 | "| "[c is j.commands[-1]]) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 152 | elif not isinstance(j, JobList): |
| 153 | raise ValueError,'Encountered unknown job.' |
| 154 | sys.exit(0) |
| 155 | |
| 156 | for j in jobs.iterjobs(): |
| 157 | if isinstance(j, Jobs.Command): |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 158 | res = os.spawnvp(os.P_WAIT, j.executable, j.getArgv()) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 159 | if res: |
| 160 | sys.exit(res) |
| 161 | elif isinstance(j, Jobs.PipedJob): |
| 162 | raise NotImplementedError,"Piped jobs aren't implemented yet." |
| 163 | else: |
| 164 | raise ValueError,'Encountered unknown job.' |
| 165 | |
| 166 | def claim(self, option): |
| 167 | # FIXME: Move to OptionList once introduced and implement. |
| 168 | pass |
| 169 | |
| 170 | def warning(self, message): |
| 171 | print >>sys.stderr,'%s: %s' % (sys.argv[0], message) |
| 172 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 173 | def printOptions(self, args): |
| 174 | for i,arg in enumerate(args): |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 175 | if isinstance(arg, Arguments.MultipleValuesArg): |
| 176 | values = list(args.getValues(arg)) |
| 177 | elif isinstance(arg, Arguments.ValueArg): |
| 178 | values = [args.getValue(arg)] |
| 179 | elif isinstance(arg, Arguments.JoinedAndSeparateValuesArg): |
| 180 | values = [args.getJoinedValue(arg), args.getSeparateValue(arg)] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 181 | else: |
| 182 | values = [] |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 183 | print 'Option %d - Name: "%s", Values: {%s}' % (i, arg.opt.name, |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 184 | ', '.join(['"%s"' % v |
| 185 | for v in values])) |
| 186 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 187 | def printPhases(self, phases, args): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 188 | def printPhase(p, f, steps, arch=None): |
| 189 | if p in steps: |
| 190 | return steps[p] |
| 191 | elif isinstance(p, Phases.BindArchAction): |
| 192 | for kid in p.inputs: |
| 193 | printPhase(kid, f, steps, p.arch) |
| 194 | steps[p] = len(steps) |
| 195 | return |
| 196 | |
| 197 | if isinstance(p, Phases.InputAction): |
| 198 | phaseName = 'input' |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 199 | inputStr = '"%s"' % args.getValue(p.filename) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 200 | else: |
| 201 | phaseName = p.phase.name |
| 202 | inputs = [printPhase(i, f, steps, arch) |
| 203 | for i in p.inputs] |
| 204 | inputStr = '{%s}' % ', '.join(map(str, inputs)) |
| 205 | if arch is not None: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 206 | phaseName += '-' + args.getValue(arch) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 207 | steps[p] = index = len(steps) |
| 208 | print "%d: %s, %s, %s" % (index,phaseName,inputStr,p.type.name) |
| 209 | return index |
| 210 | steps = {} |
| 211 | for phase in phases: |
| 212 | printPhase(phase, sys.stdout, steps) |
| 213 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 214 | def handleImmediateOptions(self, args): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 215 | # FIXME: Some driver Arguments are consumed right off the bat, |
| 216 | # like -dumpversion. Currently the gcc-dd handles these |
| 217 | # poorly, so we should be ok handling them upfront instead of |
| 218 | # after driver-driver level dispatching. |
| 219 | # |
| 220 | # FIXME: The actual order of these options in gcc is all over the |
| 221 | # place. The -dump ones seem to be first and in specification |
| 222 | # order, but there are other levels of precedence. For example, |
| 223 | # -print-search-dirs is evaluated before -print-prog-name=, |
| 224 | # regardless of order (and the last instance of -print-prog-name= |
| 225 | # wins verse itself). |
| 226 | # |
| 227 | # FIXME: Do we want to report "argument unused" type errors in the |
| 228 | # presence of things like -dumpmachine and -print-search-dirs? |
| 229 | # Probably not. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 230 | arg = args.getLastArg(self.parser.dumpmachineOption) |
| 231 | if arg: |
| 232 | print 'FIXME: %s' % arg.opt.name |
| 233 | sys.exit(1) |
| 234 | |
| 235 | arg = args.getLastArg(self.parser.dumpspecsOption) |
| 236 | if arg: |
| 237 | print 'FIXME: %s' % arg.opt.name |
| 238 | sys.exit(1) |
| 239 | |
| 240 | arg = args.getLastArg(self.parser.dumpversionOption) |
| 241 | if arg: |
| 242 | print 'FIXME: %s' % arg.opt.name |
| 243 | sys.exit(1) |
| 244 | |
| 245 | arg = args.getLastArg(self.parser.printFileNameOption) |
| 246 | if arg: |
| 247 | print 'FIXME: %s' % arg.opt.name |
| 248 | sys.exit(1) |
| 249 | |
| 250 | arg = args.getLastArg(self.parser.printMultiDirectoryOption) |
| 251 | if arg: |
| 252 | print 'FIXME: %s' % arg.opt.name |
| 253 | sys.exit(1) |
| 254 | |
| 255 | arg = args.getLastArg(self.parser.printMultiLibOption) |
| 256 | if arg: |
| 257 | print 'FIXME: %s' % arg.opt.name |
| 258 | sys.exit(1) |
| 259 | |
| 260 | arg = args.getLastArg(self.parser.printProgNameOption) |
| 261 | if arg: |
| 262 | print 'FIXME: %s' % arg.opt.name |
| 263 | sys.exit(1) |
| 264 | |
| 265 | arg = args.getLastArg(self.parser.printLibgccFilenameOption) |
| 266 | if arg: |
| 267 | print 'FIXME: %s' % arg.opt.name |
| 268 | sys.exit(1) |
| 269 | |
| 270 | arg = args.getLastArg(self.parser.printSearchDirsOption) |
| 271 | if arg: |
| 272 | print 'FIXME: %s' % arg.opt.name |
| 273 | sys.exit(1) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 274 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 275 | def buildNormalPipeline(self, args): |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 276 | hasCombine = args.getLastArg(self.parser.combineOption) |
| 277 | hasSyntaxOnly = args.getLastArg(self.parser.syntaxOnlyOption) |
| 278 | hasDashC = args.getLastArg(self.parser.cOption) |
| 279 | hasDashE = args.getLastArg(self.parser.EOption) |
| 280 | hasDashS = args.getLastArg(self.parser.SOption) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 281 | |
| 282 | inputType = None |
| 283 | inputTypeOpt = None |
| 284 | inputs = [] |
| 285 | for a in args: |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 286 | if a.opt is self.parser.inputOption: |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 287 | if inputType is None: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 288 | base,ext = os.path.splitext(args.getValue(a)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 289 | if ext and ext in Types.kTypeSuffixMap: |
| 290 | klass = Types.kTypeSuffixMap[ext] |
| 291 | else: |
| 292 | # FIXME: Its not clear why we shouldn't just |
| 293 | # revert to unknown. I think this is more likely a |
| 294 | # bug / unintended behavior in gcc. Not very |
| 295 | # important though. |
| 296 | klass = Types.ObjectType |
| 297 | else: |
| 298 | assert inputTypeOpt is not None |
| 299 | self.claim(inputTypeOpt) |
| 300 | klass = inputType |
| 301 | inputs.append((klass, a)) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 302 | elif a.opt is self.parser.filelistOption: |
| 303 | # Treat as a linker input. Investigate how gcc is |
| 304 | # handling this. |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 305 | # |
| 306 | # FIXME: This might not be good enough. We may |
| 307 | # need to introduce another type for this case, so |
| 308 | # that other code which needs to know the inputs |
| 309 | # handles this properly. Best not to try and lipo |
| 310 | # this, for example. |
Daniel Dunbar | e99f926 | 2009-01-11 22:03:55 +0000 | [diff] [blame^] | 311 | # |
| 312 | # FIXME: Actually, this is just flat out broken, the |
| 313 | # tools expect inputs to be accessible by .getValue |
| 314 | # but that of course only yields the argument. |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 315 | inputs.append((Types.ObjectType, a)) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 316 | elif a.opt is self.parser.xOption: |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 317 | self.claim(a) |
| 318 | inputTypeOpt = a |
| 319 | value = args.getValue(a) |
| 320 | if value in Types.kTypeSpecifierMap: |
| 321 | inputType = Types.kTypeSpecifierMap[value] |
| 322 | else: |
| 323 | # FIXME: How are we going to handle diagnostics. |
| 324 | self.warning("language %s not recognized" % value) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 325 | |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 326 | # FIXME: Its not clear why we shouldn't just |
| 327 | # revert to unknown. I think this is more likely a |
| 328 | # bug / unintended behavior in gcc. Not very |
| 329 | # important though. |
| 330 | inputType = ObjectType |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 331 | |
| 332 | # We claim things here so that options for which we silently allow |
| 333 | # override only ever claim the used option. |
| 334 | if hasCombine: |
| 335 | self.claim(hasCombine) |
| 336 | |
| 337 | finalPhase = Phases.Phase.eOrderPostAssemble |
| 338 | finalPhaseOpt = None |
| 339 | |
| 340 | # Determine what compilation mode we are in. |
| 341 | if hasDashE: |
| 342 | finalPhase = Phases.Phase.eOrderPreprocess |
| 343 | finalPhaseOpt = hasDashE |
| 344 | elif hasSyntaxOnly: |
| 345 | finalPhase = Phases.Phase.eOrderCompile |
| 346 | finalPhaseOpt = hasSyntaxOnly |
| 347 | elif hasDashS: |
| 348 | finalPhase = Phases.Phase.eOrderCompile |
| 349 | finalPhaseOpt = hasDashS |
| 350 | elif hasDashC: |
| 351 | finalPhase = Phases.Phase.eOrderAssemble |
| 352 | finalPhaseOpt = hasDashC |
| 353 | |
| 354 | if finalPhaseOpt: |
| 355 | self.claim(finalPhaseOpt) |
| 356 | |
| 357 | # FIXME: Support -combine. |
| 358 | if hasCombine: |
| 359 | raise NotImplementedError,"-combine is not yet supported." |
| 360 | |
| 361 | actions = [] |
| 362 | linkerInputs = [] |
| 363 | # FIXME: This is gross. |
| 364 | linkPhase = Phases.LinkPhase() |
| 365 | for klass,input in inputs: |
| 366 | # Figure out what step to start at. |
| 367 | |
| 368 | # FIXME: This should be part of the input class probably? |
| 369 | # Altough it doesn't quite fit there either, things like |
| 370 | # asm-with-preprocess don't easily fit into a linear scheme. |
| 371 | |
| 372 | # FIXME: I think we are going to end up wanting to just build |
| 373 | # a simple FSA which we run the inputs down. |
| 374 | sequence = [] |
| 375 | if klass.preprocess: |
| 376 | sequence.append(Phases.PreprocessPhase()) |
| 377 | if klass == Types.ObjectType: |
| 378 | sequence.append(linkPhase) |
| 379 | elif klass.onlyAssemble: |
| 380 | sequence.extend([Phases.AssemblePhase(), |
| 381 | linkPhase]) |
| 382 | elif klass.onlyPrecompile: |
| 383 | sequence.append(Phases.PrecompilePhase()) |
| 384 | else: |
| 385 | sequence.extend([Phases.CompilePhase(), |
| 386 | Phases.AssemblePhase(), |
| 387 | linkPhase]) |
| 388 | |
| 389 | if sequence[0].order > finalPhase: |
| 390 | assert finalPhaseOpt and finalPhaseOpt.opt |
| 391 | # FIXME: Explain what type of input file is. Or just match |
| 392 | # gcc warning. |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 393 | self.warning("%s: %s input file unused when %s is present" % (args.getValue(input), |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 394 | sequence[0].name, |
| 395 | finalPhaseOpt.opt.name)) |
| 396 | else: |
| 397 | # Build the pipeline for this file. |
| 398 | |
| 399 | current = Phases.InputAction(input, klass) |
| 400 | for transition in sequence: |
| 401 | # If the current action produces no output, or we are |
| 402 | # past what the user requested, we are done. |
| 403 | if (current.type is Types.NothingType or |
| 404 | transition.order > finalPhase): |
| 405 | break |
| 406 | else: |
| 407 | if isinstance(transition, Phases.PreprocessPhase): |
| 408 | assert isinstance(klass.preprocess, Types.InputType) |
| 409 | current = Phases.JobAction(transition, |
| 410 | [current], |
| 411 | klass.preprocess) |
| 412 | elif isinstance(transition, Phases.PrecompilePhase): |
| 413 | current = Phases.JobAction(transition, |
| 414 | [current], |
| 415 | Types.PCHType) |
| 416 | elif isinstance(transition, Phases.CompilePhase): |
| 417 | if hasSyntaxOnly: |
| 418 | output = Types.NothingType |
| 419 | else: |
| 420 | output = Types.AsmTypeNoPP |
| 421 | current = Phases.JobAction(transition, |
| 422 | [current], |
| 423 | output) |
| 424 | elif isinstance(transition, Phases.AssemblePhase): |
| 425 | current = Phases.JobAction(transition, |
| 426 | [current], |
| 427 | Types.ObjectType) |
| 428 | elif transition is linkPhase: |
| 429 | linkerInputs.append(current) |
| 430 | current = None |
| 431 | break |
| 432 | else: |
| 433 | raise RuntimeError,'Unrecognized transition: %s.' % transition |
| 434 | pass |
| 435 | |
| 436 | if current is not None: |
| 437 | assert not isinstance(current, Phases.InputAction) |
| 438 | actions.append(current) |
| 439 | |
| 440 | if linkerInputs: |
| 441 | actions.append(Phases.JobAction(linkPhase, |
| 442 | linkerInputs, |
| 443 | Types.ImageType)) |
| 444 | |
| 445 | return actions |
| 446 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 447 | def buildPipeline(self, args): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 448 | # FIXME: We need to handle canonicalization of the specified arch. |
| 449 | |
| 450 | archs = [] |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 451 | hasDashM = None |
| 452 | hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or |
| 453 | args.getLastArg(self.parser.saveTempsOption2)) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 454 | for arg in args: |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 455 | if arg.opt is self.parser.archOption: |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 456 | archs.append(arg) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 457 | elif arg.opt.name.startswith('-M'): |
| 458 | hasDashM = arg |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 459 | |
| 460 | if not archs: |
Daniel Dunbar | 9066af8 | 2009-01-09 01:00:40 +0000 | [diff] [blame] | 461 | archs.append(args.makeSeparateArg(self.hostInfo.getArchName(), |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 462 | self.parser.archOption)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 463 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 464 | actions = self.buildNormalPipeline(args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 465 | |
| 466 | # FIXME: Use custom exception for this. |
| 467 | # |
| 468 | # FIXME: We killed off some others but these aren't yet detected in |
| 469 | # a functional manner. If we added information to jobs about which |
| 470 | # "auxiliary" files they wrote then we could detect the conflict |
| 471 | # these cause downstream. |
| 472 | if len(archs) > 1: |
| 473 | if hasDashM: |
| 474 | raise ValueError,"Cannot use -M options with multiple arch flags." |
| 475 | elif hasSaveTemps: |
| 476 | raise ValueError,"Cannot use -save-temps with multiple arch flags." |
| 477 | |
| 478 | # Execute once per arch. |
| 479 | finalActions = [] |
| 480 | for p in actions: |
| 481 | # Make sure we can lipo this kind of output. If not (and it |
| 482 | # is an actual output) then we disallow, since we can't |
| 483 | # create an output file with the right name without |
| 484 | # overwriting it. We could remove this oddity by just |
| 485 | # changing the output names to include the arch, which would |
| 486 | # also fix -save-temps. Compatibility wins for now. |
| 487 | # |
| 488 | # FIXME: Is this error substantially less useful than |
| 489 | # gcc-dd's? The main problem is that "Cannot use compiler |
| 490 | # output with multiple arch flags" won't make sense to most |
| 491 | # developers. |
| 492 | if (len(archs) > 1 and |
| 493 | p.type not in (Types.NothingType,Types.ObjectType,Types.ImageType)): |
| 494 | raise ValueError,'Cannot use %s output with multiple arch flags.' % p.type.name |
| 495 | |
| 496 | inputs = [] |
| 497 | for arch in archs: |
| 498 | inputs.append(Phases.BindArchAction(p, arch)) |
| 499 | |
| 500 | # Lipo if necessary. We do it this way because we need to set |
| 501 | # the arch flag so that -Xarch_ gets rewritten. |
| 502 | if len(inputs) == 1 or p.type == Types.NothingType: |
| 503 | finalActions.extend(inputs) |
| 504 | else: |
| 505 | finalActions.append(Phases.JobAction(Phases.LipoPhase(), |
| 506 | inputs, |
| 507 | p.type)) |
| 508 | |
| 509 | # FIXME: We need to add -Wl,arch_multiple and -Wl,final_output in |
| 510 | # certain cases. This may be icky because we need to figure out the |
| 511 | # mode first. Current plan is to hack on the pipeline once it is built |
| 512 | # and we know what is being spit out. This avoids having to handling |
| 513 | # things like -c and -combine in multiple places. |
| 514 | # |
| 515 | # The annoying one of these is -Wl,final_output because it involves |
| 516 | # communication across different phases. |
| 517 | # |
| 518 | # Hopefully we can do this purely as part of the binding, but |
| 519 | # leaving comment here for now until it is clear this works. |
| 520 | |
| 521 | return finalActions |
| 522 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 523 | def bindPhases(self, phases, args): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 524 | jobs = Jobs.JobList() |
| 525 | |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 526 | finalOutput = args.getLastArg(self.parser.oOption) |
| 527 | hasSaveTemps = (args.getLastArg(self.parser.saveTempsOption) or |
| 528 | args.getLastArg(self.parser.saveTempsOption2)) |
| 529 | hasNoIntegratedCPP = args.getLastArg(self.parser.noIntegratedCPPOption) |
| 530 | hasPipe = args.getLastArg(self.parser.pipeOption) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 531 | forward = [] |
| 532 | for a in args: |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 533 | if a.opt is self.parser.inputOption: |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 534 | pass |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 535 | |
| 536 | # FIXME: Needs to be part of option. |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 537 | elif a.opt.name in ('-E', '-S', '-c', |
| 538 | '-arch', '-fsyntax-only', '-combine', '-x', |
| 539 | '-###'): |
| 540 | pass |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 542 | else: |
| 543 | forward.append(a) |
| 544 | |
| 545 | # We claim things here so that options for which we silently allow |
| 546 | # override only ever claim the used option. |
| 547 | if hasPipe: |
| 548 | self.claim(hasPipe) |
| 549 | # FIXME: Hack, override -pipe till we support it. |
| 550 | hasPipe = None |
| 551 | # Claim these here. Its not completely accurate but any warnings |
| 552 | # about these being unused are likely to be noise anyway. |
| 553 | if hasSaveTemps: |
| 554 | self.claim(hasSaveTemps) |
| 555 | if hasNoIntegratedCPP: |
| 556 | self.claim(hasNoIntegratedCPP) |
| 557 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 558 | class InputInfo: |
| 559 | def __init__(self, source, type, baseInput): |
| 560 | self.source = source |
| 561 | self.type = type |
| 562 | self.baseInput = baseInput |
| 563 | |
| 564 | def __repr__(self): |
| 565 | return '%s(%r, %r, %r)' % (self.__class__.__name__, |
| 566 | self.source, self.type, self.baseInput) |
| 567 | |
| 568 | def createJobs(phase, forwardArgs, |
| 569 | canAcceptPipe=False, atTopLevel=False, arch=None): |
| 570 | if isinstance(phase, Phases.InputAction): |
| 571 | return InputInfo(phase.filename, phase.type, phase.filename) |
| 572 | elif isinstance(phase, Phases.BindArchAction): |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 573 | archName = args.getValue(phase.arch) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 574 | filteredArgs = [] |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 575 | for arg in forwardArgs: |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 576 | if arg.opt is self.parser.archOption: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 577 | if arg is phase.arch: |
| 578 | filteredArgs.append(arg) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 579 | elif arg.opt is self.parser.XarchOption: |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 580 | # FIXME: gcc-dd has another conditional for passing |
| 581 | # through, when the arch conditional array has an empty |
| 582 | # string. Why? |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 583 | if args.getJoinedValue(arg) == archName: |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 584 | # FIXME: This is wrong, we don't want a |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 585 | # unknown arg we want an actual parsed |
| 586 | # version of this arg. |
| 587 | filteredArgs.append(args.makeUnknownArg(args.getSeparateValue(arg))) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 588 | else: |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 589 | filteredArgs.append(arg) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 590 | |
| 591 | return createJobs(phase.inputs[0], filteredArgs, |
| 592 | canAcceptPipe, atTopLevel, phase.arch) |
| 593 | |
| 594 | assert isinstance(phase, Phases.JobAction) |
Daniel Dunbar | 4312472 | 2009-01-10 02:07:54 +0000 | [diff] [blame] | 595 | tool = self.toolChain.selectTool(phase) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 596 | |
| 597 | # See if we should use an integrated CPP. We only use an |
| 598 | # integrated cpp when we have exactly one input, since this is |
| 599 | # the only use case we care about. |
| 600 | useIntegratedCPP = False |
| 601 | inputList = phase.inputs |
| 602 | if (not hasNoIntegratedCPP and |
| 603 | not hasSaveTemps and |
| 604 | tool.hasIntegratedCPP()): |
| 605 | if (len(phase.inputs) == 1 and |
| 606 | isinstance(phase.inputs[0].phase, Phases.PreprocessPhase)): |
| 607 | useIntegratedCPP = True |
| 608 | inputList = phase.inputs[0].inputs |
| 609 | |
| 610 | # Only try to use pipes when exactly one input. |
| 611 | canAcceptPipe = len(inputList) == 1 and tool.acceptsPipedInput() |
| 612 | inputs = [createJobs(p, forwardArgs, canAcceptPipe, False, arch) for p in inputList] |
| 613 | |
| 614 | # Determine if we should output to a pipe. |
| 615 | canOutputToPipe = canAcceptPipe and tool.canPipeOutput() |
| 616 | outputToPipe = False |
| 617 | if canOutputToPipe: |
| 618 | # Some things default to writing to a pipe if the final |
| 619 | # phase and there was no user override. |
| 620 | # |
| 621 | # FIXME: What is the best way to handle this? |
| 622 | if (atTopLevel and |
| 623 | isinstance(phase, Phases.PreprocessPhase) and |
| 624 | not finalOutput): |
| 625 | outputToPipe = True |
| 626 | elif hasPipe: |
| 627 | outputToPipe = True |
| 628 | |
| 629 | # Figure out where to put the job (pipes). |
| 630 | jobList = jobs |
| 631 | if canAcceptPipe and isinstance(inputs[0].source, Jobs.PipedJob): |
| 632 | jobList = inputs[0].source |
| 633 | |
| 634 | # Figure out where to put the output. |
| 635 | baseInput = inputs[0].baseInput |
| 636 | if phase.type == Types.NothingType: |
| 637 | output = None |
| 638 | elif outputToPipe: |
| 639 | if isinstance(jobList, Jobs.PipedJob): |
| 640 | output = jobList |
| 641 | else: |
| 642 | jobList = output = Jobs.PipedJob([]) |
| 643 | jobs.addJob(output) |
| 644 | else: |
| 645 | # Figure out what the derived output location would be. |
| 646 | # |
| 647 | # FIXME: gcc has some special case in here so that it doesn't |
| 648 | # create output files if they would conflict with an input. |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 649 | inputName = args.getValue(baseInput) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 650 | if phase.type is Types.ImageType: |
| 651 | namedOutput = "a.out" |
| 652 | else: |
| 653 | base,_ = os.path.splitext(inputName) |
| 654 | assert phase.type.tempSuffix is not None |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 655 | namedOutput = base + '.' + phase.type.tempSuffix |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 656 | |
| 657 | # Output to user requested destination? |
| 658 | if atTopLevel and finalOutput: |
| 659 | output = finalOutput |
| 660 | # Contruct a named destination? |
| 661 | elif atTopLevel or hasSaveTemps: |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 662 | output = args.makeSeparateArg(namedOutput, |
| 663 | self.parser.oOption) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 664 | else: |
| 665 | # Output to temp file... |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 666 | fd,filename = tempfile.mkstemp(suffix='.'+phase.type.tempSuffix) |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 667 | output = args.makeSeparateArg(filename, |
| 668 | self.parser.oOption) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | db43990 | 2009-01-07 18:40:45 +0000 | [diff] [blame] | 670 | tool.constructJob(phase, arch, jobList, inputs, output, phase.type, |
| 671 | forwardArgs, args) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 672 | |
| 673 | return InputInfo(output, phase.type, baseInput) |
| 674 | |
| 675 | # It is an error to provide a -o option if we are making multiple |
| 676 | # output files. |
| 677 | if finalOutput and len([a for a in phases if a.type is not Types.NothingType]) > 1: |
| 678 | # FIXME: Custom exception. |
| 679 | raise ValueError,"Cannot specify -o when generating multiple files." |
| 680 | |
| 681 | for phase in phases: |
| 682 | createJobs(phase, forward, canAcceptPipe=True, atTopLevel=True) |
| 683 | |
| 684 | return jobs |