1 // options.h -- handle command line options for gold -*- C++ -*-
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
23 class Input_file_group;
27 class Command_line_options;
30 } // End namespace gold::options.
32 // The position independent options which apply to the whole link.
33 // There are a lot of them.
40 // -I: dynamic linker name.
42 dynamic_linker() const
43 { return this->dynamic_linker_; }
45 // -L: Library search path.
46 typedef std::list<const char*> Dir_list;
50 { return this->search_path_; }
52 // -o: Output file name.
54 output_file_name() const
55 { return this->output_file_name_; }
57 // -r: Whether we are doing a relocatable link.
59 is_relocatable() const
60 { return this->is_relocatable_; }
62 // --shared: Whether generating a shared object.
65 { return this->is_shared_; }
67 // --static: Whether doing a static link.
70 { return this->is_static_; }
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;
81 set_dynamic_linker(const char* arg)
82 { this->dynamic_linker_ = arg; }
85 add_to_search_path(const char* arg)
86 { this->search_path_.push_back(arg); }
89 set_output_file_name(const char* arg)
90 { this->output_file_name_ = arg; }
94 { this->is_relocatable_ = true; }
98 { this->is_shared_ = true; }
102 { this->is_static_ = true; }
108 const char* dynamic_linker_;
109 Dir_list search_path_;
110 const char* output_file_name_;
111 bool is_relocatable_;
116 // The current state of the position dependent options.
118 class Position_dependent_options
121 Position_dependent_options();
123 // -Bstatic: Whether we are searching for a static archive rather
124 // than a shared object.
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.
133 { return this->as_needed_; }
137 { this->do_static_search_ = true; }
141 { this->do_static_search_ = false; }
145 { this->as_needed_ = true; }
149 { this->as_needed_ = false; }
152 bool do_static_search_;
156 // A single file or library argument from the command line.
158 class Input_file_argument
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)
172 { return this->name_.c_str(); }
174 const Position_dependent_options&
176 { return this->options_; }
180 { return this->is_lib_; }
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
188 Position_dependent_options options_;
191 // A file or library, or a group, from the command line.
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.
209 { return this->is_file_; }
211 // Return whether this is a group.
214 { return !this->is_file_; }
216 // Return the information about the file.
217 const Input_file_argument&
220 gold_assert(this->is_file_);
224 // Return the information about the group.
225 const Input_file_group*
228 gold_assert(!this->is_file_);
235 gold_assert(!this->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
251 typedef std::vector<Input_argument> Files;
252 typedef Files::const_iterator const_iterator;
258 // Add a file to the end of the group.
260 add_file(const Input_file_argument& arg)
261 { this->files_.push_back(Input_argument(arg)); }
263 // Iterators to iterate over the group contents.
267 { return this->files_.begin(); }
271 { return this->files_.end(); }
277 // A list of files from the command line or a script.
279 class Input_arguments
282 typedef std::vector<Input_argument> Input_argument_list;
283 typedef Input_argument_list::const_iterator const_iterator;
286 : input_argument_list_(), in_group_(false)
291 add_file(const Input_file_argument& arg);
293 // Start a group (the --start-group option).
297 // End a group (the --end-group option).
301 // Return whether we are currently in a group.
304 { return this->in_group_; }
306 // Iterators to iterate over the list of input files.
310 { return this->input_argument_list_.begin(); }
314 { return this->input_argument_list_.end(); }
316 // Return whether the list is empty.
319 { return this->input_argument_list_.empty(); }
322 Input_argument_list input_argument_list_;
326 // All the information read from the command line.
331 typedef Input_arguments::const_iterator const_iterator;
335 // Process the command line options. This will exit with an
336 // appropriate error message if an unrecognized option is seen.
338 process(int argc, char** argv);
340 // Handle a -l option.
342 process_l_option(int, char**, char*);
344 // Handle a --start-group option.
346 start_group(const char* arg);
348 // Handle a --end-group option.
350 end_group(const char* arg);
352 // Get the general options.
353 const General_options&
355 { return this->options_; }
357 // Iterators to iterate over the list of input files.
361 { return this->inputs_.begin(); }
365 { return this->inputs_.end(); }
368 Command_line(const Command_line&);
369 Command_line& operator=(const Command_line&);
371 // Report usage error.
373 usage() ATTRIBUTE_NORETURN;
375 usage(const char* msg, const char* opt) ATTRIBUTE_NORETURN;
377 usage(const char* msg, char opt) ATTRIBUTE_NORETURN;
379 // Apply a command line option.
381 apply_option(const gold::options::One_option&, const char*);
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)