Full support for --sysroot.
[binutils-gdb.git] / gold / options.h
1 // options.h -- handle command line options for gold -*- C++ -*-
2
3 // Copyright 2006, 2007 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 // Command_line
24 // Holds everything we get from the command line.
25 // General_options (from Command_line::options())
26 // Options which are not position dependent.
27 // Input_argument (from Command_line::inputs())
28 // The list of input files, including -l options.
29 // Position_dependent_options (from Input_argument::options())
30 // Position dependent options which apply to this argument.
31
32 #ifndef GOLD_OPTIONS_H
33 #define GOLD_OPTIONS_H
34
35 #include <cstdlib>
36 #include <list>
37 #include <string>
38 #include <vector>
39
40 namespace gold
41 {
42
43 class Command_line;
44 class Input_file_group;
45
46 namespace options {
47
48 class Command_line_options;
49 struct One_option;
50
51 } // End namespace gold::options.
52
53 // A directory to search. For each directory we record whether it is
54 // in the sysroot. We need to know this so that, if a linker script
55 // is found within the sysroot, we will apply the sysroot to any files
56 // named by that script.
57
58 class Search_directory
59 {
60 public:
61 // We need a default constructor because we put this in a
62 // std::vector.
63 Search_directory()
64 : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
65 { }
66
67 // This is the usual constructor.
68 Search_directory(const char* name, bool put_in_sysroot)
69 : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
70 { gold_assert(!this->name_.empty()); }
71
72 // This is called if we have a sysroot. The sysroot is prefixed to
73 // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
74 // set to true for any enries which are in the sysroot (this will
75 // naturally include any entries for which put_in_sysroot_ is true).
76 // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
77 // passing SYSROOT to lrealpath.
78 void
79 add_sysroot(const char* sysroot, const char* canonical_sysroot);
80
81 // Get the directory name.
82 const std::string&
83 name() const
84 { return this->name_; }
85
86 // Return whether this directory is in the sysroot.
87 bool
88 is_in_sysroot() const
89 { return this->is_in_sysroot_; }
90
91 private:
92 std::string name_;
93 bool put_in_sysroot_;
94 bool is_in_sysroot_;
95 };
96
97 // The position independent options which apply to the whole link.
98 // There are a lot of them.
99
100 class General_options
101 {
102 public:
103 General_options();
104
105 // -E: export dynamic symbols.
106 bool
107 export_dynamic() const
108 { return this->export_dynamic_; }
109
110 // -I: dynamic linker name.
111 const char*
112 dynamic_linker() const
113 { return this->dynamic_linker_; }
114
115 // -L: Library search path.
116 typedef std::vector<Search_directory> Dir_list;
117
118 const Dir_list&
119 search_path() const
120 { return this->search_path_; }
121
122 // -O: optimization level (0: don't try to optimize output size).
123 int
124 optimization_level() const
125 { return this->optimization_level_; }
126
127 // -o: Output file name.
128 const char*
129 output_file_name() const
130 { return this->output_file_name_; }
131
132 // -r: Whether we are doing a relocatable link.
133 bool
134 is_relocatable() const
135 { return this->is_relocatable_; }
136
137 // --eh-frame-hdr: Whether to generate an exception frame header.
138 bool
139 create_eh_frame_hdr() const
140 { return this->create_eh_frame_hdr_; }
141
142 // --rpath: The runtime search path.
143 const Dir_list&
144 rpath() const
145 { return this->rpath_; }
146
147 // --rpath-link: The link time search patch for shared libraries.
148 const Dir_list&
149 rpath_link() const
150 { return this->rpath_link_; }
151
152 // --shared: Whether generating a shared object.
153 bool
154 is_shared() const
155 { return this->is_shared_; }
156
157 // --static: Whether doing a static link.
158 bool
159 is_static() const
160 { return this->is_static_; }
161
162 // --sysroot: The system root of a cross-linker.
163 const std::string&
164 sysroot() const
165 { return this->sysroot_; }
166
167 private:
168 // Don't copy this structure.
169 General_options(const General_options&);
170 General_options& operator=(const General_options&);
171
172 friend class Command_line;
173 friend class options::Command_line_options;
174
175 void
176 set_export_dynamic()
177 { this->export_dynamic_ = true; }
178
179 void
180 set_dynamic_linker(const char* arg)
181 { this->dynamic_linker_ = arg; }
182
183 void
184 add_to_search_path(const char* arg)
185 { this->search_path_.push_back(Search_directory(arg, false)); }
186
187 void
188 add_to_search_path_with_sysroot(const char* arg)
189 { this->search_path_.push_back(Search_directory(arg, true)); }
190
191 void
192 set_optimization_level(const char* arg)
193 { this->optimization_level_ = atoi(arg); }
194
195 void
196 set_output_file_name(const char* arg)
197 { this->output_file_name_ = arg; }
198
199 void
200 set_relocatable()
201 { this->is_relocatable_ = true; }
202
203 void
204 set_create_eh_frame_hdr()
205 { this->create_eh_frame_hdr_ = true; }
206
207 void
208 add_to_rpath(const char* arg)
209 { this->rpath_.push_back(Search_directory(arg, false)); }
210
211 void
212 add_to_rpath_link(const char* arg)
213 { this->rpath_link_.push_back(Search_directory(arg, false)); }
214
215 void
216 set_shared()
217 { this->is_shared_ = true; }
218
219 void
220 set_static()
221 { this->is_static_ = true; }
222
223 void
224 set_sysroot(const char* arg)
225 { this->sysroot_ = arg; }
226
227 void
228 ignore(const char*)
229 { }
230
231 // Apply any sysroot to the directory lists.
232 void
233 add_sysroot();
234
235 bool export_dynamic_;
236 const char* dynamic_linker_;
237 Dir_list search_path_;
238 int optimization_level_;
239 const char* output_file_name_;
240 bool is_relocatable_;
241 bool create_eh_frame_hdr_;
242 Dir_list rpath_;
243 Dir_list rpath_link_;
244 bool is_shared_;
245 bool is_static_;
246 std::string sysroot_;
247 };
248
249 // The current state of the position dependent options.
250
251 class Position_dependent_options
252 {
253 public:
254 Position_dependent_options();
255
256 // -Bstatic: Whether we are searching for a static archive rather
257 // than a shared object.
258 bool
259 do_static_search() const
260 { return this->do_static_search_; }
261
262 // --as-needed: Whether to add a DT_NEEDED argument only if the
263 // dynamic object is used.
264 bool
265 as_needed() const
266 { return this->as_needed_; }
267
268 // --whole-archive: Whether to include the entire contents of an
269 // --archive.
270 bool
271 include_whole_archive() const
272 { return this->include_whole_archive_; }
273
274 void
275 set_static_search()
276 { this->do_static_search_ = true; }
277
278 void
279 set_dynamic_search()
280 { this->do_static_search_ = false; }
281
282 void
283 set_as_needed()
284 { this->as_needed_ = true; }
285
286 void
287 clear_as_needed()
288 { this->as_needed_ = false; }
289
290 void
291 set_whole_archive()
292 { this->include_whole_archive_ = true; }
293
294 void
295 clear_whole_archive()
296 { this->include_whole_archive_ = false; }
297
298 private:
299 bool do_static_search_;
300 bool as_needed_;
301 bool include_whole_archive_;
302 };
303
304 // A single file or library argument from the command line.
305
306 class Input_file_argument
307 {
308 public:
309 // name: file name or library name
310 // is_lib: true if name is a library name: that is, emits the leading
311 // "lib" and trailing ".so"/".a" from the name
312 // extra_search_path: an extra directory to look for the file, prior
313 // to checking the normal library search path. If this is "",
314 // then no extra directory is added.
315 // options: The position dependent options at this point in the
316 // command line, such as --whole-archive.
317 Input_file_argument()
318 : name_(), is_lib_(false), extra_search_path_(""), options_()
319 { }
320
321 Input_file_argument(const char* name, bool is_lib,
322 const char* extra_search_path,
323 const Position_dependent_options& options)
324 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
325 options_(options)
326 { }
327
328 const char*
329 name() const
330 { return this->name_.c_str(); }
331
332 const Position_dependent_options&
333 options() const
334 { return this->options_; }
335
336 bool
337 is_lib() const
338 { return this->is_lib_; }
339
340 const char*
341 extra_search_path() const
342 {
343 return (this->extra_search_path_.empty()
344 ? NULL
345 : this->extra_search_path_.c_str());
346 }
347
348 // Return whether this file may require a search using the -L
349 // options.
350 bool
351 may_need_search() const
352 { return this->is_lib_ || !this->extra_search_path_.empty(); }
353
354 private:
355 // We use std::string, not const char*, here for convenience when
356 // using script files, so that we do not have to preserve the string
357 // in that case.
358 std::string name_;
359 bool is_lib_;
360 std::string extra_search_path_;
361 Position_dependent_options options_;
362 };
363
364 // A file or library, or a group, from the command line.
365
366 class Input_argument
367 {
368 public:
369 // Create a file or library argument.
370 explicit Input_argument(Input_file_argument file)
371 : is_file_(true), file_(file), group_(NULL)
372 { }
373
374 // Create a group argument.
375 explicit Input_argument(Input_file_group* group)
376 : is_file_(false), group_(group)
377 { }
378
379 // Return whether this is a file.
380 bool
381 is_file() const
382 { return this->is_file_; }
383
384 // Return whether this is a group.
385 bool
386 is_group() const
387 { return !this->is_file_; }
388
389 // Return the information about the file.
390 const Input_file_argument&
391 file() const
392 {
393 gold_assert(this->is_file_);
394 return this->file_;
395 }
396
397 // Return the information about the group.
398 const Input_file_group*
399 group() const
400 {
401 gold_assert(!this->is_file_);
402 return this->group_;
403 }
404
405 Input_file_group*
406 group()
407 {
408 gold_assert(!this->is_file_);
409 return this->group_;
410 }
411
412 private:
413 bool is_file_;
414 Input_file_argument file_;
415 Input_file_group* group_;
416 };
417
418 // A group from the command line. This is a set of arguments within
419 // --start-group ... --end-group.
420
421 class Input_file_group
422 {
423 public:
424 typedef std::vector<Input_argument> Files;
425 typedef Files::const_iterator const_iterator;
426
427 Input_file_group()
428 : files_()
429 { }
430
431 // Add a file to the end of the group.
432 void
433 add_file(const Input_file_argument& arg)
434 { this->files_.push_back(Input_argument(arg)); }
435
436 // Iterators to iterate over the group contents.
437
438 const_iterator
439 begin() const
440 { return this->files_.begin(); }
441
442 const_iterator
443 end() const
444 { return this->files_.end(); }
445
446 private:
447 Files files_;
448 };
449
450 // A list of files from the command line or a script.
451
452 class Input_arguments
453 {
454 public:
455 typedef std::vector<Input_argument> Input_argument_list;
456 typedef Input_argument_list::const_iterator const_iterator;
457
458 Input_arguments()
459 : input_argument_list_(), in_group_(false)
460 { }
461
462 // Add a file.
463 void
464 add_file(const Input_file_argument& arg);
465
466 // Start a group (the --start-group option).
467 void
468 start_group();
469
470 // End a group (the --end-group option).
471 void
472 end_group();
473
474 // Return whether we are currently in a group.
475 bool
476 in_group() const
477 { return this->in_group_; }
478
479 // Iterators to iterate over the list of input files.
480
481 const_iterator
482 begin() const
483 { return this->input_argument_list_.begin(); }
484
485 const_iterator
486 end() const
487 { return this->input_argument_list_.end(); }
488
489 // Return whether the list is empty.
490 bool
491 empty() const
492 { return this->input_argument_list_.empty(); }
493
494 private:
495 Input_argument_list input_argument_list_;
496 bool in_group_;
497 };
498
499 // All the information read from the command line.
500
501 class Command_line
502 {
503 public:
504 typedef Input_arguments::const_iterator const_iterator;
505
506 Command_line();
507
508 // Process the command line options. This will exit with an
509 // appropriate error message if an unrecognized option is seen.
510 void
511 process(int argc, char** argv);
512
513 // Handle a -l option.
514 int
515 process_l_option(int, char**, char*);
516
517 // Handle a --start-group option.
518 void
519 start_group(const char* arg);
520
521 // Handle a --end-group option.
522 void
523 end_group(const char* arg);
524
525 // Get the general options.
526 const General_options&
527 options() const
528 { return this->options_; }
529
530 // Iterators to iterate over the list of input files.
531
532 const_iterator
533 begin() const
534 { return this->inputs_.begin(); }
535
536 const_iterator
537 end() const
538 { return this->inputs_.end(); }
539
540 private:
541 Command_line(const Command_line&);
542 Command_line& operator=(const Command_line&);
543
544 // Report usage error.
545 void
546 usage() ATTRIBUTE_NORETURN;
547 void
548 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
549 void
550 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
551
552 // Apply a command line option.
553 void
554 apply_option(const gold::options::One_option&, const char*);
555
556 // Add a file.
557 void
558 add_file(const char* name, bool is_lib);
559
560 General_options options_;
561 Position_dependent_options position_options_;
562 Input_arguments inputs_;
563 };
564
565 } // End namespace gold.
566
567 #endif // !defined(GOLD_OPTIONS_H)