* tls.m4 (GCC_CHECK_TLS): Rename have_tls to gcc_cv_have_tls.
[binutils.git] / gold / options.h
blob9848639dff52fd7d88d3ae99f9b49c9a453f4057
1 // options.h -- handle command line options for gold -*- C++ -*-
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.
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.
32 #ifndef GOLD_OPTIONS_H
33 #define GOLD_OPTIONS_H
35 #include <cstdlib>
36 #include <list>
37 #include <string>
38 #include <vector>
40 namespace gold
43 class Command_line;
44 class Input_file_group;
46 namespace options {
48 class Command_line_options;
49 struct One_option;
51 } // End namespace gold::options.
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.
58 class Search_directory
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 { }
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()); }
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);
81 // Get the directory name.
82 const std::string&
83 name() const
84 { return this->name_; }
86 // Return whether this directory is in the sysroot.
87 bool
88 is_in_sysroot() const
89 { return this->is_in_sysroot_; }
91 private:
92 std::string name_;
93 bool put_in_sysroot_;
94 bool is_in_sysroot_;
97 // The position independent options which apply to the whole link.
98 // There are a lot of them.
100 class General_options
102 public:
103 General_options();
105 // -E: export dynamic symbols.
106 bool
107 export_dynamic() const
108 { return this->export_dynamic_; }
110 // -I: dynamic linker name.
111 const char*
112 dynamic_linker() const
113 { return this->dynamic_linker_; }
115 // -L: Library search path.
116 typedef std::vector<Search_directory> Dir_list;
118 const Dir_list&
119 search_path() const
120 { return this->search_path_; }
122 // -O: optimization level (0: don't try to optimize output size).
124 optimization_level() const
125 { return this->optimization_level_; }
127 // -o: Output file name.
128 const char*
129 output_file_name() const
130 { return this->output_file_name_; }
132 // -r: Whether we are doing a relocatable link.
133 bool
134 is_relocatable() const
135 { return this->is_relocatable_; }
137 // -s: Strip all symbols.
138 bool
139 strip_all() const
140 { return this->strip_ == STRIP_ALL; }
142 // -S: Strip debugging information.
143 bool
144 strip_debug() const
145 { return this->strip_ == STRIP_ALL || this->strip_ == STRIP_DEBUG; }
147 // --eh-frame-hdr: Whether to generate an exception frame header.
148 bool
149 create_eh_frame_hdr() const
150 { return this->create_eh_frame_hdr_; }
152 // --rpath: The runtime search path.
153 const Dir_list&
154 rpath() const
155 { return this->rpath_; }
157 // --rpath-link: The link time search patch for shared libraries.
158 const Dir_list&
159 rpath_link() const
160 { return this->rpath_link_; }
162 // --shared: Whether generating a shared object.
163 bool
164 is_shared() const
165 { return this->is_shared_; }
167 // --static: Whether doing a static link.
168 bool
169 is_static() const
170 { return this->is_static_; }
172 // --statis: Print resource usage statistics.
173 bool
174 print_stats() const
175 { return this->print_stats_; }
177 // --sysroot: The system root of a cross-linker.
178 const std::string&
179 sysroot() const
180 { return this->sysroot_; }
182 private:
183 // Don't copy this structure.
184 General_options(const General_options&);
185 General_options& operator=(const General_options&);
187 friend class Command_line;
188 friend class options::Command_line_options;
190 // Which symbols to strip.
191 enum Strip
193 // Don't strip any symbols.
194 STRIP_NONE,
195 // Strip all symbols.
196 STRIP_ALL,
197 // Strip debugging information.
198 STRIP_DEBUG
201 void
202 set_export_dynamic()
203 { this->export_dynamic_ = true; }
205 void
206 set_dynamic_linker(const char* arg)
207 { this->dynamic_linker_ = arg; }
209 void
210 add_to_search_path(const char* arg)
211 { this->search_path_.push_back(Search_directory(arg, false)); }
213 void
214 add_to_search_path_with_sysroot(const char* arg)
215 { this->search_path_.push_back(Search_directory(arg, true)); }
217 void
218 set_optimization_level(const char* arg)
219 { this->optimization_level_ = atoi(arg); }
221 void
222 set_output_file_name(const char* arg)
223 { this->output_file_name_ = arg; }
225 void
226 set_relocatable()
227 { this->is_relocatable_ = true; }
229 void
230 set_strip_all()
231 { this->strip_ = STRIP_ALL; }
233 // Note: normalize_options() depends on the fact that this turns off
234 // STRIP_ALL if it were already set.
235 void
236 set_strip_debug()
237 { this->strip_ = STRIP_DEBUG; }
239 void
240 set_create_eh_frame_hdr()
241 { this->create_eh_frame_hdr_ = true; }
243 void
244 add_to_rpath(const char* arg)
245 { this->rpath_.push_back(Search_directory(arg, false)); }
247 void
248 add_to_rpath_link(const char* arg)
249 { this->rpath_link_.push_back(Search_directory(arg, false)); }
251 void
252 set_shared()
253 { this->is_shared_ = true; }
255 void
256 set_static()
257 { this->is_static_ = true; }
259 void
260 set_stats()
261 { this->print_stats_ = true; }
263 void
264 set_sysroot(const char* arg)
265 { this->sysroot_ = arg; }
267 void
268 ignore(const char*)
271 // Apply any sysroot to the directory lists.
272 void
273 add_sysroot();
275 bool export_dynamic_;
276 const char* dynamic_linker_;
277 Dir_list search_path_;
278 int optimization_level_;
279 const char* output_file_name_;
280 bool is_relocatable_;
281 Strip strip_;
282 bool create_eh_frame_hdr_;
283 Dir_list rpath_;
284 Dir_list rpath_link_;
285 bool is_shared_;
286 bool is_static_;
287 bool print_stats_;
288 std::string sysroot_;
291 // The current state of the position dependent options.
293 class Position_dependent_options
295 public:
296 Position_dependent_options();
298 // -Bstatic: Whether we are searching for a static archive rather
299 // than a shared object.
300 bool
301 do_static_search() const
302 { return this->do_static_search_; }
304 // --as-needed: Whether to add a DT_NEEDED argument only if the
305 // dynamic object is used.
306 bool
307 as_needed() const
308 { return this->as_needed_; }
310 // --whole-archive: Whether to include the entire contents of an
311 // --archive.
312 bool
313 include_whole_archive() const
314 { return this->include_whole_archive_; }
316 void
317 set_static_search()
318 { this->do_static_search_ = true; }
320 void
321 set_dynamic_search()
322 { this->do_static_search_ = false; }
324 void
325 set_as_needed()
326 { this->as_needed_ = true; }
328 void
329 clear_as_needed()
330 { this->as_needed_ = false; }
332 void
333 set_whole_archive()
334 { this->include_whole_archive_ = true; }
336 void
337 clear_whole_archive()
338 { this->include_whole_archive_ = false; }
340 private:
341 bool do_static_search_;
342 bool as_needed_;
343 bool include_whole_archive_;
346 // A single file or library argument from the command line.
348 class Input_file_argument
350 public:
351 // name: file name or library name
352 // is_lib: true if name is a library name: that is, emits the leading
353 // "lib" and trailing ".so"/".a" from the name
354 // extra_search_path: an extra directory to look for the file, prior
355 // to checking the normal library search path. If this is "",
356 // then no extra directory is added.
357 // options: The position dependent options at this point in the
358 // command line, such as --whole-archive.
359 Input_file_argument()
360 : name_(), is_lib_(false), extra_search_path_(""), options_()
363 Input_file_argument(const char* name, bool is_lib,
364 const char* extra_search_path,
365 const Position_dependent_options& options)
366 : name_(name), is_lib_(is_lib), extra_search_path_(extra_search_path),
367 options_(options)
370 const char*
371 name() const
372 { return this->name_.c_str(); }
374 const Position_dependent_options&
375 options() const
376 { return this->options_; }
378 bool
379 is_lib() const
380 { return this->is_lib_; }
382 const char*
383 extra_search_path() const
385 return (this->extra_search_path_.empty()
386 ? NULL
387 : this->extra_search_path_.c_str());
390 // Return whether this file may require a search using the -L
391 // options.
392 bool
393 may_need_search() const
394 { return this->is_lib_ || !this->extra_search_path_.empty(); }
396 private:
397 // We use std::string, not const char*, here for convenience when
398 // using script files, so that we do not have to preserve the string
399 // in that case.
400 std::string name_;
401 bool is_lib_;
402 std::string extra_search_path_;
403 Position_dependent_options options_;
406 // A file or library, or a group, from the command line.
408 class Input_argument
410 public:
411 // Create a file or library argument.
412 explicit Input_argument(Input_file_argument file)
413 : is_file_(true), file_(file), group_(NULL)
416 // Create a group argument.
417 explicit Input_argument(Input_file_group* group)
418 : is_file_(false), group_(group)
421 // Return whether this is a file.
422 bool
423 is_file() const
424 { return this->is_file_; }
426 // Return whether this is a group.
427 bool
428 is_group() const
429 { return !this->is_file_; }
431 // Return the information about the file.
432 const Input_file_argument&
433 file() const
435 gold_assert(this->is_file_);
436 return this->file_;
439 // Return the information about the group.
440 const Input_file_group*
441 group() const
443 gold_assert(!this->is_file_);
444 return this->group_;
447 Input_file_group*
448 group()
450 gold_assert(!this->is_file_);
451 return this->group_;
454 private:
455 bool is_file_;
456 Input_file_argument file_;
457 Input_file_group* group_;
460 // A group from the command line. This is a set of arguments within
461 // --start-group ... --end-group.
463 class Input_file_group
465 public:
466 typedef std::vector<Input_argument> Files;
467 typedef Files::const_iterator const_iterator;
469 Input_file_group()
470 : files_()
473 // Add a file to the end of the group.
474 void
475 add_file(const Input_file_argument& arg)
476 { this->files_.push_back(Input_argument(arg)); }
478 // Iterators to iterate over the group contents.
480 const_iterator
481 begin() const
482 { return this->files_.begin(); }
484 const_iterator
485 end() const
486 { return this->files_.end(); }
488 private:
489 Files files_;
492 // A list of files from the command line or a script.
494 class Input_arguments
496 public:
497 typedef std::vector<Input_argument> Input_argument_list;
498 typedef Input_argument_list::const_iterator const_iterator;
500 Input_arguments()
501 : input_argument_list_(), in_group_(false)
504 // Add a file.
505 void
506 add_file(const Input_file_argument& arg);
508 // Start a group (the --start-group option).
509 void
510 start_group();
512 // End a group (the --end-group option).
513 void
514 end_group();
516 // Return whether we are currently in a group.
517 bool
518 in_group() const
519 { return this->in_group_; }
521 // Iterators to iterate over the list of input files.
523 const_iterator
524 begin() const
525 { return this->input_argument_list_.begin(); }
527 const_iterator
528 end() const
529 { return this->input_argument_list_.end(); }
531 // Return whether the list is empty.
532 bool
533 empty() const
534 { return this->input_argument_list_.empty(); }
536 private:
537 Input_argument_list input_argument_list_;
538 bool in_group_;
541 // All the information read from the command line.
543 class Command_line
545 public:
546 typedef Input_arguments::const_iterator const_iterator;
548 Command_line();
550 // Process the command line options. This will exit with an
551 // appropriate error message if an unrecognized option is seen.
552 void
553 process(int argc, char** argv);
555 // Handle a -l option.
557 process_l_option(int, char**, char*);
559 // Handle a --start-group option.
560 void
561 start_group(const char* arg);
563 // Handle a --end-group option.
564 void
565 end_group(const char* arg);
567 // Get the general options.
568 const General_options&
569 options() const
570 { return this->options_; }
572 // Iterators to iterate over the list of input files.
574 const_iterator
575 begin() const
576 { return this->inputs_.begin(); }
578 const_iterator
579 end() const
580 { return this->inputs_.end(); }
582 private:
583 Command_line(const Command_line&);
584 Command_line& operator=(const Command_line&);
586 // Report usage error.
587 void
588 usage() ATTRIBUTE_NORETURN;
589 void
590 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
591 void
592 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
594 // Apply a command line option.
595 void
596 apply_option(const gold::options::One_option&, const char*);
598 // Add a file.
599 void
600 add_file(const char* name, bool is_lib);
602 // Examine the result of processing the command-line, and verify
603 // the flags do not contradict each other or are otherwise illegal.
604 void
605 normalize_options();
607 General_options options_;
608 Position_dependent_options position_options_;
609 Input_arguments inputs_;
612 } // End namespace gold.
614 #endif // !defined(GOLD_OPTIONS_H)