Skip to content

Commit a40d1fd

Browse files
committed
auto merge of #434 : alexcrichton/cargo/issue-433, r=huonw
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
2 parents bc02ddb + 76ccf94 commit a40d1fd

File tree

6 files changed

+47
-40
lines changed

6 files changed

+47
-40
lines changed

src/cargo/ops/cargo_run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn run(manifest_path: &Path,
2222
Some(path) => path,
2323
None => exe,
2424
};
25-
let process = compile.process(exe).args(args);
25+
let process = compile.process(exe, &root).args(args).cwd(os::getcwd());
2626

2727
try!(options.shell.status("Running", process.to_string()));
2828
Ok(process.exec().err())

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::collections::HashMap;
22
use std::dynamic_lib::DynamicLibrary;
33
use std::os;
4+
use semver::Version;
45

5-
use core::PackageId;
6+
use core::{PackageId, Package};
67
use util;
78

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

5253
/// Prepares a new process with an appropriate environment to run against
5354
/// the artifacts produced by the build process.
54-
pub fn process<T: ToCStr>(&self, cmd: T) -> util::ProcessBuilder {
55+
///
56+
/// The package argument is also used to configure environment variables as
57+
/// well as the working directory of the child process.
58+
pub fn process<T: ToCStr>(&self, cmd: T, pkg: &Package)
59+
-> util::ProcessBuilder {
5560
let mut search_path = DynamicLibrary::search_path();
5661
for dir in self.native_dirs.values() {
5762
search_path.push(dir.clone());
@@ -64,6 +69,31 @@ impl Compilation {
6469
for (k, v) in self.extra_env.iter() {
6570
cmd = cmd.env(k.as_slice(), v.as_ref().map(|s| s.as_slice()));
6671
}
67-
return cmd;
72+
73+
cmd.env("CARGO_MANIFEST_DIR", Some(pkg.get_manifest_path().dir_path()))
74+
.env("CARGO_PKG_VERSION_MAJOR",
75+
Some(pkg.get_version().major.to_string()))
76+
.env("CARGO_PKG_VERSION_MINOR",
77+
Some(pkg.get_version().minor.to_string()))
78+
.env("CARGO_PKG_VERSION_PATCH",
79+
Some(pkg.get_version().patch.to_string()))
80+
.env("CARGO_PKG_VERSION_PRE",
81+
pre_version_component(pkg.get_version()))
82+
.cwd(pkg.get_root())
83+
}
84+
}
85+
86+
fn pre_version_component(v: &Version) -> Option<String> {
87+
if v.pre.is_empty() {
88+
return None;
6889
}
90+
91+
let mut ret = String::new();
92+
93+
for (i, x) in v.pre.iter().enumerate() {
94+
if i != 0 { ret.push_char('.') };
95+
ret.push_str(x.to_string().as_slice());
96+
}
97+
98+
Some(ret)
6999
}

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::{HashMap, HashSet};
22
use std::str;
3-
use semver::Version;
43

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

150-
let env = &mut self.compilation.extra_env;
151-
env.insert("CARGO_PKG_VERSION_MAJOR".to_string(),
152-
Some(pkg.get_version().major.to_string()));
153-
env.insert("CARGO_PKG_VERSION_MINOR".to_string(),
154-
Some(pkg.get_version().minor.to_string()));
155-
env.insert("CARGO_PKG_VERSION_PATCH".to_string(),
156-
Some(pkg.get_version().patch.to_string()));
157-
env.insert("CARGO_PKG_VERSION_PRE".to_string(),
158-
pre_version_component(pkg.get_version()));
159-
160149
return Ok(());
161-
162-
fn pre_version_component(v: &Version) -> Option<String> {
163-
if v.pre.is_empty() {
164-
return None;
165-
}
166-
167-
let mut ret = String::new();
168-
169-
for (i, x) in v.pre.iter().enumerate() {
170-
if i != 0 { ret.push_char('.') };
171-
ret.push_str(x.to_string().as_slice());
172-
}
173-
174-
Some(ret)
175-
}
176150
}
177151

178152
fn build_requirements(&mut self, pkg: &'a Package, target: &'a Target,

src/cargo/ops/cargo_rustc/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ pub fn process<T: ToCStr>(cmd: T, pkg: &Package, cx: &Context) -> ProcessBuilder
438438

439439
// We want to use the same environment and such as normal processes, but we
440440
// want to override the dylib search path with the one we just calculated.
441-
cx.compilation.process(cmd).cwd(pkg.get_root())
442-
.env(DynamicLibrary::envvar(),
443-
Some(search_path.as_slice()))
441+
cx.compilation.process(cmd, pkg)
442+
.env(DynamicLibrary::envvar(), Some(search_path.as_slice()))
444443
}

src/cargo/ops/cargo_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn run_tests(manifest_path: &Path,
2121
Some(path) => path,
2222
None => exe.clone(),
2323
};
24-
let cmd = compile.process(exe).args(args);
24+
let cmd = compile.process(exe, &package).args(args);
2525
try!(options.shell.concise(|shell| {
2626
shell.status("Running", to_display.display().to_string())
2727
}));
@@ -43,7 +43,7 @@ pub fn run_tests(manifest_path: &Path,
4343

4444
for (lib, name) in libs {
4545
try!(options.shell.status("Doc-tests", name));
46-
let mut p = compile.process("rustdoc")
46+
let mut p = compile.process("rustdoc", &package)
4747
.arg("--test").arg(lib)
4848
.arg("--crate-name").arg(name)
4949
.arg("-L").arg(&compile.root_output)

tests/test_cargo_compile.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,29 +799,33 @@ test!(crate_version_env_vars {
799799
static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
800800
static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
801801
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
802+
static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
802803
803804
fn main() {
804-
let s = format!("{}-{}-{} @ {}", VERSION_MAJOR, VERSION_MINOR,
805-
VERSION_PATCH, VERSION_PRE);
805+
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
806+
VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
807+
CARGO_MANIFEST_DIR);
806808
assert_eq!(s, foo::version());
807809
println!("{}", s);
808810
}
809811
"#)
810812
.file("src/lib.rs", r#"
811813
pub fn version() -> String {
812-
format!("{}-{}-{} @ {}",
814+
format!("{}-{}-{} @ {} in {}",
813815
env!("CARGO_PKG_VERSION_MAJOR"),
814816
env!("CARGO_PKG_VERSION_MINOR"),
815817
env!("CARGO_PKG_VERSION_PATCH"),
816-
env!("CARGO_PKG_VERSION_PRE"))
818+
env!("CARGO_PKG_VERSION_PRE"),
819+
env!("CARGO_MANIFEST_DIR"))
817820
}
818821
"#);
819822

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

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

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

0 commit comments

Comments
 (0)