2006-10-26 Joseph S. Myers <joseph@codesourcery.com>
[binutils.git] / gold / options.cc
blob8e0465f2af919cc7eb16a70492e91c7ab1796876
1 // options.c -- handle command line options for gold
3 #include <iostream>
5 #include "gold.h"
6 #include "options.h"
8 namespace gold
11 // The information we keep for a single command line option.
13 struct options::One_option
15 // The single character option name, or '\0' if this is only a long
16 // option.
17 char short_option;
19 // The long option name, or NULL if this is only a short option.
20 const char* long_option;
22 // Description of the option for --help output, or NULL if there is none.
23 const char* doc;
25 // How to print the option name in --help output, or NULL to use the
26 // default.
27 const char* help_output;
29 // Long option dash control. This is ignored if long_option is
30 // NULL.
31 enum
33 // Long option normally takes one dash; two dashes are also
34 // accepted.
35 ONE_DASH,
36 // Long option normally takes two dashes; one dash is also
37 // accepted.
38 TWO_DASHES,
39 // Long option always takes two dashes.
40 EXACTLY_TWO_DASHES
41 } dash;
43 // Function for special handling, or NULL. Returns the number of
44 // arguments to skip. This will normally be at least 1, but it may
45 // be 0 if this function changes *argv. ARG points to the location
46 // in *ARGV where the option starts, which may be helpful for a
47 // short option.
48 int (*special)(int argc, char** argv, char *arg, Command_line*);
50 // If this is a position independent option which does not take an
51 // argument, this is the member function to call to record it.
52 void (General_options::*general_noarg)();
54 // If this is a position independent function which takes an
55 // argument, this is the member function to call to record it.
56 void (General_options::*general_arg)(const char*);
58 // If this is a position dependent option which does not take an
59 // argument, this is the member function to call to record it.
60 void (Position_dependent_options::*dependent_noarg)();
62 // If this is a position dependent option which takes an argument,
63 // this is the member function to record it.
64 void (Position_dependent_options::*dependent_arg)(const char*);
66 // Return whether this option takes an argument.
67 bool
68 takes_argument() const
69 { return this->general_arg != NULL || this->dependent_arg != NULL; }
72 class options::Command_line_options
74 public:
75 static const One_option options[];
76 static const int options_size;
79 } // End namespace gold.
81 namespace
84 // Handle the special -l option, which adds an input file.
86 int
87 library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
89 return cmdline->process_l_option(argc, argv, arg);
92 // Report usage information for ld --help, and exit.
94 int
95 help(int, char**, char*, gold::Command_line*)
97 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
99 const int options_size = gold::options::Command_line_options::options_size;
100 const gold::options::One_option* options =
101 gold::options::Command_line_options::options;
102 for (int i = 0; i < options_size; ++i)
104 if (options[i].doc == NULL)
105 continue;
107 printf(" ");
108 int len = 2;
109 bool comma = false;
111 int j = i;
114 if (options[j].help_output != NULL)
116 if (comma)
118 printf(", ");
119 len += 2;
121 printf(options[j].help_output);
122 len += std::strlen(options[i].help_output);
124 else
126 if (options[j].short_option != '\0')
128 if (comma)
130 printf(", ");
131 len += 2;
133 printf("-%c", options[j].short_option);
134 len += 2;
137 if (options[j].long_option != NULL)
139 if (comma)
141 printf(", ");
142 len += 2;
144 if (options[j].dash == gold::options::One_option::ONE_DASH)
146 printf("-");
147 ++len;
149 else
151 printf("--");
152 len += 2;
154 printf("%s", options[j].long_option);
155 len += std::strlen(options[j].long_option);
158 ++j;
160 while (j < options_size && options[j].doc == NULL);
162 if (len > 30)
164 printf("\n");
165 len = 0;
167 for (; len < 30; ++len)
168 std::putchar(' ');
170 std::puts(options[i].doc);
173 gold::gold_exit(true);
175 return 0;
178 } // End anonymous namespace.
180 namespace gold
183 // Helper macros used to specify the options. We could also do this
184 // using constructors, but then g++ would generate code to initialize
185 // the array. We want the array to be initialized statically so that
186 // we get better startup time.
188 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
189 { short_option, long_option, doc, help, options::One_option::dash, \
190 NULL, func, NULL, NULL, NULL }
191 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
192 { short_option, long_option, doc, help, options::One_option::dash, \
193 NULL, NULL, func, NULL, NULL }
194 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
195 { short_option, long_option, doc, help, options::One_option::dash, \
196 NULL, NULL, NULL, func, NULL }
197 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
198 { short_option, long_option, doc, help, options::One_option::dash, \
199 NULL, NULL, NULL, NULL, func }
200 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
201 { short_option, long_option, doc, help, options::One_option::dash, \
202 func, NULL, NULL, NULL, NULL }
204 // Here is the actual list of options which we accept.
206 const options::One_option
207 options::Command_line_options::options[] =
209 SPECIAL('l', "library", N_("Search for library LIBNAME"),
210 N_("-lLIBNAME --library LIBNAME"), TWO_DASHES,
211 &library),
212 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
213 N_("-L DIR, --library-path DIR"), TWO_DASHES,
214 &General_options::add_to_search_path),
215 GENERAL_ARG('o', "output", N_("Set output file name"),
216 N_("-o FILE, --output FILE"), TWO_DASHES,
217 &General_options::set_output_file_name),
218 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
219 ONE_DASH, &General_options::set_relocatable),
220 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
221 NULL, ONE_DASH, &General_options::set_shared),
222 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
223 NULL, ONE_DASH, &General_options::set_static),
224 SPECIAL('\0', "help", N_("Report usage information"), NULL,
225 TWO_DASHES, &help)
228 const int options::Command_line_options::options_size =
229 sizeof (options) / sizeof (options[0]);
231 // The default values for the general options.
233 General_options::General_options()
234 : search_path_(),
235 output_file_name_("a.out"),
236 is_relocatable_(false),
237 is_shared_(false),
238 is_static_(false)
242 // The default values for the position dependent options.
244 Position_dependent_options::Position_dependent_options()
245 : do_static_search_(false)
249 // Construct a Command_line.
251 Command_line::Command_line()
255 // Process the command line options.
257 void
258 Command_line::process(int argc, char** argv)
260 const int options_size = options::Command_line_options::options_size;
261 const options::One_option* options =
262 options::Command_line_options::options;
263 bool no_more_options = false;
264 int i = 0;
265 while (i < argc)
267 if (argv[i][0] != '-' || no_more_options)
269 this->inputs_.push_back(Input_argument(argv[i], false,
270 this->position_options_));
271 ++i;
272 continue;
275 // Option starting with '-'.
276 int dashes = 1;
277 if (argv[i][1] == '-')
279 dashes = 2;
280 if (argv[i][2] == '\0')
282 no_more_options = true;
283 continue;
287 // Look for a long option match.
288 char* opt = argv[i] + dashes;
289 char first = opt[0];
290 int skiparg = 0;
291 char* arg = strchr(opt, '=');
292 if (arg != NULL)
293 *arg = '\0';
294 else if (i + 1 < argc)
296 arg = argv[i + 1];
297 skiparg = 1;
300 int j;
301 for (j = 0; j < options_size; ++j)
303 if (options[j].long_option != NULL
304 && (dashes == 2
305 || (options[j].dash
306 != options::One_option::EXACTLY_TWO_DASHES))
307 && first == options[j].long_option[0]
308 && strcmp(opt, options[j].long_option) == 0)
310 if (options[j].special)
311 i += options[j].special(argc - 1, argv + i, opt, this);
312 else
314 if (!options[j].takes_argument())
316 arg = NULL;
317 skiparg = 0;
319 else
321 if (arg == NULL)
322 this->usage(_("missing argument"), argv[i]);
324 this->apply_option(options[j], arg);
325 i += skiparg + 1;
327 break;
330 if (j < options_size)
331 continue;
333 // If we saw two dashes, we need to see a long option.
334 if (dashes == 2)
335 this->usage(_("unknown option"), argv[i]);
337 // Look for a short option match. There may be more than one
338 // short option in a given argument.
339 bool done = false;
340 char* s = argv[i] + 1;
341 ++i;
342 while (*s != '\0' && !done)
344 char opt = *s;
345 int j;
346 for (j = 0; j < options_size; ++j)
348 if (options[j].short_option == opt)
350 if (options[j].special)
352 // Undo the argument skip done above.
353 --i;
354 i += options[j].special(argc - i, argv + i, s, this);
355 done = true;
357 else
359 arg = NULL;
360 if (options[j].takes_argument())
362 if (s[1] != '\0')
364 arg = s + 1;
365 done = true;
367 else if (i < argc)
369 arg = argv[i];
370 ++i;
372 else
373 this->usage(_("missing argument"), opt);
375 this->apply_option(options[j], arg);
377 break;
381 if (j >= options_size)
382 this->usage(_("unknown option"), *s);
384 ++s;
388 // FIXME: We should only do this when configured in native mode.
389 this->options_.add_to_search_path("/lib");
390 this->options_.add_to_search_path("/usr/lib");
393 // Apply a command line option.
395 void
396 Command_line::apply_option(const options::One_option& opt,
397 const char* arg)
399 if (arg == NULL)
401 if (opt.general_noarg)
402 (this->options_.*(opt.general_noarg))();
403 else if (opt.dependent_noarg)
404 (this->position_options_.*(opt.dependent_noarg))();
405 else
406 gold_unreachable();
408 else
410 if (opt.general_arg)
411 (this->options_.*(opt.general_arg))(arg);
412 else if (opt.dependent_arg)
413 (this->position_options_.*(opt.dependent_arg))(arg);
414 else
415 gold_unreachable();
419 // Handle the -l option, which requires special treatment.
422 Command_line::process_l_option(int argc, char** argv, char* arg)
424 int ret;
425 const char* libname;
426 if (arg[1] != '\0')
428 ret = 1;
429 libname = arg + 1;
431 else if (argc > 1)
433 ret = 2;
434 libname = argv[argc + 1];
436 else
437 this->usage(_("missing argument"), arg);
439 this->inputs_.push_back(Input_argument(libname, true,
440 this->position_options_));
442 return ret;
445 // Report a usage error. */
447 void
448 Command_line::usage()
450 fprintf(stderr,
451 _("%s: use the --help option for usage information\n"),
452 program_name);
453 gold_exit(false);
456 void
457 Command_line::usage(const char* msg, const char *opt)
459 fprintf(stderr,
460 _("%s: %s: %s\n"),
461 program_name, opt, msg);
462 this->usage();
465 void
466 Command_line::usage(const char* msg, char opt)
468 fprintf(stderr,
469 _("%s: -%c: %s\n"),
470 program_name, opt, msg);
471 this->usage();
474 } // End namespace gold.