From aa5c11bf8e2152ab02c7712dd731681a5709d656 Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Mon, 15 May 2017 07:01:44 +0200 Subject: [PATCH 1/2] Add functionality for workspaces and tests - Adds ability to get directory containing Workspace manifest - Adds test for the functionality --- src/cargo/core/workspace.rs | 11 ++++ src/cargo/ops/cargo_rustc/custom_build.rs | 4 ++ tests/workspaces.rs | 69 +++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 6486233a8d0..4402d5c118c 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -201,6 +201,17 @@ impl<'cfg> Workspace<'cfg> { }.parent().unwrap() } + /// Returns the root path of this workspace. + /// + /// That is, this returns the path of the directory containing the + /// `Cargo.toml` which is the root of this workspace. + pub fn workspace_dir(&self) -> Option<&Path> { + match self.root_manifest { + Some(ref p) => p.parent(), + None => None + } + } + pub fn target_dir(&self) -> Filesystem { self.target_dir.clone().unwrap_or_else(|| { Filesystem::new(self.root().join("target")) diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index 930662151e7..047b84c5b2a 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -120,6 +120,10 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) cmd.env("CARGO_MANIFEST_LINKS", links); } + if let Some(ws_dir) = cx.ws.workspace_dir() { + cmd.env("CARGO_WORKSPACE_DIR", ws_dir); + } + // Be sure to pass along all enabled features for this package, this is the // last piece of statically known information that we have. for feat in cx.resolve.features(unit.pkg.package_id()).iter() { diff --git a/tests/workspaces.rs b/tests/workspaces.rs index 4670d28f77a..2b69035e3ed 100644 --- a/tests/workspaces.rs +++ b/tests/workspaces.rs @@ -1469,3 +1469,72 @@ Caused by: ")); } +fn missing_workspace_build_dir(){ + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + fn main() { + use std::env; + use std::env::VarError::NotPresent; + + assert_eq!(env::var("CARGO_WORSPACE_DIR"), Err(NotPresent)); + } + "#); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); +} + +#[test] +fn workspace_build_dir(){ + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.5.0" + authors = [] + + + [workspace] + members = ["bar"] + "#) + .file("src/lib.rs", "") + .file("build.rs", r#" + use std::env; + + fn main() { + assert!(env::var("CARGO_WORKSPACE_DIR").unwrap().ends_with("foo")); + } + "#) + .file("bar/Cargo.toml", r#" + [project] + name = "bar" + version = "0.5.0" + authors = [] + links = "bar" + build = "build.rs" + "#) + .file("bar/src/lib.rs", "") + .file("bar/build.rs", r#" + use std::env; + + fn main() { + assert!(env::var("CARGO_WORKSPACE_DIR").unwrap().ends_with("foo")); + } + "#); + p.build(); + + assert_that(p.cargo("build"), + execs().with_status(0)); + assert_that(&p.root().join("target"), existing_dir()); + assert_that(p.cargo("build").cwd(p.root().join("bar")), + execs().with_status(0)); +} + From 6b57d0e7f982126ecdf27c1df7c44dddf95e03b4 Mon Sep 17 00:00:00 2001 From: Ygg01 Date: Mon, 15 May 2017 07:01:59 +0200 Subject: [PATCH 2/2] Adds documentation for functionality --- src/doc/environment-variables.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/doc/environment-variables.md b/src/doc/environment-variables.md index 77bfa71052e..0c42268386c 100644 --- a/src/doc/environment-variables.md +++ b/src/doc/environment-variables.md @@ -75,6 +75,8 @@ let out_dir = env::var("OUT_DIR").unwrap(); script). Also note that this is the value of the current working directory of the build script when it starts. +* `CARGO_WORKSPACE_DIR` - The directory containing the manifest of the workspace, + for the package being built. * `CARGO_MANIFEST_LINKS` - the manifest `links` value. * `CARGO_FEATURE_` - For each activated feature of the package being built, this environment variable will be present