Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo-run: support target and release the same as cargo-build #438

Merged
merged 1 commit into from
Aug 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/bin/cargo-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,10 +42,10 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {

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,
};

Expand Down
6 changes: 5 additions & 1 deletion src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
40 changes: 40 additions & 0 deletions tests/test_cargo_cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]"]
"#)
.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));
})
24 changes: 24 additions & 0 deletions tests/test_cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
})