* options.cc: Include "../bfd/bfdver.h".
[deliverable/binutils-gdb.git] / gold / options.cc
1 // options.c -- handle command line options for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <vector>
27 #include <iostream>
28 #include <sys/stat.h>
29 #include "filenames.h"
30 #include "libiberty.h"
31 #include "demangle.h"
32 #include "../bfd/bfdver.h"
33
34 #include "debug.h"
35 #include "script.h"
36 #include "target-select.h"
37 #include "options.h"
38
39 namespace gold
40 {
41
42 General_options
43 Position_dependent_options::default_options_;
44
45 namespace options
46 {
47
48 // This global variable is set up as General_options is constructed.
49 static std::vector<const One_option*> registered_options;
50
51 // These are set up at the same time -- the variables that accept one
52 // dash, two, or require -z. A single variable may be in more than
53 // one of thes data structures.
54 typedef Unordered_map<std::string, One_option*> Option_map;
55 static Option_map* long_options = NULL;
56 static One_option* short_options[128];
57
58 void
59 One_option::register_option()
60 {
61 registered_options.push_back(this);
62
63 // We can't make long_options a static Option_map because we can't
64 // guarantee that will be initialized before register_option() is
65 // first called.
66 if (long_options == NULL)
67 long_options = new Option_map;
68
69 // TWO_DASHES means that two dashes are preferred, but one is ok too.
70 if (!this->longname.empty())
71 (*long_options)[this->longname] = this;
72
73 const int shortname_as_int = static_cast<int>(this->shortname);
74 gold_assert(shortname_as_int >= 0 && shortname_as_int < 128);
75 if (this->shortname != '\0')
76 short_options[shortname_as_int] = this;
77 }
78
79 void
80 One_option::print() const
81 {
82 bool comma = false;
83 printf(" ");
84 int len = 2;
85 if (this->shortname != '\0')
86 {
87 len += printf("-%c", this->shortname);
88 if (this->helparg)
89 {
90 // -z takes long-names only.
91 gold_assert(this->dashes != DASH_Z);
92 len += printf(" %s", gettext(this->helparg));
93 }
94 comma = true;
95 }
96 if (!this->longname.empty()
97 && !(this->longname[0] == this->shortname
98 && this->longname[1] == '\0'))
99 {
100 if (comma)
101 len += printf(", ");
102 switch (this->dashes)
103 {
104 case options::ONE_DASH: case options::EXACTLY_ONE_DASH:
105 len += printf("-");
106 break;
107 case options::TWO_DASHES: case options::EXACTLY_TWO_DASHES:
108 len += printf("--");
109 break;
110 case options::DASH_Z:
111 len += printf("-z ");
112 break;
113 default:
114 gold_unreachable();
115 }
116 len += printf("%s", this->longname.c_str());
117 if (this->helparg)
118 {
119 // For most options, we print "--frob FOO". But for -z
120 // we print "-z frob=FOO".
121 len += printf("%c%s", this->dashes == options::DASH_Z ? '=' : ' ',
122 gettext(this->helparg));
123 }
124 }
125
126 if (len >= 30)
127 {
128 printf("\n");
129 len = 0;
130 }
131 for (; len < 30; ++len)
132 std::putchar(' ');
133
134 // TODO: if we're boolean, add " (default)" when appropriate.
135 printf("%s\n", gettext(this->helpstring));
136 }
137
138 void
139 help()
140 {
141 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
142
143 std::vector<const One_option*>::const_iterator it;
144 for (it = registered_options.begin(); it != registered_options.end(); ++it)
145 (*it)->print();
146
147 // config.guess and libtool.m4 look in ld --help output for the
148 // string "supported targets".
149 printf(_("%s: supported targets:"), gold::program_name);
150 std::vector<const char*> supported_names;
151 gold::supported_target_names(&supported_names);
152 for (std::vector<const char*>::const_iterator p = supported_names.begin();
153 p != supported_names.end();
154 ++p)
155 printf(" %s", *p);
156 printf("\n");
157
158 // REPORT_BUGS_TO is defined in bfd/bfdver.h.
159 const char* report = REPORT_BUGS_TO;
160 if (*report != '\0')
161 printf(_("Report bugs to %s\n"), report);
162 }
163
164 // For bool, arg will be NULL (boolean options take no argument);
165 // we always just set to true.
166 void
167 parse_bool(const char*, const char*, bool* retval)
168 {
169 *retval = true;
170 }
171
172 void
173 parse_uint(const char* option_name, const char* arg, int* retval)
174 {
175 char* endptr;
176 *retval = strtol(arg, &endptr, 0);
177 if (*endptr != '\0' || retval < 0)
178 gold_fatal(_("%s: invalid option value (expected an integer): %s"),
179 option_name, arg);
180 }
181
182 void
183 parse_uint64(const char* option_name, const char* arg, uint64_t *retval)
184 {
185 char* endptr;
186 *retval = strtoull(arg, &endptr, 0);
187 if (*endptr != '\0')
188 gold_fatal(_("%s: invalid option value (expected an integer): %s"),
189 option_name, arg);
190 }
191
192 void
193 parse_double(const char* option_name, const char* arg, double* retval)
194 {
195 char* endptr;
196 *retval = strtod(arg, &endptr);
197 if (*endptr != '\0')
198 gold_fatal(_("%s: invalid option value "
199 "(expected a floating point number): %s"),
200 option_name, arg);
201 }
202
203 void
204 parse_string(const char* option_name, const char* arg, const char** retval)
205 {
206 if (*arg == '\0')
207 gold_fatal(_("%s: must take a non-empty argument"), option_name);
208 *retval = arg;
209 }
210
211 void
212 parse_optional_string(const char*, const char* arg, const char** retval)
213 {
214 *retval = arg;
215 }
216
217 void
218 parse_dirlist(const char*, const char* arg, Dir_list* retval)
219 {
220 retval->push_back(Search_directory(arg, false));
221 }
222
223 void
224 parse_choices(const char* option_name, const char* arg, const char** retval,
225 const char* choices[], int num_choices)
226 {
227 for (int i = 0; i < num_choices; i++)
228 if (strcmp(choices[i], arg) == 0)
229 {
230 *retval = arg;
231 return;
232 }
233
234 // If we get here, the user did not enter a valid choice, so we die.
235 std::string choices_list;
236 for (int i = 0; i < num_choices; i++)
237 {
238 choices_list += choices[i];
239 if (i != num_choices - 1)
240 choices_list += ", ";
241 }
242 gold_fatal(_("%s: must take one of the following arguments: %s"),
243 option_name, choices_list.c_str());
244 }
245
246 } // End namespace options.
247
248 // Define the handler for "special" options (set via DEFINE_special).
249
250 void
251 General_options::parse_help(const char*, const char*, Command_line*)
252 {
253 options::help();
254 ::exit(EXIT_SUCCESS);
255 }
256
257 void
258 General_options::parse_version(const char* opt, const char*, Command_line*)
259 {
260 gold::print_version(opt[0] == '-' && opt[1] == 'v');
261 ::exit(EXIT_SUCCESS);
262 }
263
264 void
265 General_options::parse_Bstatic(const char*, const char*, Command_line*)
266 {
267 this->set_Bdynamic(false);
268 }
269
270 void
271 General_options::parse_defsym(const char*, const char* arg,
272 Command_line* cmdline)
273 {
274 cmdline->script_options().define_symbol(arg);
275 }
276
277 void
278 General_options::parse_library(const char*, const char* arg,
279 Command_line* cmdline)
280 {
281 Input_file_argument file(arg, true, "", false, *this);
282 cmdline->inputs().add_file(file);
283 }
284
285 void
286 General_options::parse_R(const char* option, const char* arg,
287 Command_line* cmdline)
288 {
289 struct stat s;
290 if (::stat(arg, &s) != 0 || S_ISDIR(s.st_mode))
291 this->add_to_rpath(arg);
292 else
293 this->parse_just_symbols(option, arg, cmdline);
294 }
295
296 void
297 General_options::parse_just_symbols(const char*, const char* arg,
298 Command_line* cmdline)
299 {
300 Input_file_argument file(arg, false, "", true, *this);
301 cmdline->inputs().add_file(file);
302 }
303
304 void
305 General_options::parse_static(const char*, const char*, Command_line*)
306 {
307 this->set_static(true);
308 }
309
310 void
311 General_options::parse_script(const char*, const char* arg,
312 Command_line* cmdline)
313 {
314 if (!read_commandline_script(arg, cmdline))
315 gold::gold_fatal(_("unable to parse script file %s"), arg);
316 }
317
318 void
319 General_options::parse_version_script(const char*, const char* arg,
320 Command_line* cmdline)
321 {
322 if (!read_version_script(arg, cmdline))
323 gold::gold_fatal(_("unable to parse version script file %s"), arg);
324 }
325
326 void
327 General_options::parse_start_group(const char*, const char*,
328 Command_line* cmdline)
329 {
330 cmdline->inputs().start_group();
331 }
332
333 void
334 General_options::parse_end_group(const char*, const char*,
335 Command_line* cmdline)
336 {
337 cmdline->inputs().end_group();
338 }
339
340 } // End namespace gold.
341
342 namespace
343 {
344
345 void
346 usage()
347 {
348 fprintf(stderr,
349 _("%s: use the --help option for usage information\n"),
350 gold::program_name);
351 ::exit(EXIT_FAILURE);
352 }
353
354 void
355 usage(const char* msg, const char *opt)
356 {
357 fprintf(stderr,
358 _("%s: %s: %s\n"),
359 gold::program_name, opt, msg);
360 usage();
361 }
362
363 // Recognize input and output target names. The GNU linker accepts
364 // these with --format and --oformat. This code is intended to be
365 // minimally compatible. In practice for an ELF target this would be
366 // the same target as the input files; that name always start with
367 // "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex",
368 // "binary", "ihex".
369
370 gold::General_options::Object_format
371 string_to_object_format(const char* arg)
372 {
373 if (strncmp(arg, "elf", 3) == 0)
374 return gold::General_options::OBJECT_FORMAT_ELF;
375 else if (strcmp(arg, "binary") == 0)
376 return gold::General_options::OBJECT_FORMAT_BINARY;
377 else
378 {
379 gold::gold_error(_("format '%s' not supported; treating as elf "
380 "(supported formats: elf, binary)"),
381 arg);
382 return gold::General_options::OBJECT_FORMAT_ELF;
383 }
384 }
385
386 // If the default sysroot is relocatable, try relocating it based on
387 // the prefix FROM.
388
389 char*
390 get_relative_sysroot(const char* from)
391 {
392 char* path = make_relative_prefix(gold::program_name, from,
393 TARGET_SYSTEM_ROOT);
394 if (path != NULL)
395 {
396 struct stat s;
397 if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
398 return path;
399 free(path);
400 }
401
402 return NULL;
403 }
404
405 // Return the default sysroot. This is set by the --with-sysroot
406 // option to configure. Note we do not free the return value of
407 // get_relative_sysroot, which is a small memory leak, but is
408 // necessary since we store this pointer directly in General_options.
409
410 const char*
411 get_default_sysroot()
412 {
413 const char* sysroot = TARGET_SYSTEM_ROOT;
414 if (*sysroot == '\0')
415 return NULL;
416
417 if (TARGET_SYSTEM_ROOT_RELOCATABLE)
418 {
419 char* path = get_relative_sysroot(BINDIR);
420 if (path == NULL)
421 path = get_relative_sysroot(TOOLBINDIR);
422 if (path != NULL)
423 return path;
424 }
425
426 return sysroot;
427 }
428
429 // Parse a long option. Such options have the form
430 // <-|--><option>[=arg]. If "=arg" is not present but the option
431 // takes an argument, the next word is taken to the be the argument.
432 // If equals_only is set, then only the <option>=<arg> form is
433 // accepted, not the <option><space><arg> form. Returns a One_option
434 // struct or NULL if argv[i] cannot be parsed as a long option. In
435 // the not-NULL case, *arg is set to the option's argument (NULL if
436 // the option takes no argument), and *i is advanced past this option.
437 // NOTE: it is safe for argv and arg to point to the same place.
438 gold::options::One_option*
439 parse_long_option(int argc, const char** argv, bool equals_only,
440 const char** arg, int* i)
441 {
442 const char* const this_argv = argv[*i];
443
444 const char* equals = strchr(this_argv, '=');
445 const char* option_start = this_argv + strspn(this_argv, "-");
446 std::string option(option_start,
447 equals ? equals - option_start : strlen(option_start));
448
449 gold::options::Option_map::iterator it
450 = gold::options::long_options->find(option);
451 if (it == gold::options::long_options->end())
452 return NULL;
453
454 gold::options::One_option* retval = it->second;
455
456 // If the dash-count doesn't match, we fail.
457 if (this_argv[0] != '-') // no dashes at all: had better be "-z <longopt>"
458 {
459 if (retval->dashes != gold::options::DASH_Z)
460 return NULL;
461 }
462 else if (this_argv[1] != '-') // one dash
463 {
464 if (retval->dashes != gold::options::ONE_DASH
465 && retval->dashes != gold::options::EXACTLY_ONE_DASH
466 && retval->dashes != gold::options::TWO_DASHES)
467 return NULL;
468 }
469 else // two dashes (or more!)
470 {
471 if (retval->dashes != gold::options::TWO_DASHES
472 && retval->dashes != gold::options::EXACTLY_TWO_DASHES
473 && retval->dashes != gold::options::ONE_DASH)
474 return NULL;
475 }
476
477 // Now that we know the option is good (or else bad in a way that
478 // will cause us to die), increment i to point past this argv.
479 ++(*i);
480
481 // Figure out the option's argument, if any.
482 if (!retval->takes_argument())
483 {
484 if (equals)
485 usage(_("unexpected argument"), this_argv);
486 else
487 *arg = NULL;
488 }
489 else
490 {
491 if (equals)
492 *arg = equals + 1;
493 else if (retval->takes_optional_argument())
494 *arg = retval->default_value;
495 else if (*i < argc && !equals_only)
496 *arg = argv[(*i)++];
497 else
498 usage(_("missing argument"), this_argv);
499 }
500
501 return retval;
502 }
503
504 // Parse a short option. Such options have the form -<option>[arg].
505 // If "arg" is not present but the option takes an argument, the next
506 // word is taken to the be the argument. If the option does not take
507 // an argument, it may be followed by another short option. Returns a
508 // One_option struct or NULL if argv[i] cannot be parsed as a short
509 // option. In the not-NULL case, *arg is set to the option's argument
510 // (NULL if the option takes no argument), and *i is advanced past
511 // this option. This function keeps *i the same if we parsed a short
512 // option that does not take an argument, that looks to be followed by
513 // another short option in the same word.
514 gold::options::One_option*
515 parse_short_option(int argc, const char** argv, int pos_in_argv_i,
516 const char** arg, int* i)
517 {
518 const char* const this_argv = argv[*i];
519
520 if (this_argv[0] != '-')
521 return NULL;
522
523 // We handle -z as a special case.
524 static gold::options::One_option dash_z("", gold::options::DASH_Z,
525 'z', "", "-z", "Z-OPTION", false,
526 NULL);
527 gold::options::One_option* retval = NULL;
528 if (this_argv[pos_in_argv_i] == 'z')
529 retval = &dash_z;
530 else
531 {
532 const int char_as_int = static_cast<int>(this_argv[pos_in_argv_i]);
533 if (char_as_int > 0 && char_as_int < 128)
534 retval = gold::options::short_options[char_as_int];
535 }
536
537 if (retval == NULL)
538 return NULL;
539
540 // Figure out the option's argument, if any.
541 if (!retval->takes_argument())
542 {
543 *arg = NULL;
544 // We only advance past this argument if it's the only one in argv.
545 if (this_argv[pos_in_argv_i + 1] == '\0')
546 ++(*i);
547 }
548 else
549 {
550 // If we take an argument, we'll eat up this entire argv entry.
551 ++(*i);
552 if (this_argv[pos_in_argv_i + 1] != '\0')
553 *arg = this_argv + pos_in_argv_i + 1;
554 else if (retval->takes_optional_argument())
555 *arg = retval->default_value;
556 else if (*i < argc)
557 *arg = argv[(*i)++];
558 else
559 usage(_("missing argument"), this_argv);
560 }
561
562 // If we're a -z option, we need to parse our argument as a
563 // long-option, e.g. "-z stacksize=8192".
564 if (retval == &dash_z)
565 {
566 int dummy_i = 0;
567 const char* dash_z_arg = *arg;
568 retval = parse_long_option(1, arg, true, arg, &dummy_i);
569 if (retval == NULL)
570 usage(_("unknown -z option"), dash_z_arg);
571 }
572
573 return retval;
574 }
575
576 } // End anonymous namespace.
577
578 namespace gold
579 {
580
581 General_options::General_options()
582 : execstack_status_(General_options::EXECSTACK_FROM_INPUT), static_(false),
583 do_demangle_(false)
584 {
585 }
586
587 General_options::Object_format
588 General_options::format_enum() const
589 {
590 return string_to_object_format(this->format());
591 }
592
593 General_options::Object_format
594 General_options::oformat_enum() const
595 {
596 return string_to_object_format(this->oformat());
597 }
598
599 // Add the sysroot, if any, to the search paths.
600
601 void
602 General_options::add_sysroot()
603 {
604 if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
605 {
606 this->set_sysroot(get_default_sysroot());
607 if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
608 return;
609 }
610
611 char* canonical_sysroot = lrealpath(this->sysroot());
612
613 for (Dir_list::iterator p = this->library_path_.value.begin();
614 p != this->library_path_.value.end();
615 ++p)
616 p->add_sysroot(this->sysroot(), canonical_sysroot);
617
618 free(canonical_sysroot);
619 }
620
621 // Set up variables and other state that isn't set up automatically by
622 // the parse routine, and ensure options don't contradict each other
623 // and are otherwise kosher.
624
625 void
626 General_options::finalize()
627 {
628 // Normalize the strip modifiers. They have a total order:
629 // strip_all > strip_debug > strip_debug_gdb. If one is true, set
630 // all beneath it to true as well.
631 if (this->strip_all())
632 this->set_strip_debug(true);
633 if (this->strip_debug())
634 this->set_strip_debug_gdb(true);
635
636 // If the user specifies both -s and -r, convert the -s to -S.
637 // -r requires us to keep externally visible symbols!
638 if (this->strip_all() && this->relocatable())
639 {
640 this->set_strip_all(false);
641 gold_assert(this->strip_debug());
642 }
643
644 // For us, -dc and -dp are synonyms for --define-common.
645 if (this->dc())
646 this->set_define_common(true);
647 if (this->dp())
648 this->set_define_common(true);
649
650 // We also set --define-common if we're not relocatable, as long as
651 // the user didn't explicitly ask for something different.
652 if (!this->user_set_define_common())
653 this->set_define_common(!this->relocatable());
654
655 // execstack_status_ is a three-state variable; update it based on
656 // -z [no]execstack.
657 if (this->execstack())
658 this->set_execstack_status(EXECSTACK_YES);
659 else if (this->noexecstack())
660 this->set_execstack_status(EXECSTACK_NO);
661
662 // Handle the optional argument for --demangle.
663 if (this->user_set_demangle())
664 {
665 this->set_do_demangle(true);
666 const char* style = this->demangle();
667 if (*style != '\0')
668 {
669 enum demangling_styles style_code;
670
671 style_code = cplus_demangle_name_to_style(style);
672 if (style_code == unknown_demangling)
673 gold_fatal("unknown demangling style '%s'", style);
674 cplus_demangle_set_style(style_code);
675 }
676 }
677 else if (this->user_set_no_demangle())
678 this->set_do_demangle(false);
679 else
680 {
681 // Testing COLLECT_NO_DEMANGLE makes our default demangling
682 // behaviour identical to that of gcc's linker wrapper.
683 this->set_do_demangle(getenv("COLLECT_NO_DEMANGLE") == NULL);
684 }
685
686 // If --thread_count is specified, it applies to
687 // --thread-count-{initial,middle,final}, though it doesn't override
688 // them.
689 if (this->thread_count() > 0 && this->thread_count_initial() == 0)
690 this->set_thread_count_initial(this->thread_count());
691 if (this->thread_count() > 0 && this->thread_count_middle() == 0)
692 this->set_thread_count_middle(this->thread_count());
693 if (this->thread_count() > 0 && this->thread_count_final() == 0)
694 this->set_thread_count_final(this->thread_count());
695
696 // Let's warn if you set the thread-count but we're going to ignore it.
697 #ifndef ENABLE_THREADS
698 if (this->threads())
699 {
700 gold_warning(_("ignoring --threads: "
701 "%s was compiled without thread support"),
702 program_name);
703 this->set_threads(false);
704 }
705 if (this->thread_count() > 0 || this->thread_count_initial() > 0
706 || this->thread_count_middle() > 0 || this->thread_count_final() > 0)
707 gold_warning(_("ignoring --thread-count: "
708 "%s was compiled without thread support"),
709 program_name);
710 #endif
711
712 // Even if they don't specify it, we add -L /lib and -L /usr/lib.
713 // FIXME: We should only do this when configured in native mode.
714 this->add_to_library_path_with_sysroot("/lib");
715 this->add_to_library_path_with_sysroot("/usr/lib");
716
717 // Normalize library_path() by adding the sysroot to all directories
718 // in the path, as appropriate.
719 this->add_sysroot();
720
721 // Now that we've normalized the options, check for contradictory ones.
722 if (this->shared() && this->relocatable())
723 gold_fatal(_("-shared and -r are incompatible"));
724
725 if (this->oformat_enum() != General_options::OBJECT_FORMAT_ELF
726 && (this->shared() || this->relocatable()))
727 gold_fatal(_("binary output format not compatible with -shared or -r"));
728
729 if (this->user_set_hash_bucket_empty_fraction()
730 && (this->hash_bucket_empty_fraction() < 0.0
731 || this->hash_bucket_empty_fraction() >= 1.0))
732 gold_fatal(_("--hash-bucket-empty-fraction value %g out of range "
733 "[0.0, 1.0)"),
734 this->hash_bucket_empty_fraction());
735
736 // FIXME: we can/should be doing a lot more sanity checking here.
737 }
738
739 // Search_directory methods.
740
741 // This is called if we have a sysroot. Apply the sysroot if
742 // appropriate. Record whether the directory is in the sysroot.
743
744 void
745 Search_directory::add_sysroot(const char* sysroot,
746 const char* canonical_sysroot)
747 {
748 gold_assert(*sysroot != '\0');
749 if (this->put_in_sysroot_)
750 {
751 if (!IS_DIR_SEPARATOR(this->name_[0])
752 && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
753 this->name_ = '/' + this->name_;
754 this->name_ = sysroot + this->name_;
755 this->is_in_sysroot_ = true;
756 }
757 else
758 {
759 // Check whether this entry is in the sysroot. To do this
760 // correctly, we need to use canonical names. Otherwise we will
761 // get confused by the ../../.. paths that gcc tends to use.
762 char* canonical_name = lrealpath(this->name_.c_str());
763 int canonical_name_len = strlen(canonical_name);
764 int canonical_sysroot_len = strlen(canonical_sysroot);
765 if (canonical_name_len > canonical_sysroot_len
766 && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
767 {
768 canonical_name[canonical_sysroot_len] = '\0';
769 if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
770 this->is_in_sysroot_ = true;
771 }
772 free(canonical_name);
773 }
774 }
775
776 // Input_arguments methods.
777
778 // Add a file to the list.
779
780 void
781 Input_arguments::add_file(const Input_file_argument& file)
782 {
783 if (!this->in_group_)
784 this->input_argument_list_.push_back(Input_argument(file));
785 else
786 {
787 gold_assert(!this->input_argument_list_.empty());
788 gold_assert(this->input_argument_list_.back().is_group());
789 this->input_argument_list_.back().group()->add_file(file);
790 }
791 }
792
793 // Start a group.
794
795 void
796 Input_arguments::start_group()
797 {
798 if (this->in_group_)
799 gold_fatal(_("May not nest groups"));
800 Input_file_group* group = new Input_file_group();
801 this->input_argument_list_.push_back(Input_argument(group));
802 this->in_group_ = true;
803 }
804
805 // End a group.
806
807 void
808 Input_arguments::end_group()
809 {
810 if (!this->in_group_)
811 gold_fatal(_("Group end without group start"));
812 this->in_group_ = false;
813 }
814
815 // Command_line options.
816
817 Command_line::Command_line()
818 {
819 }
820
821 // Process the command line options. For process_one_option, i is the
822 // index of argv to process next, and must be an option (that is,
823 // start with a dash). The return value is the index of the next
824 // option to process (i+1 or i+2, or argc to indicate processing is
825 // done). no_more_options is set to true if (and when) "--" is seen
826 // as an option.
827
828 int
829 Command_line::process_one_option(int argc, const char** argv, int i,
830 bool* no_more_options)
831 {
832 gold_assert(argv[i][0] == '-' && !(*no_more_options));
833
834 // If we are reading "--", then just set no_more_options and return.
835 if (argv[i][1] == '-' && argv[i][2] == '\0')
836 {
837 *no_more_options = true;
838 return i + 1;
839 }
840
841 int new_i = i;
842 options::One_option* option = NULL;
843 const char* arg = NULL;
844
845 // First, try to process argv as a long option.
846 option = parse_long_option(argc, argv, false, &arg, &new_i);
847 if (option)
848 {
849 option->reader->parse_to_value(argv[i], arg, this, &this->options_);
850 return new_i;
851 }
852
853 // Now, try to process argv as a short option. Since several short
854 // options can be combined in one argv, we may have to parse a lot
855 // until we're done reading this argv.
856 int pos_in_argv_i = 1;
857 while (new_i == i)
858 {
859 option = parse_short_option(argc, argv, pos_in_argv_i, &arg, &new_i);
860 if (!option)
861 break;
862 option->reader->parse_to_value(argv[i], arg, this, &this->options_);
863 ++pos_in_argv_i;
864 }
865 if (option)
866 return new_i;
867
868 // I guess it's neither a long option nor a short option.
869 usage(_("unknown option"), argv[i]);
870 return argc;
871 }
872
873
874 void
875 Command_line::process(int argc, const char** argv)
876 {
877 bool no_more_options = false;
878 int i = 0;
879 while (i < argc)
880 {
881 this->position_options_.copy_from_options(this->options());
882 if (no_more_options || argv[i][0] != '-')
883 {
884 Input_file_argument file(argv[i], false, "", false,
885 this->position_options_);
886 this->inputs_.add_file(file);
887 ++i;
888 }
889 else
890 i = process_one_option(argc, argv, i, &no_more_options);
891 }
892
893 if (this->inputs_.in_group())
894 {
895 fprintf(stderr, _("%s: missing group end\n"), program_name);
896 usage();
897 }
898
899 // Normalize the options and ensure they don't contradict each other.
900 this->options_.finalize();
901 }
902
903 } // End namespace gold.
This page took 0.051135 seconds and 5 git commands to generate.