Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.c -- handle command line options for gold |
2 | ||
3 | #include <iostream> | |
4 | ||
5 | #include "gold.h" | |
6 | #include "options.h" | |
7 | ||
61ba1cf9 ILT |
8 | namespace gold |
9 | { | |
10 | ||
bae7f79e ILT |
11 | // The information we keep for a single command line option. |
12 | ||
61ba1cf9 | 13 | struct options::One_option |
bae7f79e ILT |
14 | { |
15 | // The single character option name, or '\0' if this is only a long | |
16 | // option. | |
17 | char short_option; | |
18 | ||
19 | // The long option name, or NULL if this is only a short option. | |
20 | const char* long_option; | |
21 | ||
22 | // Description of the option for --help output, or NULL if there is none. | |
23 | const char* doc; | |
24 | ||
25 | // How to print the option name in --help output, or NULL to use the | |
26 | // default. | |
27 | const char* help_output; | |
28 | ||
29 | // Long option dash control. This is ignored if long_option is | |
30 | // NULL. | |
31 | enum | |
32 | { | |
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; | |
42 | ||
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. | |
61ba1cf9 | 48 | int (*special)(int argc, char** argv, char *arg, Command_line*); |
bae7f79e ILT |
49 | |
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. | |
61ba1cf9 | 52 | void (General_options::*general_noarg)(); |
bae7f79e ILT |
53 | |
54 | // If this is a position independent function which takes an | |
55 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 56 | void (General_options::*general_arg)(const char*); |
bae7f79e ILT |
57 | |
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. | |
61ba1cf9 | 60 | void (Position_dependent_options::*dependent_noarg)(); |
bae7f79e ILT |
61 | |
62 | // If this is a position dependent option which takes an argument, | |
63 | // this is the member function to record it. | |
61ba1cf9 | 64 | void (Position_dependent_options::*dependent_arg)(const char*); |
bae7f79e ILT |
65 | |
66 | // Return whether this option takes an argument. | |
67 | bool | |
68 | takes_argument() const | |
69 | { return this->general_arg != NULL || this->dependent_arg != NULL; } | |
70 | }; | |
71 | ||
61ba1cf9 | 72 | class options::Command_line_options |
bae7f79e ILT |
73 | { |
74 | public: | |
75 | static const One_option options[]; | |
76 | static const int options_size; | |
77 | }; | |
78 | ||
61ba1cf9 ILT |
79 | } // End namespace gold. |
80 | ||
bae7f79e ILT |
81 | namespace |
82 | { | |
83 | ||
61ba1cf9 ILT |
84 | // Handle the special -l option, which adds an input file. |
85 | ||
86 | int | |
87 | library(int argc, char** argv, char* arg, gold::Command_line* cmdline) | |
88 | { | |
89 | return cmdline->process_l_option(argc, argv, arg); | |
90 | } | |
91 | ||
bae7f79e ILT |
92 | // Report usage information for ld --help, and exit. |
93 | ||
94 | int | |
95 | help(int, char**, char*, gold::Command_line*) | |
96 | { | |
97 | printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name); | |
98 | ||
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) | |
103 | { | |
104 | if (options[i].doc == NULL) | |
105 | continue; | |
106 | ||
107 | printf(" "); | |
108 | int len = 2; | |
109 | bool comma = false; | |
110 | ||
111 | int j = i; | |
112 | do | |
113 | { | |
114 | if (options[j].help_output != NULL) | |
115 | { | |
116 | if (comma) | |
117 | { | |
118 | printf(", "); | |
119 | len += 2; | |
120 | } | |
121 | printf(options[j].help_output); | |
122 | len += std::strlen(options[i].help_output); | |
123 | } | |
124 | else | |
125 | { | |
126 | if (options[j].short_option != '\0') | |
127 | { | |
128 | if (comma) | |
129 | { | |
130 | printf(", "); | |
131 | len += 2; | |
132 | } | |
133 | printf("-%c", options[j].short_option); | |
134 | len += 2; | |
135 | } | |
136 | ||
137 | if (options[j].long_option != NULL) | |
138 | { | |
139 | if (comma) | |
140 | { | |
141 | printf(", "); | |
142 | len += 2; | |
143 | } | |
144 | if (options[j].dash == gold::options::One_option::ONE_DASH) | |
145 | { | |
146 | printf("-"); | |
147 | ++len; | |
148 | } | |
149 | else | |
150 | { | |
151 | printf("--"); | |
152 | len += 2; | |
153 | } | |
154 | printf("%s", options[j].long_option); | |
155 | len += std::strlen(options[j].long_option); | |
156 | } | |
157 | } | |
158 | ++j; | |
159 | } | |
160 | while (j < options_size && options[j].doc == NULL); | |
161 | ||
162 | if (len > 30) | |
163 | { | |
164 | printf("\n"); | |
165 | len = 0; | |
166 | } | |
167 | for (; len < 30; ++len) | |
168 | std::putchar(' '); | |
169 | ||
170 | std::puts(options[i].doc); | |
171 | } | |
172 | ||
173 | gold::gold_exit(true); | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
61ba1cf9 ILT |
178 | } // End anonymous namespace. |
179 | ||
180 | namespace gold | |
181 | { | |
bae7f79e ILT |
182 | |
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. | |
187 | ||
188 | #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 189 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
190 | NULL, func, NULL, NULL, NULL } |
191 | #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 192 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
193 | NULL, NULL, func, NULL, NULL } |
194 | #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 195 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
196 | NULL, NULL, NULL, func, NULL } |
197 | #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 198 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
199 | NULL, NULL, NULL, NULL, func } |
200 | #define SPECIAL(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 201 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
202 | func, NULL, NULL, NULL, NULL } |
203 | ||
204 | // Here is the actual list of options which we accept. | |
205 | ||
61ba1cf9 ILT |
206 | const options::One_option |
207 | options::Command_line_options::options[] = | |
bae7f79e | 208 | { |
61ba1cf9 ILT |
209 | SPECIAL('l', "library", N_("Search for library LIBNAME"), |
210 | N_("-lLIBNAME --library LIBNAME"), TWO_DASHES, | |
211 | &library), | |
bae7f79e ILT |
212 | GENERAL_ARG('L', "library-path", N_("Add directory to search path"), |
213 | N_("-L DIR, --library-path DIR"), TWO_DASHES, | |
61ba1cf9 ILT |
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), | |
bae7f79e | 218 | GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL, |
61ba1cf9 | 219 | ONE_DASH, &General_options::set_relocatable), |
bae7f79e | 220 | GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"), |
61ba1cf9 | 221 | NULL, ONE_DASH, &General_options::set_static), |
bae7f79e ILT |
222 | SPECIAL('\0', "help", N_("Report usage information"), NULL, |
223 | TWO_DASHES, &help) | |
224 | }; | |
225 | ||
61ba1cf9 | 226 | const int options::Command_line_options::options_size = |
bae7f79e ILT |
227 | sizeof (options) / sizeof (options[0]); |
228 | ||
229 | // The default values for the general options. | |
230 | ||
61ba1cf9 ILT |
231 | General_options::General_options() |
232 | : search_path_(), | |
233 | output_file_name_("a.out"), | |
234 | is_relocatable_(false), | |
235 | is_static_(false) | |
bae7f79e ILT |
236 | { |
237 | } | |
238 | ||
239 | // The default values for the position dependent options. | |
240 | ||
61ba1cf9 | 241 | Position_dependent_options::Position_dependent_options() |
bae7f79e ILT |
242 | : do_static_search_(false) |
243 | { | |
244 | } | |
245 | ||
246 | // Construct a Command_line. | |
247 | ||
61ba1cf9 | 248 | Command_line::Command_line() |
bae7f79e ILT |
249 | { |
250 | } | |
251 | ||
252 | // Process the command line options. | |
253 | ||
254 | void | |
61ba1cf9 | 255 | Command_line::process(int argc, char** argv) |
bae7f79e | 256 | { |
61ba1cf9 ILT |
257 | const int options_size = options::Command_line_options::options_size; |
258 | const options::One_option* options = | |
259 | options::Command_line_options::options; | |
bae7f79e ILT |
260 | bool no_more_options = false; |
261 | int i = 0; | |
262 | while (i < argc) | |
263 | { | |
264 | if (argv[i][0] != '-' || no_more_options) | |
265 | { | |
61ba1cf9 | 266 | this->inputs_.push_back(Input_argument(argv[i], false, |
bae7f79e ILT |
267 | this->position_options_)); |
268 | ++i; | |
269 | continue; | |
270 | } | |
271 | ||
272 | // Option starting with '-'. | |
273 | int dashes = 1; | |
274 | if (argv[i][1] == '-') | |
275 | { | |
276 | dashes = 2; | |
277 | if (argv[i][2] == '\0') | |
278 | { | |
279 | no_more_options = true; | |
280 | continue; | |
281 | } | |
282 | } | |
283 | ||
284 | // Look for a long option match. | |
285 | char* opt = argv[i] + dashes; | |
286 | char first = opt[0]; | |
287 | int skiparg = 0; | |
288 | char* arg = strchr(opt, '='); | |
289 | if (arg != NULL) | |
290 | *arg = '\0'; | |
291 | else if (i + 1 < argc) | |
292 | { | |
293 | arg = argv[i + 1]; | |
294 | skiparg = 1; | |
295 | } | |
296 | ||
297 | int j; | |
298 | for (j = 0; j < options_size; ++j) | |
299 | { | |
300 | if (options[j].long_option != NULL | |
301 | && (dashes == 2 | |
302 | || (options[j].dash | |
61ba1cf9 | 303 | != options::One_option::EXACTLY_TWO_DASHES)) |
bae7f79e ILT |
304 | && first == options[j].long_option[0] |
305 | && strcmp(opt, options[j].long_option) == 0) | |
306 | { | |
307 | if (options[j].special) | |
308 | i += options[j].special(argc - 1, argv + i, opt, this); | |
309 | else | |
310 | { | |
311 | if (!options[j].takes_argument()) | |
312 | { | |
313 | arg = NULL; | |
314 | skiparg = 0; | |
315 | } | |
316 | else | |
317 | { | |
318 | if (arg == NULL) | |
319 | this->usage(_("missing argument"), argv[i]); | |
320 | } | |
321 | this->apply_option(options[j], arg); | |
322 | i += skiparg + 1; | |
323 | } | |
324 | break; | |
325 | } | |
326 | } | |
327 | if (j < options_size) | |
328 | continue; | |
329 | ||
330 | // If we saw two dashes, we need to see a long option. | |
331 | if (dashes == 2) | |
332 | this->usage(_("unknown option"), argv[i]); | |
333 | ||
334 | // Look for a short option match. There may be more than one | |
335 | // short option in a given argument. | |
336 | bool done = false; | |
337 | char* s = argv[i] + 1; | |
338 | ++i; | |
339 | while (*s != '\0' && !done) | |
340 | { | |
341 | char opt = *s; | |
342 | int j; | |
343 | for (j = 0; j < options_size; ++j) | |
344 | { | |
345 | if (options[j].short_option == opt) | |
346 | { | |
347 | if (options[j].special) | |
348 | { | |
349 | // Undo the argument skip done above. | |
350 | --i; | |
351 | i += options[j].special(argc - i, argv + i, s, this); | |
352 | done = true; | |
353 | } | |
354 | else | |
355 | { | |
356 | arg = NULL; | |
357 | if (options[j].takes_argument()) | |
358 | { | |
359 | if (s[1] != '\0') | |
360 | { | |
361 | arg = s + 1; | |
362 | done = true; | |
363 | } | |
364 | else if (i < argc) | |
365 | { | |
366 | arg = argv[i]; | |
367 | ++i; | |
368 | } | |
369 | else | |
370 | this->usage(_("missing argument"), opt); | |
371 | } | |
372 | this->apply_option(options[j], arg); | |
373 | } | |
374 | break; | |
375 | } | |
376 | } | |
377 | ||
378 | if (j >= options_size) | |
379 | this->usage(_("unknown option"), *s); | |
380 | ||
381 | ++s; | |
382 | } | |
383 | } | |
61ba1cf9 ILT |
384 | |
385 | // FIXME: We should only do this when configured in native mode. | |
386 | this->options_.add_to_search_path("/lib"); | |
387 | this->options_.add_to_search_path("/usr/lib"); | |
bae7f79e ILT |
388 | } |
389 | ||
390 | // Apply a command line option. | |
391 | ||
392 | void | |
61ba1cf9 ILT |
393 | Command_line::apply_option(const options::One_option& opt, |
394 | const char* arg) | |
bae7f79e ILT |
395 | { |
396 | if (arg == NULL) | |
397 | { | |
398 | if (opt.general_noarg) | |
399 | (this->options_.*(opt.general_noarg))(); | |
400 | else if (opt.dependent_noarg) | |
401 | (this->position_options_.*(opt.dependent_noarg))(); | |
402 | else | |
61ba1cf9 | 403 | gold_unreachable(); |
bae7f79e ILT |
404 | } |
405 | else | |
406 | { | |
407 | if (opt.general_arg) | |
408 | (this->options_.*(opt.general_arg))(arg); | |
409 | else if (opt.dependent_arg) | |
410 | (this->position_options_.*(opt.dependent_arg))(arg); | |
411 | else | |
61ba1cf9 | 412 | gold_unreachable(); |
bae7f79e ILT |
413 | } |
414 | } | |
415 | ||
61ba1cf9 ILT |
416 | // Handle the -l option, which requires special treatment. |
417 | ||
418 | int | |
419 | Command_line::process_l_option(int argc, char** argv, char* arg) | |
420 | { | |
421 | int ret; | |
422 | const char* libname; | |
423 | if (arg[1] != '\0') | |
424 | { | |
425 | ret = 1; | |
426 | libname = arg + 1; | |
427 | } | |
428 | else if (argc > 1) | |
429 | { | |
430 | ret = 2; | |
431 | libname = argv[argc + 1]; | |
432 | } | |
433 | else | |
434 | this->usage(_("missing argument"), arg); | |
435 | ||
436 | this->inputs_.push_back(Input_argument(libname, true, | |
437 | this->position_options_)); | |
438 | ||
439 | return ret; | |
440 | } | |
441 | ||
bae7f79e ILT |
442 | // Report a usage error. */ |
443 | ||
444 | void | |
61ba1cf9 | 445 | Command_line::usage() |
bae7f79e ILT |
446 | { |
447 | fprintf(stderr, | |
448 | _("%s: use the --help option for usage information\n"), | |
61ba1cf9 ILT |
449 | program_name); |
450 | gold_exit(false); | |
bae7f79e ILT |
451 | } |
452 | ||
453 | void | |
61ba1cf9 | 454 | Command_line::usage(const char* msg, const char *opt) |
bae7f79e ILT |
455 | { |
456 | fprintf(stderr, | |
457 | _("%s: %s: %s\n"), | |
61ba1cf9 | 458 | program_name, opt, msg); |
bae7f79e ILT |
459 | this->usage(); |
460 | } | |
461 | ||
462 | void | |
61ba1cf9 | 463 | Command_line::usage(const char* msg, char opt) |
bae7f79e ILT |
464 | { |
465 | fprintf(stderr, | |
466 | _("%s: -%c: %s\n"), | |
61ba1cf9 | 467 | program_name, opt, msg); |
bae7f79e ILT |
468 | this->usage(); |
469 | } | |
61ba1cf9 ILT |
470 | |
471 | } // End namespace gold. |