]> git.lizzy.rs Git - rust.git/blob - src/librustc/driver/config.rs
Add a few more derivings to AST types
[rust.git] / src / librustc / driver / config.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Contains infrastructure for configuring the compiler, including parsing
12 //! command line options.
13
14 use driver::{early_error, early_warn};
15 use driver::driver;
16 use driver::session::Session;
17
18 use back;
19 use back::link;
20 use back::target_strs;
21 use back::{arm, x86, x86_64, mips, mipsel};
22 use lint;
23
24 use syntax::abi;
25 use syntax::ast;
26 use syntax::ast::{IntTy, UintTy};
27 use syntax::attr;
28 use syntax::attr::AttrMetaMethods;
29 use syntax::diagnostic::{ColorConfig, Auto, Always, Never};
30 use syntax::parse;
31 use syntax::parse::token::InternedString;
32
33 use std::collections::{HashSet, HashMap};
34 use getopts::{optopt, optmulti, optflag, optflagopt};
35 use getopts;
36 use std::cell::{RefCell};
37 use std::fmt;
38
39 use llvm;
40
41 pub struct Config {
42     pub os: abi::Os,
43     pub arch: abi::Architecture,
44     pub target_strs: target_strs::t,
45     pub int_type: IntTy,
46     pub uint_type: UintTy,
47 }
48
49 #[deriving(Clone, PartialEq)]
50 pub enum OptLevel {
51     No, // -O0
52     Less, // -O1
53     Default, // -O2
54     Aggressive // -O3
55 }
56
57 #[deriving(Clone, PartialEq)]
58 pub enum DebugInfoLevel {
59     NoDebugInfo,
60     LimitedDebugInfo,
61     FullDebugInfo,
62 }
63
64 #[deriving(Clone)]
65 pub struct Options {
66     // The crate config requested for the session, which may be combined
67     // with additional crate configurations during the compile process
68     pub crate_types: Vec<CrateType>,
69
70     pub gc: bool,
71     pub optimize: OptLevel,
72     pub debuginfo: DebugInfoLevel,
73     pub lint_opts: Vec<(String, lint::Level)>,
74     pub describe_lints: bool,
75     pub output_types: Vec<back::link::OutputType> ,
76     // This was mutable for rustpkg, which updates search paths based on the
77     // parsed code. It remains mutable in case its replacements wants to use
78     // this.
79     pub addl_lib_search_paths: RefCell<HashSet<Path>>,
80     pub maybe_sysroot: Option<Path>,
81     pub target_triple: String,
82     // User-specified cfg meta items. The compiler itself will add additional
83     // items to the crate config, and during parsing the entire crate config
84     // will be added to the crate AST node.  This should not be used for
85     // anything except building the full crate config prior to parsing.
86     pub cfg: ast::CrateConfig,
87     pub test: bool,
88     pub parse_only: bool,
89     pub no_trans: bool,
90     pub no_analysis: bool,
91     pub debugging_opts: u64,
92     /// Whether to write dependency files. It's (enabled, optional filename).
93     pub write_dependency_info: (bool, Option<Path>),
94     /// Crate id-related things to maybe print. It's (crate_name, crate_file_name).
95     pub print_metas: (bool, bool),
96     pub cg: CodegenOptions,
97     pub color: ColorConfig,
98     pub externs: HashMap<String, Vec<String>>,
99     pub crate_name: Option<String>,
100     /// An optional name to use as the crate for std during std injection,
101     /// written `extern crate std = "name"`. Default to "std". Used by
102     /// out-of-tree drivers.
103     pub alt_std_name: Option<String>
104 }
105
106 /// Some reasonable defaults
107 pub fn basic_options() -> Options {
108     Options {
109         crate_types: Vec::new(),
110         gc: false,
111         optimize: No,
112         debuginfo: NoDebugInfo,
113         lint_opts: Vec::new(),
114         describe_lints: false,
115         output_types: Vec::new(),
116         addl_lib_search_paths: RefCell::new(HashSet::new()),
117         maybe_sysroot: None,
118         target_triple: driver::host_triple().to_string(),
119         cfg: Vec::new(),
120         test: false,
121         parse_only: false,
122         no_trans: false,
123         no_analysis: false,
124         debugging_opts: 0,
125         write_dependency_info: (false, None),
126         print_metas: (false, false),
127         cg: basic_codegen_options(),
128         color: Auto,
129         externs: HashMap::new(),
130         crate_name: None,
131         alt_std_name: None,
132     }
133 }
134
135 // The type of entry function, so
136 // users can have their own entry
137 // functions that don't start a
138 // scheduler
139 #[deriving(PartialEq)]
140 pub enum EntryFnType {
141     EntryMain,
142     EntryStart,
143     EntryNone,
144 }
145
146 #[deriving(PartialEq, PartialOrd, Clone, Ord, Eq, Hash)]
147 pub enum CrateType {
148     CrateTypeExecutable,
149     CrateTypeDylib,
150     CrateTypeRlib,
151     CrateTypeStaticlib,
152 }
153
154 macro_rules! debugging_opts(
155     ([ $opt:ident ] $cnt:expr ) => (
156         pub static $opt: u64 = 1 << $cnt;
157     );
158     ([ $opt:ident, $($rest:ident),* ] $cnt:expr ) => (
159         pub static $opt: u64 = 1 << $cnt;
160         debugging_opts!([ $($rest),* ] $cnt + 1)
161     )
162 )
163
164 debugging_opts!(
165     [
166         VERBOSE,
167         TIME_PASSES,
168         COUNT_LLVM_INSNS,
169         TIME_LLVM_PASSES,
170         TRANS_STATS,
171         ASM_COMMENTS,
172         NO_VERIFY,
173         BORROWCK_STATS,
174         NO_LANDING_PADS,
175         DEBUG_LLVM,
176         SHOW_SPAN,
177         COUNT_TYPE_SIZES,
178         META_STATS,
179         NO_OPT,
180         GC,
181         PRINT_LINK_ARGS,
182         PRINT_LLVM_PASSES,
183         LTO,
184         AST_JSON,
185         AST_JSON_NOEXPAND,
186         LS,
187         SAVE_ANALYSIS,
188         FLOWGRAPH_PRINT_LOANS,
189         FLOWGRAPH_PRINT_MOVES,
190         FLOWGRAPH_PRINT_ASSIGNS,
191         FLOWGRAPH_PRINT_ALL
192     ]
193     0
194 )
195
196 pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
197     vec!(("verbose", "in general, enable more debug printouts", VERBOSE),
198      ("time-passes", "measure time of each rustc pass", TIME_PASSES),
199      ("count-llvm-insns", "count where LLVM \
200                            instrs originate", COUNT_LLVM_INSNS),
201      ("time-llvm-passes", "measure time of each LLVM pass",
202       TIME_LLVM_PASSES),
203      ("trans-stats", "gather trans statistics", TRANS_STATS),
204      ("asm-comments", "generate comments into the assembly (may change behavior)",
205       ASM_COMMENTS),
206      ("no-verify", "skip LLVM verification", NO_VERIFY),
207      ("borrowck-stats", "gather borrowck statistics",  BORROWCK_STATS),
208      ("no-landing-pads", "omit landing pads for unwinding",
209       NO_LANDING_PADS),
210      ("debug-llvm", "enable debug output from LLVM", DEBUG_LLVM),
211      ("show-span", "show spans for compiler debugging", SHOW_SPAN),
212      ("count-type-sizes", "count the sizes of aggregate types",
213       COUNT_TYPE_SIZES),
214      ("meta-stats", "gather metadata statistics", META_STATS),
215      ("no-opt", "do not optimize, even if -O is passed", NO_OPT),
216      ("print-link-args", "Print the arguments passed to the linker",
217       PRINT_LINK_ARGS),
218      ("gc", "Garbage collect shared data (experimental)", GC),
219      ("print-llvm-passes",
220       "Prints the llvm optimization passes being run",
221       PRINT_LLVM_PASSES),
222      ("lto", "Perform LLVM link-time optimizations", LTO),
223      ("ast-json", "Print the AST as JSON and halt", AST_JSON),
224      ("ast-json-noexpand", "Print the pre-expansion AST as JSON and halt", AST_JSON_NOEXPAND),
225      ("ls", "List the symbols defined by a library crate", LS),
226      ("save-analysis", "Write syntax and type analysis information \
227                         in addition to normal output", SAVE_ANALYSIS),
228      ("flowgraph-print-loans", "Include loan analysis data in \
229                        --pretty flowgraph output", FLOWGRAPH_PRINT_LOANS),
230      ("flowgraph-print-moves", "Include move analysis data in \
231                        --pretty flowgraph output", FLOWGRAPH_PRINT_MOVES),
232      ("flowgraph-print-assigns", "Include assignment analysis data in \
233                        --pretty flowgraph output", FLOWGRAPH_PRINT_ASSIGNS),
234      ("flowgraph-print-all", "Include all dataflow analysis data in \
235                        --pretty flowgraph output", FLOWGRAPH_PRINT_ALL))
236 }
237
238 /// Declare a macro that will define all CodegenOptions fields and parsers all
239 /// at once. The goal of this macro is to define an interface that can be
240 /// programmatically used by the option parser in order to initialize the struct
241 /// without hardcoding field names all over the place.
242 ///
243 /// The goal is to invoke this macro once with the correct fields, and then this
244 /// macro generates all necessary code. The main gotcha of this macro is the
245 /// cgsetters module which is a bunch of generated code to parse an option into
246 /// its respective field in the struct. There are a few hand-written parsers for
247 /// parsing specific types of values in this module.
248 macro_rules! cgoptions(
249     ($($opt:ident : $t:ty = ($init:expr, $parse:ident, $desc:expr)),* ,) =>
250 (
251     #[deriving(Clone)]
252     pub struct CodegenOptions { $(pub $opt: $t),* }
253
254     pub fn basic_codegen_options() -> CodegenOptions {
255         CodegenOptions { $($opt: $init),* }
256     }
257
258     pub type CodegenSetter = fn(&mut CodegenOptions, v: Option<&str>) -> bool;
259     pub static CG_OPTIONS: &'static [(&'static str, CodegenSetter,
260                                       &'static str)] =
261         &[ $( (stringify!($opt), cgsetters::$opt, $desc) ),* ];
262
263     mod cgsetters {
264         use super::CodegenOptions;
265
266         $(
267             pub fn $opt(cg: &mut CodegenOptions, v: Option<&str>) -> bool {
268                 $parse(&mut cg.$opt, v)
269             }
270         )*
271
272         fn parse_bool(slot: &mut bool, v: Option<&str>) -> bool {
273             match v {
274                 Some(..) => false,
275                 None => { *slot = true; true }
276             }
277         }
278
279         fn parse_opt_string(slot: &mut Option<String>, v: Option<&str>) -> bool {
280             match v {
281                 Some(s) => { *slot = Some(s.to_string()); true },
282                 None => false,
283             }
284         }
285
286         fn parse_string(slot: &mut String, v: Option<&str>) -> bool {
287             match v {
288                 Some(s) => { *slot = s.to_string(); true },
289                 None => false,
290             }
291         }
292
293         fn parse_list(slot: &mut Vec<String>, v: Option<&str>)
294                       -> bool {
295             match v {
296                 Some(s) => {
297                     for s in s.words() {
298                         slot.push(s.to_string());
299                     }
300                     true
301                 },
302                 None => false,
303             }
304         }
305
306     }
307 ) )
308
309 cgoptions!(
310     ar: Option<String> = (None, parse_opt_string,
311         "tool to assemble archives with"),
312     linker: Option<String> = (None, parse_opt_string,
313         "system linker to link outputs with"),
314     link_args: Vec<String> = (Vec::new(), parse_list,
315         "extra arguments to pass to the linker (space separated)"),
316     target_cpu: String = ("generic".to_string(), parse_string,
317         "select target processor (llc -mcpu=help for details)"),
318     target_feature: String = ("".to_string(), parse_string,
319         "target specific attributes (llc -mattr=help for details)"),
320     passes: Vec<String> = (Vec::new(), parse_list,
321         "a list of extra LLVM passes to run (space separated)"),
322     llvm_args: Vec<String> = (Vec::new(), parse_list,
323         "a list of arguments to pass to llvm (space separated)"),
324     save_temps: bool = (false, parse_bool,
325         "save all temporary output files during compilation"),
326     rpath: bool = (false, parse_bool,
327         "set rpath values in libs/exes"),
328     no_prepopulate_passes: bool = (false, parse_bool,
329         "don't pre-populate the pass manager with a list of passes"),
330     no_vectorize_loops: bool = (false, parse_bool,
331         "don't run the loop vectorization optimization passes"),
332     no_vectorize_slp: bool = (false, parse_bool,
333         "don't run LLVM's SLP vectorization pass"),
334     soft_float: bool = (false, parse_bool,
335         "generate software floating point library calls"),
336     prefer_dynamic: bool = (false, parse_bool,
337         "prefer dynamic linking to static linking"),
338     no_integrated_as: bool = (false, parse_bool,
339         "use an external assembler rather than LLVM's integrated one"),
340     no_redzone: bool = (false, parse_bool,
341         "disable the use of the redzone"),
342     relocation_model: String = ("pic".to_string(), parse_string,
343          "choose the relocation model to use (llc -relocation-model for details)"),
344     code_model: String = ("default".to_string(), parse_string,
345          "choose the code model to use (llc -code-model for details)"),
346     metadata: Vec<String> = (Vec::new(), parse_list,
347          "metadata to mangle symbol names with"),
348     extra_filename: String = ("".to_string(), parse_string,
349          "extra data to put in each output filename"),
350 )
351
352 pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions
353 {
354     let mut cg = basic_codegen_options();
355     for option in matches.opt_strs("C").move_iter() {
356         let mut iter = option.as_slice().splitn('=', 1);
357         let key = iter.next().unwrap();
358         let value = iter.next();
359         let option_to_lookup = key.replace("-", "_");
360         let mut found = false;
361         for &(candidate, setter, _) in CG_OPTIONS.iter() {
362             if option_to_lookup.as_slice() != candidate { continue }
363             if !setter(&mut cg, value) {
364                 match value {
365                     Some(..) => {
366                         early_error(format!("codegen option `{}` takes no \
367                                              value", key).as_slice())
368                     }
369                     None => {
370                         early_error(format!("codegen option `{0}` requires \
371                                              a value (-C {0}=<value>)",
372                                             key).as_slice())
373                     }
374                 }
375             }
376             found = true;
377             break;
378         }
379         if !found {
380             early_error(format!("unknown codegen option: `{}`",
381                                 key).as_slice());
382         }
383     }
384     return cg;
385 }
386
387 pub fn default_lib_output() -> CrateType {
388     CrateTypeRlib
389 }
390
391 pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
392     let tos = match sess.targ_cfg.os {
393         abi::OsWin32 =>   InternedString::new("win32"),
394         abi::OsMacos =>   InternedString::new("macos"),
395         abi::OsLinux =>   InternedString::new("linux"),
396         abi::OsAndroid => InternedString::new("android"),
397         abi::OsFreebsd => InternedString::new("freebsd"),
398         abi::OsiOS =>     InternedString::new("ios"),
399     };
400
401     // ARM is bi-endian, however using NDK seems to default
402     // to little-endian unless a flag is provided.
403     let (end,arch,wordsz) = match sess.targ_cfg.arch {
404         abi::X86 =>    ("little", "x86",    "32"),
405         abi::X86_64 => ("little", "x86_64", "64"),
406         abi::Arm =>    ("little", "arm",    "32"),
407         abi::Mips =>   ("big",    "mips",   "32"),
408         abi::Mipsel => ("little", "mipsel", "32")
409     };
410
411     let fam = match sess.targ_cfg.os {
412         abi::OsWin32 => InternedString::new("windows"),
413         _ => InternedString::new("unix")
414     };
415
416     let mk = attr::mk_name_value_item_str;
417     return vec!(// Target bindings.
418          attr::mk_word_item(fam.clone()),
419          mk(InternedString::new("target_os"), tos),
420          mk(InternedString::new("target_family"), fam),
421          mk(InternedString::new("target_arch"), InternedString::new(arch)),
422          mk(InternedString::new("target_endian"), InternedString::new(end)),
423          mk(InternedString::new("target_word_size"),
424             InternedString::new(wordsz))
425     );
426 }
427
428 pub fn append_configuration(cfg: &mut ast::CrateConfig,
429                             name: InternedString) {
430     if !cfg.iter().any(|mi| mi.name() == name) {
431         cfg.push(attr::mk_word_item(name))
432     }
433 }
434
435 pub fn build_configuration(sess: &Session) -> ast::CrateConfig {
436     // Combine the configuration requested by the session (command line) with
437     // some default and generated configuration items
438     let default_cfg = default_configuration(sess);
439     let mut user_cfg = sess.opts.cfg.clone();
440     // If the user wants a test runner, then add the test cfg
441     if sess.opts.test {
442         append_configuration(&mut user_cfg, InternedString::new("test"))
443     }
444     user_cfg.move_iter().collect::<Vec<_>>().append(default_cfg.as_slice())
445 }
446
447 pub fn get_os(triple: &str) -> Option<abi::Os> {
448     for &(name, os) in os_names.iter() {
449         if triple.contains(name) { return Some(os) }
450     }
451     None
452 }
453 static os_names : &'static [(&'static str, abi::Os)] = &[
454     ("mingw32", abi::OsWin32),
455     ("win32",   abi::OsWin32),
456     ("darwin",  abi::OsMacos),
457     ("android", abi::OsAndroid),
458     ("linux",   abi::OsLinux),
459     ("freebsd", abi::OsFreebsd),
460     ("ios",     abi::OsiOS)];
461
462 pub fn get_arch(triple: &str) -> Option<abi::Architecture> {
463     for &(arch, abi) in architecture_abis.iter() {
464         if triple.contains(arch) { return Some(abi) }
465     }
466     None
467 }
468 static architecture_abis : &'static [(&'static str, abi::Architecture)] = &[
469     ("i386",   abi::X86),
470     ("i486",   abi::X86),
471     ("i586",   abi::X86),
472     ("i686",   abi::X86),
473     ("i786",   abi::X86),
474
475     ("x86_64", abi::X86_64),
476
477     ("arm",    abi::Arm),
478     ("xscale", abi::Arm),
479     ("thumb",  abi::Arm),
480
481     ("mipsel", abi::Mipsel),
482     ("mips",   abi::Mips)];
483
484 pub fn build_target_config(sopts: &Options) -> Config {
485     let os = match get_os(sopts.target_triple.as_slice()) {
486       Some(os) => os,
487       None => early_error("unknown operating system")
488     };
489     let arch = match get_arch(sopts.target_triple.as_slice()) {
490       Some(arch) => arch,
491       None => {
492           early_error(format!("unknown architecture: {}",
493                               sopts.target_triple.as_slice()).as_slice())
494       }
495     };
496     let (int_type, uint_type) = match arch {
497       abi::X86 => (ast::TyI32, ast::TyU32),
498       abi::X86_64 => (ast::TyI64, ast::TyU64),
499       abi::Arm => (ast::TyI32, ast::TyU32),
500       abi::Mips => (ast::TyI32, ast::TyU32),
501       abi::Mipsel => (ast::TyI32, ast::TyU32)
502     };
503     let target_triple = sopts.target_triple.clone();
504     let target_strs = match arch {
505       abi::X86 => x86::get_target_strs(target_triple, os),
506       abi::X86_64 => x86_64::get_target_strs(target_triple, os),
507       abi::Arm => arm::get_target_strs(target_triple, os),
508       abi::Mips => mips::get_target_strs(target_triple, os),
509       abi::Mipsel => mipsel::get_target_strs(target_triple, os)
510     };
511     Config {
512         os: os,
513         arch: arch,
514         target_strs: target_strs,
515         int_type: int_type,
516         uint_type: uint_type,
517     }
518 }
519
520 // rustc command line options
521 pub fn optgroups() -> Vec<getopts::OptGroup> {
522     vec!(
523         optflag("h", "help", "Display this message"),
524         optmulti("", "cfg", "Configure the compilation environment", "SPEC"),
525         optmulti("L", "",   "Add a directory to the library search path", "PATH"),
526         optmulti("", "crate-type", "Comma separated list of types of crates
527                                     for the compiler to emit",
528                  "[bin|lib|rlib|dylib|staticlib]"),
529         optmulti("", "emit", "Comma separated list of types of output for the compiler to emit",
530                  "[asm|bc|ir|obj|link]"),
531         optopt("", "crate-name", "Specify the name of the crate being built",
532                "NAME"),
533         optflag("", "print-crate-name", "Output the crate name and exit"),
534         optflag("", "print-file-name", "Output the file(s) that would be written if compilation \
535               continued and exit"),
536         optflag("", "crate-file-name", "deprecated in favor of --print-file-name"),
537         optflag("g",  "",  "Equivalent to --debuginfo=2"),
538         optopt("",  "debuginfo",  "Emit DWARF debug info to the objects created:
539              0 = no debug info,
540              1 = line-tables only (for stacktraces and breakpoints),
541              2 = full debug info with variable and type information (same as -g)", "LEVEL"),
542         optflag("", "no-trans", "Run all passes except translation; no output"),
543         optflag("", "no-analysis",
544               "Parse and expand the source, but run no analysis and produce no output"),
545         optflag("O", "", "Equivalent to --opt-level=2"),
546         optopt("o", "", "Write output to <filename>", "FILENAME"),
547         optopt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"),
548         optopt( "",  "out-dir", "Write output to compiler-chosen filename in <dir>", "DIR"),
549         optflag("", "parse-only", "Parse only; do not compile, assemble, or link"),
550         optopt("", "explain", "Provide a detailed explanation of an error message", "OPT"),
551         optflagopt("", "pretty",
552                    "Pretty-print the input instead of compiling;
553                    valid types are: `normal` (un-annotated source),
554                    `expanded` (crates expanded),
555                    `typed` (crates expanded, with type annotations),
556                    `expanded,identified` (fully parenthesized, AST nodes with IDs), or
557                    `flowgraph=<nodeid>` (graphviz formatted flowgraph for node)",
558                  "TYPE"),
559         optflagopt("", "dep-info",
560                  "Output dependency info to <filename> after compiling, \
561                   in a format suitable for use by Makefiles", "FILENAME"),
562         optopt("", "sysroot", "Override the system root", "PATH"),
563         optflag("", "test", "Build a test harness"),
564         optopt("", "target", "Target triple cpu-manufacturer-kernel[-os]
565                             to compile for (see chapter 3.4 of http://www.sourceware.org/autobook/
566                             for details)", "TRIPLE"),
567         optmulti("W", "warn", "Set lint warnings", "OPT"),
568         optmulti("A", "allow", "Set lint allowed", "OPT"),
569         optmulti("D", "deny", "Set lint denied", "OPT"),
570         optmulti("F", "forbid", "Set lint forbidden", "OPT"),
571         optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
572         optmulti("Z", "", "Set internal debugging options", "FLAG"),
573         optflagopt("v", "version", "Print version info and exit", "verbose"),
574         optopt("", "color", "Configure coloring of output:
575             auto   = colorize, if output goes to a tty (default);
576             always = always colorize output;
577             never  = never colorize output", "auto|always|never"),
578         optmulti("", "extern", "Specify where an external rust library is located",
579                  "NAME=PATH"),
580     )
581 }
582
583
584 // Convert strings provided as --cfg [cfgspec] into a crate_cfg
585 fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
586     cfgspecs.move_iter().map(|s| {
587         parse::parse_meta_from_source_str("cfgspec".to_string(),
588                                           s.to_string(),
589                                           Vec::new(),
590                                           &parse::new_parse_sess())
591     }).collect::<ast::CrateConfig>()
592 }
593
594 pub fn build_session_options(matches: &getopts::Matches) -> Options {
595
596     let unparsed_crate_types = matches.opt_strs("crate-type");
597     let crate_types = parse_crate_types_from_list(unparsed_crate_types)
598         .unwrap_or_else(|e| early_error(e.as_slice()));
599
600     let parse_only = matches.opt_present("parse-only");
601     let no_trans = matches.opt_present("no-trans");
602     let no_analysis = matches.opt_present("no-analysis");
603
604     let mut lint_opts = vec!();
605     let mut describe_lints = false;
606
607     for &level in [lint::Allow, lint::Warn, lint::Deny, lint::Forbid].iter() {
608         for lint_name in matches.opt_strs(level.as_str()).move_iter() {
609             if lint_name.as_slice() == "help" {
610                 describe_lints = true;
611             } else {
612                 lint_opts.push((lint_name.replace("-", "_").into_string(), level));
613             }
614         }
615     }
616
617     let mut debugging_opts = 0;
618     let debug_flags = matches.opt_strs("Z");
619     let debug_map = debugging_opts_map();
620     for debug_flag in debug_flags.iter() {
621         let mut this_bit = 0;
622         for tuple in debug_map.iter() {
623             let (name, bit) = match *tuple { (ref a, _, b) => (a, b) };
624             if *name == debug_flag.as_slice() {
625                 this_bit = bit;
626                 break;
627             }
628         }
629         if this_bit == 0 {
630             early_error(format!("unknown debug flag: {}",
631                                 *debug_flag).as_slice())
632         }
633         debugging_opts |= this_bit;
634     }
635
636     if debugging_opts & DEBUG_LLVM != 0 {
637         unsafe { llvm::LLVMSetDebug(1); }
638     }
639
640     let mut output_types = Vec::new();
641     if !parse_only && !no_trans {
642         let unparsed_output_types = matches.opt_strs("emit");
643         for unparsed_output_type in unparsed_output_types.iter() {
644             for part in unparsed_output_type.as_slice().split(',') {
645                 let output_type = match part.as_slice() {
646                     "asm"  => link::OutputTypeAssembly,
647                     "ir"   => link::OutputTypeLlvmAssembly,
648                     "bc"   => link::OutputTypeBitcode,
649                     "obj"  => link::OutputTypeObject,
650                     "link" => link::OutputTypeExe,
651                     _ => {
652                         early_error(format!("unknown emission type: `{}`",
653                                             part).as_slice())
654                     }
655                 };
656                 output_types.push(output_type)
657             }
658         }
659     };
660     output_types.as_mut_slice().sort();
661     output_types.dedup();
662     if output_types.len() == 0 {
663         output_types.push(link::OutputTypeExe);
664     }
665
666     let sysroot_opt = matches.opt_str("sysroot").map(|m| Path::new(m));
667     let target = matches.opt_str("target").unwrap_or(
668         driver::host_triple().to_string());
669     let opt_level = {
670         if (debugging_opts & NO_OPT) != 0 {
671             No
672         } else if matches.opt_present("O") {
673             if matches.opt_present("opt-level") {
674                 early_error("-O and --opt-level both provided");
675             }
676             Default
677         } else if matches.opt_present("opt-level") {
678             match matches.opt_str("opt-level").as_ref().map(|s| s.as_slice()) {
679                 None      |
680                 Some("0") => No,
681                 Some("1") => Less,
682                 Some("2") => Default,
683                 Some("3") => Aggressive,
684                 Some(arg) => {
685                     early_error(format!("optimization level needs to be \
686                                          between 0-3 (instead was `{}`)",
687                                         arg).as_slice());
688                 }
689             }
690         } else {
691             No
692         }
693     };
694     let gc = debugging_opts & GC != 0;
695     let debuginfo = if matches.opt_present("g") {
696         if matches.opt_present("debuginfo") {
697             early_error("-g and --debuginfo both provided");
698         }
699         FullDebugInfo
700     } else if matches.opt_present("debuginfo") {
701         match matches.opt_str("debuginfo").as_ref().map(|s| s.as_slice()) {
702             Some("0") => NoDebugInfo,
703             Some("1") => LimitedDebugInfo,
704             None      |
705             Some("2") => FullDebugInfo,
706             Some(arg) => {
707                 early_error(format!("optimization level needs to be between \
708                                      0-3 (instead was `{}`)",
709                                     arg).as_slice());
710             }
711         }
712     } else {
713         NoDebugInfo
714     };
715
716     let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| {
717         Path::new(s.as_slice())
718     }).collect();
719
720     let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
721     let test = matches.opt_present("test");
722     let write_dependency_info = (matches.opt_present("dep-info"),
723                                  matches.opt_str("dep-info")
724                                         .map(|p| Path::new(p)));
725
726     let print_metas = (matches.opt_present("print-crate-name"),
727                        matches.opt_present("print-file-name") ||
728                        matches.opt_present("crate-file-name"));
729     if matches.opt_present("crate-file-name") {
730         early_warn("the --crate-file-name argument has been renamed to \
731                     --print-file-name");
732     }
733     let cg = build_codegen_options(matches);
734
735     let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
736         Some("auto")   => Auto,
737         Some("always") => Always,
738         Some("never")  => Never,
739
740         None => Auto,
741
742         Some(arg) => {
743             early_error(format!("argument for --color must be auto, always \
744                                  or never (instead was `{}`)",
745                                 arg).as_slice())
746         }
747     };
748
749     let mut externs = HashMap::new();
750     for arg in matches.opt_strs("extern").iter() {
751         let mut parts = arg.as_slice().splitn('=', 1);
752         let name = match parts.next() {
753             Some(s) => s,
754             None => early_error("--extern value must not be empty"),
755         };
756         let location = match parts.next() {
757             Some(s) => s,
758             None => early_error("--extern value must be of the format `foo=bar`"),
759         };
760         let locs = externs.find_or_insert(name.to_string(), Vec::new());
761         locs.push(location.to_string());
762     }
763
764     let crate_name = matches.opt_str("crate-name");
765
766     Options {
767         crate_types: crate_types,
768         gc: gc,
769         optimize: opt_level,
770         debuginfo: debuginfo,
771         lint_opts: lint_opts,
772         describe_lints: describe_lints,
773         output_types: output_types,
774         addl_lib_search_paths: RefCell::new(addl_lib_search_paths),
775         maybe_sysroot: sysroot_opt,
776         target_triple: target,
777         cfg: cfg,
778         test: test,
779         parse_only: parse_only,
780         no_trans: no_trans,
781         no_analysis: no_analysis,
782         debugging_opts: debugging_opts,
783         write_dependency_info: write_dependency_info,
784         print_metas: print_metas,
785         cg: cg,
786         color: color,
787         externs: externs,
788         crate_name: crate_name,
789         alt_std_name: None
790     }
791 }
792
793 pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateType>, String> {
794
795     let mut crate_types: Vec<CrateType> = Vec::new();
796     for unparsed_crate_type in list_list.iter() {
797         for part in unparsed_crate_type.as_slice().split(',') {
798             let new_part = match part {
799                 "lib"       => default_lib_output(),
800                 "rlib"      => CrateTypeRlib,
801                 "staticlib" => CrateTypeStaticlib,
802                 "dylib"     => CrateTypeDylib,
803                 "bin"       => CrateTypeExecutable,
804                 _ => {
805                     return Err(format!("unknown crate type: `{}`",
806                                        part));
807                 }
808             };
809             crate_types.push(new_part)
810         }
811     }
812
813     return Ok(crate_types);
814 }
815
816 impl fmt::Show for CrateType {
817     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
818         match *self {
819             CrateTypeExecutable => "bin".fmt(f),
820             CrateTypeDylib => "dylib".fmt(f),
821             CrateTypeRlib => "rlib".fmt(f),
822             CrateTypeStaticlib => "staticlib".fmt(f)
823         }
824     }
825 }
826
827 #[cfg(test)]
828 mod test {
829
830     use driver::config::{build_configuration, optgroups, build_session_options};
831     use driver::session::build_session;
832
833     use getopts::getopts;
834     use syntax::attr;
835     use syntax::attr::AttrMetaMethods;
836     use syntax::diagnostics;
837
838     // When the user supplies --test we should implicitly supply --cfg test
839     #[test]
840     fn test_switch_implies_cfg_test() {
841         let matches =
842             &match getopts(["--test".to_string()], optgroups().as_slice()) {
843               Ok(m) => m,
844               Err(f) => fail!("test_switch_implies_cfg_test: {}", f)
845             };
846         let registry = diagnostics::registry::Registry::new([]);
847         let sessopts = build_session_options(matches);
848         let sess = build_session(sessopts, None, registry);
849         let cfg = build_configuration(&sess);
850         assert!((attr::contains_name(cfg.as_slice(), "test")));
851     }
852
853     // When the user supplies --test and --cfg test, don't implicitly add
854     // another --cfg test
855     #[test]
856     fn test_switch_implies_cfg_test_unless_cfg_test() {
857         let matches =
858             &match getopts(["--test".to_string(), "--cfg=test".to_string()],
859                            optgroups().as_slice()) {
860               Ok(m) => m,
861               Err(f) => {
862                 fail!("test_switch_implies_cfg_test_unless_cfg_test: {}", f)
863               }
864             };
865         let registry = diagnostics::registry::Registry::new([]);
866         let sessopts = build_session_options(matches);
867         let sess = build_session(sessopts, None, registry);
868         let cfg = build_configuration(&sess);
869         let mut test_items = cfg.iter().filter(|m| m.name().equiv(&("test")));
870         assert!(test_items.next().is_some());
871         assert!(test_items.next().is_none());
872     }
873 }