1 // Copyright 2012-2013 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.
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.
12 use back::target_strs;
14 use driver::driver::host_triple;
16 use metadata::filesearch;
19 use util::nodemap::NodeMap;
21 use syntax::attr::AttrMetaMethods;
22 use syntax::ast::NodeId;
23 use syntax::ast::{IntTy, UintTy};
24 use syntax::codemap::Span;
25 use syntax::diagnostic;
26 use syntax::parse::ParseSess;
27 use syntax::{abi, ast, codemap};
30 use std::cell::{Cell, RefCell};
31 use collections::HashSet;
35 pub arch: abi::Architecture,
36 pub target_strs: target_strs::t,
38 pub uint_type: UintTy,
41 macro_rules! debugging_opts(
42 ([ $opt:ident ] $cnt:expr ) => (
43 pub static $opt: u64 = 1 << $cnt;
45 ([ $opt:ident, $($rest:ident),* ] $cnt:expr ) => (
46 pub static $opt: u64 = 1 << $cnt;
47 debugging_opts!([ $($rest),* ] $cnt + 1)
78 pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> {
79 vec!(("verbose", "in general, enable more debug printouts", VERBOSE),
80 ("time-passes", "measure time of each rustc pass", TIME_PASSES),
81 ("count-llvm-insns", "count where LLVM \
82 instrs originate", COUNT_LLVM_INSNS),
83 ("time-llvm-passes", "measure time of each LLVM pass",
85 ("trans-stats", "gather trans statistics", TRANS_STATS),
86 ("asm-comments", "generate comments into the assembly (may change behavior)",
88 ("no-verify", "skip LLVM verification", NO_VERIFY),
89 ("borrowck-stats", "gather borrowck statistics", BORROWCK_STATS),
90 ("no-landing-pads", "omit landing pads for unwinding",
92 ("debug-llvm", "enable debug output from LLVM", DEBUG_LLVM),
93 ("show-span", "show spans for compiler debugging", SHOW_SPAN),
94 ("count-type-sizes", "count the sizes of aggregate types",
96 ("meta-stats", "gather metadata statistics", META_STATS),
97 ("no-opt", "do not optimize, even if -O is passed", NO_OPT),
98 ("print-link-args", "Print the arguments passed to the linker",
100 ("gc", "Garbage collect shared data (experimental)", GC),
101 ("print-llvm-passes",
102 "Prints the llvm optimization passes being run",
104 ("lto", "Perform LLVM link-time optimizations", LTO),
105 ("ast-json", "Print the AST as JSON and halt", AST_JSON),
106 ("ast-json-noexpand", "Print the pre-expansion AST as JSON and halt", AST_JSON_NOEXPAND),
107 ("ls", "List the symbols defined by a library crate", LS))
110 #[deriving(Clone, Eq)]
118 #[deriving(Clone, Eq)]
119 pub enum DebugInfoLevel {
127 // The crate config requested for the session, which may be combined
128 // with additional crate configurations during the compile process
129 pub crate_types: Vec<CrateType> ,
132 pub optimize: OptLevel,
133 pub debuginfo: DebugInfoLevel,
134 pub lint_opts: Vec<(lint::Lint, lint::level)> ,
135 pub output_types: Vec<back::link::OutputType> ,
136 // This was mutable for rustpkg, which updates search paths based on the
137 // parsed code. It remains mutable in case its replacements wants to use
139 pub addl_lib_search_paths: RefCell<HashSet<Path>>,
140 pub maybe_sysroot: Option<Path>,
141 pub target_triple: ~str,
142 // User-specified cfg meta items. The compiler itself will add additional
143 // items to the crate config, and during parsing the entire crate config
144 // will be added to the crate AST node. This should not be used for
145 // anything except building the full crate config prior to parsing.
146 pub cfg: ast::CrateConfig,
148 pub parse_only: bool,
150 pub no_analysis: bool,
151 pub debugging_opts: u64,
152 /// Whether to write dependency files. It's (enabled, optional filename).
153 pub write_dependency_info: (bool, Option<Path>),
154 /// Crate id-related things to maybe print. It's (crate_id, crate_name, crate_file_name).
155 pub print_metas: (bool, bool, bool),
156 pub cg: CodegenOptions,
159 // The type of entry function, so
160 // users can have their own entry
161 // functions that don't start a
164 pub enum EntryFnType {
170 #[deriving(Eq, Ord, Clone, TotalOrd, TotalEq)]
179 pub targ_cfg: Config,
181 pub cstore: metadata::cstore::CStore,
182 pub parse_sess: ParseSess,
183 // For a library crate, this is always none
184 pub entry_fn: RefCell<Option<(NodeId, codemap::Span)>>,
185 pub entry_type: Cell<Option<EntryFnType>>,
186 pub macro_registrar_fn: Cell<Option<ast::NodeId>>,
187 pub default_sysroot: Option<Path>,
188 pub building_library: Cell<bool>,
189 // The name of the root source file of the crate, in the local file system. The path is always
190 // expected to be absolute. `None` means that there is no source file.
191 pub local_crate_source_file: Option<Path>,
192 pub working_dir: Path,
193 pub lints: RefCell<NodeMap<Vec<(lint::Lint, codemap::Span, ~str)>>>,
194 pub node_id: Cell<ast::NodeId>,
195 pub crate_types: RefCell<Vec<CrateType>>,
196 pub features: front::feature_gate::Features,
198 /// The maximum recursion limit for potentially infinitely recursive
199 /// operations such as auto-dereference and monomorphization.
200 pub recursion_limit: Cell<uint>,
204 pub fn span_fatal(&self, sp: Span, msg: &str) -> ! {
205 self.diagnostic().span_fatal(sp, msg)
207 pub fn fatal(&self, msg: &str) -> ! {
208 self.diagnostic().handler().fatal(msg)
210 pub fn span_err(&self, sp: Span, msg: &str) {
211 self.diagnostic().span_err(sp, msg)
213 pub fn err(&self, msg: &str) {
214 self.diagnostic().handler().err(msg)
216 pub fn err_count(&self) -> uint {
217 self.diagnostic().handler().err_count()
219 pub fn has_errors(&self) -> bool {
220 self.diagnostic().handler().has_errors()
222 pub fn abort_if_errors(&self) {
223 self.diagnostic().handler().abort_if_errors()
225 pub fn span_warn(&self, sp: Span, msg: &str) {
226 self.diagnostic().span_warn(sp, msg)
228 pub fn warn(&self, msg: &str) {
229 self.diagnostic().handler().warn(msg)
231 pub fn span_note(&self, sp: Span, msg: &str) {
232 self.diagnostic().span_note(sp, msg)
234 pub fn span_end_note(&self, sp: Span, msg: &str) {
235 self.diagnostic().span_end_note(sp, msg)
237 pub fn fileline_note(&self, sp: Span, msg: &str) {
238 self.diagnostic().fileline_note(sp, msg)
240 pub fn note(&self, msg: &str) {
241 self.diagnostic().handler().note(msg)
243 pub fn span_bug(&self, sp: Span, msg: &str) -> ! {
244 self.diagnostic().span_bug(sp, msg)
246 pub fn bug(&self, msg: &str) -> ! {
247 self.diagnostic().handler().bug(msg)
249 pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
250 self.diagnostic().span_unimpl(sp, msg)
252 pub fn unimpl(&self, msg: &str) -> ! {
253 self.diagnostic().handler().unimpl(msg)
255 pub fn add_lint(&self,
260 let mut lints = self.lints.borrow_mut();
261 match lints.find_mut(&id) {
262 Some(arr) => { arr.push((lint, sp, msg)); return; }
265 lints.insert(id, vec!((lint, sp, msg)));
267 pub fn next_node_id(&self) -> ast::NodeId {
268 self.reserve_node_ids(1)
270 pub fn reserve_node_ids(&self, count: ast::NodeId) -> ast::NodeId {
271 let v = self.node_id.get();
273 match v.checked_add(&count) {
274 Some(next) => { self.node_id.set(next); }
275 None => self.bug("Input too large, ran out of node ids!")
280 pub fn diagnostic<'a>(&'a self) -> &'a diagnostic::SpanHandler {
281 &self.parse_sess.span_diagnostic
283 pub fn debugging_opt(&self, opt: u64) -> bool {
284 (self.opts.debugging_opts & opt) != 0
286 pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
287 &self.parse_sess.span_diagnostic.cm
289 // This exists to help with refactoring to eliminate impossible
291 pub fn impossible_case(&self, sp: Span, msg: &str) -> ! {
292 self.span_bug(sp, format!("impossible case reached: {}", msg));
294 pub fn verbose(&self) -> bool { self.debugging_opt(VERBOSE) }
295 pub fn time_passes(&self) -> bool { self.debugging_opt(TIME_PASSES) }
296 pub fn count_llvm_insns(&self) -> bool {
297 self.debugging_opt(COUNT_LLVM_INSNS)
299 pub fn count_type_sizes(&self) -> bool {
300 self.debugging_opt(COUNT_TYPE_SIZES)
302 pub fn time_llvm_passes(&self) -> bool {
303 self.debugging_opt(TIME_LLVM_PASSES)
305 pub fn trans_stats(&self) -> bool { self.debugging_opt(TRANS_STATS) }
306 pub fn meta_stats(&self) -> bool { self.debugging_opt(META_STATS) }
307 pub fn asm_comments(&self) -> bool { self.debugging_opt(ASM_COMMENTS) }
308 pub fn no_verify(&self) -> bool { self.debugging_opt(NO_VERIFY) }
309 pub fn borrowck_stats(&self) -> bool { self.debugging_opt(BORROWCK_STATS) }
310 pub fn print_llvm_passes(&self) -> bool {
311 self.debugging_opt(PRINT_LLVM_PASSES)
313 pub fn lto(&self) -> bool {
314 self.debugging_opt(LTO)
316 pub fn no_landing_pads(&self) -> bool {
317 self.debugging_opt(NO_LANDING_PADS)
319 pub fn show_span(&self) -> bool {
320 self.debugging_opt(SHOW_SPAN)
322 pub fn sysroot<'a>(&'a self) -> &'a Path {
323 match self.opts.maybe_sysroot {
324 Some (ref sysroot) => sysroot,
325 None => self.default_sysroot.as_ref()
326 .expect("missing sysroot and default_sysroot in Session")
329 pub fn target_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
330 filesearch::FileSearch::new(
332 self.opts.target_triple,
333 &self.opts.addl_lib_search_paths)
335 pub fn host_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
336 filesearch::FileSearch::new(
339 &self.opts.addl_lib_search_paths)
343 /// Some reasonable defaults
344 pub fn basic_options() -> Options {
346 crate_types: Vec::new(),
349 debuginfo: NoDebugInfo,
350 lint_opts: Vec::new(),
351 output_types: Vec::new(),
352 addl_lib_search_paths: RefCell::new(HashSet::new()),
354 target_triple: host_triple().to_owned(),
361 write_dependency_info: (false, None),
362 print_metas: (false, false, false),
363 cg: basic_codegen_options(),
367 /// Declare a macro that will define all CodegenOptions fields and parsers all
368 /// at once. The goal of this macro is to define an interface that can be
369 /// programmatically used by the option parser in order to initialize the struct
370 /// without hardcoding field names all over the place.
372 /// The goal is to invoke this macro once with the correct fields, and then this
373 /// macro generates all necessary code. The main gotcha of this macro is the
374 /// cgsetters module which is a bunch of generated code to parse an option into
375 /// its respective field in the struct. There are a few hand-written parsers for
376 /// parsing specific types of values in this module.
377 macro_rules! cgoptions(
378 ($($opt:ident : $t:ty = ($init:expr, $parse:ident, $desc:expr)),* ,) =>
381 pub struct CodegenOptions { $(pub $opt: $t),* }
383 pub fn basic_codegen_options() -> CodegenOptions {
384 CodegenOptions { $($opt: $init),* }
387 pub type CodegenSetter = fn(&mut CodegenOptions, v: Option<&str>) -> bool;
388 pub static CG_OPTIONS: &'static [(&'static str, CodegenSetter,
390 &[ $( (stringify!($opt), cgsetters::$opt, $desc) ),* ];
393 use super::CodegenOptions;
396 pub fn $opt(cg: &mut CodegenOptions, v: Option<&str>) -> bool {
397 $parse(&mut cg.$opt, v)
401 fn parse_bool(slot: &mut bool, v: Option<&str>) -> bool {
404 None => { *slot = true; true }
408 fn parse_opt_string(slot: &mut Option<~str>, v: Option<&str>) -> bool {
410 Some(s) => { *slot = Some(s.to_owned()); true },
415 fn parse_string(slot: &mut ~str, v: Option<&str>) -> bool {
417 Some(s) => { *slot = s.to_owned(); true },
422 fn parse_list(slot: &mut Vec<~str>, v: Option<&str>)
427 slot.push(s.to_owned());
439 ar: Option<~str> = (None, parse_opt_string,
440 "tool to assemble archives with"),
441 linker: Option<~str> = (None, parse_opt_string,
442 "system linker to link outputs with"),
443 link_args: Vec<~str> = (Vec::new(), parse_list,
444 "extra arguments to pass to the linker (space separated)"),
445 target_cpu: ~str = ("generic".to_owned(), parse_string,
446 "select target processor (llc -mcpu=help for details)"),
447 target_feature: ~str = ("".to_owned(), parse_string,
448 "target specific attributes (llc -mattr=help for details)"),
449 passes: Vec<~str> = (Vec::new(), parse_list,
450 "a list of extra LLVM passes to run (space separated)"),
451 llvm_args: Vec<~str> = (Vec::new(), parse_list,
452 "a list of arguments to pass to llvm (space separated)"),
453 save_temps: bool = (false, parse_bool,
454 "save all temporary output files during compilation"),
455 android_cross_path: Option<~str> = (None, parse_opt_string,
456 "the path to the Android NDK"),
457 no_rpath: bool = (false, parse_bool,
458 "disables setting the rpath in libs/exes"),
459 no_prepopulate_passes: bool = (false, parse_bool,
460 "don't pre-populate the pass manager with a list of passes"),
461 no_vectorize_loops: bool = (false, parse_bool,
462 "don't run the loop vectorization optimization passes"),
463 no_vectorize_slp: bool = (false, parse_bool,
464 "don't run LLVM's SLP vectorization pass"),
465 soft_float: bool = (false, parse_bool,
466 "generate software floating point library calls"),
467 prefer_dynamic: bool = (false, parse_bool,
468 "prefer dynamic linking to static linking"),
469 no_integrated_as: bool = (false, parse_bool,
470 "use an external assembler rather than LLVM's integrated one"),
471 relocation_model: ~str = ("pic".to_owned(), parse_string,
472 "choose the relocation model to use (llc -relocation-model for details)"),
475 // Seems out of place, but it uses session, so I'm putting it here
476 pub fn expect<T:Clone>(sess: &Session, opt: Option<T>, msg: || -> ~str) -> T {
477 diagnostic::expect(sess.diagnostic(), opt, msg)
480 pub fn building_library(options: &Options, krate: &ast::Crate) -> bool {
481 if options.test { return false }
482 for output in options.crate_types.iter() {
484 CrateTypeExecutable => {}
485 CrateTypeStaticlib | CrateTypeDylib | CrateTypeRlib => return true
488 match syntax::attr::first_attr_value_str_by_name(krate.attrs.as_slice(),
492 s.equiv(&("rlib")) ||
493 s.equiv(&("dylib")) ||
494 s.equiv(&("staticlib"))
500 pub fn default_lib_output() -> CrateType {
504 pub fn collect_crate_types(session: &Session,
505 attrs: &[ast::Attribute]) -> Vec<CrateType> {
506 // If we're generating a test executable, then ignore all other output
507 // styles at all other locations
508 if session.opts.test {
509 return vec!(CrateTypeExecutable)
512 // Only check command line flags if present. If no types are specified by
513 // command line, then reuse the empty `base` Vec to hold the types that
514 // will be found in crate attributes.
515 let mut base = session.opts.crate_types.clone();
519 let iter = attrs.iter().filter_map(|a| {
520 if a.name().equiv(&("crate_type")) {
521 match a.value_str() {
522 Some(ref n) if n.equiv(&("rlib")) => Some(CrateTypeRlib),
523 Some(ref n) if n.equiv(&("dylib")) => Some(CrateTypeDylib),
524 Some(ref n) if n.equiv(&("lib")) => {
525 Some(default_lib_output())
527 Some(ref n) if n.equiv(&("staticlib")) => {
528 Some(CrateTypeStaticlib)
530 Some(ref n) if n.equiv(&("bin")) => Some(CrateTypeExecutable),
532 session.add_lint(lint::UnknownCrateType,
535 "invalid `crate_type` value".to_owned());
539 session.add_lint(lint::UnknownCrateType, ast::CRATE_NODE_ID,
540 a.span, "`crate_type` requires a value".to_owned());
550 base.push(CrateTypeExecutable);
552 base.as_mut_slice().sort();
558 pub fn sess_os_to_meta_os(os: abi::Os) -> metadata::loader::Os {
559 use metadata::loader;
562 abi::OsWin32 => loader::OsWin32,
563 abi::OsLinux => loader::OsLinux,
564 abi::OsAndroid => loader::OsAndroid,
565 abi::OsMacos => loader::OsMacos,
566 abi::OsFreebsd => loader::OsFreebsd