aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2020-12-10 17:51:39 +0000
committerJonas Schievink <[email protected]>2020-12-10 17:51:39 +0000
commit41d73d131266c864495ce592749b74f580b17b23 (patch)
treeda7d2a43957a3d81ba2e25c097196daf48d20406 /crates
parent6095debda842a84904398e410589669dda51cf14 (diff)
Replicate Cargo environment variables
Diffstat (limited to 'crates')
-rw-r--r--crates/project_model/src/cargo_workspace.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index e6491b754..4c1ba5e75 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -192,6 +192,9 @@ impl CargoWorkspace {
192 192
193 meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); 193 meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
194 for meta_pkg in meta.packages { 194 for meta_pkg in meta.packages {
195 let id = meta_pkg.id.clone();
196 inject_cargo_env(&meta_pkg, envs.entry(id).or_default());
197
195 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = 198 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } =
196 meta_pkg; 199 meta_pkg;
197 let is_member = ws_members.contains(&id); 200 let is_member = ws_members.contains(&id);
@@ -385,3 +388,39 @@ fn is_dylib(path: &Path) -> bool {
385 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), 388 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
386 } 389 }
387} 390}
391
392/// Recreates the compile-time environment variables that Cargo sets.
393///
394/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
395fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) {
396 // FIXME: Missing variables:
397 // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
398
399 let mut manifest_dir = package.manifest_path.clone();
400 manifest_dir.pop();
401 if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
402 env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
403 }
404
405 env.push(("CARGO_PKG_VERSION".into(), package.version.to_string()));
406 env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string()));
407 env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string()));
408 env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string()));
409
410 let pre = package.version.pre.iter().map(|id| id.to_string()).collect::<Vec<_>>();
411 let pre = pre.join(".");
412 env.push(("CARGO_PKG_VERSION_PRE".into(), pre));
413
414 let authors = package.authors.join(";");
415 env.push(("CARGO_PKG_AUTHORS".into(), authors));
416
417 env.push(("CARGO_PKG_NAME".into(), package.name.clone()));
418 env.push(("CARGO_PKG_DESCRIPTION".into(), package.description.clone().unwrap_or_default()));
419 //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
420 env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default()));
421 env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default()));
422
423 let license_file =
424 package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default();
425 env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
426}