-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.rs
208 lines (179 loc) · 5.62 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#![deny(warnings)]
#[macro_use]
extern crate error_chain;
extern crate fs2;
extern crate libc;
extern crate rustc_version;
extern crate serde_json;
extern crate tempdir;
extern crate toml;
extern crate walkdir;
use std::hash::{Hash, Hasher};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::{env, io, process};
use rustc_version::Channel;
use errors::*;
use rustc::Target;
mod cargo;
mod cli;
mod errors;
mod extensions;
mod flock;
mod rustc;
mod sysroot;
mod util;
mod xargo;
// We use a different sysroot for Native compilation to avoid file locking
//
// Cross compilation requires `lib/rustlib/$HOST` to match `rustc`'s sysroot,
// whereas Native compilation wants to use a custom `lib/rustlib/$HOST`. If each
// mode has its own sysroot then we avoid sharing that directory and thus file
// locking it.
pub enum CompilationMode {
Cross(Target),
Native(String),
}
impl CompilationMode {
fn hash<H>(&self, hasher: &mut H) -> Result<()>
where H: Hasher
{
match *self {
CompilationMode::Cross(ref target) => target.hash(hasher)?,
CompilationMode::Native(ref triple) => triple.hash(hasher),
}
Ok(())
}
fn triple(&self) -> &str {
match *self {
CompilationMode::Cross(ref target) => target.triple(),
CompilationMode::Native(ref triple) => triple,
}
}
fn is_native(&self) -> bool {
match *self {
CompilationMode::Native(_) => true,
_ => false,
}
}
}
pub fn main() {
fn show_backtrace() -> bool {
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
}
match run() {
Err(e) => {
let stderr = io::stderr();
let mut stderr = stderr.lock();
writeln!(stderr, "error: {}", e).ok();
for e in e.iter().skip(1) {
writeln!(stderr, "caused by: {}", e).ok();
}
if show_backtrace() {
if let Some(backtrace) = e.backtrace() {
writeln!(stderr, "{:?}", backtrace).ok();
}
} else {
writeln!(stderr,
"note: run with `RUST_BACKTRACE=1` for a backtrace")
.ok();
}
process::exit(1)
}
Ok(status) => {
if !status.success() {
process::exit(status.code().unwrap_or(1))
}
}
}
}
fn run() -> Result<ExitStatus> {
let args = cli::args();
let verbose = args.verbose();
let meta = rustc::version();
if let Some(sc) = args.subcommand() {
if !sc.needs_sysroot() {
return cargo::run(&args, verbose);
}
} else if args.version() {
writeln!(io::stderr(),
concat!("xargo ", env!("CARGO_PKG_VERSION"), "{}"),
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
.ok();
return cargo::run(&args, verbose);
}
let cd = CurrentDirectory::get()?;
let config = cargo::config()?;
if let Some(root) = cargo::root()? {
// We can't build sysroot with stable or beta due to unstable features
let sysroot = rustc::sysroot(verbose)?;
let src = match meta.channel {
Channel::Dev => rustc::Src::from_env().ok_or(
"The XARGO_RUST_SRC env variable must be set and point to the \
Rust source directory when working with the 'dev' channel")?,
Channel::Nightly => {
if let Some(src) = rustc::Src::from_env() {
src
} else {
sysroot.src()?
}
}
Channel::Stable | Channel::Beta => {
return cargo::run(&args, verbose)
}
};
let cmode = if let Some(triple) = args.target() {
if triple == meta.host {
Some(CompilationMode::Native(meta.host.clone()))
} else {
Target::new(triple, &cd, verbose)?.map(CompilationMode::Cross)
}
} else {
if let Some(ref config) = config {
if let Some(triple) = config.target()? {
Target::new(triple, &cd, verbose)
?
.map(CompilationMode::Cross)
} else {
Some(CompilationMode::Native(meta.host.clone()))
}
} else {
Some(CompilationMode::Native(meta.host.clone()))
}
};
if let Some(cmode) = cmode {
let home = xargo::home(&cmode)?;
let rustflags = cargo::rustflags(config.as_ref(), cmode.triple())?;
sysroot::update(&cmode,
&home,
&root,
&rustflags,
&meta,
&src,
&sysroot,
verbose)?;
return xargo::run(&args,
&cmode,
rustflags,
&home,
&meta,
config.as_ref(),
verbose);
}
}
cargo::run(&args, verbose)
}
pub struct CurrentDirectory {
path: PathBuf,
}
impl CurrentDirectory {
fn get() -> Result<CurrentDirectory> {
env::current_dir()
.chain_err(|| "couldn't get the current directory")
.map(|cd| CurrentDirectory { path: cd })
}
fn path(&self) -> &Path {
&self.path
}
}