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