Add Elf_file interface which can be used by both Sized_relobj and
[binutils.git] / gold / options.h
blob27db787edc2ceffb8284dce62a48f866a4e570be
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>
18 #include <cassert>
20 namespace gold
23 class Command_line;
24 class Input_file_group;
26 namespace options {
28 class Command_line_options;
29 struct One_option;
31 } // End namespace gold::options.
33 // The position independent options which apply to the whole link.
34 // There are a lot of them.
36 class General_options
38 public:
39 General_options();
41 // -L: Library search path.
42 typedef std::list<const char*> Dir_list;
44 const Dir_list&
45 search_path() const
46 { return this->search_path_; }
48 // -o: Output file name.
49 const char*
50 output_file_name() const
51 { return this->output_file_name_; }
53 // -r: Whether we are doing a relocatable link.
54 bool
55 is_relocatable() const
56 { return this->is_relocatable_; }
58 // --shared: Whether generating a shared object.
59 bool
60 is_shared() const
61 { return this->is_shared_; }
63 // --static: Whether doing a static link.
64 bool
65 is_static() const
66 { return this->is_static_; }
68 private:
69 friend class Command_line;
70 friend class options::Command_line_options;
72 void
73 add_to_search_path(const char* arg)
74 { this->search_path_.push_back(arg); }
76 void
77 set_output_file_name(const char* arg)
78 { this->output_file_name_ = arg; }
80 void
81 set_relocatable()
82 { this->is_relocatable_ = true; }
84 void
85 set_shared()
86 { this->is_shared_ = true; }
88 void
89 set_static()
90 { this->is_static_ = true; }
92 void
93 ignore(const char*)
94 { }
96 Dir_list search_path_;
97 const char* output_file_name_;
98 bool is_relocatable_;
99 bool is_shared_;
100 bool is_static_;
102 // Don't copy this structure.
103 General_options(const General_options&);
104 General_options& operator=(const General_options&);
107 // The current state of the position dependent options.
109 class Position_dependent_options
111 public:
112 Position_dependent_options();
114 // -Bstatic: Whether we are searching for a static archive rather
115 // -than a shared object.
116 bool
117 do_static_search()
118 { return this->do_static_search_; }
120 private:
121 friend class Command_line;
122 friend class options::Command_line_options;
124 void
125 set_static_search()
126 { this->do_static_search_ = true; }
128 void
129 set_dynamic_search()
130 { this->do_static_search_ = false; }
132 bool do_static_search_;
135 // A single file or library argument from the command line.
137 class Input_file_argument
139 public:
140 Input_file_argument()
141 : name_(NULL), is_lib_(false), options_()
144 Input_file_argument(const char* name, bool is_lib,
145 const Position_dependent_options& options)
146 : name_(name), is_lib_(is_lib), options_(options)
149 const char*
150 name() const
151 { return this->name_; }
153 const Position_dependent_options&
154 options() const
155 { return this->options_; }
157 bool
158 is_lib() const
159 { return this->is_lib_; }
161 private:
162 const char* name_;
163 bool is_lib_;
164 Position_dependent_options options_;
167 // A file or library, or a group, from the command line.
169 class Input_argument
171 public:
172 // Create a file or library argument.
173 explicit Input_argument(Input_file_argument file)
174 : is_file_(true), file_(file), group_(NULL)
177 // Create a group argument.
178 explicit Input_argument(Input_file_group* group)
179 : is_file_(false), group_(group)
182 // Return whether this is a file.
183 bool
184 is_file() const
185 { return this->is_file_; }
187 // Return whether this is a group.
188 bool
189 is_group() const
190 { return !this->is_file_; }
192 // Return the information about the file.
193 const Input_file_argument&
194 file() const
196 assert(this->is_file_);
197 return this->file_;
200 // Return the information about the group.
201 const Input_file_group*
202 group() const
204 assert(!this->is_file_);
205 return this->group_;
208 Input_file_group*
209 group()
211 assert(!this->is_file_);
212 return this->group_;
215 private:
216 bool is_file_;
217 Input_file_argument file_;
218 Input_file_group* group_;
221 // A group from the command line. This is a set of arguments within
222 // --start-group ... --end-group.
224 class Input_file_group
226 public:
227 typedef std::vector<Input_argument> Files;
228 typedef Files::const_iterator const_iterator;
230 Input_file_group()
231 : files_()
234 // Add a file to the end of the group.
235 void
236 add_file(const Input_file_argument& arg)
237 { this->files_.push_back(Input_argument(arg)); }
239 // Iterators to iterate over the group contents.
241 const_iterator
242 begin() const
243 { return this->files_.begin(); }
245 const_iterator
246 end() const
247 { return this->files_.end(); }
249 private:
250 Files files_;
253 // All the information read from the command line.
255 class Command_line
257 public:
258 typedef std::vector<Input_argument> Input_arguments;
259 typedef Input_arguments::const_iterator const_iterator;
261 Command_line();
263 // Process the command line options. This will exit with an
264 // appropriate error message if an unrecognized option is seen.
265 void
266 process(int argc, char** argv);
268 // Handle a -l option.
270 process_l_option(int, char**, char*);
272 // Handle a --start-group option.
273 void
274 start_group(const char* arg);
276 // Handle a --end-group option.
277 void
278 end_group(const char* arg);
280 // Get the general options.
281 const General_options&
282 options() const
283 { return this->options_; }
285 // Iterators to iterate over the list of input files.
287 const_iterator
288 begin() const
289 { return this->inputs_.begin(); }
291 const_iterator
292 end() const
293 { return this->inputs_.end(); }
295 private:
296 Command_line(const Command_line&);
297 Command_line& operator=(const Command_line&);
299 // Report usage error.
300 void
301 usage() ATTRIBUTE_NORETURN;
302 void
303 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
304 void
305 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
307 // Apply a command line option.
308 void
309 apply_option(const gold::options::One_option&, const char*);
311 // Add a file.
312 void
313 add_file(const char* name, bool is_lib);
315 General_options options_;
316 Position_dependent_options position_options_;
317 Input_arguments inputs_;
318 bool in_group_;
321 } // End namespace gold.
323 #endif // !defined(GOLD_OPTIONS_H)