]>
Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.c -- handle command line options for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <[email protected]>. | |
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 | ||
ad2d6943 ILT |
23 | #include "gold.h" |
24 | ||
bae7f79e | 25 | #include <iostream> |
ad2d6943 ILT |
26 | #include <sys/stat.h> |
27 | #include "filenames.h" | |
28 | #include "libiberty.h" | |
bae7f79e | 29 | |
bae7f79e ILT |
30 | #include "options.h" |
31 | ||
61ba1cf9 ILT |
32 | namespace gold |
33 | { | |
34 | ||
bae7f79e ILT |
35 | // The information we keep for a single command line option. |
36 | ||
61ba1cf9 | 37 | struct options::One_option |
bae7f79e ILT |
38 | { |
39 | // The single character option name, or '\0' if this is only a long | |
40 | // option. | |
41 | char short_option; | |
42 | ||
43 | // The long option name, or NULL if this is only a short option. | |
44 | const char* long_option; | |
45 | ||
46 | // Description of the option for --help output, or NULL if there is none. | |
47 | const char* doc; | |
48 | ||
49 | // How to print the option name in --help output, or NULL to use the | |
50 | // default. | |
51 | const char* help_output; | |
52 | ||
53 | // Long option dash control. This is ignored if long_option is | |
54 | // NULL. | |
55 | enum | |
56 | { | |
57 | // Long option normally takes one dash; two dashes are also | |
58 | // accepted. | |
59 | ONE_DASH, | |
60 | // Long option normally takes two dashes; one dash is also | |
61 | // accepted. | |
62 | TWO_DASHES, | |
63 | // Long option always takes two dashes. | |
64 | EXACTLY_TWO_DASHES | |
65 | } dash; | |
66 | ||
67 | // Function for special handling, or NULL. Returns the number of | |
68 | // arguments to skip. This will normally be at least 1, but it may | |
69 | // be 0 if this function changes *argv. ARG points to the location | |
70 | // in *ARGV where the option starts, which may be helpful for a | |
71 | // short option. | |
61ba1cf9 | 72 | int (*special)(int argc, char** argv, char *arg, Command_line*); |
bae7f79e ILT |
73 | |
74 | // If this is a position independent option which does not take an | |
75 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 76 | void (General_options::*general_noarg)(); |
bae7f79e ILT |
77 | |
78 | // If this is a position independent function which takes an | |
79 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 80 | void (General_options::*general_arg)(const char*); |
bae7f79e ILT |
81 | |
82 | // If this is a position dependent option which does not take an | |
83 | // argument, this is the member function to call to record it. | |
61ba1cf9 | 84 | void (Position_dependent_options::*dependent_noarg)(); |
bae7f79e ILT |
85 | |
86 | // If this is a position dependent option which takes an argument, | |
87 | // this is the member function to record it. | |
61ba1cf9 | 88 | void (Position_dependent_options::*dependent_arg)(const char*); |
bae7f79e ILT |
89 | |
90 | // Return whether this option takes an argument. | |
91 | bool | |
92 | takes_argument() const | |
93 | { return this->general_arg != NULL || this->dependent_arg != NULL; } | |
94 | }; | |
95 | ||
61ba1cf9 | 96 | class options::Command_line_options |
bae7f79e ILT |
97 | { |
98 | public: | |
99 | static const One_option options[]; | |
100 | static const int options_size; | |
101 | }; | |
102 | ||
61ba1cf9 ILT |
103 | } // End namespace gold. |
104 | ||
bae7f79e ILT |
105 | namespace |
106 | { | |
107 | ||
61ba1cf9 ILT |
108 | // Handle the special -l option, which adds an input file. |
109 | ||
110 | int | |
111 | library(int argc, char** argv, char* arg, gold::Command_line* cmdline) | |
112 | { | |
113 | return cmdline->process_l_option(argc, argv, arg); | |
114 | } | |
115 | ||
ead1e424 ILT |
116 | // Handle the special --start-group option. |
117 | ||
118 | int | |
119 | start_group(int, char**, char* arg, gold::Command_line* cmdline) | |
120 | { | |
121 | cmdline->start_group(arg); | |
122 | return 1; | |
123 | } | |
124 | ||
125 | // Handle the special --end-group option. | |
126 | ||
127 | int | |
128 | end_group(int, char**, char* arg, gold::Command_line* cmdline) | |
129 | { | |
130 | cmdline->end_group(arg); | |
131 | return 1; | |
132 | } | |
133 | ||
bae7f79e ILT |
134 | // Report usage information for ld --help, and exit. |
135 | ||
136 | int | |
137 | help(int, char**, char*, gold::Command_line*) | |
138 | { | |
139 | printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name); | |
140 | ||
141 | const int options_size = gold::options::Command_line_options::options_size; | |
142 | const gold::options::One_option* options = | |
143 | gold::options::Command_line_options::options; | |
144 | for (int i = 0; i < options_size; ++i) | |
145 | { | |
146 | if (options[i].doc == NULL) | |
147 | continue; | |
148 | ||
149 | printf(" "); | |
150 | int len = 2; | |
151 | bool comma = false; | |
152 | ||
153 | int j = i; | |
154 | do | |
155 | { | |
156 | if (options[j].help_output != NULL) | |
157 | { | |
158 | if (comma) | |
159 | { | |
160 | printf(", "); | |
161 | len += 2; | |
162 | } | |
163 | printf(options[j].help_output); | |
164 | len += std::strlen(options[i].help_output); | |
a6badf5a | 165 | comma = true; |
bae7f79e ILT |
166 | } |
167 | else | |
168 | { | |
169 | if (options[j].short_option != '\0') | |
170 | { | |
171 | if (comma) | |
172 | { | |
173 | printf(", "); | |
174 | len += 2; | |
175 | } | |
176 | printf("-%c", options[j].short_option); | |
177 | len += 2; | |
a6badf5a | 178 | comma = true; |
bae7f79e ILT |
179 | } |
180 | ||
181 | if (options[j].long_option != NULL) | |
182 | { | |
183 | if (comma) | |
184 | { | |
185 | printf(", "); | |
186 | len += 2; | |
187 | } | |
188 | if (options[j].dash == gold::options::One_option::ONE_DASH) | |
189 | { | |
190 | printf("-"); | |
191 | ++len; | |
192 | } | |
193 | else | |
194 | { | |
195 | printf("--"); | |
196 | len += 2; | |
197 | } | |
198 | printf("%s", options[j].long_option); | |
199 | len += std::strlen(options[j].long_option); | |
a6badf5a | 200 | comma = true; |
bae7f79e ILT |
201 | } |
202 | } | |
203 | ++j; | |
204 | } | |
205 | while (j < options_size && options[j].doc == NULL); | |
206 | ||
a6badf5a | 207 | if (len >= 30) |
bae7f79e ILT |
208 | { |
209 | printf("\n"); | |
210 | len = 0; | |
211 | } | |
212 | for (; len < 30; ++len) | |
213 | std::putchar(' '); | |
214 | ||
215 | std::puts(options[i].doc); | |
216 | } | |
217 | ||
218 | gold::gold_exit(true); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
8486ee48 ILT |
223 | // Report version information. |
224 | ||
225 | int | |
226 | version(int, char**, char* opt, gold::Command_line*) | |
227 | { | |
228 | gold::print_version(opt[0] == 'v' && opt[1] == '\0'); | |
229 | gold::gold_exit(true); | |
230 | return 0; | |
231 | } | |
232 | ||
ad2d6943 ILT |
233 | // If the default sysroot is relocatable, try relocating it based on |
234 | // the prefix FROM. | |
235 | ||
236 | char* | |
237 | get_relative_sysroot(const char* from) | |
238 | { | |
239 | char* path = make_relative_prefix(gold::program_name, from, | |
240 | TARGET_SYSTEM_ROOT); | |
241 | if (path != NULL) | |
242 | { | |
243 | struct stat s; | |
244 | if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode)) | |
245 | return path; | |
246 | free(path); | |
247 | } | |
248 | ||
249 | return NULL; | |
250 | } | |
251 | ||
252 | // Return the default sysroot. This is set by the --with-sysroot | |
253 | // option to configure. | |
254 | ||
255 | std::string | |
256 | get_default_sysroot() | |
257 | { | |
258 | const char* sysroot = TARGET_SYSTEM_ROOT; | |
259 | if (*sysroot == '\0') | |
260 | return ""; | |
261 | ||
262 | if (TARGET_SYSTEM_ROOT_RELOCATABLE) | |
263 | { | |
264 | char* path = get_relative_sysroot (BINDIR); | |
265 | if (path == NULL) | |
266 | path = get_relative_sysroot (TOOLBINDIR); | |
267 | if (path != NULL) | |
268 | { | |
269 | std::string ret = path; | |
270 | free(path); | |
271 | return ret; | |
272 | } | |
273 | } | |
274 | ||
275 | return sysroot; | |
276 | } | |
277 | ||
61ba1cf9 ILT |
278 | } // End anonymous namespace. |
279 | ||
280 | namespace gold | |
281 | { | |
bae7f79e ILT |
282 | |
283 | // Helper macros used to specify the options. We could also do this | |
284 | // using constructors, but then g++ would generate code to initialize | |
285 | // the array. We want the array to be initialized statically so that | |
286 | // we get better startup time. | |
287 | ||
288 | #define GENERAL_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 289 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
290 | NULL, func, NULL, NULL, NULL } |
291 | #define GENERAL_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 292 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
293 | NULL, NULL, func, NULL, NULL } |
294 | #define POSDEP_NOARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 295 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
296 | NULL, NULL, NULL, func, NULL } |
297 | #define POSDEP_ARG(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 298 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
299 | NULL, NULL, NULL, NULL, func } |
300 | #define SPECIAL(short_option, long_option, doc, help, dash, func) \ | |
61ba1cf9 | 301 | { short_option, long_option, doc, help, options::One_option::dash, \ |
bae7f79e ILT |
302 | func, NULL, NULL, NULL, NULL } |
303 | ||
304 | // Here is the actual list of options which we accept. | |
305 | ||
61ba1cf9 ILT |
306 | const options::One_option |
307 | options::Command_line_options::options[] = | |
bae7f79e | 308 | { |
61ba1cf9 | 309 | SPECIAL('l', "library", N_("Search for library LIBNAME"), |
a6badf5a | 310 | N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES, |
61ba1cf9 | 311 | &library), |
ead1e424 ILT |
312 | SPECIAL('(', "start-group", N_("Start a library search group"), NULL, |
313 | TWO_DASHES, &start_group), | |
314 | SPECIAL(')', "end-group", N_("End a library search group"), NULL, | |
315 | TWO_DASHES, &end_group), | |
a6badf5a ILT |
316 | GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"), |
317 | NULL, TWO_DASHES, &General_options::set_export_dynamic), | |
dbe717ef ILT |
318 | GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"), |
319 | N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES, | |
320 | &General_options::set_dynamic_linker), | |
bae7f79e ILT |
321 | GENERAL_ARG('L', "library-path", N_("Add directory to search path"), |
322 | N_("-L DIR, --library-path DIR"), TWO_DASHES, | |
61ba1cf9 | 323 | &General_options::add_to_search_path), |
652ec9bd ILT |
324 | GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH, |
325 | &General_options::ignore), | |
ca3a67a5 ILT |
326 | GENERAL_ARG('O', NULL, N_("Optimize output file size"), |
327 | N_("-O level"), ONE_DASH, | |
328 | &General_options::set_optimization_level), | |
61ba1cf9 ILT |
329 | GENERAL_ARG('o', "output", N_("Set output file name"), |
330 | N_("-o FILE, --output FILE"), TWO_DASHES, | |
331 | &General_options::set_output_file_name), | |
bae7f79e | 332 | GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL, |
61ba1cf9 | 333 | ONE_DASH, &General_options::set_relocatable), |
15b3cfae | 334 | GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"), |
41f542e7 ILT |
335 | N_("-R DIR, -rpath DIR"), ONE_DASH, |
336 | &General_options::add_to_rpath), | |
7da52175 | 337 | GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"), |
192f9b85 | 338 | NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr), |
15b3cfae ILT |
339 | GENERAL_ARG('\0', "rpath-link", |
340 | N_("Add DIR to link time shared library search path"), | |
341 | N_("--rpath-link DIR"), TWO_DASHES, | |
342 | &General_options::add_to_rpath_link), | |
92e059d8 ILT |
343 | GENERAL_NOARG('\0', "shared", N_("Generate shared library"), |
344 | NULL, ONE_DASH, &General_options::set_shared), | |
bae7f79e | 345 | GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"), |
61ba1cf9 | 346 | NULL, ONE_DASH, &General_options::set_static), |
ad2d6943 ILT |
347 | GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"), |
348 | N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot), | |
dbe717ef | 349 | POSDEP_NOARG('\0', "as-needed", |
a6badf5a | 350 | N_("Only set DT_NEEDED for dynamic libs if used"), |
dbe717ef ILT |
351 | NULL, TWO_DASHES, &Position_dependent_options::set_as_needed), |
352 | POSDEP_NOARG('\0', "no-as-needed", | |
a6badf5a | 353 | N_("Always DT_NEEDED for dynamic libs (default)"), |
dbe717ef | 354 | NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed), |
4973341a ILT |
355 | POSDEP_NOARG('\0', "whole-archive", |
356 | N_("Include all archive contents"), | |
357 | NULL, TWO_DASHES, | |
358 | &Position_dependent_options::set_whole_archive), | |
359 | POSDEP_NOARG('\0', "no-whole-archive", | |
360 | N_("Include only needed archive contents"), | |
361 | NULL, TWO_DASHES, | |
362 | &Position_dependent_options::clear_whole_archive), | |
bae7f79e | 363 | SPECIAL('\0', "help", N_("Report usage information"), NULL, |
8486ee48 ILT |
364 | TWO_DASHES, &help), |
365 | SPECIAL('v', "version", N_("Report version information"), NULL, | |
366 | TWO_DASHES, &version) | |
bae7f79e ILT |
367 | }; |
368 | ||
61ba1cf9 | 369 | const int options::Command_line_options::options_size = |
bae7f79e ILT |
370 | sizeof (options) / sizeof (options[0]); |
371 | ||
372 | // The default values for the general options. | |
373 | ||
61ba1cf9 | 374 | General_options::General_options() |
a6badf5a ILT |
375 | : export_dynamic_(false), |
376 | dynamic_linker_(NULL), | |
dbe717ef | 377 | search_path_(), |
ca3a67a5 | 378 | optimization_level_(0), |
61ba1cf9 ILT |
379 | output_file_name_("a.out"), |
380 | is_relocatable_(false), | |
7da52175 | 381 | create_eh_frame_hdr_(false), |
4973341a | 382 | rpath_(), |
15b3cfae | 383 | rpath_link_(), |
92e059d8 | 384 | is_shared_(false), |
ad2d6943 ILT |
385 | is_static_(false), |
386 | sysroot_() | |
bae7f79e ILT |
387 | { |
388 | } | |
389 | ||
390 | // The default values for the position dependent options. | |
391 | ||
61ba1cf9 | 392 | Position_dependent_options::Position_dependent_options() |
4973341a ILT |
393 | : do_static_search_(false), |
394 | as_needed_(false), | |
395 | include_whole_archive_(false) | |
bae7f79e ILT |
396 | { |
397 | } | |
398 | ||
ad2d6943 ILT |
399 | // Add the sysroot, if any, to the search paths. |
400 | ||
401 | void | |
402 | General_options::add_sysroot() | |
403 | { | |
404 | if (this->sysroot_.empty()) | |
405 | { | |
406 | this->sysroot_ = get_default_sysroot(); | |
407 | if (this->sysroot_.empty()) | |
408 | return; | |
409 | } | |
410 | ||
411 | const char* sysroot = this->sysroot_.c_str(); | |
412 | char* canonical_sysroot = lrealpath(sysroot); | |
413 | ||
414 | for (Dir_list::iterator p = this->search_path_.begin(); | |
415 | p != this->search_path_.end(); | |
416 | ++p) | |
417 | p->add_sysroot(sysroot, canonical_sysroot); | |
418 | ||
419 | free(canonical_sysroot); | |
420 | } | |
421 | ||
422 | // Search_directory methods. | |
423 | ||
424 | // This is called if we have a sysroot. Apply the sysroot if | |
425 | // appropriate. Record whether the directory is in the sysroot. | |
426 | ||
427 | void | |
428 | Search_directory::add_sysroot(const char* sysroot, | |
429 | const char* canonical_sysroot) | |
430 | { | |
431 | gold_assert(*sysroot != '\0'); | |
432 | if (this->put_in_sysroot_) | |
433 | { | |
434 | if (!IS_DIR_SEPARATOR(this->name_[0]) | |
435 | && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1])) | |
436 | this->name_ = '/' + this->name_; | |
437 | this->name_ = sysroot + this->name_; | |
438 | this->is_in_sysroot_ = true; | |
439 | } | |
440 | else | |
441 | { | |
442 | // Check whether this entry is in the sysroot. To do this | |
443 | // correctly, we need to use canonical names. Otherwise we will | |
444 | // get confused by the ../../.. paths that gcc tends to use. | |
445 | char* canonical_name = lrealpath(this->name_.c_str()); | |
446 | int canonical_name_len = strlen(canonical_name); | |
447 | int canonical_sysroot_len = strlen(canonical_sysroot); | |
448 | if (canonical_name_len > canonical_sysroot_len | |
449 | && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len])) | |
450 | { | |
451 | canonical_name[canonical_sysroot_len] = '\0'; | |
452 | if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0) | |
453 | this->is_in_sysroot_ = true; | |
454 | } | |
455 | free(canonical_name); | |
456 | } | |
457 | } | |
458 | ||
dbe717ef ILT |
459 | // Input_arguments methods. |
460 | ||
461 | // Add a file to the list. | |
462 | ||
463 | void | |
464 | Input_arguments::add_file(const Input_file_argument& file) | |
465 | { | |
466 | if (!this->in_group_) | |
467 | this->input_argument_list_.push_back(Input_argument(file)); | |
468 | else | |
469 | { | |
a3ad94ed ILT |
470 | gold_assert(!this->input_argument_list_.empty()); |
471 | gold_assert(this->input_argument_list_.back().is_group()); | |
dbe717ef ILT |
472 | this->input_argument_list_.back().group()->add_file(file); |
473 | } | |
474 | } | |
475 | ||
476 | // Start a group. | |
477 | ||
478 | void | |
479 | Input_arguments::start_group() | |
480 | { | |
a3ad94ed | 481 | gold_assert(!this->in_group_); |
dbe717ef ILT |
482 | Input_file_group* group = new Input_file_group(); |
483 | this->input_argument_list_.push_back(Input_argument(group)); | |
484 | this->in_group_ = true; | |
485 | } | |
486 | ||
487 | // End a group. | |
488 | ||
489 | void | |
490 | Input_arguments::end_group() | |
491 | { | |
a3ad94ed | 492 | gold_assert(this->in_group_); |
dbe717ef ILT |
493 | this->in_group_ = false; |
494 | } | |
495 | ||
ead1e424 | 496 | // Command_line options. |
bae7f79e | 497 | |
61ba1cf9 | 498 | Command_line::Command_line() |
dbe717ef | 499 | : options_(), position_options_(), inputs_() |
bae7f79e ILT |
500 | { |
501 | } | |
502 | ||
503 | // Process the command line options. | |
504 | ||
505 | void | |
61ba1cf9 | 506 | Command_line::process(int argc, char** argv) |
bae7f79e | 507 | { |
61ba1cf9 ILT |
508 | const int options_size = options::Command_line_options::options_size; |
509 | const options::One_option* options = | |
510 | options::Command_line_options::options; | |
bae7f79e ILT |
511 | bool no_more_options = false; |
512 | int i = 0; | |
513 | while (i < argc) | |
514 | { | |
515 | if (argv[i][0] != '-' || no_more_options) | |
516 | { | |
ead1e424 | 517 | this->add_file(argv[i], false); |
bae7f79e ILT |
518 | ++i; |
519 | continue; | |
520 | } | |
521 | ||
522 | // Option starting with '-'. | |
523 | int dashes = 1; | |
524 | if (argv[i][1] == '-') | |
525 | { | |
526 | dashes = 2; | |
527 | if (argv[i][2] == '\0') | |
528 | { | |
529 | no_more_options = true; | |
530 | continue; | |
531 | } | |
532 | } | |
533 | ||
534 | // Look for a long option match. | |
535 | char* opt = argv[i] + dashes; | |
536 | char first = opt[0]; | |
537 | int skiparg = 0; | |
538 | char* arg = strchr(opt, '='); | |
ad2d6943 | 539 | bool argument_with_equals = arg != NULL; |
bae7f79e | 540 | if (arg != NULL) |
ad2d6943 ILT |
541 | { |
542 | *arg = '\0'; | |
543 | ++arg; | |
544 | } | |
bae7f79e ILT |
545 | else if (i + 1 < argc) |
546 | { | |
547 | arg = argv[i + 1]; | |
548 | skiparg = 1; | |
549 | } | |
550 | ||
551 | int j; | |
552 | for (j = 0; j < options_size; ++j) | |
553 | { | |
554 | if (options[j].long_option != NULL | |
555 | && (dashes == 2 | |
556 | || (options[j].dash | |
61ba1cf9 | 557 | != options::One_option::EXACTLY_TWO_DASHES)) |
bae7f79e ILT |
558 | && first == options[j].long_option[0] |
559 | && strcmp(opt, options[j].long_option) == 0) | |
560 | { | |
561 | if (options[j].special) | |
562 | i += options[j].special(argc - 1, argv + i, opt, this); | |
563 | else | |
564 | { | |
565 | if (!options[j].takes_argument()) | |
566 | { | |
ad2d6943 ILT |
567 | if (argument_with_equals) |
568 | this->usage(_("unexpected argument"), argv[i]); | |
bae7f79e ILT |
569 | arg = NULL; |
570 | skiparg = 0; | |
571 | } | |
572 | else | |
573 | { | |
574 | if (arg == NULL) | |
575 | this->usage(_("missing argument"), argv[i]); | |
576 | } | |
577 | this->apply_option(options[j], arg); | |
578 | i += skiparg + 1; | |
579 | } | |
580 | break; | |
581 | } | |
582 | } | |
583 | if (j < options_size) | |
584 | continue; | |
585 | ||
586 | // If we saw two dashes, we need to see a long option. | |
587 | if (dashes == 2) | |
588 | this->usage(_("unknown option"), argv[i]); | |
589 | ||
590 | // Look for a short option match. There may be more than one | |
591 | // short option in a given argument. | |
592 | bool done = false; | |
593 | char* s = argv[i] + 1; | |
594 | ++i; | |
595 | while (*s != '\0' && !done) | |
596 | { | |
597 | char opt = *s; | |
598 | int j; | |
599 | for (j = 0; j < options_size; ++j) | |
600 | { | |
601 | if (options[j].short_option == opt) | |
602 | { | |
603 | if (options[j].special) | |
604 | { | |
605 | // Undo the argument skip done above. | |
606 | --i; | |
607 | i += options[j].special(argc - i, argv + i, s, this); | |
608 | done = true; | |
609 | } | |
610 | else | |
611 | { | |
612 | arg = NULL; | |
613 | if (options[j].takes_argument()) | |
614 | { | |
615 | if (s[1] != '\0') | |
616 | { | |
617 | arg = s + 1; | |
618 | done = true; | |
619 | } | |
620 | else if (i < argc) | |
621 | { | |
622 | arg = argv[i]; | |
623 | ++i; | |
624 | } | |
625 | else | |
626 | this->usage(_("missing argument"), opt); | |
627 | } | |
628 | this->apply_option(options[j], arg); | |
629 | } | |
630 | break; | |
631 | } | |
632 | } | |
633 | ||
634 | if (j >= options_size) | |
635 | this->usage(_("unknown option"), *s); | |
636 | ||
637 | ++s; | |
638 | } | |
639 | } | |
61ba1cf9 | 640 | |
dbe717ef | 641 | if (this->inputs_.in_group()) |
ead1e424 ILT |
642 | { |
643 | fprintf(stderr, _("%s: missing group end"), program_name); | |
644 | this->usage(); | |
645 | } | |
646 | ||
61ba1cf9 | 647 | // FIXME: We should only do this when configured in native mode. |
ad2d6943 ILT |
648 | this->options_.add_to_search_path_with_sysroot("/lib"); |
649 | this->options_.add_to_search_path_with_sysroot("/usr/lib"); | |
650 | ||
651 | this->options_.add_sysroot(); | |
bae7f79e ILT |
652 | } |
653 | ||
654 | // Apply a command line option. | |
655 | ||
656 | void | |
61ba1cf9 ILT |
657 | Command_line::apply_option(const options::One_option& opt, |
658 | const char* arg) | |
bae7f79e ILT |
659 | { |
660 | if (arg == NULL) | |
661 | { | |
662 | if (opt.general_noarg) | |
663 | (this->options_.*(opt.general_noarg))(); | |
664 | else if (opt.dependent_noarg) | |
665 | (this->position_options_.*(opt.dependent_noarg))(); | |
666 | else | |
61ba1cf9 | 667 | gold_unreachable(); |
bae7f79e ILT |
668 | } |
669 | else | |
670 | { | |
671 | if (opt.general_arg) | |
672 | (this->options_.*(opt.general_arg))(arg); | |
673 | else if (opt.dependent_arg) | |
674 | (this->position_options_.*(opt.dependent_arg))(arg); | |
675 | else | |
61ba1cf9 | 676 | gold_unreachable(); |
bae7f79e ILT |
677 | } |
678 | } | |
679 | ||
ead1e424 ILT |
680 | // Add an input file or library. |
681 | ||
682 | void | |
683 | Command_line::add_file(const char* name, bool is_lib) | |
684 | { | |
51dee2fe | 685 | Input_file_argument file(name, is_lib, "", this->position_options_); |
dbe717ef | 686 | this->inputs_.add_file(file); |
ead1e424 ILT |
687 | } |
688 | ||
61ba1cf9 ILT |
689 | // Handle the -l option, which requires special treatment. |
690 | ||
691 | int | |
692 | Command_line::process_l_option(int argc, char** argv, char* arg) | |
693 | { | |
694 | int ret; | |
695 | const char* libname; | |
696 | if (arg[1] != '\0') | |
697 | { | |
698 | ret = 1; | |
699 | libname = arg + 1; | |
700 | } | |
701 | else if (argc > 1) | |
702 | { | |
703 | ret = 2; | |
704 | libname = argv[argc + 1]; | |
705 | } | |
706 | else | |
707 | this->usage(_("missing argument"), arg); | |
708 | ||
ead1e424 | 709 | this->add_file(libname, true); |
61ba1cf9 ILT |
710 | |
711 | return ret; | |
712 | } | |
713 | ||
ead1e424 ILT |
714 | // Handle the --start-group option. |
715 | ||
716 | void | |
717 | Command_line::start_group(const char* arg) | |
718 | { | |
dbe717ef | 719 | if (this->inputs_.in_group()) |
ead1e424 | 720 | this->usage(_("may not nest groups"), arg); |
dbe717ef | 721 | this->inputs_.start_group(); |
ead1e424 ILT |
722 | } |
723 | ||
724 | // Handle the --end-group option. | |
725 | ||
726 | void | |
727 | Command_line::end_group(const char* arg) | |
728 | { | |
dbe717ef | 729 | if (!this->inputs_.in_group()) |
ead1e424 | 730 | this->usage(_("group end without group start"), arg); |
dbe717ef | 731 | this->inputs_.end_group(); |
ead1e424 ILT |
732 | } |
733 | ||
bae7f79e ILT |
734 | // Report a usage error. */ |
735 | ||
736 | void | |
61ba1cf9 | 737 | Command_line::usage() |
bae7f79e ILT |
738 | { |
739 | fprintf(stderr, | |
740 | _("%s: use the --help option for usage information\n"), | |
61ba1cf9 ILT |
741 | program_name); |
742 | gold_exit(false); | |
bae7f79e ILT |
743 | } |
744 | ||
745 | void | |
61ba1cf9 | 746 | Command_line::usage(const char* msg, const char *opt) |
bae7f79e ILT |
747 | { |
748 | fprintf(stderr, | |
749 | _("%s: %s: %s\n"), | |
61ba1cf9 | 750 | program_name, opt, msg); |
bae7f79e ILT |
751 | this->usage(); |
752 | } | |
753 | ||
754 | void | |
61ba1cf9 | 755 | Command_line::usage(const char* msg, char opt) |
bae7f79e ILT |
756 | { |
757 | fprintf(stderr, | |
758 | _("%s: -%c: %s\n"), | |
61ba1cf9 | 759 | program_name, opt, msg); |
bae7f79e ILT |
760 | this->usage(); |
761 | } | |
61ba1cf9 ILT |
762 | |
763 | } // End namespace gold. |