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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
extern crate cargo_metadata;
#[macro_use]
extern crate error_chain;
extern crate fs2;
#[cfg(any(
    all(target_os = "linux", not(target_env = "musl")),
    target_os = "macos"
))]
extern crate libc;
extern crate rustc_version;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
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 config;
mod errors;
mod extensions;
mod flock;
mod rustc;
mod sysroot;
mod util;
mod xargo;

pub use cli::Args;

// 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.
#[derive(Debug)]
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(())
    }

    /// Returns the condensed target triple (removes any `.json` extension and path components).
    fn triple(&self) -> &str {
        match *self {
            CompilationMode::Cross(ref target) => target.triple(),
            CompilationMode::Native(ref triple) => triple,
        }
    }

    /// Returns the original target triple passed to xargo (perhaps with `.json` extension).
    fn orig_triple(&self) -> &str {
        match *self {
            CompilationMode::Cross(ref target) => target.orig_triple(),
            CompilationMode::Native(ref triple) => triple,
        }
    }

    fn is_native(&self) -> bool {
        match *self {
            CompilationMode::Native(_) => true,
            _ => false,
        }
    }
}

pub fn main_common(command_name: &str) {
    fn show_backtrace() -> bool {
        env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
    }

    match run(command_name) {
        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(Some(status)) => {
            if !status.success() {
                process::exit(status.code().unwrap_or(1))
            }
        }
        Ok(None) => {}
    }
}

fn run(command_name: &str) -> Result<Option<ExitStatus>> {
    use cli::Command;

    let (command, args) = cli::args(command_name)?;
    match command {
        Command::Build => Ok(Some(build(args, command_name)?)),
        Command::Help => {
            print!(include_str!("help.txt"), command_name = command_name);
            Ok(None)
        }
        Command::Version => {
            writeln!(
                io::stdout(),
                concat!("cargo-xbuild ", env!("CARGO_PKG_VERSION"), "{}"),
                include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
            )
            .unwrap();
            Ok(None)
        }
    }
}

pub fn build(args: cli::Args, command_name: &str) -> Result<ExitStatus> {
    let verbose = args.verbose();
    let quiet = args.quiet();
    let meta = rustc::version();
    let cd = CurrentDirectory::get()?;
    let config = cargo::config()?;

    let mut cmd = cargo_metadata::MetadataCommand::new();
    if let Some(manifest_path) = args.manifest_path() {
        cmd.manifest_path(manifest_path);
    }
    let metadata = cmd
        .exec()
        .expect("cargo metadata invocation failed");
    let root = Path::new(&metadata.workspace_root);
    let crate_config = config::Config::from_metadata(&metadata)
        .map_err(|_| "parsing package.metadata.cargo-xbuild section failed")?;

    // 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 => {
            bail!(
                "The sysroot can't be built for the {:?} channel. \
                 Switch to nightly.",
                meta.channel
            );
        }
    };

    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(CompilationMode::Native(_)) = cmode {
        eprintln!(
            "WARNING: You're currently building for the host system. This is likely an \
            error and will cause build scripts of dependencies to break.\n\n\

            To build for the target system either pass a `--target` argument or \
            set the build.target configuration key in a `.cargo/config` file.\n",
        );
    }

    if let Some(cmode) = cmode {
        let home = xargo::home(root, &crate_config, quiet)?;
        let rustflags = cargo::rustflags(config.as_ref(), cmode.triple())?;

        sysroot::update(
            &cmode,
            &home,
            &root,
            &crate_config,
            &rustflags,
            &meta,
            &src,
            &sysroot,
            verbose,
        )?;
        return xargo::run(
            &args,
            &cmode,
            rustflags,
            &home,
            &meta,
            command_name,
            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
    }
}