From 40e8fb4d7c122137c589653179dd40efa5548fe1 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Tue, 25 Dec 2018 14:10:36 -0800 Subject: [PATCH] Add support for CARGO_TARGET_DIR_PREFIX This change adds support for a new environment variable, CARGO_TARGET_DIR_PREFIX, to cargo. This variable, when set, is treated as a prefix to the target directory. Note that support for the functionality behind this variable is not trivial to implement with the current design. In particular, we wanted to stick as close to the existing CARGO_TARGET_DIR logic. However, the Config in which it is implemented really does not know anything about the directory of the particular crate we concerned with. As a quick work around to this problem, we just pass in the path to the Cargo.toml from the "upper layer". That works, but ultimately it would be better to make the other layer handle the CARGO_TARGET_DIR_PREFIX logic. This change addresses https://github.com/rust-lang/cargo/issues/5544. TODO: Definitely not finished. This patch needs more tests and may need additional config.toml support (?). TODO: There is also the potential for a permission related problems. E.g., when user root compiles something below /tmp/ and then user nobody tries to do the same the resulting directory ${CARGO_TARGET_DIR_PREFIX}/tmp/ may be owned by root, causing the build for nobody to fail with a permission denied error. --- src/cargo/core/workspace.rs | 15 +++-- src/cargo/ops/cargo_install.rs | 2 +- src/cargo/util/config.rs | 17 +++++- .../src/reference/environment-variables.md | 4 ++ tests/testsuite/build.rs | 57 +++++++++++++++++++ tests/testsuite/support/mod.rs | 1 + 6 files changed, 88 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index c36f10d33c1..f6d324c9f15 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -138,8 +138,6 @@ impl<'cfg> Workspace<'cfg> { /// root and all member packages. It will then validate the workspace /// before returning it, so `Ok` is only returned for valid workspaces. pub fn new(manifest_path: &Path, config: &'cfg Config) -> CargoResult> { - let target_dir = config.target_dir()?; - let mut ws = Workspace { config, current_manifest: manifest_path.to_path_buf(), @@ -148,7 +146,7 @@ impl<'cfg> Workspace<'cfg> { packages: HashMap::new(), }, root_manifest: None, - target_dir, + target_dir: None, members: Vec::new(), member_ids: HashSet::new(), default_members: Vec::new(), @@ -158,6 +156,11 @@ impl<'cfg> Workspace<'cfg> { ignore_lock: false, }; ws.root_manifest = ws.find_root(manifest_path)?; + if let Some(ref root_manifest) = ws.root_manifest { + ws.target_dir = config.target_dir(root_manifest)?; + } else { + ws.target_dir = config.target_dir(manifest_path)?; + } ws.find_members()?; ws.validate()?; Ok(ws) @@ -198,13 +201,13 @@ impl<'cfg> Workspace<'cfg> { { let key = ws.current_manifest.parent().unwrap(); let id = package.package_id(); - let package = MaybePackage::Package(package); - ws.packages.packages.insert(key.to_path_buf(), package); ws.target_dir = if let Some(dir) = target_dir { Some(dir) } else { - ws.config.target_dir()? + ws.config.target_dir(package.manifest_path())? }; + let package = MaybePackage::Package(package); + ws.packages.packages.insert(key.to_path_buf(), package); ws.members.push(ws.current_manifest.clone()); ws.member_ids.insert(id); ws.default_members.push(ws.current_manifest.clone()); diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 04f9befe33b..7130fe6b64b 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -204,7 +204,7 @@ fn install_one( let mut needs_cleanup = false; let overidden_target_dir = if source_id.is_path() { None - } else if let Some(dir) = config.target_dir()? { + } else if let Some(dir) = config.target_dir(pkg.manifest_path())? { Some(dir) } else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() { let p = td.path().to_owned(); diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index fa7b2b2bcb0..616abbf421e 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -322,11 +322,26 @@ impl Config { &self.cwd } - pub fn target_dir(&self) -> CargoResult> { + pub fn target_dir(&self, manifest: impl Into) -> CargoResult> { if let Some(ref dir) = self.target_dir { Ok(Some(dir.clone())) } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") { Ok(Some(Filesystem::new(self.cwd.join(dir)))) + } else if let Some(dir) = env::var_os("CARGO_TARGET_DIR_PREFIX") { + let prefix = Path::new(&dir); + if !prefix.is_absolute() { + failure::bail!("CARGO_TARGET_DIR_PREFIX must describe an absolute path"); + } + let mut manifest = manifest.into(); + let result = manifest.pop(); + assert!(result); + + match manifest.strip_prefix("/") { + Ok(dir) => Ok(Some(Filesystem::new(prefix.join(&dir).join("target")))), + // FIXME: This logic is probably not safe on Windows. Not sure how + // to make a path relative there. + Err(_) => failure::bail!("Current directory must be an absolute path"), + } } else if let Some(val) = self.get_path("build.target-dir")? { let val = self.cwd.join(val.val); Ok(Some(Filesystem::new(val))) diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 7f63a48d652..0e2d9223c80 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -13,6 +13,10 @@ system: checkouts of crates. By default these are stored under `$HOME/.cargo`, but this variable overrides the location of this directory. Once a crate is cached it is not removed by the clean command. +* `CARGO_TARGET_DIR_PREFIX` — Prefix to the location where to place all + generated artifacts. The current working directory will be appended to this + prefix to form the final path for generated artifacts. Note that + `CARGO_TARGET_DIR`, if set, takes precedence over this variable. * `CARGO_TARGET_DIR` — Location of where to place all generated artifacts, relative to the current working directory. * `RUSTC` — Instead of running `rustc`, Cargo will execute this specified diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index c80e528dfae..ff016dc83b6 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -3019,6 +3019,63 @@ fn explicit_color_config_is_propagated_to_rustc() { .run(); } +#[cargo_test] +fn custom_target_dir_prefix() { + fn test(cwd: &str) { + let tmpdir = tempfile::Builder::new() + .tempdir() + .unwrap() + .path() + .to_path_buf(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#, + ) + .file("src/main.rs", "fn main() {}") + .build(); + + let root = p.root(); + let root_suffix = root.strip_prefix("/").unwrap(); + let exe_name = format!("foo{}", env::consts::EXE_SUFFIX); + + p.cargo("build") + .env("CARGO_TARGET_DIR_PREFIX", tmpdir.clone()) + .cwd(p.root().join(cwd)) + .run(); + + assert!( + tmpdir + .clone() + .join(root_suffix) + .join("target/debug") + .join(&exe_name) + .is_file() + ); + assert!(!&p.root().join("target/debug").join(&exe_name).is_file()); + + p.cargo("build").run(); + assert!( + tmpdir + .clone() + .join(root_suffix) + .join("target/debug") + .join(&exe_name) + .is_file() + ); + assert!(&p.root().join("target/debug").join(&exe_name).is_file()) + }; + + test("."); + test("src"); +} + #[cargo_test] fn compiler_json_error_format() { let p = project() diff --git a/tests/testsuite/support/mod.rs b/tests/testsuite/support/mod.rs index f1da02e255c..970ea6fe084 100644 --- a/tests/testsuite/support/mod.rs +++ b/tests/testsuite/support/mod.rs @@ -1724,6 +1724,7 @@ fn _process(t: &OsStr) -> cargo::util::ProcessBuilder { .env_remove("GIT_AUTHOR_EMAIL") .env_remove("GIT_COMMITTER_NAME") .env_remove("GIT_COMMITTER_EMAIL") + .env_remove("CARGO_TARGET_DIR_PREFIX") // we assume no prefix .env_remove("CARGO_TARGET_DIR") // we assume 'target' .env_remove("MSYSTEM"); // assume cmd.exe everywhere on windows p