From 85a96e44ca0d3cebf13e4db070c62533fdd6ea45 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 3 Nov 2014 23:20:19 -0800 Subject: [PATCH] Follow through on the OUT_DIR promise for rustc compiles --- src/cargo/ops/cargo_rustc/mod.rs | 9 ++++++ tests/test_cargo_compile_custom_build.rs | 39 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index be5dcf86e8a..647fb0e6f41 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -582,6 +582,15 @@ fn build_deps_args(mut cmd: ProcessBuilder, target: &Target, package: &Package, cmd = cmd.arg("-L").arg(layout.root()); cmd = cmd.arg("-L").arg(layout.deps()); + let has_build_cmd = package.get_targets().iter().any(|t| { + t.get_profile().is_custom_build() + }); + cmd = cmd.env("OUT_DIR", if has_build_cmd { + Some(layout.build_out(package)) + } else { + None + }); + // Traverse the entire dependency graph looking for -L paths to pass for // native dependencies. // OLD-BUILD: to-remove diff --git a/tests/test_cargo_compile_custom_build.rs b/tests/test_cargo_compile_custom_build.rs index e8d83c3f5ff..11c5b568800 100644 --- a/tests/test_cargo_compile_custom_build.rs +++ b/tests/test_cargo_compile_custom_build.rs @@ -753,3 +753,42 @@ test!(output_separate_lines { {running} `rustc [..] --crate-name foo [..] -L foo -l foo:static` ", compiling = COMPILING, running = RUNNING).as_slice())); }) + +test!(code_generation { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + build = "build.rs" + "#) + .file("src/main.rs", r#" + include!(concat!(env!("OUT_DIR"), "/hello.rs")) + + fn main() { + println!("{}", message()); + } + "#) + .file("build.rs", r#" + use std::os; + use std::io::File; + + fn main() { + let dst = Path::new(os::getenv("OUT_DIR").unwrap()); + let mut f = File::create(&dst.join("hello.rs")).unwrap(); + f.write_str(" + pub fn message() -> &'static str { + \"Hello, World!\" + } + ").unwrap(); + } + "#); + assert_that(p.cargo_process("run"), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} foo v0.5.0 (file://[..]) +{running} `target[..]foo` +Hello, World! +", compiling = COMPILING, running = RUNNING).as_slice())); +})