aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-10 18:30:11 +0000
committerGitHub <[email protected]>2020-12-10 18:30:11 +0000
commit41321d96789ed918eebda02ada76758765d19d16 (patch)
tree1d2b0272f9ae48e143b2e6fa7260c6664eb40b03 /crates/project_model
parent814e31957ea0e063272132afd15c3e8f4ed54454 (diff)
parent4d4b91117a02b55e913216459ae661d8dd166579 (diff)
Merge #6807
6807: Replicate Cargo environment variables r=jonas-schievink a=jonas-schievink These might be relied on by procedural macros, and can also be accessed via `env!`. Required for fixing https://github.com/rust-analyzer/rust-analyzer/issues/6696. We do not yet propagate these to any proc macros though. Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates/project_model')
-rw-r--r--crates/project_model/Cargo.toml1
-rw-r--r--crates/project_model/src/cargo_workspace.rs39
2 files changed, 40 insertions, 0 deletions
diff --git a/crates/project_model/Cargo.toml b/crates/project_model/Cargo.toml
index e0c591603..c55e85709 100644
--- a/crates/project_model/Cargo.toml
+++ b/crates/project_model/Cargo.toml
@@ -16,6 +16,7 @@ cargo_metadata = "=0.12.0"
16serde = { version = "1.0.106", features = ["derive"] } 16serde = { version = "1.0.106", features = ["derive"] }
17serde_json = "1.0.48" 17serde_json = "1.0.48"
18anyhow = "1.0.26" 18anyhow = "1.0.26"
19itertools = "0.9.0"
19 20
20arena = { path = "../arena", version = "0.0.0" } 21arena = { path = "../arena", version = "0.0.0" }
21cfg = { path = "../cfg", version = "0.0.0" } 22cfg = { path = "../cfg", version = "0.0.0" }
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index e6491b754..894c5c952 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -11,6 +11,7 @@ use anyhow::{Context, Result};
11use arena::{Arena, Idx}; 11use arena::{Arena, Idx};
12use base_db::Edition; 12use base_db::Edition;
13use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId}; 13use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
14use itertools::Itertools;
14use paths::{AbsPath, AbsPathBuf}; 15use paths::{AbsPath, AbsPathBuf};
15use rustc_hash::FxHashMap; 16use rustc_hash::FxHashMap;
16 17
@@ -192,6 +193,9 @@ impl CargoWorkspace {
192 193
193 meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); 194 meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
194 for meta_pkg in meta.packages { 195 for meta_pkg in meta.packages {
196 let id = meta_pkg.id.clone();
197 inject_cargo_env(&meta_pkg, envs.entry(id).or_default());
198
195 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = 199 let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } =
196 meta_pkg; 200 meta_pkg;
197 let is_member = ws_members.contains(&id); 201 let is_member = ws_members.contains(&id);
@@ -385,3 +389,38 @@ fn is_dylib(path: &Path) -> bool {
385 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"), 389 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
386 } 390 }
387} 391}
392
393/// Recreates the compile-time environment variables that Cargo sets.
394///
395/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
396fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) {
397 // FIXME: Missing variables:
398 // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
399
400 let mut manifest_dir = package.manifest_path.clone();
401 manifest_dir.pop();
402 if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
403 env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
404 }
405
406 env.push(("CARGO_PKG_VERSION".into(), package.version.to_string()));
407 env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string()));
408 env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string()));
409 env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string()));
410
411 let pre = package.version.pre.iter().map(|id| id.to_string()).format(".");
412 env.push(("CARGO_PKG_VERSION_PRE".into(), pre.to_string()));
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}