Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 1 | class Option(object): |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 2 | """Option - Root option class.""" |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 3 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 4 | def __init__(self, name, group=None, isLinkerInput=False, noOptAsInput=False): |
| 5 | assert group is None or isinstance(group, OptionGroup) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 6 | self.name = name |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 7 | self.group = group |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 8 | self.isLinkerInput = isLinkerInput |
| 9 | self.noOptAsInput = noOptAsInput |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 10 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 11 | def matches(self, opt): |
| 12 | """matches(opt) -> bool |
| 13 | |
| 14 | Predicate for whether this option is part of the given option |
| 15 | (which may be a group).""" |
| 16 | if self is opt: |
| 17 | return True |
| 18 | elif self.group: |
| 19 | return self.group.matches(opt) |
| 20 | else: |
| 21 | return False |
| 22 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 23 | def accept(self, index, arg, it): |
| 24 | """accept(index, arg, iterator) -> Arg or None |
| 25 | |
| 26 | Accept the argument at the given index, returning an Arg, or |
| 27 | return None if the option does not accept this argument. |
| 28 | |
| 29 | May raise MissingArgumentError. |
| 30 | """ |
| 31 | abstract |
| 32 | |
| 33 | def __repr__(self): |
| 34 | return '<%s name=%r>' % (self.__class__.__name__, |
| 35 | self.name) |
| 36 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 37 | class OptionGroup(Option): |
| 38 | """OptionGroup - A fake option class used to group options so that |
| 39 | the driver can efficiently refer to an entire set of options.""" |
| 40 | |
| 41 | def __init__(self, name): |
| 42 | super(OptionGroup, self).__init__(name) |
| 43 | |
| 44 | def accept(self, index, arg, it): |
| 45 | raise RuntimeError,"accept() should never be called on an OptionGroup" |
| 46 | |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 47 | # Dummy options |
| 48 | |
| 49 | class InputOption(Option): |
| 50 | def __init__(self): |
| 51 | super(InputOption, self).__init__('<input>') |
| 52 | |
| 53 | def accept(self): |
| 54 | raise RuntimeError,"accept() should never be used on InputOption instance." |
| 55 | |
| 56 | class UnknownOption(Option): |
| 57 | def __init__(self): |
| 58 | super(UnknownOption, self).__init__('<unknown>') |
| 59 | |
| 60 | def accept(self): |
| 61 | raise RuntimeError,"accept() should never be used on UnknownOption instance." |
| 62 | |
| 63 | # Normal options |
| 64 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 65 | class FlagOption(Option): |
| 66 | """An option which takes no arguments.""" |
| 67 | |
| 68 | def accept(self, index, arg, it): |
| 69 | if arg == self.name: |
| 70 | return Arg(index, self) |
| 71 | |
| 72 | class JoinedOption(Option): |
| 73 | """An option which literally prefixes its argument.""" |
| 74 | |
| 75 | def accept(self, index, arg, it): |
| 76 | if arg.startswith(self.name): |
| 77 | return JoinedValueArg(index, self) |
| 78 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 79 | class CommaJoinedOption(Option): |
| 80 | """An option which literally prefixs its argument, but which |
| 81 | conceptually may have an arbitrary number of arguments which are |
| 82 | separated by commas.""" |
| 83 | |
| 84 | def accept(self, index, arg, it): |
| 85 | if arg.startswith(self.name): |
| 86 | return CommaJoinedValuesArg(index, self) |
| 87 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 88 | class SeparateOption(Option): |
| 89 | """An option which is followed by its value.""" |
| 90 | |
| 91 | def accept(self, index, arg, it): |
| 92 | if arg == self.name: |
| 93 | try: |
| 94 | _,value = it.next() |
| 95 | except StopIteration: |
| 96 | raise MissingArgumentError,self |
| 97 | return SeparateValueArg(index, self) |
| 98 | |
| 99 | class MultiArgOption(Option): |
| 100 | """An option which takes multiple arguments.""" |
| 101 | |
| 102 | def __init__(self, name, numArgs): |
| 103 | assert numArgs > 1 |
| 104 | super(MultiArgOption, self).__init__(name) |
| 105 | self.numArgs = numArgs |
| 106 | |
| 107 | def accept(self, index, arg, it): |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 108 | if arg == self.name: |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 109 | try: |
| 110 | values = [it.next()[1] for i in range(self.numArgs)] |
| 111 | except StopIteration: |
| 112 | raise MissingArgumentError,self |
| 113 | return MultipleValuesArg(index, self) |
| 114 | |
| 115 | class JoinedOrSeparateOption(Option): |
| 116 | """An option which either literally prefixes its value or is |
| 117 | followed by an value.""" |
| 118 | |
| 119 | def accept(self, index, arg, it): |
| 120 | if arg.startswith(self.name): |
| 121 | if len(arg) != len(self.name): # Joined case |
| 122 | return JoinedValueArg(index, self) |
| 123 | else: |
| 124 | try: |
| 125 | _,value = it.next() |
| 126 | except StopIteration: |
| 127 | raise MissingArgumentError,self |
| 128 | return SeparateValueArg(index, self) |
| 129 | |
| 130 | class JoinedAndSeparateOption(Option): |
| 131 | """An option which literally prefixes its value and is followed by |
| 132 | an value.""" |
| 133 | |
| 134 | def accept(self, index, arg, it): |
| 135 | if arg.startswith(self.name): |
| 136 | try: |
| 137 | _,value = it.next() |
| 138 | except StopIteration: |
| 139 | raise MissingArgumentError,self |
| 140 | return JoinedAndSeparateValuesArg(index, self) |
| 141 | |
| 142 | ### |
| 143 | |
| 144 | class Arg(object): |
| 145 | """Arg - Base class for actual driver arguments.""" |
| 146 | def __init__(self, index, opt): |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 147 | assert opt is not None |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 148 | self.index = index |
| 149 | self.opt = opt |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 150 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 151 | def __repr__(self): |
| 152 | return '<%s index=%r opt=%r>' % (self.__class__.__name__, |
| 153 | self.index, |
| 154 | self.opt) |
| 155 | |
| 156 | def render(self, args): |
| 157 | """render(args) -> [str] |
| 158 | |
| 159 | Map the argument into a list of actual program arguments, |
| 160 | given the source argument array.""" |
| 161 | assert self.opt |
| 162 | return [self.opt.name] |
| 163 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 164 | def renderAsInput(self, args): |
| 165 | return self.render(args) |
| 166 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 167 | class ValueArg(Arg): |
| 168 | """ValueArg - An instance of an option which has an argument.""" |
| 169 | |
| 170 | def getValue(self, args): |
| 171 | abstract |
| 172 | |
Daniel Dunbar | 996ce96 | 2009-01-12 07:40:25 +0000 | [diff] [blame] | 173 | def getValues(self, args): |
| 174 | return [self.getValue(args)] |
| 175 | |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 176 | class PositionalArg(ValueArg): |
| 177 | """PositionalArg - A simple positional argument.""" |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 178 | |
| 179 | def getValue(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 180 | return args.getInputString(self.index) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 181 | |
| 182 | def render(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 183 | return [args.getInputString(self.index)] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 184 | |
| 185 | class JoinedValueArg(ValueArg): |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 186 | """JoinedValueArg - A single value argument where the value is |
| 187 | joined (suffixed) to the option.""" |
| 188 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 189 | def getValue(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 190 | return args.getInputString(self.index)[len(self.opt.name):] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 192 | def render(self, args): |
| 193 | return [self.opt.name + self.getValue(args)] |
| 194 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 195 | def renderAsInput(self, args): |
| 196 | if self.opt.noOptAsInput: |
| 197 | return [self.getValue(args)] |
| 198 | return self.render(args) |
| 199 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 200 | class SeparateValueArg(ValueArg): |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 201 | """SeparateValueArg - A single value argument where the value |
| 202 | follows the option in the argument vector.""" |
| 203 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 204 | def getValue(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 205 | return args.getInputString(self.index, offset=1) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 206 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 207 | def render(self, args): |
| 208 | return [self.opt.name, self.getValue(args)] |
| 209 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 210 | def renderAsInput(self, args): |
| 211 | if self.opt.noOptAsInput: |
| 212 | return [self.getValue(args)] |
| 213 | return self.render(args) |
| 214 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 215 | class MultipleValuesArg(Arg): |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 216 | """MultipleValuesArg - An argument with multiple values which |
| 217 | follow the option in the argument vector.""" |
| 218 | |
| 219 | # FIXME: Should we unify this with SeparateValueArg? |
| 220 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 221 | def getValues(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 222 | return [args.getInputString(self.index, offset=1+i) |
| 223 | for i in range(self.opt.numArgs)] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 224 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 225 | def render(self, args): |
| 226 | return [self.opt.name] + self.getValues(args) |
| 227 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 228 | class CommaJoinedValuesArg(Arg): |
| 229 | """CommaJoinedValuesArg - An argument with multiple values joined |
| 230 | by commas and joined (suffixed) to the option. |
| 231 | |
| 232 | The key point of this arg is that it renders its values into |
| 233 | separate arguments, which allows it to be used as a generic |
| 234 | mechanism for passing arguments through to tools.""" |
| 235 | |
| 236 | def getValues(self, args): |
| 237 | return args.getInputString(self.index)[len(self.opt.name):].split(',') |
| 238 | |
| 239 | def render(self, args): |
| 240 | return [self.opt.name + ','.join(self.getValues(args))] |
| 241 | |
| 242 | def renderAsInput(self, args): |
| 243 | return self.getValues(args) |
| 244 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 245 | # FIXME: Man, this is lame. It is only used by -Xarch. Maybe easier to |
| 246 | # just special case? |
| 247 | class JoinedAndSeparateValuesArg(Arg): |
| 248 | """JoinedAndSeparateValuesArg - An argument with both joined and |
| 249 | separate values.""" |
| 250 | |
| 251 | def getJoinedValue(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 252 | return args.getInputString(self.index)[len(self.opt.name):] |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 253 | |
| 254 | def getSeparateValue(self, args): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 255 | return args.getInputString(self.index, offset=1) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 256 | |
| 257 | def render(self, args): |
| 258 | return ([self.opt.name + self.getJoinedValue(args)] + |
| 259 | [self.getSeparateValue(args)]) |
| 260 | |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 261 | ### |
| 262 | |
| 263 | class InputIndex: |
| 264 | def __init__(self, sourceId, pos): |
| 265 | self.sourceId = sourceId |
| 266 | self.pos = pos |
| 267 | |
| 268 | def __repr__(self): |
| 269 | return 'InputIndex(%d, %d)' % (self.sourceId, self.pos) |
| 270 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 271 | class ArgList: |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 272 | """ArgList - Collect an input argument vector along with a set of parsed Args |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 273 | and supporting information.""" |
| 274 | |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 275 | def __init__(self, parser, argv): |
| 276 | self.parser = parser |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 277 | self.argv = list(argv) |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 278 | self.syntheticArgv = [] |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 279 | self.lastArgs = {} |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 280 | self.lastGroupArgs = {} |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 281 | self.args = [] |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 282 | |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 283 | def getArgs(self, option): |
| 284 | # FIXME: How efficient do we want to make this. One reasonable |
| 285 | # solution would be to embed a linked list inside each arg and |
| 286 | # automatically chain them (with pointers to head and |
| 287 | # tail). This gives us efficient access to the (first, last, |
| 288 | # all) arg(s) with little overhead. |
| 289 | for arg in self.args: |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 290 | if arg.opt.matches(option): |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 291 | yield arg |
| 292 | |
Daniel Dunbar | 996ce96 | 2009-01-12 07:40:25 +0000 | [diff] [blame] | 293 | def getArgs2(self, optionA, optionB): |
| 294 | """getArgs2 - Iterate over all arguments for two options, in |
| 295 | the order they were specified.""" |
| 296 | # As long as getArgs is efficient, we can easily make this |
| 297 | # efficient by iterating both at once and always taking the |
| 298 | # earlier arg. |
| 299 | for arg in self.args: |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 300 | if (arg.opt.matches(optionA) or |
| 301 | arg.opt.matches(optionB)): |
Daniel Dunbar | 996ce96 | 2009-01-12 07:40:25 +0000 | [diff] [blame] | 302 | yield arg |
| 303 | |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 304 | def getArgs3(self, optionA, optionB, optionC): |
| 305 | """getArgs3 - Iterate over all arguments for three options, in |
| 306 | the order they were specified.""" |
| 307 | for arg in self.args: |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 308 | if (arg.opt.matches(optionA) or |
| 309 | arg.opt.matches(optionB) or |
| 310 | arg.opt.matches(optionC)): |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 311 | yield arg |
| 312 | |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 313 | def getLastArg(self, option): |
| 314 | return self.lastArgs.get(option) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 315 | |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 316 | def getInputString(self, index, offset=0): |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 317 | # Source 0 is argv. |
| 318 | if index.sourceId == 0: |
| 319 | return self.argv[index.pos + offset] |
| 320 | |
| 321 | # Source 1 is synthetic argv. |
| 322 | if index.sourceId == 1: |
| 323 | return self.syntheticArgv[index.pos + offset] |
| 324 | |
| 325 | raise RuntimeError,'Unknown source ID for index.' |
| 326 | |
Daniel Dunbar | 0fe5a4f | 2009-01-11 22:42:24 +0000 | [diff] [blame] | 327 | def addLastArg(self, output, option): |
| 328 | """addLastArgs - Extend the given output vector with the last |
| 329 | instance of a given option.""" |
| 330 | arg = self.getLastArg(option) |
| 331 | if arg: |
| 332 | output.extend(self.render(arg)) |
| 333 | |
| 334 | def addAllArgs(self, output, option): |
| 335 | """addAllArgs - Extend the given output vector with all |
| 336 | instances of a given option.""" |
| 337 | for arg in self.getArgs(option): |
| 338 | output.extend(self.render(arg)) |
| 339 | |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 340 | def addAllArgs2(self, output, optionA, optionB): |
| 341 | """addAllArgs2 - Extend the given output vector with all |
| 342 | instances of two given option, with relative order preserved.""" |
| 343 | for arg in self.getArgs2(optionA, optionB): |
| 344 | output.extend(self.render(arg)) |
| 345 | |
| 346 | def addAllArgs3(self, output, optionA, optionB, optionC): |
| 347 | """addAllArgs3 - Extend the given output vector with all |
| 348 | instances of three given option, with relative order preserved.""" |
| 349 | for arg in self.getArgs3(optionA, optionB, optionC): |
| 350 | output.extend(self.render(arg)) |
| 351 | |
Daniel Dunbar | 0fe5a4f | 2009-01-11 22:42:24 +0000 | [diff] [blame] | 352 | def addAllArgsTranslated(self, output, option, translation): |
| 353 | """addAllArgsTranslated - Extend the given output vector with |
| 354 | all instances of a given option, rendered as separate |
| 355 | arguments with the actual option name translated to a user |
| 356 | specified string. For example, '-foox' will be render as |
| 357 | ['-bar', 'x'] if '-foo' was the option and '-bar' was the |
| 358 | translation. |
| 359 | |
| 360 | This routine expects that the option can only yield ValueArg |
| 361 | instances.""" |
| 362 | for arg in self.getArgs(option): |
| 363 | assert isinstance(arg, ValueArg) |
| 364 | output.append(translation) |
| 365 | output.append(self.getValue(arg)) |
| 366 | |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 367 | def makeIndex(self, *strings): |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 368 | pos = len(self.syntheticArgv) |
| 369 | self.syntheticArgv.extend(strings) |
| 370 | return InputIndex(1, pos) |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 372 | def makeFlagArg(self, option): |
| 373 | return Arg(self.makeIndex(option.name), |
| 374 | option) |
| 375 | |
| 376 | def makeInputArg(self, string): |
| 377 | return PositionalArg(self.makeIndex(string), |
| 378 | self.parser.inputOption) |
| 379 | |
| 380 | def makeUnknownArg(self, string): |
| 381 | return PositionalArg(self.makeIndex(string), |
| 382 | self.parser.unknownOption) |
| 383 | |
| 384 | def makeSeparateArg(self, string, option): |
| 385 | return SeparateValueArg(self.makeIndex(option.name, string), |
| 386 | option) |
| 387 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 388 | # Support use as a simple arg list. |
| 389 | |
| 390 | def __iter__(self): |
| 391 | return iter(self.args) |
| 392 | |
| 393 | def append(self, arg): |
| 394 | self.args.append(arg) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 395 | self.lastArgs[arg.opt] = arg |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 396 | if arg.opt.group is not None: |
| 397 | self.lastArgs[arg.opt.group] = arg |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 398 | |
| 399 | # Forwarding methods. |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 400 | # |
| 401 | # FIXME: Clean this up once restructuring is done. |
| 402 | |
| 403 | def render(self, arg): |
| 404 | return arg.render(self) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 405 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 406 | def renderAsInput(self, arg): |
| 407 | return arg.renderAsInput(self) |
| 408 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 409 | def getValue(self, arg): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 410 | return arg.getValue(self) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 411 | |
| 412 | def getValues(self, arg): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 413 | return arg.getValues(self) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 414 | |
| 415 | def getSeparateValue(self, arg): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 416 | return arg.getSeparateValue(self) |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 417 | |
| 418 | def getJoinedValue(self, arg): |
Daniel Dunbar | fb2c5c4 | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 419 | return arg.getJoinedValue(self) |
Daniel Dunbar | 1dd2ada | 2009-01-06 06:32:49 +0000 | [diff] [blame] | 420 | |
| 421 | ### |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 423 | class OptionParser: |
| 424 | def __init__(self): |
| 425 | self.options = [] |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 426 | self.inputOption = InputOption() |
| 427 | self.unknownOption = UnknownOption() |
| 428 | |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 429 | # Driver driver options |
| 430 | self.archOption = self.addOption(SeparateOption('-arch')) |
| 431 | |
| 432 | # Misc driver options |
| 433 | self.addOption(FlagOption('-pass-exit-codes')) |
| 434 | self.addOption(FlagOption('--help')) |
| 435 | self.addOption(FlagOption('--target-help')) |
| 436 | |
| 437 | self.dumpspecsOption = self.addOption(FlagOption('-dumpspecs')) |
| 438 | self.dumpversionOption = self.addOption(FlagOption('-dumpversion')) |
| 439 | self.dumpmachineOption = self.addOption(FlagOption('-dumpmachine')) |
| 440 | self.printSearchDirsOption = self.addOption(FlagOption('-print-search-dirs')) |
| 441 | self.printLibgccFilenameOption = self.addOption(FlagOption('-print-libgcc-file-name')) |
| 442 | # FIXME: Hrm, where does this come from? It isn't always true that |
| 443 | # we take both - and --. For example, gcc --S ... ends up sending |
| 444 | # -fS to cc1. Investigate. |
| 445 | # |
| 446 | # FIXME: Need to implement some form of alias support inside |
| 447 | # getLastOption to handle this. |
| 448 | self.printLibgccFileNameOption2 = self.addOption(FlagOption('--print-libgcc-file-name')) |
| 449 | self.printFileNameOption = self.addOption(JoinedOption('-print-file-name=')) |
| 450 | self.printProgNameOption = self.addOption(JoinedOption('-print-prog-name=')) |
| 451 | self.printProgNameOption2 = self.addOption(JoinedOption('--print-prog-name=')) |
| 452 | self.printMultiDirectoryOption = self.addOption(FlagOption('-print-multi-directory')) |
| 453 | self.printMultiLibOption = self.addOption(FlagOption('-print-multi-lib')) |
| 454 | self.addOption(FlagOption('-print-multi-os-directory')) |
| 455 | |
| 456 | # Hmmm, who really takes this? |
| 457 | self.addOption(FlagOption('--version')) |
| 458 | |
| 459 | # Pipeline control |
| 460 | self.hashHashHashOption = self.addOption(FlagOption('-###')) |
| 461 | self.EOption = self.addOption(FlagOption('-E')) |
| 462 | self.SOption = self.addOption(FlagOption('-S')) |
| 463 | self.cOption = self.addOption(FlagOption('-c')) |
| 464 | self.combineOption = self.addOption(FlagOption('-combine')) |
| 465 | self.noIntegratedCPPOption = self.addOption(FlagOption('-no-integrated-cpp')) |
| 466 | self.pipeOption = self.addOption(FlagOption('-pipe')) |
| 467 | self.saveTempsOption = self.addOption(FlagOption('-save-temps')) |
| 468 | self.saveTempsOption2 = self.addOption(FlagOption('--save-temps')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 469 | # FIXME: Error out if this is used. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 470 | self.addOption(JoinedOption('-specs=')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 471 | # FIXME: Implement. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 472 | self.addOption(FlagOption('-time')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 473 | # FIXME: Implement. |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 474 | self.vOption = self.addOption(FlagOption('-v')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 475 | |
| 476 | # Input/output stuff |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 477 | self.oOption = self.addOption(JoinedOrSeparateOption('-o', noOptAsInput=True)) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 478 | self.xOption = self.addOption(JoinedOrSeparateOption('-x')) |
| 479 | |
Daniel Dunbar | ee8cc26 | 2009-01-12 02:24:21 +0000 | [diff] [blame] | 480 | self.ObjCOption = self.addOption(FlagOption('-ObjC')) |
| 481 | self.ObjCXXOption = self.addOption(FlagOption('-ObjC++')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 482 | |
| 483 | # FIXME: Weird, gcc claims this here in help but I'm not sure why; |
| 484 | # perhaps interaction with preprocessor? Investigate. |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 485 | |
| 486 | # FIXME: This is broken in Darwin cc1, it wants std* and this |
| 487 | # is std=. May need an option group for this as well. |
| 488 | self.stdOption = self.addOption(JoinedOption('-std=')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 489 | self.addOption(JoinedOrSeparateOption('--sysroot')) |
| 490 | |
| 491 | # Version control |
| 492 | self.addOption(JoinedOrSeparateOption('-B')) |
| 493 | self.addOption(JoinedOrSeparateOption('-V')) |
| 494 | self.addOption(JoinedOrSeparateOption('-b')) |
| 495 | |
| 496 | # Blanket pass-through options. |
| 497 | |
Daniel Dunbar | 996ce96 | 2009-01-12 07:40:25 +0000 | [diff] [blame] | 498 | self.WaOption = self.addOption(CommaJoinedOption('-Wa,')) |
| 499 | self.XassemblerOption = self.addOption(SeparateOption('-Xassembler')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 500 | |
Daniel Dunbar | b37c965 | 2009-01-12 21:44:10 +0000 | [diff] [blame] | 501 | self.WpOption = self.addOption(CommaJoinedOption('-Wp,')) |
| 502 | self.XpreprocessorOption = self.addOption(SeparateOption('-Xpreprocessor')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 503 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 504 | self.addOption(CommaJoinedOption('-Wl,', isLinkerInput=True)) |
| 505 | self.addOption(SeparateOption('-Xlinker', isLinkerInput=True, noOptAsInput=True)) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 506 | |
| 507 | #### |
| 508 | # Bring on the random garbage. |
| 509 | |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 510 | self.MOption = self.addOption(FlagOption('-M')) |
| 511 | self.MDOption = self.addOption(FlagOption('-MD')) |
| 512 | self.MGOption = self.addOption(FlagOption('-MG')) |
| 513 | self.MMDOption = self.addOption(FlagOption('-MMD')) |
| 514 | self.MPOption = self.addOption(FlagOption('-MP')) |
| 515 | self.MMOption = self.addOption(FlagOption('-MM')) |
| 516 | self.MFOption = self.addOption(JoinedOrSeparateOption('-MF')) |
| 517 | self.MTOption = self.addOption(JoinedOrSeparateOption('-MT')) |
| 518 | self.MQOption = self.addOption(JoinedOrSeparateOption('-MQ')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 519 | self.MachOption = self.addOption(FlagOption('-Mach')) |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 520 | self.undefOption = self.addOption(FlagOption('-undef')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 521 | |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 522 | self.wOption = self.addOption(FlagOption('-w')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 523 | self.addOption(JoinedOrSeparateOption('-allowable_client')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 524 | self.client_nameOption = self.addOption(JoinedOrSeparateOption('-client_name')) |
| 525 | self.compatibility_versionOption = self.addOption(JoinedOrSeparateOption('-compatibility_version')) |
| 526 | self.current_versionOption = self.addOption(JoinedOrSeparateOption('-current_version')) |
| 527 | self.dylinkerOption = self.addOption(FlagOption('-dylinker')) |
| 528 | self.dylinker_install_nameOption = self.addOption(JoinedOrSeparateOption('-dylinker_install_name')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 529 | self.addOption(JoinedOrSeparateOption('-exported_symbols_list')) |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 530 | |
| 531 | self.iGroup = OptionGroup('-i') |
| 532 | self.addOption(JoinedOrSeparateOption('-idirafter', self.iGroup)) |
| 533 | self.addOption(JoinedOrSeparateOption('-iquote', self.iGroup)) |
| 534 | self.isysrootOption = self.addOption(JoinedOrSeparateOption('-isysroot', self.iGroup)) |
| 535 | self.addOption(JoinedOrSeparateOption('-include', self.iGroup)) |
| 536 | self.addOption(JoinedOption('-i', self.iGroup)) |
| 537 | |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 538 | self.keep_private_externsOption = self.addOption(JoinedOrSeparateOption('-keep_private_externs')) |
| 539 | self.private_bundleOption = self.addOption(FlagOption('-private_bundle')) |
| 540 | self.seg1addrOption = self.addOption(JoinedOrSeparateOption('-seg1addr')) |
| 541 | self.segprotOption = self.addOption(JoinedOrSeparateOption('-segprot')) |
| 542 | self.sub_libraryOption = self.addOption(JoinedOrSeparateOption('-sub_library')) |
| 543 | self.sub_umbrellaOption = self.addOption(JoinedOrSeparateOption('-sub_umbrella')) |
| 544 | self.umbrellaOption = self.addOption(JoinedOrSeparateOption('-umbrella')) |
| 545 | self.undefinedOption = self.addOption(JoinedOrSeparateOption('-undefined')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 546 | self.addOption(JoinedOrSeparateOption('-unexported_symbols_list')) |
| 547 | self.addOption(JoinedOrSeparateOption('-weak_framework')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 548 | self.headerpad_max_install_namesOption = self.addOption(JoinedOption('-headerpad_max_install_names')) |
| 549 | self.twolevel_namespaceOption = self.addOption(FlagOption('-twolevel_namespace')) |
| 550 | self.twolevel_namespace_hintsOption = self.addOption(FlagOption('-twolevel_namespace_hints')) |
| 551 | self.prebindOption = self.addOption(FlagOption('-prebind')) |
| 552 | self.noprebindOption = self.addOption(FlagOption('-noprebind')) |
| 553 | self.nofixprebindingOption = self.addOption(FlagOption('-nofixprebinding')) |
| 554 | self.prebind_all_twolevel_modulesOption = self.addOption(FlagOption('-prebind_all_twolevel_modules')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 555 | self.remapOption = self.addOption(FlagOption('-remap')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 556 | self.read_only_relocsOption = self.addOption(SeparateOption('-read_only_relocs')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 557 | self.addOption(FlagOption('-single_module')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 558 | self.nomultidefsOption = self.addOption(FlagOption('-nomultidefs')) |
| 559 | self.nostartfilesOption = self.addOption(FlagOption('-nostartfiles')) |
| 560 | self.nodefaultlibsOption = self.addOption(FlagOption('-nodefaultlibs')) |
| 561 | self.nostdlibOption = self.addOption(FlagOption('-nostdlib')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 562 | self.nostdincOption = self.addOption(FlagOption('-nostdinc')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 563 | self.objectOption = self.addOption(FlagOption('-object')) |
| 564 | self.preloadOption = self.addOption(FlagOption('-preload')) |
| 565 | self.staticOption = self.addOption(FlagOption('-static')) |
| 566 | self.pagezero_sizeOption = self.addOption(FlagOption('-pagezero_size')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 567 | self.addOption(FlagOption('-shared')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 568 | self.staticLibgccOption = self.addOption(FlagOption('-static-libgcc')) |
| 569 | self.sharedLibgccOption = self.addOption(FlagOption('-shared-libgcc')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 570 | self.COption = self.addOption(FlagOption('-C')) |
| 571 | self.CCOption = self.addOption(FlagOption('-CC')) |
| 572 | self.HOption = self.addOption(FlagOption('-H')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 573 | self.addOption(FlagOption('-R')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 574 | self.POption = self.addOption(FlagOption('-P')) |
| 575 | self.QOption = self.addOption(FlagOption('-Q')) |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 576 | self.QnOption = self.addOption(FlagOption('-Qn')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 577 | self.addOption(FlagOption('-all_load')) |
| 578 | self.addOption(FlagOption('--constant-cfstrings')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 579 | self.traditionalOption = self.addOption(FlagOption('-traditional')) |
| 580 | self.traditionalCPPOption = self.addOption(FlagOption('-traditional-cpp')) |
| 581 | # FIXME: Alias. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 582 | self.addOption(FlagOption('--traditional')) |
| 583 | self.addOption(FlagOption('-no_dead_strip_inits_and_terms')) |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 584 | self.addOption(JoinedOption('-weak-l', isLinkerInput=True)) |
| 585 | self.addOption(SeparateOption('-weak_framework', isLinkerInput=True)) |
| 586 | self.addOption(SeparateOption('-weak_library', isLinkerInput=True)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 587 | self.whyloadOption = self.addOption(FlagOption('-whyload')) |
| 588 | self.whatsloadedOption = self.addOption(FlagOption('-whatsloaded')) |
| 589 | self.sectalignOption = self.addOption(MultiArgOption('-sectalign', numArgs=3)) |
| 590 | self.sectobjectsymbolsOption = self.addOption(MultiArgOption('-sectobjectsymbols', numArgs=2)) |
| 591 | self.segcreateOption = self.addOption(MultiArgOption('-segcreate', numArgs=3)) |
| 592 | self.segs_read_Option = self.addOption(JoinedOption('-segs_read_')) |
| 593 | self.seglinkeditOption = self.addOption(FlagOption('-seglinkedit')) |
| 594 | self.noseglinkeditOption = self.addOption(FlagOption('-noseglinkedit')) |
| 595 | self.sectcreateOption = self.addOption(MultiArgOption('-sectcreate', numArgs=3)) |
| 596 | self.sectorderOption = self.addOption(MultiArgOption('-sectorder', numArgs=3)) |
| 597 | self.Zall_loadOption = self.addOption(FlagOption('-Zall_load')) |
| 598 | self.Zallowable_clientOption = self.addOption(SeparateOption('-Zallowable_client')) |
| 599 | self.Zbind_at_loadOption = self.addOption(SeparateOption('-Zbind_at_load')) |
| 600 | self.ZbundleOption = self.addOption(FlagOption('-Zbundle')) |
| 601 | self.Zbundle_loaderOption = self.addOption(JoinedOrSeparateOption('-Zbundle_loader')) |
| 602 | self.Zdead_stripOption = self.addOption(FlagOption('-Zdead_strip')) |
| 603 | self.Zdylib_fileOption = self.addOption(JoinedOrSeparateOption('-Zdylib_file')) |
| 604 | self.ZdynamicOption = self.addOption(FlagOption('-Zdynamic')) |
| 605 | self.ZdynamiclibOption = self.addOption(FlagOption('-Zdynamiclib')) |
| 606 | self.Zexported_symbols_listOption = self.addOption(JoinedOrSeparateOption('-Zexported_symbols_list')) |
| 607 | self.Zflat_namespaceOption = self.addOption(FlagOption('-Zflat_namespace')) |
| 608 | self.Zfn_seg_addr_table_filenameOption = self.addOption(JoinedOrSeparateOption('-Zfn_seg_addr_table_filename')) |
| 609 | self.Zforce_cpusubtype_ALLOption = self.addOption(FlagOption('-Zforce_cpusubtype_ALL')) |
| 610 | self.Zforce_flat_namespaceOption = self.addOption(FlagOption('-Zforce_flat_namespace')) |
| 611 | self.Zimage_baseOption = self.addOption(FlagOption('-Zimage_base')) |
| 612 | self.ZinitOption = self.addOption(JoinedOrSeparateOption('-Zinit')) |
| 613 | self.Zmulti_moduleOption = self.addOption(FlagOption('-Zmulti_module')) |
| 614 | self.Zmultiply_definedOption = self.addOption(JoinedOrSeparateOption('-Zmultiply_defined')) |
| 615 | self.ZmultiplydefinedunusedOption = self.addOption(JoinedOrSeparateOption('-Zmultiplydefinedunused')) |
| 616 | self.ZmultiplydefinedunusedOption = self.addOption(JoinedOrSeparateOption('-Zmultiplydefinedunused')) |
| 617 | self.Zno_dead_strip_inits_and_termsOption = self.addOption(FlagOption('-Zno_dead_strip_inits_and_terms')) |
| 618 | self.Zseg_addr_tableOption = self.addOption(JoinedOrSeparateOption('-Zseg_addr_table')) |
| 619 | self.ZsegaddrOption = self.addOption(JoinedOrSeparateOption('-Zsegaddr')) |
| 620 | self.Zsegs_read_only_addrOption = self.addOption(JoinedOrSeparateOption('-Zsegs_read_only_addr')) |
| 621 | self.Zsegs_read_write_addrOption = self.addOption(JoinedOrSeparateOption('-Zsegs_read_write_addr')) |
| 622 | self.Zsingle_moduleOption = self.addOption(FlagOption('-Zsingle_module')) |
Daniel Dunbar | 0fe5a4f | 2009-01-11 22:42:24 +0000 | [diff] [blame] | 623 | self.ZumbrellaOption = self.addOption(JoinedOrSeparateOption('-Zumbrella')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 624 | self.Zunexported_symbols_listOption = self.addOption(JoinedOrSeparateOption('-Zunexported_symbols_list')) |
| 625 | self.Zweak_reference_mismatchesOption = self.addOption(JoinedOrSeparateOption('-Zweak_reference_mismatches')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 626 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 627 | self.addOption(SeparateOption('-filelist', isLinkerInput=True)) |
| 628 | self.addOption(SeparateOption('-framework', isLinkerInput=True)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 629 | # FIXME: Alias. |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 630 | self.addOption(SeparateOption('-install_name')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 631 | self.Zinstall_nameOption = self.addOption(JoinedOrSeparateOption('-Zinstall_name')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 632 | self.addOption(SeparateOption('-seg_addr_table')) |
| 633 | self.addOption(SeparateOption('-seg_addr_table_filename')) |
| 634 | |
| 635 | # Where are these coming from? I can't find them... |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 636 | self.eOption = self.addOption(JoinedOrSeparateOption('-e')) |
| 637 | self.rOption = self.addOption(JoinedOrSeparateOption('-r')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 638 | |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 639 | self.pgOption = self.addOption(FlagOption('-pg')) |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 640 | self.pOption = self.addOption(FlagOption('-p')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 641 | |
| 642 | doNotReallySupport = 1 |
| 643 | if doNotReallySupport: |
| 644 | # Archaic gcc option. |
| 645 | self.addOption(FlagOption('-cpp-precomp')) |
| 646 | self.addOption(FlagOption('-no-cpp-precomp')) |
| 647 | |
| 648 | # C options for testing |
| 649 | |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 650 | # FIXME: This is broken, we need -A as a single option to send |
| 651 | # stuff to cc1, but the way the ld spec is constructed it |
| 652 | # wants to see -A options but only as a separate arg. |
| 653 | self.AOption = self.addOption(JoinedOrSeparateOption('-A')) |
| 654 | self.DOption = self.addOption(JoinedOrSeparateOption('-D')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 655 | self.FOption = self.addOption(JoinedOrSeparateOption('-F')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 656 | self.IOption = self.addOption(JoinedOrSeparateOption('-I')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 657 | self.LOption = self.addOption(JoinedOrSeparateOption('-L')) |
| 658 | self.TOption = self.addOption(JoinedOrSeparateOption('-T')) |
Daniel Dunbar | 6325fcf | 2009-01-12 09:23:15 +0000 | [diff] [blame] | 659 | self.UOption = self.addOption(JoinedOrSeparateOption('-U')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 660 | self.ZOption = self.addOption(JoinedOrSeparateOption('-Z')) |
| 661 | |
Daniel Dunbar | 2ec55bc | 2009-01-12 03:33:58 +0000 | [diff] [blame] | 662 | self.addOption(JoinedOrSeparateOption('-l', isLinkerInput=True)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 663 | self.uOption = self.addOption(JoinedOrSeparateOption('-u')) |
| 664 | self.tOption = self.addOption(JoinedOrSeparateOption('-t')) |
| 665 | self.yOption = self.addOption(JoinedOption('-y')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 666 | |
| 667 | # FIXME: What is going on here? '-X' goes to linker, and -X ... goes nowhere? |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 668 | self.XOption = self.addOption(FlagOption('-X')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 669 | # Not exactly sure how to decompose this. I split out -Xarch_ |
| 670 | # because we need to recognize that in the driver driver part. |
| 671 | # FIXME: Man, this is lame it needs its own option. |
| 672 | self.XarchOption = self.addOption(JoinedAndSeparateOption('-Xarch_')) |
| 673 | self.addOption(JoinedOption('-X')) |
| 674 | |
| 675 | # The driver needs to know about this flag. |
| 676 | self.syntaxOnlyOption = self.addOption(FlagOption('-fsyntax-only')) |
| 677 | |
| 678 | # FIXME: Wrong? |
| 679 | # FIXME: What to do about the ambiguity of options like |
| 680 | # -dumpspecs? How is this handled in gcc? |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 681 | # FIXME: Naming convention. |
| 682 | self.dOption = self.addOption(FlagOption('-d')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 683 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 684 | # Use a group for this in anticipation of adding more -d |
| 685 | # options explicitly. Note that we don't put many -d things in |
| 686 | # the -d group (like -dylinker, or '-d' by itself) because it |
| 687 | # is really a gcc bug that it ships these to cc1. |
| 688 | self.dGroup = OptionGroup('-d') |
| 689 | self.addOption(JoinedOption('-d', group=self.dGroup)) |
Daniel Dunbar | 996ce96 | 2009-01-12 07:40:25 +0000 | [diff] [blame] | 690 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 691 | self.gGroup = OptionGroup('-g') |
| 692 | self.g3Option = self.addOption(JoinedOption('-g3', self.gGroup)) |
| 693 | self.gOption = self.addOption(JoinedOption('-g', self.gGroup)) |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 694 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 695 | self.fGroup = OptionGroup('-f') |
| 696 | self.fastOption = self.addOption(FlagOption('-fast', self.fGroup)) |
| 697 | self.fastfOption = self.addOption(FlagOption('-fastf', self.fGroup)) |
| 698 | self.fastcpOption = self.addOption(FlagOption('-fastcp', self.fGroup)) |
| 699 | |
| 700 | self.f_appleKextOption = self.addOption(FlagOption('-fapple-kext', self.fGroup)) |
| 701 | self.f_noEliminateUnusedDebugSymbolsOption = self.addOption(FlagOption('-fno-eliminate-unused-debug-symbols', self.fGroup)) |
| 702 | self.f_exceptionsOption = self.addOption(FlagOption('-fexceptions', self.fGroup)) |
| 703 | self.f_objcOption = self.addOption(FlagOption('-fobjc', self.fGroup)) |
| 704 | self.f_openmpOption = self.addOption(FlagOption('-fopenmp', self.fGroup)) |
| 705 | self.f_gnuRuntimeOption = self.addOption(FlagOption('-fgnu-runtime', self.fGroup)) |
| 706 | self.f_mudflapOption = self.addOption(FlagOption('-fmudflap', self.fGroup)) |
| 707 | self.f_mudflapthOption = self.addOption(FlagOption('-fmudflapth', self.fGroup)) |
| 708 | self.f_nestedFunctionsOption = self.addOption(FlagOption('-fnested-functions', self.fGroup)) |
| 709 | self.f_pieOption = self.addOption(FlagOption('-fpie', self.fGroup)) |
| 710 | self.f_profileArcsOption = self.addOption(FlagOption('-fprofile-arcs', self.fGroup)) |
| 711 | self.f_profileGenerateOption = self.addOption(FlagOption('-fprofile-generate', self.fGroup)) |
| 712 | self.f_createProfileOption = self.addOption(FlagOption('-fcreate-profile', self.fGroup)) |
| 713 | self.f_traditionalOption = self.addOption(FlagOption('-ftraditional', self.fGroup)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 714 | self.coverageOption = self.addOption(FlagOption('-coverage')) |
| 715 | self.coverageOption2 = self.addOption(FlagOption('--coverage')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 716 | self.addOption(JoinedOption('-f')) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 717 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 718 | self.mGroup = OptionGroup('-m') |
| 719 | self.m_32Option = self.addOption(FlagOption('-m32', self.mGroup)) |
| 720 | self.m_64Option = self.addOption(FlagOption('-m64', self.mGroup)) |
| 721 | self.m_dynamicNoPicOption = self.addOption(JoinedOption('-mdynamic-no-pic', self.mGroup)) |
| 722 | self.m_iphoneosVersionMinOption = self.addOption(JoinedOption('-miphoneos-version-min=', self.mGroup)) |
| 723 | self.m_kernelOption = self.addOption(FlagOption('-mkernel', self.mGroup)) |
| 724 | self.m_macosxVersionMinOption = self.addOption(JoinedOption('-mmacosx-version-min=', self.mGroup)) |
| 725 | self.m_tuneOption = self.addOption(JoinedOption('-mtune=', self.mGroup)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 726 | |
| 727 | # Ugh. Need to disambiguate our naming convetion. -m x goes to |
| 728 | # the linker sometimes, wheres -mxxxx is used for a variety of |
| 729 | # other things. |
| 730 | self.mOption = self.addOption(SeparateOption('-m')) |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 731 | self.addOption(JoinedOption('-m', self.mGroup)) |
Daniel Dunbar | 6f5d65a | 2009-01-11 22:12:37 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 8bf90fd | 2009-01-13 05:54:38 +0000 | [diff] [blame^] | 733 | # FIXME: Why does Darwin send -a* to cc1? |
| 734 | self.aGroup = OptionGroup('-a') |
| 735 | self.ansiOption = self.addOption(FlagOption('-ansi', self.aGroup)) |
| 736 | self.aOption = self.addOption(JoinedOption('-a', self.aGroup)) |
| 737 | |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 738 | self.trigraphsOption = self.addOption(FlagOption('-trigraphs')) |
| 739 | self.pedanticOption = self.addOption(FlagOption('-pedantic')) |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 740 | self.OOption = self.addOption(JoinedOption('-O')) |
| 741 | self.WOption = self.addOption(JoinedOption('-W')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 742 | # FIXME: Weird. This option isn't really separate, --param=a=b |
Daniel Dunbar | 816dd50 | 2009-01-12 17:53:19 +0000 | [diff] [blame] | 743 | # works. There is something else going on which interprets the |
| 744 | # '='. |
| 745 | self._paramOption = self.addOption(SeparateOption('--param')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 746 | |
Daniel Dunbar | dff9f50 | 2009-01-12 18:51:02 +0000 | [diff] [blame] | 747 | # FIXME: What is this? I think only one is valid, but have a |
| 748 | # log that uses both. |
| 749 | self.pthreadOption = self.addOption(FlagOption('-pthread')) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 750 | self.addOption(FlagOption('-pthreads')) |
| 751 | |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 752 | def addOption(self, opt): |
| 753 | self.options.append(opt) |
Daniel Dunbar | ba6e323 | 2009-01-06 06:12:13 +0000 | [diff] [blame] | 754 | return opt |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 756 | def parseArgs(self, argv): |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 757 | """ |
Daniel Dunbar | 1e5f3eb | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 758 | parseArgs([str]) -> ArgList |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 759 | |
| 760 | Parse command line into individual option instances. |
| 761 | """ |
| 762 | |
| 763 | iargs = enumerate(argv) |
| 764 | it = iter(iargs) |
Daniel Dunbar | 39cbfaa | 2009-01-07 18:54:26 +0000 | [diff] [blame] | 765 | args = ArgList(self, argv) |
Daniel Dunbar | efb4aeb | 2009-01-07 01:57:39 +0000 | [diff] [blame] | 766 | for pos,a in it: |
| 767 | i = InputIndex(0, pos) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 768 | # FIXME: Handle '@' |
| 769 | if not a: |
| 770 | # gcc's handling of empty arguments doesn't make |
| 771 | # sense, but this is not a common use case. :) |
| 772 | # |
| 773 | # We just ignore them here (note that other things may |
| 774 | # still take them as arguments). |
| 775 | pass |
| 776 | elif a[0] == '-' and a != '-': |
| 777 | args.append(self.lookupOptForArg(i, a, it)) |
| 778 | else: |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 779 | args.append(PositionalArg(i, self.inputOption)) |
Daniel Dunbar | a567751 | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 780 | return args |
| 781 | |
Daniel Dunbar | 5039f21 | 2009-01-06 02:30:10 +0000 | [diff] [blame] | 782 | def lookupOptForArg(self, i, string, it): |
| 783 | for o in self.options: |
| 784 | arg = o.accept(i, string, it) |
| 785 | if arg is not None: |
| 786 | return arg |
| 787 | return PositionalArg(i, self.unknownOption) |