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