1 // options.c -- handle command line options for gold
8 // The information we keep for a single command line option.
10 struct gold::options::One_option
12 // The single character option name, or '\0' if this is only a long
16 // The long option name, or NULL if this is only a short option.
17 const char* long_option
;
19 // Description of the option for --help output, or NULL if there is none.
22 // How to print the option name in --help output, or NULL to use the
24 const char* help_output
;
26 // Long option dash control. This is ignored if long_option is
30 // Long option normally takes one dash; two dashes are also
33 // Long option normally takes two dashes; one dash is also
36 // Long option always takes two dashes.
40 // Function for special handling, or NULL. Returns the number of
41 // arguments to skip. This will normally be at least 1, but it may
42 // be 0 if this function changes *argv. ARG points to the location
43 // in *ARGV where the option starts, which may be helpful for a
45 int (*special
)(int argc
, char** argv
, char *arg
, gold::Command_line
*);
47 // If this is a position independent option which does not take an
48 // argument, this is the member function to call to record it.
49 void (gold::General_options::*general_noarg
)();
51 // If this is a position independent function which takes an
52 // argument, this is the member function to call to record it.
53 void (gold::General_options::*general_arg
)(const char*);
55 // If this is a position dependent option which does not take an
56 // argument, this is the member function to call to record it.
57 void (gold::Position_dependent_options::*dependent_noarg
)();
59 // If this is a position dependent option which takes an argument,
60 // this is the member function to record it.
61 void (gold::Position_dependent_options::*dependent_arg
)(const char*);
63 // Return whether this option takes an argument.
65 takes_argument() const
66 { return this->general_arg
!= NULL
|| this->dependent_arg
!= NULL
; }
69 class gold::options::Command_line_options
72 static const One_option options
[];
73 static const int options_size
;
79 // Report usage information for ld --help, and exit.
82 help(int, char**, char*, gold::Command_line
*)
84 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
86 const int options_size
= gold::options::Command_line_options::options_size
;
87 const gold::options::One_option
* options
=
88 gold::options::Command_line_options::options
;
89 for (int i
= 0; i
< options_size
; ++i
)
91 if (options
[i
].doc
== NULL
)
101 if (options
[j
].help_output
!= NULL
)
108 printf(options
[j
].help_output
);
109 len
+= std::strlen(options
[i
].help_output
);
113 if (options
[j
].short_option
!= '\0')
120 printf("-%c", options
[j
].short_option
);
124 if (options
[j
].long_option
!= NULL
)
131 if (options
[j
].dash
== gold::options::One_option::ONE_DASH
)
141 printf("%s", options
[j
].long_option
);
142 len
+= std::strlen(options
[j
].long_option
);
147 while (j
< options_size
&& options
[j
].doc
== NULL
);
154 for (; len
< 30; ++len
)
157 std::puts(options
[i
].doc
);
160 gold::gold_exit(true);
165 } // End empty namespace.
167 // Helper macros used to specify the options. We could also do this
168 // using constructors, but then g++ would generate code to initialize
169 // the array. We want the array to be initialized statically so that
170 // we get better startup time.
172 #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \
173 { short_option, long_option, doc, help, gold::options::One_option::dash, \
174 NULL, func, NULL, NULL, NULL }
175 #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \
176 { short_option, long_option, doc, help, gold::options::One_option::dash, \
177 NULL, NULL, func, NULL, NULL }
178 #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \
179 { short_option, long_option, doc, help, gold::options::One_option::dash, \
180 NULL, NULL, NULL, func, NULL }
181 #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \
182 { short_option, long_option, doc, help, gold::options::One_option::dash, \
183 NULL, NULL, NULL, NULL, func }
184 #define SPECIAL(short_option, long_option, doc, help, dash, func) \
185 { short_option, long_option, doc, help, gold::options::One_option::dash, \
186 func, NULL, NULL, NULL, NULL }
188 // Here is the actual list of options which we accept.
190 const gold::options::One_option
191 gold::options::Command_line_options::options
[] =
193 GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
194 N_("-L DIR, --library-path DIR"), TWO_DASHES
,
195 &gold::General_options::add_to_search_path
),
196 GENERAL_NOARG('r', NULL
, N_("Generate relocatable output"), NULL
,
197 ONE_DASH
, &gold::General_options::set_relocatable
),
198 GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
199 NULL
, ONE_DASH
, &gold::General_options::set_static
),
200 SPECIAL('\0', "help", N_("Report usage information"), NULL
,
204 const int gold::options::Command_line_options::options_size
=
205 sizeof (options
) / sizeof (options
[0]);
207 // The default values for the general options.
209 gold::General_options::General_options()
210 : is_relocatable_(false)
214 // The default values for the position dependent options.
216 gold::Position_dependent_options::Position_dependent_options()
217 : do_static_search_(false)
221 // Construct a Command_line.
223 gold::Command_line::Command_line()
227 // Process the command line options.
230 gold::Command_line::process(int argc
, char** argv
)
232 const int options_size
= gold::options::Command_line_options::options_size
;
233 const gold::options::One_option
* options
=
234 gold::options::Command_line_options::options
;
235 bool no_more_options
= false;
239 if (argv
[i
][0] != '-' || no_more_options
)
241 this->inputs_
.push_back(Input_argument(argv
[i
],
242 this->position_options_
));
247 // Option starting with '-'.
249 if (argv
[i
][1] == '-')
252 if (argv
[i
][2] == '\0')
254 no_more_options
= true;
259 // Look for a long option match.
260 char* opt
= argv
[i
] + dashes
;
263 char* arg
= strchr(opt
, '=');
266 else if (i
+ 1 < argc
)
273 for (j
= 0; j
< options_size
; ++j
)
275 if (options
[j
].long_option
!= NULL
278 != gold::options::One_option::EXACTLY_TWO_DASHES
))
279 && first
== options
[j
].long_option
[0]
280 && strcmp(opt
, options
[j
].long_option
) == 0)
282 if (options
[j
].special
)
283 i
+= options
[j
].special(argc
- 1, argv
+ i
, opt
, this);
286 if (!options
[j
].takes_argument())
294 this->usage(_("missing argument"), argv
[i
]);
296 this->apply_option(options
[j
], arg
);
302 if (j
< options_size
)
305 // If we saw two dashes, we need to see a long option.
307 this->usage(_("unknown option"), argv
[i
]);
309 // Look for a short option match. There may be more than one
310 // short option in a given argument.
312 char* s
= argv
[i
] + 1;
314 while (*s
!= '\0' && !done
)
318 for (j
= 0; j
< options_size
; ++j
)
320 if (options
[j
].short_option
== opt
)
322 if (options
[j
].special
)
324 // Undo the argument skip done above.
326 i
+= options
[j
].special(argc
- i
, argv
+ i
, s
, this);
332 if (options
[j
].takes_argument())
345 this->usage(_("missing argument"), opt
);
347 this->apply_option(options
[j
], arg
);
353 if (j
>= options_size
)
354 this->usage(_("unknown option"), *s
);
361 // Apply a command line option.
364 gold::Command_line::apply_option(const gold::options::One_option
& opt
,
369 if (opt
.general_noarg
)
370 (this->options_
.*(opt
.general_noarg
))();
371 else if (opt
.dependent_noarg
)
372 (this->position_options_
.*(opt
.dependent_noarg
))();
374 gold::gold_unreachable();
379 (this->options_
.*(opt
.general_arg
))(arg
);
380 else if (opt
.dependent_arg
)
381 (this->position_options_
.*(opt
.dependent_arg
))(arg
);
383 gold::gold_unreachable();
387 // Report a usage error. */
390 gold::Command_line::usage()
393 _("%s: use the --help option for usage information\n"),
395 gold::gold_exit(false);
399 gold::Command_line::usage(const char* msg
, const char *opt
)
403 gold::program_name
, opt
, msg
);
408 gold::Command_line::usage(const char* msg
, char opt
)
412 gold::program_name
, opt
, msg
);
This page took 0.038314 seconds and 4 git commands to generate.