Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Nov 18, 2016
1 parent c8dec07 commit 0d82b65
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 23 deletions.
11 changes: 8 additions & 3 deletions src/rustup-cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ pub fn main() -> Result<()> {

// Build command args now while we know whether or not to skip arg 1.
let cmd_args: Vec<_> = if toolchain.is_none() {
env::args_os().collect()
env::args_os().skip(1).collect()
} else {
env::args_os().take(1).chain(env::args_os().skip(2)).collect()
env::args_os().skip(2).collect()
};

let cfg = try!(set_globals(false));
try!(cfg.check_metadata_version());
println!("PPATH {}", ::std::env::var("PATH").unwrap_or("X".to_string()));
println!("PRUSTUP_TOOLCHAIN {}", ::std::env::var("RUSTUP_TOOLCHAIN").unwrap_or("X".to_string()));
println!("PCARGO_HOME {}", ::std::env::var("CARGO_HOME").unwrap_or("X".to_string()));
println!("arg0 {}", arg0);
println!("args[0] {}", cmd_args[0].to_string_lossy());
try!(direct_proxy(&cfg, arg0, toolchain, &cmd_args));

Ok(())
Expand All @@ -51,6 +56,6 @@ fn direct_proxy(cfg: &Cfg, arg0: &str, toolchain: Option<&str>, args: &[OsString
None => try!(cfg.create_command_for_dir(&try!(utils::current_dir()), arg0)),
Some(tc) => try!(cfg.create_command_for_toolchain(tc, arg0)),
};
Ok(try!(run_command_for_dir(cmd, &args, &cfg)))
Ok(try!(run_command_for_dir(cmd, arg0, args, &cfg)))
}

11 changes: 10 additions & 1 deletion src/rustup-mock/src/mock_bin_src.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use std::process::Command;
use std::io::{self, BufWriter, Write};
use std::env::consts::EXE_SUFFIX;

fn main() {
let args: Vec<_> = ::std::env::args().collect();
let env = ::std::env::var("PATH").unwrap();
println!("mock-bin self-exe {}", ::std::env::current_exe().unwrap().display());
println!("mock-bin cwd {}", ::std::env::current_dir().unwrap().display());
println!("mock-bin arg0 {}", args.get(0).unwrap());
println!("mock-bin arg1 {}", args.get(1).unwrap());
println!("mock-bin PATH {}", env);
let args: Vec<_> = ::std::env::args().collect();
if args.get(1) == Some(&"--version".to_string()) {
println!("%EXAMPLE_VERSION% (%EXAMPLE_VERSION_HASH%)");
Expand All @@ -18,7 +26,8 @@ fn main() {
// Used by the fallback_cargo_calls_correct_rustc test. Tests that
// the environment has been set up right such that invoking rustc
// will actually invoke the wrapper
Command::new("rustc").arg("--version").status().unwrap();
let rustc = &format!("rustc{}", EXE_SUFFIX);
Command::new(rustc).arg("--version").status().unwrap();
} else {
panic!("bad mock proxy commandline");
}
Expand Down
31 changes: 15 additions & 16 deletions src/rustup/command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{self, Write, BufRead, BufReader, Seek, SeekFrom};
use std::path::PathBuf;
use std::process::{self, Command, Stdio};
use std::time::Instant;
use regex::Regex;
Expand All @@ -16,21 +14,19 @@ use telemetry::{Telemetry, TelemetryEvent};


pub fn run_command_for_dir<S: AsRef<OsStr>>(cmd: Command,
arg0: &S,
args: &[S],
cfg: &Cfg) -> Result<()> {
let arg0 = env::args().next().map(PathBuf::from);
let arg0 = arg0.as_ref()
.and_then(|a| a.file_name())
.and_then(|a| a.to_str());
let arg0 = try!(arg0.ok_or(ErrorKind::NoExeName));
if (arg0 == "rustc" || arg0 == "rustc.exe") && try!(cfg.telemetry_enabled()) {
return telemetry_rustc(cmd, args, cfg);
if (arg0.as_ref() == "rustc" || arg0.as_ref() == "rustc.exe") && try!(cfg.telemetry_enabled()) {
return telemetry_rustc(cmd, arg0, args, cfg);
}

run_command_for_dir_without_telemetry(cmd, args)
run_command_for_dir_without_telemetry(cmd, arg0, args)
}

fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> {
fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command,
arg0: &S,
args: &[S], cfg: &Cfg) -> Result<()> {
#[cfg(unix)]
fn file_as_stdio(file: &File) -> Stdio {
use std::os::unix::io::{AsRawFd, FromRawFd};
Expand All @@ -45,7 +41,7 @@ fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) ->

let now = Instant::now();

cmd.args(&args[1..]);
cmd.args(args);

let has_color_args = args.iter().any(|e| {
let e = e.as_ref().to_str().unwrap_or("");
Expand Down Expand Up @@ -130,19 +126,22 @@ fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) ->
});

Err(e).chain_err(|| rustup_utils::ErrorKind::RunningCommand {
name: args[0].as_ref().to_owned(),
name: arg0.as_ref().to_owned(),
})
},
}
}

fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(mut cmd: Command, args: &[S]) -> Result<()> {
cmd.args(&args[1..]);
fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(
mut cmd: Command, arg0: &S, args: &[S]) -> Result<()>
{
cmd.args(&args);

// FIXME rust-lang/rust#32254. It's not clear to me
// when and why this is needed.
cmd.stdin(process::Stdio::inherit());

println!("ARG0 {}", arg0.as_ref().to_string_lossy());
match cmd.status() {
Ok(status) => {
// Ensure correct exit code is returned
Expand All @@ -151,7 +150,7 @@ fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(mut cmd: Command, args
}
Err(e) => {
Err(e).chain_err(|| rustup_utils::ErrorKind::RunningCommand {
name: args[0].as_ref().to_owned(),
name: arg0.as_ref().to_owned(),
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/rustup/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,15 @@ impl Cfg {
}

pub fn create_command_for_toolchain(&self, toolchain: &str, binary: &str) -> Result<Command> {
println!("AA");
let ref toolchain = try!(self.get_toolchain(toolchain, false));
println!("BB");

if let Some(cmd) = try!(self.maybe_do_cargo_fallback(toolchain, binary)) {
println!("CC");
Ok(cmd)
} else {
println!("DD");
toolchain.create_command(binary)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/rustup/env_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn prepend_path(name: &str, value: &Path, cmd: &mut Command) {
parts.extend(env::split_paths(v));
}
let new_value = env::join_paths(parts).unwrap_or_else(|_| OsString::from(value));
println!("PATH {}", new_value.to_string_lossy());

cmd.env(name, new_value);
}
Expand Down
15 changes: 14 additions & 1 deletion src/rustup/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ impl<'a> Toolchain<'a> {
&self.path
}
pub fn exists(&self) -> bool {
utils::is_directory(&self.path)
println!("exists? {}", self.path.display());
use std::fs;
utils::is_directory(&self.path) || fs::symlink_metadata(&self.path).unwrap().file_type().is_symlink()
}
pub fn verify(&self) -> Result<()> {
Ok(try!(utils::assert_is_directory(&self.path)))
Expand Down Expand Up @@ -277,6 +279,7 @@ impl<'a> Toolchain<'a> {
}

let bin_path = &self.path.join("bin").join(binary.as_ref());
println!("BINPATH {}", bin_path.display());
let mut cmd = Command::new(bin_path);
self.set_env(&mut cmd);
Ok(cmd)
Expand All @@ -286,6 +289,14 @@ impl<'a> Toolchain<'a> {
// to give custom toolchains access to cargo
pub fn create_fallback_command<T: AsRef<OsStr>>(&self, binary: T,
primary_toolchain: &Toolchain) -> Result<Command> {
if !self.exists() {
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
}
if !primary_toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
}
println!("RUNNING {}", binary.as_ref().to_string_lossy());
println!("TOOLCHAIN {}", primary_toolchain.name);
let mut cmd = try!(self.create_command(binary));
cmd.env("RUSTUP_TOOLCHAIN", &primary_toolchain.name);
Ok(cmd)
Expand All @@ -300,12 +311,14 @@ impl<'a> Toolchain<'a> {
// multirust agree.
if let Ok(cargo_home) = utils::cargo_home() {
cmd.env("CARGO_HOME", &cargo_home);
println!("CCC {}", cargo_home.display());
}

env_var::inc("RUST_RECURSION_COUNT", cmd);

cmd.env("RUSTUP_TOOLCHAIN", &self.name);
cmd.env("RUSTUP_HOME", &self.cfg.multirust_dir);
println!("RUH {}", self.cfg.multirust_dir.display());
}

pub fn set_ldpath(&self, cmd: &mut Command) {
Expand Down
11 changes: 9 additions & 2 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ fn fallback_cargo_calls_correct_rustc() {
let ref cargo_bin_path = config.cargodir.join("bin");
fs::create_dir_all(cargo_bin_path).unwrap();
let ref rustc_path = cargo_bin_path.join(format!("rustc{}", EXE_SUFFIX));
fs::hard_link(rustup_path, rustc_path).unwrap();
//fs::hard_link(rustup_path, rustc_path).unwrap();
let ref rustcc_path = cargo_bin_path.join(format!("rustcc{}", EXE_SUFFIX));
fs::hard_link(rustup_path, rustcc_path).unwrap();
fs::rename(rustcc_path, rustc_path).unwrap();

// Install a custom toolchain and a nightly toolchain for the cargo fallback
let path = config.customdir.join("custom-1");
Expand All @@ -295,12 +298,16 @@ fn fallback_cargo_calls_correct_rustc() {
expect_stdout_ok(config, &["rustc", "--version"],
"hash-c-1");

//assert!(rustc_path.exists());
println!("SRCHAU {}", rustup_path.display());

// Here --call-rustc tells the mock cargo bin to exec `rustc --version`.
// We should be ultimately calling the custom rustc, according to the
// RUSTUP_TOOLCHAIN variable set by the original "cargo" proxy, and
// interpreted by the nested "rustc" proxy.
expect_stdout_ok(config, &["cargo", "--call-rustc"],
"hash-c-1");
"hash-n-2" /* "hash-c-1" */);
::std::process::exit(-1);
});
}

Expand Down

0 comments on commit 0d82b65

Please sign in to comment.