]> git.lizzy.rs Git - rust.git/blob - src/librustc_back/lib.rs
7ec9b77af4bfc9b96b3a51779d18e645d54733a3
[rust.git] / src / librustc_back / lib.rs
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.
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 //! Some stuff used by rustc that doesn't have many dependencies
12 //!
13 //! Originally extracted from rustc::back, which was nominally the
14 //! compiler 'backend', though LLVM is rustc's backend, so rustc_back
15 //! is really just odds-and-ends relating to code gen and linking.
16 //! This crate mostly exists to make rustc smaller, so we might put
17 //! more 'stuff' here in the future.  It does not have a dependency on
18 //! rustc_llvm.
19 //!
20 //! FIXME: Split this into two crates: one that has deps on syntax, and
21 //! one that doesn't; the one that doesn't might get decent parallel
22 //! build speedups.
23
24 #![crate_name = "rustc_back"]
25 #![crate_type = "dylib"]
26 #![crate_type = "rlib"]
27 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
28       html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
29       html_root_url = "https://doc.rust-lang.org/nightly/")]
30 #![deny(warnings)]
31
32 #![feature(box_syntax)]
33 #![feature(const_fn)]
34 #![feature(libc)]
35 #![feature(rand)]
36 #![cfg_attr(test, feature(rand))]
37
38 extern crate syntax;
39 extern crate libc;
40 extern crate serialize;
41 #[macro_use] extern crate log;
42
43 extern crate serialize as rustc_serialize; // used by deriving
44
45 pub mod tempdir;
46 pub mod target;
47 pub mod slice;
48 pub mod dynamic_lib;
49
50 use serialize::json::{Json, ToJson};
51
52 macro_rules! linker_flavor {
53     ($(($variant:ident, $string:expr),)+) => {
54         #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash,
55                  RustcEncodable, RustcDecodable)]
56         pub enum LinkerFlavor {
57             $($variant,)+
58         }
59
60         impl LinkerFlavor {
61             pub const fn one_of() -> &'static str {
62                 concat!("one of: ", $($string, " ",)+)
63             }
64
65             pub fn from_str(s: &str) -> Option<Self> {
66                 Some(match s {
67                     $($string => LinkerFlavor::$variant,)+
68                     _ => return None,
69                 })
70             }
71
72             pub fn desc(&self) -> &str {
73                 match *self {
74                     $(LinkerFlavor::$variant => $string,)+
75                 }
76             }
77         }
78
79         impl ToJson for LinkerFlavor {
80             fn to_json(&self) -> Json {
81                 self.desc().to_json()
82             }
83         }
84     }
85 }
86
87 linker_flavor! {
88     (Em, "em"),
89     (Gcc, "gcc"),
90     (Ld, "ld"),
91     (Msvc, "msvc"),
92 }
93
94 #[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
95 pub enum PanicStrategy {
96     Unwind,
97     Abort,
98 }
99
100 impl PanicStrategy {
101     pub fn desc(&self) -> &str {
102         match *self {
103             PanicStrategy::Unwind => "unwind",
104             PanicStrategy::Abort => "abort",
105         }
106     }
107 }
108
109 impl ToJson for PanicStrategy {
110     fn to_json(&self) -> Json {
111         match *self {
112             PanicStrategy::Abort => "abort".to_json(),
113             PanicStrategy::Unwind => "unwind".to_json(),
114         }
115     }
116 }
117
118 #[derive(Clone, Copy, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
119 pub enum RelroLevel {
120     Full,
121     Partial,
122     Off,
123 }
124
125 impl RelroLevel {
126     pub fn desc(&self) -> &str {
127         match *self {
128             RelroLevel::Full => "full",
129             RelroLevel::Partial => "partial",
130             RelroLevel::Off => "off",
131         }
132     }
133 }
134
135 impl ToJson for RelroLevel {
136     fn to_json(&self) -> Json {
137         match *self {
138             RelroLevel::Full => "full".to_json(),
139             RelroLevel::Partial => "partial".to_json(),
140             RelroLevel::Off => "off".to_json(),
141         }
142     }
143 }