1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
31 // The information we keep for a single command line option.
33 struct options::One_option
35 // The single character option name, or '\0' if this is only a long
39 // The long option name, or NULL if this is only a short option.
40 const char* long_option
;
42 // Description of the option for --help output, or NULL if there is none.
45 // How to print the option name in --help output, or NULL to use the
47 const char* help_output
;
49 // Long option dash control. This is ignored if long_option is
53 // Long option normally takes one dash; two dashes are also
56 // Long option normally takes two dashes; one dash is also
59 // Long option always takes two dashes.
63 // Function for special handling, or NULL. Returns the number of
64 // arguments to skip. This will normally be at least 1, but it may
65 // be 0 if this function changes *argv. ARG points to the location
66 // in *ARGV where the option starts, which may be helpful for a
68 int (*special
)(int argc
, char** argv
, char *arg
, Command_line
*);
70 // If this is a position independent option which does not take an
71 // argument, this is the member function to call to record it.
72 void (General_options::*general_noarg
)();
74 // If this is a position independent function which takes an
75 // argument, this is the member function to call to record it.
76 void (General_options::*general_arg
)(const char*);
78 // If this is a position dependent option which does not take an
79 // argument, this is the member function to call to record it.
80 void (Position_dependent_options::*dependent_noarg
)();
82 // If this is a position dependent option which takes an argument,
83 // this is the member function to record it.
84 void (Position_dependent_options::*dependent_arg
)(const char*);
86 // Return whether this option takes an argument.
88 takes_argument() const
89 { return this->general_arg
!= NULL
|| this->dependent_arg
!= NULL
; }
92 class options::Command_line_options
95 static const One_option options
[];
96 static const int options_size
;
99 } // End namespace gold.
104 // Handle the special -l option, which adds an input file.
107 library(int argc
, char** argv
, char* arg
, gold::Command_line
* cmdline
)
109 return cmdline
->process_l_option(argc
, argv
, arg
);
112 // Handle the special --start-group option.
115 start_group(int, char**, char* arg
, gold::Command_line
* cmdline
)
117 cmdline
->start_group(arg
);
121 // Handle the special --end-group option.
124 end_group(int, char**, char* arg
, gold::Command_line
* cmdline
)
126 cmdline
->end_group(arg
);
130 // Report usage information for ld --help, and exit.
133 help(int, char**, char*, gold::Command_line
*)
135 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
137 const int options_size
= gold::options::Command_line_options::options_size
;
138 const gold::options::One_option
* options
=
139 gold::options::Command_line_options::options
;
140 for (int i
= 0; i
< options_size
; ++i
)
142 if (options
[i
].doc
== NULL
)
152 if (options
[j
].help_output
!= NULL
)
159 printf(options
[j
].help_output
);
160 len
+= std::strlen(options
[i
].help_output
);
165 if (options
[j
].short_option
!= '\0')
172 printf("-%c", options
[j
].short_option
);
177 if (options
[j
].long_option
!= NULL
)
184 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
194 printf("%s", options
[j
].long_option
);
195 len
+= std::strlen(options
[j
].long_option
);
201 while (j
< options_size
&& options
[j
].doc
== NULL
);
208 for (; len
< 30; ++len
)
211 std::puts(options
[i
].doc
);
214 gold::gold_exit(true);
219 } // End anonymous namespace.
224 // Helper macros used to specify the options. We could also do this
225 // using constructors, but then g++ would generate code to initialize
226 // the array. We want the array to be initialized statically so that
227 // we get better startup time.
229 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
230 { short_option, long_option, doc, help, options::One_option::dash, \
231 NULL, func, NULL, NULL, NULL }
232 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
233 { short_option, long_option, doc, help, options::One_option::dash, \
234 NULL, NULL, func, NULL, NULL }
235 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
236 { short_option, long_option, doc, help, options::One_option::dash, \
237 NULL, NULL, NULL, func, NULL }
238 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
239 { short_option, long_option, doc, help, options::One_option::dash, \
240 NULL, NULL, NULL, NULL, func }
241 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
242 { short_option, long_option, doc, help, options::One_option::dash, \
243 func, NULL, NULL, NULL, NULL }
245 // Here is the actual list of options which we accept.
247 const options::One_option
248 options::Command_line_options::options
[] =
250 SPECIAL('l', "library", N_("Search for library LIBNAME"),
251 N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES
,
253 SPECIAL('(', "start-group", N_("Start a library search group"), NULL
,
254 TWO_DASHES
, &start_group
),
255 SPECIAL(')', "end-group", N_("End a library search group"), NULL
,
256 TWO_DASHES
, &end_group
),
257 GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
258 NULL
, TWO_DASHES
, &General_options::set_export_dynamic
),
259 GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
260 N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES
,
261 &General_options::set_dynamic_linker
),
262 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
263 N_("-L DIR, --library-path DIR"), TWO_DASHES
,
264 &General_options::add_to_search_path
),
265 GENERAL_ARG('m', NULL
, N_("Ignored for compatibility"), NULL
, ONE_DASH
,
266 &General_options::ignore
),
267 GENERAL_ARG('O', NULL
, N_("Optimize output file size"),
268 N_("-O level"), ONE_DASH
,
269 &General_options::set_optimization_level
),
270 GENERAL_ARG('o', "output", N_("Set output file name"),
271 N_("-o FILE, --output FILE"), TWO_DASHES
,
272 &General_options::set_output_file_name
),
273 GENERAL_NOARG('r', NULL
, N_("Generate relocatable output"), NULL
,
274 ONE_DASH
, &General_options::set_relocatable
),
275 GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
276 N_("-R DIR, -rpath DIR"), ONE_DASH
,
277 &General_options::add_to_rpath
),
278 GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
279 NULL
, TWO_DASHES
, &General_options::set_create_eh_frame_hdr
),
280 GENERAL_ARG('\0', "rpath-link",
281 N_("Add DIR to link time shared library search path"),
282 N_("--rpath-link DIR"), TWO_DASHES
,
283 &General_options::add_to_rpath_link
),
284 GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
285 NULL
, ONE_DASH
, &General_options::set_shared
),
286 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
287 NULL
, ONE_DASH
, &General_options::set_static
),
288 POSDEP_NOARG('\0', "as-needed",
289 N_("Only set DT_NEEDED for dynamic libs if used"),
290 NULL
, TWO_DASHES
, &Position_dependent_options::set_as_needed
),
291 POSDEP_NOARG('\0', "no-as-needed",
292 N_("Always DT_NEEDED for dynamic libs (default)"),
293 NULL
, TWO_DASHES
, &Position_dependent_options::clear_as_needed
),
294 POSDEP_NOARG('\0', "whole-archive",
295 N_("Include all archive contents"),
297 &Position_dependent_options::set_whole_archive
),
298 POSDEP_NOARG('\0', "no-whole-archive",
299 N_("Include only needed archive contents"),
301 &Position_dependent_options::clear_whole_archive
),
302 SPECIAL('\0', "help", N_("Report usage information"), NULL
,
306 const int options::Command_line_options::options_size
=
307 sizeof (options
) / sizeof (options
[0]);
309 // The default values for the general options.
311 General_options::General_options()
312 : export_dynamic_(false),
313 dynamic_linker_(NULL
),
315 optimization_level_(0),
316 output_file_name_("a.out"),
317 is_relocatable_(false),
318 create_eh_frame_hdr_(false),
326 // The default values for the position dependent options.
328 Position_dependent_options::Position_dependent_options()
329 : do_static_search_(false),
331 include_whole_archive_(false)
335 // Input_arguments methods.
337 // Add a file to the list.
340 Input_arguments::add_file(const Input_file_argument
& file
)
342 if (!this->in_group_
)
343 this->input_argument_list_
.push_back(Input_argument(file
));
346 gold_assert(!this->input_argument_list_
.empty());
347 gold_assert(this->input_argument_list_
.back().is_group());
348 this->input_argument_list_
.back().group()->add_file(file
);
355 Input_arguments::start_group()
357 gold_assert(!this->in_group_
);
358 Input_file_group
* group
= new Input_file_group();
359 this->input_argument_list_
.push_back(Input_argument(group
));
360 this->in_group_
= true;
366 Input_arguments::end_group()
368 gold_assert(this->in_group_
);
369 this->in_group_
= false;
372 // Command_line options.
374 Command_line::Command_line()
375 : options_(), position_options_(), inputs_()
379 // Process the command line options.
382 Command_line::process(int argc
, char** argv
)
384 const int options_size
= options::Command_line_options::options_size
;
385 const options::One_option
* options
=
386 options::Command_line_options::options
;
387 bool no_more_options
= false;
391 if (argv
[i
][0] != '-' || no_more_options
)
393 this->add_file(argv
[i
], false);
398 // Option starting with '-'.
400 if (argv
[i
][1] == '-')
403 if (argv
[i
][2] == '\0')
405 no_more_options
= true;
410 // Look for a long option match.
411 char* opt
= argv
[i
] + dashes
;
414 char* arg
= strchr(opt
, '=');
417 else if (i
+ 1 < argc
)
424 for (j
= 0; j
< options_size
; ++j
)
426 if (options
[j
].long_option
!= NULL
429 != options::One_option::EXACTLY_TWO_DASHES
))
430 && first
== options
[j
].long_option
[0]
431 && strcmp(opt
, options
[j
].long_option
) == 0)
433 if (options
[j
].special
)
434 i
+= options
[j
].special(argc
- 1, argv
+ i
, opt
, this);
437 if (!options
[j
].takes_argument())
445 this->usage(_("missing argument"), argv
[i
]);
447 this->apply_option(options
[j
], arg
);
453 if (j
< options_size
)
456 // If we saw two dashes, we need to see a long option.
458 this->usage(_("unknown option"), argv
[i
]);
460 // Look for a short option match. There may be more than one
461 // short option in a given argument.
463 char* s
= argv
[i
] + 1;
465 while (*s
!= '\0' && !done
)
469 for (j
= 0; j
< options_size
; ++j
)
471 if (options
[j
].short_option
== opt
)
473 if (options
[j
].special
)
475 // Undo the argument skip done above.
477 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, this);
483 if (options
[j
].takes_argument())
496 this->usage(_("missing argument"), opt
);
498 this->apply_option(options
[j
], arg
);
504 if (j
>= options_size
)
505 this->usage(_("unknown option"), *s
);
511 if (this->inputs_
.in_group())
513 fprintf(stderr
, _("%s: missing group end"), program_name
);
517 // FIXME: We should only do this when configured in native mode.
518 this->options_
.add_to_search_path("/lib");
519 this->options_
.add_to_search_path("/usr/lib");
522 // Apply a command line option.
525 Command_line::apply_option(const options::One_option
& opt
,
530 if (opt
.general_noarg
)
531 (this->options_
.*(opt
.general_noarg
))();
532 else if (opt
.dependent_noarg
)
533 (this->position_options_
.*(opt
.dependent_noarg
))();
540 (this->options_
.*(opt
.general_arg
))(arg
);
541 else if (opt
.dependent_arg
)
542 (this->position_options_
.*(opt
.dependent_arg
))(arg
);
548 // Add an input file or library.
551 Command_line::add_file(const char* name
, bool is_lib
)
553 Input_file_argument
file(name
, is_lib
, this->position_options_
);
554 this->inputs_
.add_file(file
);
557 // Handle the -l option, which requires special treatment.
560 Command_line::process_l_option(int argc
, char** argv
, char* arg
)
572 libname
= argv
[argc
+ 1];
575 this->usage(_("missing argument"), arg
);
577 this->add_file(libname
, true);
582 // Handle the --start-group option.
585 Command_line::start_group(const char* arg
)
587 if (this->inputs_
.in_group())
588 this->usage(_("may not nest groups"), arg
);
589 this->inputs_
.start_group();
592 // Handle the --end-group option.
595 Command_line::end_group(const char* arg
)
597 if (!this->inputs_
.in_group())
598 this->usage(_("group end without group start"), arg
);
599 this->inputs_
.end_group();
602 // Report a usage error. */
605 Command_line::usage()
608 _("%s: use the --help option for usage information\n"),
614 Command_line::usage(const char* msg
, const char *opt
)
618 program_name
, opt
, msg
);
623 Command_line::usage(const char* msg
, char opt
)
627 program_name
, opt
, msg
);
631 } // End namespace gold.
This page took 0.083061 seconds and 5 git commands to generate.