add this momentarily, will move them to the attic soon
[binutils.git] / gold / options.h
blob56907c058eadbc8ecd76d86d1a6b2538d3da7705
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 // -I: dynamic linker name.
41 const char*
42 dynamic_linker() const
43 { return this->dynamic_linker_; }
45 // -L: Library search path.
46 typedef std::list<const char*> Dir_list;
48 const Dir_list&
49 search_path() const
50 { return this->search_path_; }
52 // -o: Output file name.
53 const char*
54 output_file_name() const
55 { return this->output_file_name_; }
57 // -r: Whether we are doing a relocatable link.
58 bool
59 is_relocatable() const
60 { return this->is_relocatable_; }
62 // --shared: Whether generating a shared object.
63 bool
64 is_shared() const
65 { return this->is_shared_; }
67 // --static: Whether doing a static link.
68 bool
69 is_static() const
70 { return this->is_static_; }
72 private:
73 // Don't copy this structure.
74 General_options(const General_options&);
75 General_options& operator=(const General_options&);
77 friend class Command_line;
78 friend class options::Command_line_options;
80 void
81 set_dynamic_linker(const char* arg)
82 { this->dynamic_linker_ = arg; }
84 void
85 add_to_search_path(const char* arg)
86 { this->search_path_.push_back(arg); }
88 void
89 set_output_file_name(const char* arg)
90 { this->output_file_name_ = arg; }
92 void
93 set_relocatable()
94 { this->is_relocatable_ = true; }
96 void
97 set_shared()
98 { this->is_shared_ = true; }
100 void
101 set_static()
102 { this->is_static_ = true; }
104 void
105 ignore(const char*)
108 const char* dynamic_linker_;
109 Dir_list search_path_;
110 const char* output_file_name_;
111 bool is_relocatable_;
112 bool is_shared_;
113 bool is_static_;
116 // The current state of the position dependent options.
118 class Position_dependent_options
120 public:
121 Position_dependent_options();
123 // -Bstatic: Whether we are searching for a static archive rather
124 // than a shared object.
125 bool
126 do_static_search() const
127 { return this->do_static_search_; }
129 // --as-needed: Whether to add a DT_NEEDED argument only if the
130 // dynamic object is used.
131 bool
132 as_needed() const
133 { return this->as_needed_; }
135 void
136 set_static_search()
137 { this->do_static_search_ = true; }
139 void
140 set_dynamic_search()
141 { this->do_static_search_ = false; }
143 void
144 set_as_needed()
145 { this->as_needed_ = true; }
147 void
148 clear_as_needed()
149 { this->as_needed_ = false; }
151 private:
152 bool do_static_search_;
153 bool as_needed_;
156 // A single file or library argument from the command line.
158 class Input_file_argument
160 public:
161 Input_file_argument()
162 : name_(), is_lib_(false), options_()
165 Input_file_argument(const char* name, bool is_lib,
166 const Position_dependent_options& options)
167 : name_(name), is_lib_(is_lib), options_(options)
170 const char*
171 name() const
172 { return this->name_.c_str(); }
174 const Position_dependent_options&
175 options() const
176 { return this->options_; }
178 bool
179 is_lib() const
180 { return this->is_lib_; }
182 private:
183 // We use std::string, not const char*, here for convenience when
184 // using script files, so that we do not have to preserve the string
185 // in that case.
186 std::string name_;
187 bool is_lib_;
188 Position_dependent_options options_;
191 // A file or library, or a group, from the command line.
193 class Input_argument
195 public:
196 // Create a file or library argument.
197 explicit Input_argument(Input_file_argument file)
198 : is_file_(true), file_(file), group_(NULL)
201 // Create a group argument.
202 explicit Input_argument(Input_file_group* group)
203 : is_file_(false), group_(group)
206 // Return whether this is a file.
207 bool
208 is_file() const
209 { return this->is_file_; }
211 // Return whether this is a group.
212 bool
213 is_group() const
214 { return !this->is_file_; }
216 // Return the information about the file.
217 const Input_file_argument&
218 file() const
220 gold_assert(this->is_file_);
221 return this->file_;
224 // Return the information about the group.
225 const Input_file_group*
226 group() const
228 gold_assert(!this->is_file_);
229 return this->group_;
232 Input_file_group*
233 group()
235 gold_assert(!this->is_file_);
236 return this->group_;
239 private:
240 bool is_file_;
241 Input_file_argument file_;
242 Input_file_group* group_;
245 // A group from the command line. This is a set of arguments within
246 // --start-group ... --end-group.
248 class Input_file_group
250 public:
251 typedef std::vector<Input_argument> Files;
252 typedef Files::const_iterator const_iterator;
254 Input_file_group()
255 : files_()
258 // Add a file to the end of the group.
259 void
260 add_file(const Input_file_argument& arg)
261 { this->files_.push_back(Input_argument(arg)); }
263 // Iterators to iterate over the group contents.
265 const_iterator
266 begin() const
267 { return this->files_.begin(); }
269 const_iterator
270 end() const
271 { return this->files_.end(); }
273 private:
274 Files files_;
277 // A list of files from the command line or a script.
279 class Input_arguments
281 public:
282 typedef std::vector<Input_argument> Input_argument_list;
283 typedef Input_argument_list::const_iterator const_iterator;
285 Input_arguments()
286 : input_argument_list_(), in_group_(false)
289 // Add a file.
290 void
291 add_file(const Input_file_argument& arg);
293 // Start a group (the --start-group option).
294 void
295 start_group();
297 // End a group (the --end-group option).
298 void
299 end_group();
301 // Return whether we are currently in a group.
302 bool
303 in_group() const
304 { return this->in_group_; }
306 // Iterators to iterate over the list of input files.
308 const_iterator
309 begin() const
310 { return this->input_argument_list_.begin(); }
312 const_iterator
313 end() const
314 { return this->input_argument_list_.end(); }
316 // Return whether the list is empty.
317 bool
318 empty() const
319 { return this->input_argument_list_.empty(); }
321 private:
322 Input_argument_list input_argument_list_;
323 bool in_group_;
326 // All the information read from the command line.
328 class Command_line
330 public:
331 typedef Input_arguments::const_iterator const_iterator;
333 Command_line();
335 // Process the command line options. This will exit with an
336 // appropriate error message if an unrecognized option is seen.
337 void
338 process(int argc, char** argv);
340 // Handle a -l option.
342 process_l_option(int, char**, char*);
344 // Handle a --start-group option.
345 void
346 start_group(const char* arg);
348 // Handle a --end-group option.
349 void
350 end_group(const char* arg);
352 // Get the general options.
353 const General_options&
354 options() const
355 { return this->options_; }
357 // Iterators to iterate over the list of input files.
359 const_iterator
360 begin() const
361 { return this->inputs_.begin(); }
363 const_iterator
364 end() const
365 { return this->inputs_.end(); }
367 private:
368 Command_line(const Command_line&);
369 Command_line& operator=(const Command_line&);
371 // Report usage error.
372 void
373 usage() ATTRIBUTE_NORETURN;
374 void
375 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
376 void
377 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
379 // Apply a command line option.
380 void
381 apply_option(const gold::options::One_option&, const char*);
383 // Add a file.
384 void
385 add_file(const char* name, bool is_lib);
387 General_options options_;
388 Position_dependent_options position_options_;
389 Input_arguments inputs_;
392 } // End namespace gold.
394 #endif // !defined(GOLD_OPTIONS_H)