Skip to content

Commit

Permalink
auto merge of #434 : alexcrichton/cargo/issue-433, r=huonw
Browse files Browse the repository at this point in the history
All subprocesses will now be invoked with CARGO_MANIFEST_DIR pointing at the root of the
source directory that they are working on (compiling). This commit also
reorganizes the version environment variables to use the new infrastructure.

Closes #433
  • Loading branch information
bors committed Aug 25, 2014
2 parents bc02ddb + 76ccf94 commit a40d1fd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run(manifest_path: &Path,
Some(path) => path,
None => exe,
};
let process = compile.process(exe).args(args);
let process = compile.process(exe, &root).args(args).cwd(os::getcwd());

try!(options.shell.status("Running", process.to_string()));
Ok(process.exec().err())
Expand Down
36 changes: 33 additions & 3 deletions src/cargo/ops/cargo_rustc/compilation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::dynamic_lib::DynamicLibrary;
use std::os;
use semver::Version;

use core::PackageId;
use core::{PackageId, Package};
use util;

/// A structure returning the result of a compilation.
Expand Down Expand Up @@ -51,7 +52,11 @@ impl Compilation {

/// Prepares a new process with an appropriate environment to run against
/// the artifacts produced by the build process.
pub fn process<T: ToCStr>(&self, cmd: T) -> util::ProcessBuilder {
///
/// The package argument is also used to configure environment variables as
/// well as the working directory of the child process.
pub fn process<T: ToCStr>(&self, cmd: T, pkg: &Package)
-> util::ProcessBuilder {
let mut search_path = DynamicLibrary::search_path();
for dir in self.native_dirs.values() {
search_path.push(dir.clone());
Expand All @@ -64,6 +69,31 @@ impl Compilation {
for (k, v) in self.extra_env.iter() {
cmd = cmd.env(k.as_slice(), v.as_ref().map(|s| s.as_slice()));
}
return cmd;

cmd.env("CARGO_MANIFEST_DIR", Some(pkg.get_manifest_path().dir_path()))
.env("CARGO_PKG_VERSION_MAJOR",
Some(pkg.get_version().major.to_string()))
.env("CARGO_PKG_VERSION_MINOR",
Some(pkg.get_version().minor.to_string()))
.env("CARGO_PKG_VERSION_PATCH",
Some(pkg.get_version().patch.to_string()))
.env("CARGO_PKG_VERSION_PRE",
pre_version_component(pkg.get_version()))
.cwd(pkg.get_root())
}
}

fn pre_version_component(v: &Version) -> Option<String> {
if v.pre.is_empty() {
return None;
}

let mut ret = String::new();

for (i, x) in v.pre.iter().enumerate() {
if i != 0 { ret.push_char('.') };
ret.push_str(x.to_string().as_slice());
}

Some(ret)
}
26 changes: 0 additions & 26 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::str;
use semver::Version;

use core::{SourceMap, Package, PackageId, PackageSet, Resolve, Target};
use util::{mod, CargoResult, ChainError, internal, Config, profile, Require};
Expand Down Expand Up @@ -147,32 +146,7 @@ impl<'a, 'b> Context<'a, 'b> {
self.compilation.root_output = self.layout(KindTarget).proxy().dest().clone();
self.compilation.deps_output = self.layout(KindTarget).proxy().deps().clone();

let env = &mut self.compilation.extra_env;
env.insert("CARGO_PKG_VERSION_MAJOR".to_string(),
Some(pkg.get_version().major.to_string()));
env.insert("CARGO_PKG_VERSION_MINOR".to_string(),
Some(pkg.get_version().minor.to_string()));
env.insert("CARGO_PKG_VERSION_PATCH".to_string(),
Some(pkg.get_version().patch.to_string()));
env.insert("CARGO_PKG_VERSION_PRE".to_string(),
pre_version_component(pkg.get_version()));

return Ok(());

fn pre_version_component(v: &Version) -> Option<String> {
if v.pre.is_empty() {
return None;
}

let mut ret = String::new();

for (i, x) in v.pre.iter().enumerate() {
if i != 0 { ret.push_char('.') };
ret.push_str(x.to_string().as_slice());
}

Some(ret)
}
}

fn build_requirements(&mut self, pkg: &'a Package, target: &'a Target,
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/ops/cargo_rustc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ pub fn process<T: ToCStr>(cmd: T, pkg: &Package, cx: &Context) -> ProcessBuilder

// We want to use the same environment and such as normal processes, but we
// want to override the dylib search path with the one we just calculated.
cx.compilation.process(cmd).cwd(pkg.get_root())
.env(DynamicLibrary::envvar(),
Some(search_path.as_slice()))
cx.compilation.process(cmd, pkg)
.env(DynamicLibrary::envvar(), Some(search_path.as_slice()))
}
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn run_tests(manifest_path: &Path,
Some(path) => path,
None => exe.clone(),
};
let cmd = compile.process(exe).args(args);
let cmd = compile.process(exe, &package).args(args);
try!(options.shell.concise(|shell| {
shell.status("Running", to_display.display().to_string())
}));
Expand All @@ -43,7 +43,7 @@ pub fn run_tests(manifest_path: &Path,

for (lib, name) in libs {
try!(options.shell.status("Doc-tests", name));
let mut p = compile.process("rustdoc")
let mut p = compile.process("rustdoc", &package)
.arg("--test").arg(lib)
.arg("--crate-name").arg(name)
.arg("-L").arg(&compile.root_output)
Expand Down
14 changes: 9 additions & 5 deletions tests/test_cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,29 +799,33 @@ test!(crate_version_env_vars {
static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
fn main() {
let s = format!("{}-{}-{} @ {}", VERSION_MAJOR, VERSION_MINOR,
VERSION_PATCH, VERSION_PRE);
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
CARGO_MANIFEST_DIR);
assert_eq!(s, foo::version());
println!("{}", s);
}
"#)
.file("src/lib.rs", r#"
pub fn version() -> String {
format!("{}-{}-{} @ {}",
format!("{}-{}-{} @ {} in {}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH"),
env!("CARGO_PKG_VERSION_PRE"))
env!("CARGO_PKG_VERSION_PRE"),
env!("CARGO_MANIFEST_DIR"))
}
"#);

assert_that(p.cargo_process("cargo-build"), execs().with_status(0));

assert_that(
process(p.bin("foo")),
execs().with_stdout("0-5-1 @ alpha.1\n"));
execs().with_stdout(format!("0-5-1 @ alpha.1 in {}\n",
p.root().display()).as_slice()));

assert_that(p.process(cargo_dir().join("cargo-test")), execs().with_status(0));
})
Expand Down

0 comments on commit a40d1fd

Please sign in to comment.