Compile all deps into the root projects target dir

This commit is contained in:
Carl Lerche 2014-05-28 17:31:23 -07:00
parent 5a59414edb
commit a06fd2e7fa
5 changed files with 53 additions and 34 deletions

View File

@ -50,10 +50,10 @@ $(BIN_TARGETS): target/%: src/bin/%.rs $(HAMMER) $(TOML) $(LIBCARGO)
TEST_SRC = $(wildcard tests/*.rs)
TEST_DEPS = $(DEPS) -L libs/hamcrest-rust/target
target/tests/test-integration: $(BIN_TARGETS) $(HAMCREST) $(TEST_SRC)
target/tests/test-integration: $(HAMCREST) $(TEST_SRC) $(BIN_TARGETS)
$(RUSTC) --test --crate-type=lib $(TEST_DEPS) -Ltarget -o $@ tests/tests.rs
target/tests/test-unit: $(HAMCREST) $(SRC) $(HAMMER) $(TOML)
target/tests/test-unit: $(TOML) $(HAMCREST) $(SRC) $(HAMMER)
mkdir -p target/tests
$(RUSTC) --test $(RUSTC_FLAGS) $(TEST_DEPS) -o $@ src/cargo/lib.rs

View File

@ -150,6 +150,20 @@ impl Target {
&self.path
}
pub fn is_lib(&self) -> bool {
match self.kind {
LibTarget => true,
_ => false
}
}
pub fn is_bin(&self) -> bool {
match self.kind {
BinTarget => true,
_ => false
}
}
pub fn rustc_crate_type(&self) -> &'static str {
match self.kind {
LibTarget => "lib",

View File

@ -1,3 +1,4 @@
use std::os;
use std::os::args;
use std::io;
use std::path::Path;
@ -12,42 +13,46 @@ type Args = Vec<String>;
pub fn compile_packages(pkg: &Package, deps: &PackageSet) -> CargoResult<()> {
debug!("compiling; pkg={}; deps={}", pkg, deps);
let target_dir = pkg.get_absolute_target_dir();
let deps_target_dir = target_dir.join("deps");
// First ensure that the destination directory exists
debug!("creating target dir; path={}", target_dir.display());
try!(mk_target(&target_dir));
try!(mk_target(&deps_target_dir));
// Traverse the dependencies in topological order
for dep in try!(topsort(deps)).iter() {
println!("Compiling {}", pkg);
try!(compile_pkg(dep, deps, false));
try!(compile_pkg(dep, &deps_target_dir, &deps_target_dir, false));
}
println!("Compiling {}", pkg);
try!(compile_pkg(pkg, deps, true));
try!(compile_pkg(pkg, &target_dir, &deps_target_dir, true));
Ok(())
}
fn compile_pkg(pkg: &Package, pkgs: &PackageSet, verbose: bool) -> CargoResult<()> {
debug!("compiling; pkg={}; targets={}; deps={}", pkg, pkg.get_targets(), pkg.get_dependencies());
// Build up the destination
// let src = pkg.get_root().join(Path::new(pkg.get_source().path.as_slice()));
let target_dir = pkg.get_absolute_target_dir();
debug!("creating target dir; path={}", target_dir.display());
// First ensure that the directory exists
try!(mk_target(&target_dir).map_err(|_| other_error("could not create target directory")));
fn compile_pkg(pkg: &Package, dest: &Path, deps_dir: &Path, primary: bool) -> CargoResult<()> {
debug!("compiling; pkg={}; targets={}", pkg, pkg.get_targets());
// compile
for target in pkg.get_targets().iter() {
try!(rustc(pkg.get_root(), target, &target_dir, pkgs.get_packages(), verbose))
// Only compile lib targets for dependencies
if primary || target.is_lib() {
try!(rustc(pkg.get_root(), target, dest, deps_dir, primary))
}
}
Ok(())
}
fn mk_target(target: &Path) -> io::IoResult<()> {
fn mk_target(target: &Path) -> CargoResult<()> {
io::fs::mkdir_recursive(target, io::UserRWX)
.map_err(|_| other_error("could not create target directory"))
}
fn rustc(root: &Path, target: &Target, dest: &Path, deps: &[Package], verbose: bool) -> CargoResult<()> {
fn rustc(root: &Path, target: &Target, dest: &Path, deps: &Path, verbose: bool) -> CargoResult<()> {
let rustc = prepare_rustc(root, target, dest, deps);
try!((if verbose {
@ -59,7 +64,7 @@ fn rustc(root: &Path, target: &Target, dest: &Path, deps: &[Package], verbose: b
Ok(())
}
fn prepare_rustc(root: &Path, target: &Target, dest: &Path, deps: &[Package]) -> ProcessBuilder {
fn prepare_rustc(root: &Path, target: &Target, dest: &Path, deps: &Path) -> ProcessBuilder {
let mut args = Vec::new();
build_base_args(&mut args, target, dest);
@ -80,13 +85,9 @@ fn build_base_args(into: &mut Args, target: &Target, dest: &Path) {
into.push(dest.display().to_str());
}
fn build_deps_args(dst: &mut Args, deps: &[Package]) {
for dep in deps.iter() {
let dir = dep.get_absolute_target_dir();
dst.push("-L".to_str());
dst.push(dir.display().to_str());
}
fn build_deps_args(dst: &mut Args, deps: &Path) {
dst.push("-L".to_str());
dst.push(deps.display().to_str());
}
fn rustc_to_cargo_err(args: &[String], cwd: &Path, err: CargoError) -> CargoError {

View File

@ -183,8 +183,8 @@ impl Execs {
fn match_output(&self, actual: &ProcessOutput) -> ham::MatchResult {
self.match_status(actual.status)
.and(self.match_stdout(&actual.output))
.and(self.match_stderr(&actual.error))
.and(self.match_stdout(actual))
.and(self.match_stderr(actual))
}
fn match_status(&self, actual: ProcessExit) -> ham::MatchResult {
@ -198,22 +198,22 @@ impl Execs {
}
}
fn match_stdout(&self, actual: &Vec<u8>) -> ham::MatchResult {
self.match_std(&self.expect_stdout, actual, "stdout")
fn match_stdout(&self, actual: &ProcessOutput) -> ham::MatchResult {
self.match_std(self.expect_stdout.as_ref(), actual.output.as_slice(), "stdout", actual.error.as_slice())
}
fn match_stderr(&self, actual: &Vec<u8>) -> ham::MatchResult {
self.match_std(&self.expect_stderr, actual, "stderr")
fn match_stderr(&self, actual: &ProcessOutput) -> ham::MatchResult {
self.match_std(self.expect_stderr.as_ref(), actual.error.as_slice(), "stderr", actual.output.as_slice())
}
fn match_std(&self, expected: &Option<String>, actual: &Vec<u8>, description: &str) -> ham::MatchResult {
fn match_std(&self, expected: Option<&String>, actual: &[u8], description: &str, extra: &[u8]) -> ham::MatchResult {
match expected.as_ref().map(|s| s.as_slice()) {
None => ham::success(),
Some(out) => {
match str::from_utf8(actual.as_slice()) {
match str::from_utf8(actual) {
None => Err(format!("{} was not utf8 encoded", description)),
Some(actual) => {
ham::expect(actual == out, format!("{} was `{}`", description, actual))
ham::expect(actual == out, format!("{} was `{}`\n other output:\n{}", description, actual, str::from_utf8(extra)))
}
}
}

View File

@ -1,10 +1,14 @@
#![feature(macro_rules)]
#![allow(deprecated_owned_vector)]
#![feature(phase)]
extern crate term;
extern crate cargo;
extern crate hamcrest;
#[phase(syntax, link)]
extern crate log;
macro_rules! test(
($name:ident $expr:expr) => (
#[test]