diff --git a/src/bin/cargo-run.rs b/src/bin/cargo-run.rs index 510027cdaba..c945d9b8204 100644 --- a/src/bin/cargo-run.rs +++ b/src/bin/cargo-run.rs @@ -22,6 +22,8 @@ Usage: Options: -h, --help Print this message -j N, --jobs N The number of jobs to run in parallel + --release Build artifacts in release mode, with optimizations + --target TRIPLE Build for the target triple -u, --update-remotes Deprecated option, use `cargo update` instead --manifest-path PATH Path to the manifest to execute -v, --verbose Use verbose output @@ -40,10 +42,10 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { let mut compile_opts = ops::CompileOptions { update: options.flag_update_remotes, - env: "compile", + env: if options.flag_release { "release" } else { "compile" }, shell: shell, jobs: options.flag_jobs, - target: None, + target: options.flag_target.as_ref().map(|t| t.as_slice()), dev_deps: true, }; diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 0862f27322c..ae5e83ce3cf 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -17,7 +17,11 @@ pub fn run(manifest_path: &Path, let root = try!(src.get_root_package()); let compile = try!(ops::compile(manifest_path, options)); - let exe = manifest_path.dir_path().join("target").join(root.get_name()); + let mut exe = manifest_path.dir_path().join("target"); + if options.env == "release" { + exe = exe.join("release"); + } + let exe = exe.join(root.get_name()); let exe = match exe.path_relative_from(&os::getcwd()) { Some(path) => path, None => exe, diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 24442162ae4..048e2091ce7 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -102,6 +102,10 @@ impl ProjectBuilder { self.build_dir().join(format!("{}{}", b, os::consts::EXE_SUFFIX)) } + pub fn release_bin(&self, b: &str) -> Path { + self.build_dir().join("release").join(format!("{}{}", b, os::consts::EXE_SUFFIX)) + } + pub fn target_bin(&self, target: &str, b: &str) -> Path { self.build_dir().join(target).join(format!("{}{}", b, os::consts::EXE_SUFFIX)) diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index b463fd4f707..d122408f40c 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -434,3 +434,43 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured ", compiling = COMPILING, running = RUNNING, foo = p.url(), triple = target, doctest = DOCTEST))); }) + +test!(simple_cargo_run { + if disabled() { return } + + let target = alternate(); + + let build = project("builder") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = ["wycats@example.com"] + "#) + .file("src/main.rs", format!(r#" + fn main() {{ + assert_eq!(std::os::getenv("TARGET").unwrap().as_slice(), "{}"); + }} + "#, target).as_slice()); + assert_that(build.cargo_process("cargo-run").arg("--target").arg(target), + execs().with_status(0)); + + let p = project("foo") + .file("Cargo.toml", format!(r#" + [package] + name = "foo" + version = "0.0.0" + authors = [] + build = '{}' + "#, build.bin("foo").display())) + .file("src/main.rs", r#" + use std::os; + fn main() { + assert_eq!(os::consts::ARCH, "x86"); + } + "#); + + let target = alternate(); + assert_that(p.cargo_process("cargo-run").arg("--target").arg(target), + execs().with_status(0)); +}) diff --git a/tests/test_cargo_run.rs b/tests/test_cargo_run.rs index adc4f0c6ebb..860a62e51f5 100644 --- a/tests/test_cargo_run.rs +++ b/tests/test_cargo_run.rs @@ -113,3 +113,27 @@ test!(run_dylib_dep { assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"), execs().with_status(0)); }) + +test!(release_works { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", r#" + fn main() { if !cfg!(ndebug) { fail!() } } + "#); + + assert_that(p.cargo_process("cargo-run").arg("--release"), + execs().with_status(0).with_stdout(format!("\ +{compiling} foo v0.0.1 ({dir}) +{running} `target{sep}release{sep}foo` +", + compiling = COMPILING, + running = RUNNING, + dir = path2url(p.root()), + sep = path::SEP).as_slice())); + assert_that(&p.release_bin("foo"), existing_file()); +})