Add support for -rpath.
[binutils-gdb.git] / gold / options.h
1 // options.h -- handle command line options for gold -*- C++ -*-
2
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.
11
12 #ifndef GOLD_OPTIONS_H
13 #define GOLD_OPTIONS_H
14
15 #include <list>
16 #include <string>
17 #include <vector>
18
19 namespace gold
20 {
21
22 class Command_line;
23 class Input_file_group;
24
25 namespace options {
26
27 class Command_line_options;
28 struct One_option;
29
30 } // End namespace gold::options.
31
32 // The position independent options which apply to the whole link.
33 // There are a lot of them.
34
35 class General_options
36 {
37 public:
38 General_options();
39
40 // -I: dynamic linker name.
41 const char*
42 dynamic_linker() const
43 { return this->dynamic_linker_; }
44
45 // -L: Library search path.
46 typedef std::vector<const char*> Dir_list;
47
48 const Dir_list&
49 search_path() const
50 { return this->search_path_; }
51
52 // -o: Output file name.
53 const char*
54 output_file_name() const
55 { return this->output_file_name_; }
56
57 // -r: Whether we are doing a relocatable link.
58 bool
59 is_relocatable() const
60 { return this->is_relocatable_; }
61
62 // --rpath: The runtime search path.
63 const Dir_list&
64 rpath() const
65 { return this->rpath_; }
66
67 // --shared: Whether generating a shared object.
68 bool
69 is_shared() const
70 { return this->is_shared_; }
71
72 // --static: Whether doing a static link.
73 bool
74 is_static() const
75 { return this->is_static_; }
76
77 private:
78 // Don't copy this structure.
79 General_options(const General_options&);
80 General_options& operator=(const General_options&);
81
82 friend class Command_line;
83 friend class options::Command_line_options;
84
85 void
86 set_dynamic_linker(const char* arg)
87 { this->dynamic_linker_ = arg; }
88
89 void
90 add_to_search_path(const char* arg)
91 { this->search_path_.push_back(arg); }
92
93 void
94 set_output_file_name(const char* arg)
95 { this->output_file_name_ = arg; }
96
97 void
98 set_relocatable()
99 { this->is_relocatable_ = true; }
100
101 void
102 add_to_rpath(const char* arg)
103 { this->rpath_.push_back(arg); }
104
105 void
106 set_shared()
107 { this->is_shared_ = true; }
108
109 void
110 set_static()
111 { this->is_static_ = true; }
112
113 void
114 ignore(const char*)
115 { }
116
117 const char* dynamic_linker_;
118 Dir_list search_path_;
119 const char* output_file_name_;
120 bool is_relocatable_;
121 Dir_list rpath_;
122 bool is_shared_;
123 bool is_static_;
124 };
125
126 // The current state of the position dependent options.
127
128 class Position_dependent_options
129 {
130 public:
131 Position_dependent_options();
132
133 // -Bstatic: Whether we are searching for a static archive rather
134 // than a shared object.
135 bool
136 do_static_search() const
137 { return this->do_static_search_; }
138
139 // --as-needed: Whether to add a DT_NEEDED argument only if the
140 // dynamic object is used.
141 bool
142 as_needed() const
143 { return this->as_needed_; }
144
145 void
146 set_static_search()
147 { this->do_static_search_ = true; }
148
149 void
150 set_dynamic_search()
151 { this->do_static_search_ = false; }
152
153 void
154 set_as_needed()
155 { this->as_needed_ = true; }
156
157 void
158 clear_as_needed()
159 { this->as_needed_ = false; }
160
161 private:
162 bool do_static_search_;
163 bool as_needed_;
164 };
165
166 // A single file or library argument from the command line.
167
168 class Input_file_argument
169 {
170 public:
171 Input_file_argument()
172 : name_(), is_lib_(false), options_()
173 { }
174
175 Input_file_argument(const char* name, bool is_lib,
176 const Position_dependent_options& options)
177 : name_(name), is_lib_(is_lib), options_(options)
178 { }
179
180 const char*
181 name() const
182 { return this->name_.c_str(); }
183
184 const Position_dependent_options&
185 options() const
186 { return this->options_; }
187
188 bool
189 is_lib() const
190 { return this->is_lib_; }
191
192 private:
193 // We use std::string, not const char*, here for convenience when
194 // using script files, so that we do not have to preserve the string
195 // in that case.
196 std::string name_;
197 bool is_lib_;
198 Position_dependent_options options_;
199 };
200
201 // A file or library, or a group, from the command line.
202
203 class Input_argument
204 {
205 public:
206 // Create a file or library argument.
207 explicit Input_argument(Input_file_argument file)
208 : is_file_(true), file_(file), group_(NULL)
209 { }
210
211 // Create a group argument.
212 explicit Input_argument(Input_file_group* group)
213 : is_file_(false), group_(group)
214 { }
215
216 // Return whether this is a file.
217 bool
218 is_file() const
219 { return this->is_file_; }
220
221 // Return whether this is a group.
222 bool
223 is_group() const
224 { return !this->is_file_; }
225
226 // Return the information about the file.
227 const Input_file_argument&
228 file() const
229 {
230 gold_assert(this->is_file_);
231 return this->file_;
232 }
233
234 // Return the information about the group.
235 const Input_file_group*
236 group() const
237 {
238 gold_assert(!this->is_file_);
239 return this->group_;
240 }
241
242 Input_file_group*
243 group()
244 {
245 gold_assert(!this->is_file_);
246 return this->group_;
247 }
248
249 private:
250 bool is_file_;
251 Input_file_argument file_;
252 Input_file_group* group_;
253 };
254
255 // A group from the command line. This is a set of arguments within
256 // --start-group ... --end-group.
257
258 class Input_file_group
259 {
260 public:
261 typedef std::vector<Input_argument> Files;
262 typedef Files::const_iterator const_iterator;
263
264 Input_file_group()
265 : files_()
266 { }
267
268 // Add a file to the end of the group.
269 void
270 add_file(const Input_file_argument& arg)
271 { this->files_.push_back(Input_argument(arg)); }
272
273 // Iterators to iterate over the group contents.
274
275 const_iterator
276 begin() const
277 { return this->files_.begin(); }
278
279 const_iterator
280 end() const
281 { return this->files_.end(); }
282
283 private:
284 Files files_;
285 };
286
287 // A list of files from the command line or a script.
288
289 class Input_arguments
290 {
291 public:
292 typedef std::vector<Input_argument> Input_argument_list;
293 typedef Input_argument_list::const_iterator const_iterator;
294
295 Input_arguments()
296 : input_argument_list_(), in_group_(false)
297 { }
298
299 // Add a file.
300 void
301 add_file(const Input_file_argument& arg);
302
303 // Start a group (the --start-group option).
304 void
305 start_group();
306
307 // End a group (the --end-group option).
308 void
309 end_group();
310
311 // Return whether we are currently in a group.
312 bool
313 in_group() const
314 { return this->in_group_; }
315
316 // Iterators to iterate over the list of input files.
317
318 const_iterator
319 begin() const
320 { return this->input_argument_list_.begin(); }
321
322 const_iterator
323 end() const
324 { return this->input_argument_list_.end(); }
325
326 // Return whether the list is empty.
327 bool
328 empty() const
329 { return this->input_argument_list_.empty(); }
330
331 private:
332 Input_argument_list input_argument_list_;
333 bool in_group_;
334 };
335
336 // All the information read from the command line.
337
338 class Command_line
339 {
340 public:
341 typedef Input_arguments::const_iterator const_iterator;
342
343 Command_line();
344
345 // Process the command line options. This will exit with an
346 // appropriate error message if an unrecognized option is seen.
347 void
348 process(int argc, char** argv);
349
350 // Handle a -l option.
351 int
352 process_l_option(int, char**, char*);
353
354 // Handle a --start-group option.
355 void
356 start_group(const char* arg);
357
358 // Handle a --end-group option.
359 void
360 end_group(const char* arg);
361
362 // Get the general options.
363 const General_options&
364 options() const
365 { return this->options_; }
366
367 // Iterators to iterate over the list of input files.
368
369 const_iterator
370 begin() const
371 { return this->inputs_.begin(); }
372
373 const_iterator
374 end() const
375 { return this->inputs_.end(); }
376
377 private:
378 Command_line(const Command_line&);
379 Command_line& operator=(const Command_line&);
380
381 // Report usage error.
382 void
383 usage() ATTRIBUTE_NORETURN;
384 void
385 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
386 void
387 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
388
389 // Apply a command line option.
390 void
391 apply_option(const gold::options::One_option&, const char*);
392
393 // Add a file.
394 void
395 add_file(const char* name, bool is_lib);
396
397 General_options options_;
398 Position_dependent_options position_options_;
399 Input_arguments inputs_;
400 };
401
402 } // End namespace gold.
403
404 #endif // !defined(GOLD_OPTIONS_H)