* gas/ppc/booke.s: Add tlbsx, tlbsxe.
[binutils.git] / gold / options.cc
blobb8339e8a625390ba35f89b31bcc9fd8104876d2d
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 // Handle the special --start-group option.
94 int
95 start_group(int, char**, char* arg, gold::Command_line* cmdline)
97 cmdline->start_group(arg);
98 return 1;
101 // Handle the special --end-group option.
104 end_group(int, char**, char* arg, gold::Command_line* cmdline)
106 cmdline->end_group(arg);
107 return 1;
110 // Report usage information for ld --help, and exit.
113 help(int, char**, char*, gold::Command_line*)
115 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
117 const int options_size = gold::options::Command_line_options::options_size;
118 const gold::options::One_option* options =
119 gold::options::Command_line_options::options;
120 for (int i = 0; i < options_size; ++i)
122 if (options[i].doc == NULL)
123 continue;
125 printf(" ");
126 int len = 2;
127 bool comma = false;
129 int j = i;
132 if (options[j].help_output != NULL)
134 if (comma)
136 printf(", ");
137 len += 2;
139 printf(options[j].help_output);
140 len += std::strlen(options[i].help_output);
142 else
144 if (options[j].short_option != '\0')
146 if (comma)
148 printf(", ");
149 len += 2;
151 printf("-%c", options[j].short_option);
152 len += 2;
155 if (options[j].long_option != NULL)
157 if (comma)
159 printf(", ");
160 len += 2;
162 if (options[j].dash == gold::options::One_option::ONE_DASH)
164 printf("-");
165 ++len;
167 else
169 printf("--");
170 len += 2;
172 printf("%s", options[j].long_option);
173 len += std::strlen(options[j].long_option);
176 ++j;
178 while (j < options_size && options[j].doc == NULL);
180 if (len > 30)
182 printf("\n");
183 len = 0;
185 for (; len < 30; ++len)
186 std::putchar(' ');
188 std::puts(options[i].doc);
191 gold::gold_exit(true);
193 return 0;
196 } // End anonymous namespace.
198 namespace gold
201 // Helper macros used to specify the options. We could also do this
202 // using constructors, but then g++ would generate code to initialize
203 // the array. We want the array to be initialized statically so that
204 // we get better startup time.
206 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
207 { short_option, long_option, doc, help, options::One_option::dash, \
208 NULL, func, NULL, NULL, NULL }
209 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
210 { short_option, long_option, doc, help, options::One_option::dash, \
211 NULL, NULL, func, NULL, NULL }
212 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
213 { short_option, long_option, doc, help, options::One_option::dash, \
214 NULL, NULL, NULL, func, NULL }
215 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
216 { short_option, long_option, doc, help, options::One_option::dash, \
217 NULL, NULL, NULL, NULL, func }
218 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
219 { short_option, long_option, doc, help, options::One_option::dash, \
220 func, NULL, NULL, NULL, NULL }
222 // Here is the actual list of options which we accept.
224 const options::One_option
225 options::Command_line_options::options[] =
227 SPECIAL('l', "library", N_("Search for library LIBNAME"),
228 N_("-lLIBNAME --library LIBNAME"), TWO_DASHES,
229 &library),
230 SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
231 TWO_DASHES, &start_group),
232 SPECIAL(')', "end-group", N_("End a library search group"), NULL,
233 TWO_DASHES, &end_group),
234 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
235 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
236 &General_options::set_dynamic_linker),
237 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
238 N_("-L DIR, --library-path DIR"), TWO_DASHES,
239 &General_options::add_to_search_path),
240 GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
241 &General_options::ignore),
242 GENERAL_ARG('o', "output", N_("Set output file name"),
243 N_("-o FILE, --output FILE"), TWO_DASHES,
244 &General_options::set_output_file_name),
245 GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
246 ONE_DASH, &General_options::set_relocatable),
247 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
248 NULL, ONE_DASH, &General_options::set_shared),
249 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
250 NULL, ONE_DASH, &General_options::set_static),
251 POSDEP_NOARG('\0', "as-needed",
252 N_("Only set DT_NEEDED for following dynamic libs if used"),
253 NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
254 POSDEP_NOARG('\0', "no-as-needed",
255 N_("Always DT_NEEDED for following dynamic libs (default)"),
256 NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
257 SPECIAL('\0', "help", N_("Report usage information"), NULL,
258 TWO_DASHES, &help)
261 const int options::Command_line_options::options_size =
262 sizeof (options) / sizeof (options[0]);
264 // The default values for the general options.
266 General_options::General_options()
267 : dynamic_linker_(NULL),
268 search_path_(),
269 output_file_name_("a.out"),
270 is_relocatable_(false),
271 is_shared_(false),
272 is_static_(false)
276 // The default values for the position dependent options.
278 Position_dependent_options::Position_dependent_options()
279 : do_static_search_(false)
283 // Input_arguments methods.
285 // Add a file to the list.
287 void
288 Input_arguments::add_file(const Input_file_argument& file)
290 if (!this->in_group_)
291 this->input_argument_list_.push_back(Input_argument(file));
292 else
294 gold_assert(!this->input_argument_list_.empty());
295 gold_assert(this->input_argument_list_.back().is_group());
296 this->input_argument_list_.back().group()->add_file(file);
300 // Start a group.
302 void
303 Input_arguments::start_group()
305 gold_assert(!this->in_group_);
306 Input_file_group* group = new Input_file_group();
307 this->input_argument_list_.push_back(Input_argument(group));
308 this->in_group_ = true;
311 // End a group.
313 void
314 Input_arguments::end_group()
316 gold_assert(this->in_group_);
317 this->in_group_ = false;
320 // Command_line options.
322 Command_line::Command_line()
323 : options_(), position_options_(), inputs_()
327 // Process the command line options.
329 void
330 Command_line::process(int argc, char** argv)
332 const int options_size = options::Command_line_options::options_size;
333 const options::One_option* options =
334 options::Command_line_options::options;
335 bool no_more_options = false;
336 int i = 0;
337 while (i < argc)
339 if (argv[i][0] != '-' || no_more_options)
341 this->add_file(argv[i], false);
342 ++i;
343 continue;
346 // Option starting with '-'.
347 int dashes = 1;
348 if (argv[i][1] == '-')
350 dashes = 2;
351 if (argv[i][2] == '\0')
353 no_more_options = true;
354 continue;
358 // Look for a long option match.
359 char* opt = argv[i] + dashes;
360 char first = opt[0];
361 int skiparg = 0;
362 char* arg = strchr(opt, '=');
363 if (arg != NULL)
364 *arg = '\0';
365 else if (i + 1 < argc)
367 arg = argv[i + 1];
368 skiparg = 1;
371 int j;
372 for (j = 0; j < options_size; ++j)
374 if (options[j].long_option != NULL
375 && (dashes == 2
376 || (options[j].dash
377 != options::One_option::EXACTLY_TWO_DASHES))
378 && first == options[j].long_option[0]
379 && strcmp(opt, options[j].long_option) == 0)
381 if (options[j].special)
382 i += options[j].special(argc - 1, argv + i, opt, this);
383 else
385 if (!options[j].takes_argument())
387 arg = NULL;
388 skiparg = 0;
390 else
392 if (arg == NULL)
393 this->usage(_("missing argument"), argv[i]);
395 this->apply_option(options[j], arg);
396 i += skiparg + 1;
398 break;
401 if (j < options_size)
402 continue;
404 // If we saw two dashes, we need to see a long option.
405 if (dashes == 2)
406 this->usage(_("unknown option"), argv[i]);
408 // Look for a short option match. There may be more than one
409 // short option in a given argument.
410 bool done = false;
411 char* s = argv[i] + 1;
412 ++i;
413 while (*s != '\0' && !done)
415 char opt = *s;
416 int j;
417 for (j = 0; j < options_size; ++j)
419 if (options[j].short_option == opt)
421 if (options[j].special)
423 // Undo the argument skip done above.
424 --i;
425 i += options[j].special(argc - i, argv + i, s, this);
426 done = true;
428 else
430 arg = NULL;
431 if (options[j].takes_argument())
433 if (s[1] != '\0')
435 arg = s + 1;
436 done = true;
438 else if (i < argc)
440 arg = argv[i];
441 ++i;
443 else
444 this->usage(_("missing argument"), opt);
446 this->apply_option(options[j], arg);
448 break;
452 if (j >= options_size)
453 this->usage(_("unknown option"), *s);
455 ++s;
459 if (this->inputs_.in_group())
461 fprintf(stderr, _("%s: missing group end"), program_name);
462 this->usage();
465 // FIXME: We should only do this when configured in native mode.
466 this->options_.add_to_search_path("/lib");
467 this->options_.add_to_search_path("/usr/lib");
470 // Apply a command line option.
472 void
473 Command_line::apply_option(const options::One_option& opt,
474 const char* arg)
476 if (arg == NULL)
478 if (opt.general_noarg)
479 (this->options_.*(opt.general_noarg))();
480 else if (opt.dependent_noarg)
481 (this->position_options_.*(opt.dependent_noarg))();
482 else
483 gold_unreachable();
485 else
487 if (opt.general_arg)
488 (this->options_.*(opt.general_arg))(arg);
489 else if (opt.dependent_arg)
490 (this->position_options_.*(opt.dependent_arg))(arg);
491 else
492 gold_unreachable();
496 // Add an input file or library.
498 void
499 Command_line::add_file(const char* name, bool is_lib)
501 Input_file_argument file(name, is_lib, this->position_options_);
502 this->inputs_.add_file(file);
505 // Handle the -l option, which requires special treatment.
508 Command_line::process_l_option(int argc, char** argv, char* arg)
510 int ret;
511 const char* libname;
512 if (arg[1] != '\0')
514 ret = 1;
515 libname = arg + 1;
517 else if (argc > 1)
519 ret = 2;
520 libname = argv[argc + 1];
522 else
523 this->usage(_("missing argument"), arg);
525 this->add_file(libname, true);
527 return ret;
530 // Handle the --start-group option.
532 void
533 Command_line::start_group(const char* arg)
535 if (this->inputs_.in_group())
536 this->usage(_("may not nest groups"), arg);
537 this->inputs_.start_group();
540 // Handle the --end-group option.
542 void
543 Command_line::end_group(const char* arg)
545 if (!this->inputs_.in_group())
546 this->usage(_("group end without group start"), arg);
547 this->inputs_.end_group();
550 // Report a usage error. */
552 void
553 Command_line::usage()
555 fprintf(stderr,
556 _("%s: use the --help option for usage information\n"),
557 program_name);
558 gold_exit(false);
561 void
562 Command_line::usage(const char* msg, const char *opt)
564 fprintf(stderr,
565 _("%s: %s: %s\n"),
566 program_name, opt, msg);
567 this->usage();
570 void
571 Command_line::usage(const char* msg, char opt)
573 fprintf(stderr,
574 _("%s: -%c: %s\n"),
575 program_name, opt, msg);
576 this->usage();
579 } // End namespace gold.