blob: ee234f1a71c7104a3a35ee25dde99cf5cccb1d4d [file] [log] [blame]
Daniel Dunbara5677512009-01-05 19:53:30 +00001class Option(object):
2 """Root option class."""
3 def __init__(self, name):
4 self.name = name
5
6 def accept(self, index, arg, it):
7 """accept(index, arg, iterator) -> Arg or None
8
9 Accept the argument at the given index, returning an Arg, or
10 return None if the option does not accept this argument.
11
12 May raise MissingArgumentError.
13 """
14 abstract
15
16 def __repr__(self):
17 return '<%s name=%r>' % (self.__class__.__name__,
18 self.name)
19
Daniel Dunbar5039f212009-01-06 02:30:10 +000020# Dummy options
21
22class InputOption(Option):
23 def __init__(self):
24 super(InputOption, self).__init__('<input>')
25
26 def accept(self):
27 raise RuntimeError,"accept() should never be used on InputOption instance."
28
29class UnknownOption(Option):
30 def __init__(self):
31 super(UnknownOption, self).__init__('<unknown>')
32
33 def accept(self):
34 raise RuntimeError,"accept() should never be used on UnknownOption instance."
35
36# Normal options
37
Daniel Dunbara5677512009-01-05 19:53:30 +000038class FlagOption(Option):
39 """An option which takes no arguments."""
40
41 def accept(self, index, arg, it):
42 if arg == self.name:
43 return Arg(index, self)
44
45class JoinedOption(Option):
46 """An option which literally prefixes its argument."""
47
48 def accept(self, index, arg, it):
49 if arg.startswith(self.name):
50 return JoinedValueArg(index, self)
51
52class SeparateOption(Option):
53 """An option which is followed by its value."""
54
55 def accept(self, index, arg, it):
56 if arg == self.name:
57 try:
58 _,value = it.next()
59 except StopIteration:
60 raise MissingArgumentError,self
61 return SeparateValueArg(index, self)
62
63class MultiArgOption(Option):
64 """An option which takes multiple arguments."""
65
66 def __init__(self, name, numArgs):
67 assert numArgs > 1
68 super(MultiArgOption, self).__init__(name)
69 self.numArgs = numArgs
70
71 def accept(self, index, arg, it):
72 if arg.startswith(self.name):
73 try:
74 values = [it.next()[1] for i in range(self.numArgs)]
75 except StopIteration:
76 raise MissingArgumentError,self
77 return MultipleValuesArg(index, self)
78
79class JoinedOrSeparateOption(Option):
80 """An option which either literally prefixes its value or is
81 followed by an value."""
82
83 def accept(self, index, arg, it):
84 if arg.startswith(self.name):
85 if len(arg) != len(self.name): # Joined case
86 return JoinedValueArg(index, self)
87 else:
88 try:
89 _,value = it.next()
90 except StopIteration:
91 raise MissingArgumentError,self
92 return SeparateValueArg(index, self)
93
94class JoinedAndSeparateOption(Option):
95 """An option which literally prefixes its value and is followed by
96 an value."""
97
98 def accept(self, index, arg, it):
99 if arg.startswith(self.name):
100 try:
101 _,value = it.next()
102 except StopIteration:
103 raise MissingArgumentError,self
104 return JoinedAndSeparateValuesArg(index, self)
105
106###
107
108class Arg(object):
109 """Arg - Base class for actual driver arguments."""
110 def __init__(self, index, opt):
Daniel Dunbar5039f212009-01-06 02:30:10 +0000111 assert opt is not None
Daniel Dunbara5677512009-01-05 19:53:30 +0000112 self.index = index
113 self.opt = opt
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000114
Daniel Dunbara5677512009-01-05 19:53:30 +0000115 def __repr__(self):
116 return '<%s index=%r opt=%r>' % (self.__class__.__name__,
117 self.index,
118 self.opt)
119
120 def render(self, args):
121 """render(args) -> [str]
122
123 Map the argument into a list of actual program arguments,
124 given the source argument array."""
125 assert self.opt
126 return [self.opt.name]
127
128class ValueArg(Arg):
129 """ValueArg - An instance of an option which has an argument."""
130
131 def getValue(self, args):
132 abstract
133
Daniel Dunbar5039f212009-01-06 02:30:10 +0000134class PositionalArg(ValueArg):
135 """PositionalArg - A simple positional argument."""
Daniel Dunbara5677512009-01-05 19:53:30 +0000136
137 def getValue(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000138 return args.getInputString(self.index)
Daniel Dunbara5677512009-01-05 19:53:30 +0000139
140 def render(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000141 return [args.getInputString(self.index)]
Daniel Dunbara5677512009-01-05 19:53:30 +0000142
143class JoinedValueArg(ValueArg):
Daniel Dunbar5039f212009-01-06 02:30:10 +0000144 """JoinedValueArg - A single value argument where the value is
145 joined (suffixed) to the option."""
146
Daniel Dunbara5677512009-01-05 19:53:30 +0000147 def getValue(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000148 return args.getInputString(self.index)[len(self.opt.name):]
Daniel Dunbara5677512009-01-05 19:53:30 +0000149
Daniel Dunbara5677512009-01-05 19:53:30 +0000150 def render(self, args):
151 return [self.opt.name + self.getValue(args)]
152
153class SeparateValueArg(ValueArg):
Daniel Dunbar5039f212009-01-06 02:30:10 +0000154 """SeparateValueArg - A single value argument where the value
155 follows the option in the argument vector."""
156
Daniel Dunbara5677512009-01-05 19:53:30 +0000157 def getValue(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000158 return args.getInputString(self.index, offset=1)
Daniel Dunbara5677512009-01-05 19:53:30 +0000159
Daniel Dunbara5677512009-01-05 19:53:30 +0000160 def render(self, args):
161 return [self.opt.name, self.getValue(args)]
162
163class MultipleValuesArg(Arg):
Daniel Dunbar5039f212009-01-06 02:30:10 +0000164 """MultipleValuesArg - An argument with multiple values which
165 follow the option in the argument vector."""
166
167 # FIXME: Should we unify this with SeparateValueArg?
168
Daniel Dunbara5677512009-01-05 19:53:30 +0000169 def getValues(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000170 return [args.getInputString(self.index, offset=1+i)
171 for i in range(self.opt.numArgs)]
Daniel Dunbara5677512009-01-05 19:53:30 +0000172
Daniel Dunbara5677512009-01-05 19:53:30 +0000173 def render(self, args):
174 return [self.opt.name] + self.getValues(args)
175
176# FIXME: Man, this is lame. It is only used by -Xarch. Maybe easier to
177# just special case?
178class JoinedAndSeparateValuesArg(Arg):
179 """JoinedAndSeparateValuesArg - An argument with both joined and
180 separate values."""
181
182 def getJoinedValue(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000183 return args.getInputString(self.index)[len(self.opt.name):]
Daniel Dunbara5677512009-01-05 19:53:30 +0000184
185 def getSeparateValue(self, args):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000186 return args.getInputString(self.index, offset=1)
Daniel Dunbara5677512009-01-05 19:53:30 +0000187
188 def render(self, args):
189 return ([self.opt.name + self.getJoinedValue(args)] +
190 [self.getSeparateValue(args)])
191
Daniel Dunbara5677512009-01-05 19:53:30 +0000192class DerivedArg(ValueArg):
193 """DerivedArg - A synthesized argument which does not correspend
Daniel Dunbar5039f212009-01-06 02:30:10 +0000194 to an item in the argument vector."""
Daniel Dunbara5677512009-01-05 19:53:30 +0000195
196 def __init__(self, value):
Daniel Dunbar5039f212009-01-06 02:30:10 +0000197 # FIXME: The UnknownOption() here is a total hack so we can
198 # rely on arg.opt not being nil. Ok for now since DerivedArg
199 # is dying.
200 super(DerivedArg, self).__init__(-1, UnknownOption())
Daniel Dunbara5677512009-01-05 19:53:30 +0000201 self.value = value
202
203 def getValue(self, args):
204 return self.value
205
Daniel Dunbara5677512009-01-05 19:53:30 +0000206 def render(self, args):
207 return [self.value]
208
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000209class ArgList:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000210 """ArgList - Collect an input argument vector along with a set of parsed Args
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000211 and supporting information."""
212
213 def __init__(self, argv):
214 self.argv = list(argv)
215 self.args = []
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000216 self.lastArgs = {}
217
218 def getLastArg(self, option):
219 return self.lastArgs.get(option)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000220
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000221 def getInputString(self, index, offset=0):
222 return self.argv[index + offset]
223
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000224 # Support use as a simple arg list.
225
226 def __iter__(self):
227 return iter(self.args)
228
229 def append(self, arg):
230 self.args.append(arg)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000231 self.lastArgs[arg.opt] = arg
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000232
233 # Forwarding methods.
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000234 #
235 # FIXME: Clean this up once restructuring is done.
236
237 def render(self, arg):
238 return arg.render(self)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000239
240 def getValue(self, arg):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000241 return arg.getValue(self)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000242
243 def getValues(self, arg):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000244 return arg.getValues(self)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000245
246 def getSeparateValue(self, arg):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000247 return arg.getSeparateValue(self)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000248
249 def getJoinedValue(self, arg):
Daniel Dunbarfb2c5c42009-01-07 01:29:28 +0000250 return arg.getJoinedValue(self)
Daniel Dunbar1dd2ada2009-01-06 06:32:49 +0000251
252###
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000253
Daniel Dunbara5677512009-01-05 19:53:30 +0000254class OptionParser:
255 def __init__(self):
256 self.options = []
Daniel Dunbar5039f212009-01-06 02:30:10 +0000257 self.inputOption = InputOption()
258 self.unknownOption = UnknownOption()
259
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000260 # Driver driver options
261 self.archOption = self.addOption(SeparateOption('-arch'))
262
263 # Misc driver options
264 self.addOption(FlagOption('-pass-exit-codes'))
265 self.addOption(FlagOption('--help'))
266 self.addOption(FlagOption('--target-help'))
267
268 self.dumpspecsOption = self.addOption(FlagOption('-dumpspecs'))
269 self.dumpversionOption = self.addOption(FlagOption('-dumpversion'))
270 self.dumpmachineOption = self.addOption(FlagOption('-dumpmachine'))
271 self.printSearchDirsOption = self.addOption(FlagOption('-print-search-dirs'))
272 self.printLibgccFilenameOption = self.addOption(FlagOption('-print-libgcc-file-name'))
273 # FIXME: Hrm, where does this come from? It isn't always true that
274 # we take both - and --. For example, gcc --S ... ends up sending
275 # -fS to cc1. Investigate.
276 #
277 # FIXME: Need to implement some form of alias support inside
278 # getLastOption to handle this.
279 self.printLibgccFileNameOption2 = self.addOption(FlagOption('--print-libgcc-file-name'))
280 self.printFileNameOption = self.addOption(JoinedOption('-print-file-name='))
281 self.printProgNameOption = self.addOption(JoinedOption('-print-prog-name='))
282 self.printProgNameOption2 = self.addOption(JoinedOption('--print-prog-name='))
283 self.printMultiDirectoryOption = self.addOption(FlagOption('-print-multi-directory'))
284 self.printMultiLibOption = self.addOption(FlagOption('-print-multi-lib'))
285 self.addOption(FlagOption('-print-multi-os-directory'))
286
287 # Hmmm, who really takes this?
288 self.addOption(FlagOption('--version'))
289
290 # Pipeline control
291 self.hashHashHashOption = self.addOption(FlagOption('-###'))
292 self.EOption = self.addOption(FlagOption('-E'))
293 self.SOption = self.addOption(FlagOption('-S'))
294 self.cOption = self.addOption(FlagOption('-c'))
295 self.combineOption = self.addOption(FlagOption('-combine'))
296 self.noIntegratedCPPOption = self.addOption(FlagOption('-no-integrated-cpp'))
297 self.pipeOption = self.addOption(FlagOption('-pipe'))
298 self.saveTempsOption = self.addOption(FlagOption('-save-temps'))
299 self.saveTempsOption2 = self.addOption(FlagOption('--save-temps'))
300 self.addOption(JoinedOption('-specs='))
301 self.addOption(FlagOption('-time'))
302 self.addOption(FlagOption('-v'))
303
304 # Input/output stuff
305 self.oOption = self.addOption(JoinedOrSeparateOption('-o'))
306 self.xOption = self.addOption(JoinedOrSeparateOption('-x'))
307
308 # FIXME: What do these actually do? The documentation is less than
309 # clear.
310 self.addOption(FlagOption('-ObjC'))
311 self.addOption(FlagOption('-ObjC++'))
312
313 # FIXME: Weird, gcc claims this here in help but I'm not sure why;
314 # perhaps interaction with preprocessor? Investigate.
315 self.addOption(JoinedOption('-std='))
316 self.addOption(JoinedOrSeparateOption('--sysroot'))
317
318 # Version control
319 self.addOption(JoinedOrSeparateOption('-B'))
320 self.addOption(JoinedOrSeparateOption('-V'))
321 self.addOption(JoinedOrSeparateOption('-b'))
322
323 # Blanket pass-through options.
324
325 self.addOption(JoinedOption('-Wa,'))
326 self.addOption(SeparateOption('-Xassembler'))
327
328 self.addOption(JoinedOption('-Wp,'))
329 self.addOption(SeparateOption('-Xpreprocessor'))
330
331 self.addOption(JoinedOption('-Wl,'))
332 self.addOption(SeparateOption('-Xlinker'))
333
334 ####
335 # Bring on the random garbage.
336
337 self.addOption(FlagOption('-MD'))
338 self.addOption(FlagOption('-MP'))
339 self.addOption(FlagOption('-MM'))
340 self.addOption(JoinedOrSeparateOption('-MF'))
341 self.addOption(JoinedOrSeparateOption('-MT'))
342 self.addOption(FlagOption('-undef'))
343
344 self.addOption(FlagOption('-w'))
345 self.addOption(JoinedOrSeparateOption('-allowable_client'))
346 self.addOption(JoinedOrSeparateOption('-client_name'))
347 self.addOption(JoinedOrSeparateOption('-compatibility_version'))
348 self.addOption(JoinedOrSeparateOption('-current_version'))
349 self.addOption(JoinedOrSeparateOption('-exported_symbols_list'))
350 self.addOption(JoinedOrSeparateOption('-idirafter'))
351 self.addOption(JoinedOrSeparateOption('-iquote'))
352 self.addOption(JoinedOrSeparateOption('-isysroot'))
353 self.addOption(JoinedOrSeparateOption('-keep_private_externs'))
354 self.addOption(JoinedOrSeparateOption('-seg1addr'))
355 self.addOption(JoinedOrSeparateOption('-segprot'))
356 self.addOption(JoinedOrSeparateOption('-sub_library'))
357 self.addOption(JoinedOrSeparateOption('-sub_umbrella'))
358 self.addOption(JoinedOrSeparateOption('-umbrella'))
359 self.addOption(JoinedOrSeparateOption('-undefined'))
360 self.addOption(JoinedOrSeparateOption('-unexported_symbols_list'))
361 self.addOption(JoinedOrSeparateOption('-weak_framework'))
362 self.addOption(JoinedOption('-headerpad_max_install_names'))
363 self.addOption(FlagOption('-twolevel_namespace'))
364 self.addOption(FlagOption('-prebind'))
365 self.addOption(FlagOption('-prebind_all_twolevel_modules'))
366 self.addOption(FlagOption('-single_module'))
367 self.addOption(FlagOption('-nomultidefs'))
368 self.addOption(FlagOption('-nostdlib'))
369 self.addOption(FlagOption('-nostdinc'))
370 self.addOption(FlagOption('-static'))
371 self.addOption(FlagOption('-shared'))
372 self.addOption(FlagOption('-C'))
373 self.addOption(FlagOption('-CC'))
374 self.addOption(FlagOption('-R'))
375 self.addOption(FlagOption('-P'))
376 self.addOption(FlagOption('-all_load'))
377 self.addOption(FlagOption('--constant-cfstrings'))
378 self.addOption(FlagOption('-traditional'))
379 self.addOption(FlagOption('--traditional'))
380 self.addOption(FlagOption('-no_dead_strip_inits_and_terms'))
381 self.addOption(MultiArgOption('-sectalign', numArgs=3))
382 self.addOption(MultiArgOption('-sectcreate', numArgs=3))
383 self.addOption(MultiArgOption('-sectorder', numArgs=3))
384
385 # I dunno why these don't end up working when joined. Maybe
386 # because of translation?
387 self.filelistOption = self.addOption(SeparateOption('-filelist'))
388 self.addOption(SeparateOption('-framework'))
389 self.addOption(SeparateOption('-install_name'))
390 self.addOption(SeparateOption('-seg_addr_table'))
391 self.addOption(SeparateOption('-seg_addr_table_filename'))
392
393 # Where are these coming from? I can't find them...
394 self.addOption(JoinedOrSeparateOption('-e')) # Gets forwarded to linker
395 self.addOption(JoinedOrSeparateOption('-r'))
396
397 # Is this actually declared anywhere? I can only find it in a
398 # spec. :(
399 self.addOption(FlagOption('-pg'))
400
401 doNotReallySupport = 1
402 if doNotReallySupport:
403 # Archaic gcc option.
404 self.addOption(FlagOption('-cpp-precomp'))
405 self.addOption(FlagOption('-no-cpp-precomp'))
406
407 # C options for testing
408
409 self.addOption(JoinedOrSeparateOption('-include'))
410 self.addOption(JoinedOrSeparateOption('-A'))
411 self.addOption(JoinedOrSeparateOption('-D'))
412 self.addOption(JoinedOrSeparateOption('-F'))
413 self.addOption(JoinedOrSeparateOption('-I'))
414 self.addOption(JoinedOrSeparateOption('-L'))
415 self.addOption(JoinedOrSeparateOption('-U'))
416 self.addOption(JoinedOrSeparateOption('-l'))
417 self.addOption(JoinedOrSeparateOption('-u'))
418
419 # FIXME: What is going on here? '-X' goes to linker, and -X ... goes nowhere?
420 self.addOption(FlagOption('-X'))
421 # Not exactly sure how to decompose this. I split out -Xarch_
422 # because we need to recognize that in the driver driver part.
423 # FIXME: Man, this is lame it needs its own option.
424 self.XarchOption = self.addOption(JoinedAndSeparateOption('-Xarch_'))
425 self.addOption(JoinedOption('-X'))
426
427 # The driver needs to know about this flag.
428 self.syntaxOnlyOption = self.addOption(FlagOption('-fsyntax-only'))
429
430 # FIXME: Wrong?
431 # FIXME: What to do about the ambiguity of options like
432 # -dumpspecs? How is this handled in gcc?
433 self.addOption(JoinedOption('-d'))
434 self.addOption(JoinedOption('-g'))
435 self.addOption(JoinedOption('-f'))
436 self.addOption(JoinedOption('-m'))
437 self.addOption(JoinedOption('-i'))
438 self.addOption(JoinedOption('-O'))
439 self.addOption(JoinedOption('-W'))
440 # FIXME: Weird. This option isn't really separate, --param=a=b
441 # works. An alias somewhere?
442 self.addOption(SeparateOption('--param'))
443
444 # FIXME: What is this? Seems to do something on Linux. I think
445 # only one is valid, but have a log that uses both.
446 self.addOption(FlagOption('-pthread'))
447 self.addOption(FlagOption('-pthreads'))
448
Daniel Dunbara5677512009-01-05 19:53:30 +0000449 def addOption(self, opt):
450 self.options.append(opt)
Daniel Dunbarba6e3232009-01-06 06:12:13 +0000451 return opt
Daniel Dunbara5677512009-01-05 19:53:30 +0000452
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000453 def parseArgs(self, argv):
Daniel Dunbara5677512009-01-05 19:53:30 +0000454 """
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000455 parseArgs([str]) -> ArgList
Daniel Dunbara5677512009-01-05 19:53:30 +0000456
457 Parse command line into individual option instances.
458 """
459
460 iargs = enumerate(argv)
461 it = iter(iargs)
Daniel Dunbar1e5f3eb2009-01-06 01:35:44 +0000462 args = ArgList(argv)
Daniel Dunbara5677512009-01-05 19:53:30 +0000463 for i,a in it:
464 # FIXME: Handle '@'
465 if not a:
466 # gcc's handling of empty arguments doesn't make
467 # sense, but this is not a common use case. :)
468 #
469 # We just ignore them here (note that other things may
470 # still take them as arguments).
471 pass
472 elif a[0] == '-' and a != '-':
473 args.append(self.lookupOptForArg(i, a, it))
474 else:
Daniel Dunbar5039f212009-01-06 02:30:10 +0000475 args.append(PositionalArg(i, self.inputOption))
Daniel Dunbara5677512009-01-05 19:53:30 +0000476 return args
477
Daniel Dunbar5039f212009-01-06 02:30:10 +0000478 def lookupOptForArg(self, i, string, it):
479 for o in self.options:
480 arg = o.accept(i, string, it)
481 if arg is not None:
482 return arg
483 return PositionalArg(i, self.unknownOption)