bfd:
[binutils.git] / gold / options.h
blob7b1fe67e6c3a6678e31c16a23ab6d489a4accba1
1 // options.h -- handle command line options for gold -*- C++ -*-
3 // Command_line
4 // Holds everything we get from the command line.
5 // General_options (from Command_line::options())
6 // Options which are not position dependent.
7 // Input_argument (from Command_line::inputs())
8 // The list of input files, including -l options.
9 // Position_dependent_options (from Input_argument::options())
10 // Position dependent options which apply to this argument.
12 #ifndef GOLD_OPTIONS_H
13 #define GOLD_OPTIONS_H
15 #include <list>
16 #include <string>
17 #include <vector>
19 namespace gold
22 class Command_line;
23 class Input_file_group;
25 namespace options {
27 class Command_line_options;
28 struct One_option;
30 } // End namespace gold::options.
32 // The position independent options which apply to the whole link.
33 // There are a lot of them.
35 class General_options
37 public:
38 General_options();
40 // -E: export dynamic symbols.
41 bool
42 export_dynamic() const
43 { return this->export_dynamic_; }
45 // -I: dynamic linker name.
46 const char*
47 dynamic_linker() const
48 { return this->dynamic_linker_; }
50 // -L: Library search path.
51 typedef std::vector<const char*> Dir_list;
53 const Dir_list&
54 search_path() const
55 { return this->search_path_; }
57 // -o: Output file name.
58 const char*
59 output_file_name() const
60 { return this->output_file_name_; }
62 // -r: Whether we are doing a relocatable link.
63 bool
64 is_relocatable() const
65 { return this->is_relocatable_; }
67 // --eh-frame-hdr: Whether to generate an exception frame header.
68 bool
69 create_eh_frame_hdr() const
70 { return this->create_eh_frame_hdr_; }
72 // --rpath: The runtime search path.
73 const Dir_list&
74 rpath() const
75 { return this->rpath_; }
77 // --shared: Whether generating a shared object.
78 bool
79 is_shared() const
80 { return this->is_shared_; }
82 // --static: Whether doing a static link.
83 bool
84 is_static() const
85 { return this->is_static_; }
87 private:
88 // Don't copy this structure.
89 General_options(const General_options&);
90 General_options& operator=(const General_options&);
92 friend class Command_line;
93 friend class options::Command_line_options;
95 void
96 set_export_dynamic()
97 { this->export_dynamic_ = true; }
99 void
100 set_dynamic_linker(const char* arg)
101 { this->dynamic_linker_ = arg; }
103 void
104 add_to_search_path(const char* arg)
105 { this->search_path_.push_back(arg); }
107 void
108 set_output_file_name(const char* arg)
109 { this->output_file_name_ = arg; }
111 void
112 set_relocatable()
113 { this->is_relocatable_ = true; }
115 void
116 create_eh_frame_hdr()
117 { this->create_eh_frame_hdr_ = true; }
119 void
120 add_to_rpath(const char* arg)
121 { this->rpath_.push_back(arg); }
123 void
124 set_shared()
125 { this->is_shared_ = true; }
127 void
128 set_static()
129 { this->is_static_ = true; }
131 void
132 ignore(const char*)
135 bool export_dynamic_;
136 const char* dynamic_linker_;
137 Dir_list search_path_;
138 const char* output_file_name_;
139 bool is_relocatable_;
140 bool create_eh_frame_hdr_;
141 Dir_list rpath_;
142 bool is_shared_;
143 bool is_static_;
146 // The current state of the position dependent options.
148 class Position_dependent_options
150 public:
151 Position_dependent_options();
153 // -Bstatic: Whether we are searching for a static archive rather
154 // than a shared object.
155 bool
156 do_static_search() const
157 { return this->do_static_search_; }
159 // --as-needed: Whether to add a DT_NEEDED argument only if the
160 // dynamic object is used.
161 bool
162 as_needed() const
163 { return this->as_needed_; }
165 // --whole-archive: Whether to include the entire contents of an
166 // --archive.
167 bool
168 include_whole_archive() const
169 { return this->include_whole_archive_; }
171 void
172 set_static_search()
173 { this->do_static_search_ = true; }
175 void
176 set_dynamic_search()
177 { this->do_static_search_ = false; }
179 void
180 set_as_needed()
181 { this->as_needed_ = true; }
183 void
184 clear_as_needed()
185 { this->as_needed_ = false; }
187 void
188 set_whole_archive()
189 { this->include_whole_archive_ = true; }
191 void
192 clear_whole_archive()
193 { this->include_whole_archive_ = false; }
195 private:
196 bool do_static_search_;
197 bool as_needed_;
198 bool include_whole_archive_;
201 // A single file or library argument from the command line.
203 class Input_file_argument
205 public:
206 Input_file_argument()
207 : name_(), is_lib_(false), options_()
210 Input_file_argument(const char* name, bool is_lib,
211 const Position_dependent_options& options)
212 : name_(name), is_lib_(is_lib), options_(options)
215 const char*
216 name() const
217 { return this->name_.c_str(); }
219 const Position_dependent_options&
220 options() const
221 { return this->options_; }
223 bool
224 is_lib() const
225 { return this->is_lib_; }
227 private:
228 // We use std::string, not const char*, here for convenience when
229 // using script files, so that we do not have to preserve the string
230 // in that case.
231 std::string name_;
232 bool is_lib_;
233 Position_dependent_options options_;
236 // A file or library, or a group, from the command line.
238 class Input_argument
240 public:
241 // Create a file or library argument.
242 explicit Input_argument(Input_file_argument file)
243 : is_file_(true), file_(file), group_(NULL)
246 // Create a group argument.
247 explicit Input_argument(Input_file_group* group)
248 : is_file_(false), group_(group)
251 // Return whether this is a file.
252 bool
253 is_file() const
254 { return this->is_file_; }
256 // Return whether this is a group.
257 bool
258 is_group() const
259 { return !this->is_file_; }
261 // Return the information about the file.
262 const Input_file_argument&
263 file() const
265 gold_assert(this->is_file_);
266 return this->file_;
269 // Return the information about the group.
270 const Input_file_group*
271 group() const
273 gold_assert(!this->is_file_);
274 return this->group_;
277 Input_file_group*
278 group()
280 gold_assert(!this->is_file_);
281 return this->group_;
284 private:
285 bool is_file_;
286 Input_file_argument file_;
287 Input_file_group* group_;
290 // A group from the command line. This is a set of arguments within
291 // --start-group ... --end-group.
293 class Input_file_group
295 public:
296 typedef std::vector<Input_argument> Files;
297 typedef Files::const_iterator const_iterator;
299 Input_file_group()
300 : files_()
303 // Add a file to the end of the group.
304 void
305 add_file(const Input_file_argument& arg)
306 { this->files_.push_back(Input_argument(arg)); }
308 // Iterators to iterate over the group contents.
310 const_iterator
311 begin() const
312 { return this->files_.begin(); }
314 const_iterator
315 end() const
316 { return this->files_.end(); }
318 private:
319 Files files_;
322 // A list of files from the command line or a script.
324 class Input_arguments
326 public:
327 typedef std::vector<Input_argument> Input_argument_list;
328 typedef Input_argument_list::const_iterator const_iterator;
330 Input_arguments()
331 : input_argument_list_(), in_group_(false)
334 // Add a file.
335 void
336 add_file(const Input_file_argument& arg);
338 // Start a group (the --start-group option).
339 void
340 start_group();
342 // End a group (the --end-group option).
343 void
344 end_group();
346 // Return whether we are currently in a group.
347 bool
348 in_group() const
349 { return this->in_group_; }
351 // Iterators to iterate over the list of input files.
353 const_iterator
354 begin() const
355 { return this->input_argument_list_.begin(); }
357 const_iterator
358 end() const
359 { return this->input_argument_list_.end(); }
361 // Return whether the list is empty.
362 bool
363 empty() const
364 { return this->input_argument_list_.empty(); }
366 private:
367 Input_argument_list input_argument_list_;
368 bool in_group_;
371 // All the information read from the command line.
373 class Command_line
375 public:
376 typedef Input_arguments::const_iterator const_iterator;
378 Command_line();
380 // Process the command line options. This will exit with an
381 // appropriate error message if an unrecognized option is seen.
382 void
383 process(int argc, char** argv);
385 // Handle a -l option.
387 process_l_option(int, char**, char*);
389 // Handle a --start-group option.
390 void
391 start_group(const char* arg);
393 // Handle a --end-group option.
394 void
395 end_group(const char* arg);
397 // Get the general options.
398 const General_options&
399 options() const
400 { return this->options_; }
402 // Iterators to iterate over the list of input files.
404 const_iterator
405 begin() const
406 { return this->inputs_.begin(); }
408 const_iterator
409 end() const
410 { return this->inputs_.end(); }
412 private:
413 Command_line(const Command_line&);
414 Command_line& operator=(const Command_line&);
416 // Report usage error.
417 void
418 usage() ATTRIBUTE_NORETURN;
419 void
420 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
421 void
422 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
424 // Apply a command line option.
425 void
426 apply_option(const gold::options::One_option&, const char*);
428 // Add a file.
429 void
430 add_file(const char* name, bool is_lib);
432 General_options options_;
433 Position_dependent_options position_options_;
434 Input_arguments inputs_;
437 } // End namespace gold.
439 #endif // !defined(GOLD_OPTIONS_H)