From 41d73d131266c864495ce592749b74f580b17b23 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 10 Dec 2020 18:51:39 +0100 Subject: Replicate Cargo environment variables --- crates/project_model/src/cargo_workspace.rs | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'crates/project_model') 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 { meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); for meta_pkg in meta.packages { + let id = meta_pkg.id.clone(); + inject_cargo_env(&meta_pkg, envs.entry(id).or_default()); + let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = meta_pkg; let is_member = ws_members.contains(&id); @@ -385,3 +388,39 @@ fn is_dylib(path: &Path) -> bool { Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), } } + +/// Recreates the compile-time environment variables that Cargo sets. +/// +/// Should be synced with +fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) { + // FIXME: Missing variables: + // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_ + + let mut manifest_dir = package.manifest_path.clone(); + manifest_dir.pop(); + if let Some(cargo_manifest_dir) = manifest_dir.to_str() { + env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into())); + } + + env.push(("CARGO_PKG_VERSION".into(), package.version.to_string())); + env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string())); + env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string())); + env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string())); + + let pre = package.version.pre.iter().map(|id| id.to_string()).collect::>(); + let pre = pre.join("."); + env.push(("CARGO_PKG_VERSION_PRE".into(), pre)); + + let authors = package.authors.join(";"); + env.push(("CARGO_PKG_AUTHORS".into(), authors)); + + env.push(("CARGO_PKG_NAME".into(), package.name.clone())); + env.push(("CARGO_PKG_DESCRIPTION".into(), package.description.clone().unwrap_or_default())); + //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default())); + env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default())); + env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default())); + + let license_file = + package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default(); + env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file)); +} -- cgit v1.2.3