mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 11:59:49 +00:00
internal: simplify handling of the build scripts
This commit is contained in:
parent
8da560264e
commit
f4de2ece0d
18 changed files with 404 additions and 491 deletions
|
@ -1,323 +0,0 @@
|
|||
//! Handles build script specific information
|
||||
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
process::{Command, Stdio},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use base_db::CrateName;
|
||||
use cargo_metadata::camino::Utf8Path;
|
||||
use cargo_metadata::{BuildScript, Message};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::Deserialize;
|
||||
use stdx::format_to;
|
||||
|
||||
use crate::{cfg_flag::CfgFlag, CargoConfig};
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(crate) struct PackageBuildData {
|
||||
/// List of config flags defined by this package's build script
|
||||
pub(crate) cfgs: Vec<CfgFlag>,
|
||||
/// List of cargo-related environment variables with their value
|
||||
///
|
||||
/// If the package has a build script which defines environment variables,
|
||||
/// they can also be found here.
|
||||
pub(crate) envs: Vec<(String, String)>,
|
||||
/// Directory where a build script might place its output
|
||||
pub(crate) out_dir: Option<AbsPathBuf>,
|
||||
/// Path to the proc-macro library file if this package exposes proc-macros
|
||||
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
||||
pub(crate) struct WorkspaceBuildData {
|
||||
per_package: FxHashMap<String, PackageBuildData>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, PartialEq, Eq, Clone)]
|
||||
pub struct BuildDataResult {
|
||||
per_workspace: FxHashMap<AbsPathBuf, WorkspaceBuildData>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct BuildDataConfig {
|
||||
cargo_toml: AbsPathBuf,
|
||||
cargo_features: CargoConfig,
|
||||
packages: Arc<Vec<cargo_metadata::Package>>,
|
||||
}
|
||||
|
||||
impl PartialEq for BuildDataConfig {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Arc::ptr_eq(&self.packages, &other.packages)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for BuildDataConfig {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BuildDataCollector {
|
||||
wrap_rustc: bool,
|
||||
configs: FxHashMap<AbsPathBuf, BuildDataConfig>,
|
||||
}
|
||||
|
||||
impl BuildDataCollector {
|
||||
pub fn new(wrap_rustc: bool) -> Self {
|
||||
Self { wrap_rustc, configs: FxHashMap::default() }
|
||||
}
|
||||
|
||||
pub(crate) fn add_config(&mut self, workspace_root: &AbsPath, config: BuildDataConfig) {
|
||||
self.configs.insert(workspace_root.to_path_buf(), config);
|
||||
}
|
||||
|
||||
pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result<BuildDataResult> {
|
||||
let mut res = BuildDataResult::default();
|
||||
for (path, config) in self.configs.iter() {
|
||||
let workspace_build_data = WorkspaceBuildData::collect(
|
||||
&config.cargo_toml,
|
||||
&config.cargo_features,
|
||||
&config.packages,
|
||||
self.wrap_rustc,
|
||||
progress,
|
||||
)?;
|
||||
res.per_workspace.insert(path.clone(), workspace_build_data);
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl WorkspaceBuildData {
|
||||
pub(crate) fn get(&self, package_id: &str) -> Option<&PackageBuildData> {
|
||||
self.per_package.get(package_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl BuildDataResult {
|
||||
pub(crate) fn get(&self, workspace_root: &AbsPath) -> Option<&WorkspaceBuildData> {
|
||||
self.per_workspace.get(workspace_root)
|
||||
}
|
||||
pub fn error(&self) -> Option<String> {
|
||||
let mut buf = String::new();
|
||||
for (_workspace_root, build_data) in &self.per_workspace {
|
||||
if let Some(err) = &build_data.error {
|
||||
format_to!(buf, "cargo check failed:\n{}", err);
|
||||
}
|
||||
}
|
||||
if buf.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl BuildDataConfig {
|
||||
pub(crate) fn new(
|
||||
cargo_toml: AbsPathBuf,
|
||||
cargo_features: CargoConfig,
|
||||
packages: Arc<Vec<cargo_metadata::Package>>,
|
||||
) -> Self {
|
||||
Self { cargo_toml, cargo_features, packages }
|
||||
}
|
||||
}
|
||||
|
||||
impl WorkspaceBuildData {
|
||||
fn collect(
|
||||
cargo_toml: &AbsPath,
|
||||
cargo_features: &CargoConfig,
|
||||
packages: &Vec<cargo_metadata::Package>,
|
||||
wrap_rustc: bool,
|
||||
progress: &dyn Fn(String),
|
||||
) -> Result<WorkspaceBuildData> {
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
|
||||
if wrap_rustc {
|
||||
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
|
||||
// that to compile only proc macros and build scripts during the initial
|
||||
// `cargo check`.
|
||||
let myself = std::env::current_exe()?;
|
||||
cmd.env("RUSTC_WRAPPER", myself);
|
||||
cmd.env("RA_RUSTC_WRAPPER", "1");
|
||||
}
|
||||
|
||||
cmd.current_dir(cargo_toml.parent().unwrap());
|
||||
cmd.args(&["check", "--quiet", "--workspace", "--message-format=json", "--manifest-path"])
|
||||
.arg(cargo_toml.as_ref());
|
||||
|
||||
// --all-targets includes tests, benches and examples in addition to the
|
||||
// default lib and bins. This is an independent concept from the --targets
|
||||
// flag below.
|
||||
cmd.arg("--all-targets");
|
||||
|
||||
if let Some(target) = &cargo_features.target {
|
||||
cmd.args(&["--target", target]);
|
||||
}
|
||||
|
||||
if cargo_features.all_features {
|
||||
cmd.arg("--all-features");
|
||||
} else {
|
||||
if cargo_features.no_default_features {
|
||||
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
|
||||
// https://github.com/oli-obk/cargo_metadata/issues/79
|
||||
cmd.arg("--no-default-features");
|
||||
}
|
||||
if !cargo_features.features.is_empty() {
|
||||
cmd.arg("--features");
|
||||
cmd.arg(cargo_features.features.join(" "));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
|
||||
|
||||
let mut res = WorkspaceBuildData::default();
|
||||
|
||||
let mut callback_err = None;
|
||||
let output = stdx::process::streaming_output(
|
||||
cmd,
|
||||
&mut |line| {
|
||||
if callback_err.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Copy-pasted from existing cargo_metadata. It seems like we
|
||||
// should be using sered_stacker here?
|
||||
let mut deserializer = serde_json::Deserializer::from_str(line);
|
||||
deserializer.disable_recursion_limit();
|
||||
let message = Message::deserialize(&mut deserializer)
|
||||
.unwrap_or_else(|_| Message::TextLine(line.to_string()));
|
||||
|
||||
match message {
|
||||
Message::BuildScriptExecuted(BuildScript {
|
||||
package_id,
|
||||
out_dir,
|
||||
cfgs,
|
||||
env,
|
||||
..
|
||||
}) => {
|
||||
let cfgs = {
|
||||
let mut acc = Vec::new();
|
||||
for cfg in cfgs {
|
||||
match cfg.parse::<CfgFlag>() {
|
||||
Ok(it) => acc.push(it),
|
||||
Err(err) => {
|
||||
callback_err = Some(anyhow::format_err!(
|
||||
"invalid cfg from cargo-metadata: {}",
|
||||
err
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
acc
|
||||
};
|
||||
let package_build_data =
|
||||
res.per_package.entry(package_id.repr).or_default();
|
||||
// cargo_metadata crate returns default (empty) path for
|
||||
// older cargos, which is not absolute, so work around that.
|
||||
if !out_dir.as_str().is_empty() {
|
||||
let out_dir =
|
||||
AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string()));
|
||||
package_build_data.out_dir = Some(out_dir);
|
||||
package_build_data.cfgs = cfgs;
|
||||
}
|
||||
|
||||
package_build_data.envs = env;
|
||||
}
|
||||
Message::CompilerArtifact(message) => {
|
||||
progress(format!("metadata {}", message.target.name));
|
||||
|
||||
if message.target.kind.iter().any(|k| k == "proc-macro") {
|
||||
let package_id = message.package_id;
|
||||
// Skip rmeta file
|
||||
if let Some(filename) =
|
||||
message.filenames.iter().find(|name| is_dylib(name))
|
||||
{
|
||||
let filename = AbsPathBuf::assert(PathBuf::from(&filename));
|
||||
let package_build_data =
|
||||
res.per_package.entry(package_id.repr).or_default();
|
||||
package_build_data.proc_macro_dylib_path = Some(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::CompilerMessage(message) => {
|
||||
progress(message.target.name);
|
||||
}
|
||||
Message::BuildFinished(_) => {}
|
||||
Message::TextLine(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
&mut |_| (),
|
||||
)?;
|
||||
|
||||
for package in packages {
|
||||
let package_build_data = res.per_package.entry(package.id.repr.clone()).or_default();
|
||||
inject_cargo_env(package, package_build_data);
|
||||
if let Some(out_dir) = &package_build_data.out_dir {
|
||||
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
|
||||
if let Some(out_dir) = out_dir.as_os_str().to_str().map(|s| s.to_owned()) {
|
||||
package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !output.status.success() {
|
||||
let mut stderr = String::from_utf8(output.stderr).unwrap_or_default();
|
||||
if stderr.is_empty() {
|
||||
stderr = "cargo check failed".to_string();
|
||||
}
|
||||
res.error = Some(stderr)
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: File a better way to know if it is a dylib
|
||||
fn is_dylib(path: &Utf8Path) -> bool {
|
||||
match path.extension().map(|e| e.to_string().to_lowercase()) {
|
||||
None => false,
|
||||
Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Recreates the compile-time environment variables that Cargo sets.
|
||||
///
|
||||
/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
|
||||
fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut PackageBuildData) {
|
||||
let env = &mut build_data.envs;
|
||||
|
||||
// FIXME: Missing variables:
|
||||
// CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
|
||||
|
||||
let mut manifest_dir = package.manifest_path.clone();
|
||||
manifest_dir.pop();
|
||||
env.push(("CARGO_MANIFEST_DIR".into(), manifest_dir.into_string()));
|
||||
|
||||
// Not always right, but works for common cases.
|
||||
env.push(("CARGO".into(), "cargo".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()));
|
||||
env.push(("CARGO_PKG_VERSION_PRE".into(), package.version.pre.to_string()));
|
||||
|
||||
let authors = package.authors.join(";");
|
||||
env.push(("CARGO_PKG_AUTHORS".into(), authors));
|
||||
|
||||
env.push(("CARGO_PKG_NAME".into(), package.name.clone()));
|
||||
// FIXME: This isn't really correct (a package can have many crates with different names), but
|
||||
// it's better than leaving the variable unset.
|
||||
env.push(("CARGO_CRATE_NAME".into(), CrateName::normalize_dashes(&package.name).to_string()));
|
||||
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.to_string()).unwrap_or_default();
|
||||
env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
|
||||
}
|
207
crates/project_model/src/build_scripts.rs
Normal file
207
crates/project_model/src/build_scripts.rs
Normal file
|
@ -0,0 +1,207 @@
|
|||
//! Workspace information we get from cargo consists of two pieces. The first is
|
||||
//! the output of `cargo metadata`. The second is the output of running
|
||||
//! `build.rs` files (`OUT_DIR` env var, extra cfg flags) and compiling proc
|
||||
//! macro.
|
||||
//!
|
||||
//! This module implements this second part. We use "build script" terminology
|
||||
//! here, but it covers procedural macros as well.
|
||||
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
process::{Command, Stdio},
|
||||
};
|
||||
|
||||
use anyhow::Result;
|
||||
use cargo_metadata::{camino::Utf8Path, Message};
|
||||
use la_arena::ArenaMap;
|
||||
use paths::AbsPathBuf;
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub struct WorkspaceBuildScripts {
|
||||
pub(crate) outputs: ArenaMap<Package, BuildScriptOutput>,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub(crate) struct BuildScriptOutput {
|
||||
/// List of config flags defined by this package's build script.
|
||||
pub(crate) cfgs: Vec<CfgFlag>,
|
||||
/// List of cargo-related environment variables with their value.
|
||||
///
|
||||
/// If the package has a build script which defines environment variables,
|
||||
/// they can also be found here.
|
||||
pub(crate) envs: Vec<(String, String)>,
|
||||
/// Directory where a build script might place its output.
|
||||
pub(crate) out_dir: Option<AbsPathBuf>,
|
||||
/// Path to the proc-macro library file if this package exposes proc-macros.
|
||||
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
|
||||
}
|
||||
|
||||
impl WorkspaceBuildScripts {
|
||||
pub fn run(
|
||||
config: &CargoConfig,
|
||||
workspace: &CargoWorkspace,
|
||||
progress: &dyn Fn(String),
|
||||
) -> Result<WorkspaceBuildScripts> {
|
||||
let mut cmd = Command::new(toolchain::cargo());
|
||||
|
||||
if config.wrap_rustc_in_build_scripts {
|
||||
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
|
||||
// that to compile only proc macros and build scripts during the initial
|
||||
// `cargo check`.
|
||||
let myself = std::env::current_exe()?;
|
||||
cmd.env("RUSTC_WRAPPER", myself);
|
||||
cmd.env("RA_RUSTC_WRAPPER", "1");
|
||||
}
|
||||
cmd.current_dir(workspace.workspace_root());
|
||||
cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
|
||||
|
||||
// --all-targets includes tests, benches and examples in addition to the
|
||||
// default lib and bins. This is an independent concept from the --targets
|
||||
// flag below.
|
||||
cmd.arg("--all-targets");
|
||||
|
||||
if let Some(target) = &config.target {
|
||||
cmd.args(&["--target", target]);
|
||||
}
|
||||
|
||||
if config.all_features {
|
||||
cmd.arg("--all-features");
|
||||
} else {
|
||||
if config.no_default_features {
|
||||
cmd.arg("--no-default-features");
|
||||
}
|
||||
if !config.features.is_empty() {
|
||||
cmd.arg("--features");
|
||||
cmd.arg(config.features.join(" "));
|
||||
}
|
||||
}
|
||||
|
||||
cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
|
||||
|
||||
let mut res = WorkspaceBuildScripts::default();
|
||||
// NB: Cargo.toml could have been modified between `cargo metadata` and
|
||||
// `cargo check`. We shouldn't assume that package ids we see here are
|
||||
// exactly those from `config`.
|
||||
let mut by_id: FxHashMap<String, Package> = FxHashMap::default();
|
||||
|
||||
for package in workspace.packages() {
|
||||
res.outputs.insert(package, BuildScriptOutput::default());
|
||||
by_id.insert(workspace[package].id.clone(), package);
|
||||
}
|
||||
|
||||
let mut callback_err = None;
|
||||
let mut stderr = String::new();
|
||||
let output = stdx::process::streaming_output(
|
||||
cmd,
|
||||
&mut |line| {
|
||||
if callback_err.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Copy-pasted from existing cargo_metadata. It seems like we
|
||||
// should be using sered_stacker here?
|
||||
let mut deserializer = serde_json::Deserializer::from_str(line);
|
||||
deserializer.disable_recursion_limit();
|
||||
let message = Message::deserialize(&mut deserializer)
|
||||
.unwrap_or_else(|_| Message::TextLine(line.to_string()));
|
||||
|
||||
match message {
|
||||
Message::BuildScriptExecuted(message) => {
|
||||
let package = match by_id.get(&message.package_id.repr) {
|
||||
Some(it) => *it,
|
||||
None => return,
|
||||
};
|
||||
let cfgs = {
|
||||
let mut acc = Vec::new();
|
||||
for cfg in message.cfgs {
|
||||
match cfg.parse::<CfgFlag>() {
|
||||
Ok(it) => acc.push(it),
|
||||
Err(err) => {
|
||||
callback_err = Some(anyhow::format_err!(
|
||||
"invalid cfg from cargo-metadata: {}",
|
||||
err
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
acc
|
||||
};
|
||||
let package_build_data = &mut res.outputs[package];
|
||||
// cargo_metadata crate returns default (empty) path for
|
||||
// older cargos, which is not absolute, so work around that.
|
||||
if !message.out_dir.as_str().is_empty() {
|
||||
let out_dir =
|
||||
AbsPathBuf::assert(PathBuf::from(message.out_dir.into_os_string()));
|
||||
package_build_data.out_dir = Some(out_dir);
|
||||
package_build_data.cfgs = cfgs;
|
||||
}
|
||||
|
||||
package_build_data.envs = message.env;
|
||||
}
|
||||
Message::CompilerArtifact(message) => {
|
||||
let package = match by_id.get(&message.package_id.repr) {
|
||||
Some(it) => *it,
|
||||
None => return,
|
||||
};
|
||||
|
||||
progress(format!("metadata {}", message.target.name));
|
||||
|
||||
if message.target.kind.iter().any(|k| k == "proc-macro") {
|
||||
// Skip rmeta file
|
||||
if let Some(filename) =
|
||||
message.filenames.iter().find(|name| is_dylib(name))
|
||||
{
|
||||
let filename = AbsPathBuf::assert(PathBuf::from(&filename));
|
||||
res.outputs[package].proc_macro_dylib_path = Some(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::CompilerMessage(message) => {
|
||||
progress(message.target.name);
|
||||
}
|
||||
Message::BuildFinished(_) => {}
|
||||
Message::TextLine(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
},
|
||||
&mut |line| {
|
||||
stderr.push_str(line);
|
||||
stderr.push('\n');
|
||||
},
|
||||
)?;
|
||||
|
||||
for package in workspace.packages() {
|
||||
let package_build_data = &mut res.outputs[package];
|
||||
// inject_cargo_env(package, package_build_data);
|
||||
if let Some(out_dir) = &package_build_data.out_dir {
|
||||
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
|
||||
if let Some(out_dir) = out_dir.as_os_str().to_str().map(|s| s.to_owned()) {
|
||||
package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !output.status.success() {
|
||||
if stderr.is_empty() {
|
||||
stderr = "cargo check failed".to_string();
|
||||
}
|
||||
res.error = Some(stderr)
|
||||
}
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: File a better way to know if it is a dylib.
|
||||
fn is_dylib(path: &Utf8Path) -> bool {
|
||||
match path.extension().map(|e| e.to_string().to_lowercase()) {
|
||||
None => false,
|
||||
Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::iter;
|
||||
use std::path::PathBuf;
|
||||
use std::{convert::TryInto, ops, process::Command, sync::Arc};
|
||||
use std::{convert::TryInto, ops, process::Command};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use base_db::Edition;
|
||||
|
@ -13,8 +13,8 @@ use rustc_hash::FxHashMap;
|
|||
use serde::Deserialize;
|
||||
use serde_json::from_value;
|
||||
|
||||
use crate::utf8_stdout;
|
||||
use crate::CfgOverrides;
|
||||
use crate::{build_data::BuildDataConfig, utf8_stdout};
|
||||
|
||||
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
|
||||
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
||||
|
@ -31,7 +31,6 @@ pub struct CargoWorkspace {
|
|||
packages: Arena<PackageData>,
|
||||
targets: Arena<TargetData>,
|
||||
workspace_root: AbsPathBuf,
|
||||
build_data_config: BuildDataConfig,
|
||||
}
|
||||
|
||||
impl ops::Index<Package> for CargoWorkspace {
|
||||
|
@ -81,6 +80,8 @@ pub struct CargoConfig {
|
|||
|
||||
/// crates to disable `#[cfg(test)]` on
|
||||
pub unset_test_crates: Vec<String>,
|
||||
|
||||
pub wrap_rustc_in_build_scripts: bool,
|
||||
}
|
||||
|
||||
impl CargoConfig {
|
||||
|
@ -103,7 +104,7 @@ pub type Target = Idx<TargetData>;
|
|||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub struct PackageData {
|
||||
/// Version given in the `Cargo.toml`
|
||||
pub version: String,
|
||||
pub version: semver::Version,
|
||||
/// Name as given in the `Cargo.toml`
|
||||
pub name: String,
|
||||
/// Path containing the `Cargo.toml`
|
||||
|
@ -288,11 +289,7 @@ impl CargoWorkspace {
|
|||
Ok(meta)
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
cargo_toml: &AbsPath,
|
||||
config: &CargoConfig,
|
||||
mut meta: cargo_metadata::Metadata,
|
||||
) -> CargoWorkspace {
|
||||
pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
|
||||
let mut pkg_by_id = FxHashMap::default();
|
||||
let mut packages = Arena::default();
|
||||
let mut targets = Arena::default();
|
||||
|
@ -314,7 +311,7 @@ impl CargoWorkspace {
|
|||
let pkg = packages.alloc(PackageData {
|
||||
id: id.repr.clone(),
|
||||
name: name.clone(),
|
||||
version: version.to_string(),
|
||||
version: version.clone(),
|
||||
manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)),
|
||||
targets: Vec::new(),
|
||||
is_member,
|
||||
|
@ -374,10 +371,8 @@ impl CargoWorkspace {
|
|||
|
||||
let workspace_root =
|
||||
AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
|
||||
let build_data_config =
|
||||
BuildDataConfig::new(cargo_toml.to_path_buf(), config.clone(), Arc::new(meta.packages));
|
||||
|
||||
CargoWorkspace { packages, targets, workspace_root, build_data_config }
|
||||
CargoWorkspace { packages, targets, workspace_root }
|
||||
}
|
||||
|
||||
pub fn from_cargo_metadata3(
|
||||
|
@ -386,7 +381,7 @@ impl CargoWorkspace {
|
|||
progress: &dyn Fn(String),
|
||||
) -> Result<CargoWorkspace> {
|
||||
let meta = CargoWorkspace::fetch_metadata(cargo_toml, config, progress)?;
|
||||
Ok(CargoWorkspace::new(cargo_toml, config, meta))
|
||||
Ok(CargoWorkspace::new(meta))
|
||||
}
|
||||
|
||||
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
|
||||
|
@ -412,10 +407,6 @@ impl CargoWorkspace {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn build_data_config(&self) -> &BuildDataConfig {
|
||||
&self.build_data_config
|
||||
}
|
||||
|
||||
fn is_unique(&self, name: &str) -> bool {
|
||||
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ mod project_json;
|
|||
mod sysroot;
|
||||
mod workspace;
|
||||
mod rustc_cfg;
|
||||
mod build_data;
|
||||
mod build_scripts;
|
||||
|
||||
use std::{
|
||||
fs::{self, read_dir, ReadDir},
|
||||
|
@ -34,7 +34,7 @@ use paths::{AbsPath, AbsPathBuf};
|
|||
use rustc_hash::FxHashSet;
|
||||
|
||||
pub use crate::{
|
||||
build_data::{BuildDataCollector, BuildDataResult},
|
||||
build_scripts::WorkspaceBuildScripts,
|
||||
cargo_workspace::{
|
||||
CargoConfig, CargoWorkspace, Package, PackageData, PackageDependency, RustcSource, Target,
|
||||
TargetData, TargetKind,
|
||||
|
|
|
@ -6,20 +6,19 @@ use std::{collections::VecDeque, fmt, fs, process::Command};
|
|||
|
||||
use anyhow::{format_err, Context, Result};
|
||||
use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
|
||||
use cargo_workspace::DepKind;
|
||||
use cfg::{CfgDiff, CfgOptions};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
use proc_macro_api::ProcMacroClient;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
|
||||
use crate::{
|
||||
build_data::{BuildDataResult, PackageBuildData, WorkspaceBuildData},
|
||||
cargo_workspace,
|
||||
build_scripts::BuildScriptOutput,
|
||||
cargo_workspace::{DepKind, PackageData, RustcSource},
|
||||
cfg_flag::CfgFlag,
|
||||
rustc_cfg,
|
||||
sysroot::SysrootCrate,
|
||||
utf8_stdout, BuildDataCollector, CargoConfig, CargoWorkspace, ProjectJson, ProjectManifest,
|
||||
Sysroot, TargetKind,
|
||||
utf8_stdout, CargoConfig, CargoWorkspace, ProjectJson, ProjectManifest, Sysroot, TargetKind,
|
||||
WorkspaceBuildScripts,
|
||||
};
|
||||
|
||||
pub type CfgOverrides = FxHashMap<String, CfgDiff>;
|
||||
|
@ -134,7 +133,7 @@ impl ProjectWorkspace {
|
|||
cargo_version
|
||||
)
|
||||
})?;
|
||||
let cargo = CargoWorkspace::new(&cargo_toml, config, meta);
|
||||
let cargo = CargoWorkspace::new(meta);
|
||||
|
||||
let sysroot = if config.no_sysroot {
|
||||
Sysroot::default()
|
||||
|
@ -148,7 +147,6 @@ impl ProjectWorkspace {
|
|||
};
|
||||
|
||||
let rustc_dir = if let Some(rustc_source) = &config.rustc_source {
|
||||
use cargo_workspace::RustcSource;
|
||||
match rustc_source {
|
||||
RustcSource::Path(path) => Some(path.clone()),
|
||||
RustcSource::Discover => Sysroot::discover_rustc(&cargo_toml),
|
||||
|
@ -163,7 +161,7 @@ impl ProjectWorkspace {
|
|||
.with_context(|| {
|
||||
format!("Failed to read Cargo metadata for Rust sources")
|
||||
})?;
|
||||
CargoWorkspace::new(&rustc_dir, config, meta)
|
||||
CargoWorkspace::new(meta)
|
||||
}),
|
||||
None => None,
|
||||
};
|
||||
|
@ -201,7 +199,7 @@ impl ProjectWorkspace {
|
|||
/// Returns the roots for the current `ProjectWorkspace`
|
||||
/// The return type contains the path and whether or not
|
||||
/// the root is a member of the current workspace
|
||||
pub fn to_roots(&self, build_data: Option<&BuildDataResult>) -> Vec<PackageRoot> {
|
||||
pub fn to_roots(&self, build_scripts: &WorkspaceBuildScripts) -> Vec<PackageRoot> {
|
||||
match self {
|
||||
ProjectWorkspace::Json { project, sysroot, rustc_cfg: _ } => project
|
||||
.crates()
|
||||
|
@ -229,10 +227,7 @@ impl ProjectWorkspace {
|
|||
|
||||
let mut include = vec![pkg_root.clone()];
|
||||
include.extend(
|
||||
build_data
|
||||
.and_then(|it| it.get(cargo.workspace_root()))
|
||||
.and_then(|map| map.get(&cargo[pkg].id))
|
||||
.and_then(|it| it.out_dir.clone()),
|
||||
build_scripts.outputs.get(pkg).and_then(|it| it.out_dir.clone()),
|
||||
);
|
||||
|
||||
// In case target's path is manually set in Cargo.toml to be
|
||||
|
@ -307,7 +302,7 @@ impl ProjectWorkspace {
|
|||
|
||||
pub fn to_crate_graph(
|
||||
&self,
|
||||
build_data: Option<&BuildDataResult>,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
proc_macro_client: Option<&ProcMacroClient>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
) -> CrateGraph {
|
||||
|
@ -332,13 +327,9 @@ impl ProjectWorkspace {
|
|||
&proc_macro_loader,
|
||||
load,
|
||||
cargo,
|
||||
build_data.and_then(|it| it.get(cargo.workspace_root())),
|
||||
build_scripts,
|
||||
sysroot,
|
||||
rustc,
|
||||
rustc
|
||||
.as_ref()
|
||||
.zip(build_data)
|
||||
.and_then(|(it, map)| map.get(it.workspace_root())),
|
||||
)
|
||||
}
|
||||
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
|
||||
|
@ -352,15 +343,6 @@ impl ProjectWorkspace {
|
|||
}
|
||||
crate_graph
|
||||
}
|
||||
|
||||
pub fn collect_build_data_configs(&self, collector: &mut BuildDataCollector) {
|
||||
match self {
|
||||
ProjectWorkspace::Cargo { cargo, .. } => {
|
||||
collector.add_config(cargo.workspace_root(), cargo.build_data_config().clone());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn project_json_to_crate_graph(
|
||||
|
@ -435,10 +417,9 @@ fn cargo_to_crate_graph(
|
|||
proc_macro_loader: &dyn Fn(&AbsPath) -> Vec<ProcMacro>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
cargo: &CargoWorkspace,
|
||||
build_data_map: Option<&WorkspaceBuildData>,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
sysroot: &Sysroot,
|
||||
rustc: &Option<CargoWorkspace>,
|
||||
rustc_build_data_map: Option<&WorkspaceBuildData>,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("cargo_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
|
@ -481,7 +462,7 @@ fn cargo_to_crate_graph(
|
|||
let crate_id = add_target_crate_root(
|
||||
&mut crate_graph,
|
||||
&cargo[pkg],
|
||||
build_data_map.and_then(|it| it.get(&cargo[pkg].id)),
|
||||
build_scripts.outputs.get(pkg),
|
||||
&cfg_options,
|
||||
proc_macro_loader,
|
||||
file_id,
|
||||
|
@ -555,7 +536,6 @@ fn cargo_to_crate_graph(
|
|||
rustc_workspace,
|
||||
load,
|
||||
&mut crate_graph,
|
||||
rustc_build_data_map,
|
||||
&cfg_options,
|
||||
proc_macro_loader,
|
||||
&mut pkg_to_lib_crate,
|
||||
|
@ -615,7 +595,6 @@ fn handle_rustc_crates(
|
|||
rustc_workspace: &CargoWorkspace,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
crate_graph: &mut CrateGraph,
|
||||
rustc_build_data_map: Option<&WorkspaceBuildData>,
|
||||
cfg_options: &CfgOptions,
|
||||
proc_macro_loader: &dyn Fn(&AbsPath) -> Vec<ProcMacro>,
|
||||
pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
|
||||
|
@ -651,7 +630,7 @@ fn handle_rustc_crates(
|
|||
let crate_id = add_target_crate_root(
|
||||
crate_graph,
|
||||
&rustc_workspace[pkg],
|
||||
rustc_build_data_map.and_then(|it| it.get(&rustc_workspace[pkg].id)),
|
||||
None,
|
||||
cfg_options,
|
||||
proc_macro_loader,
|
||||
file_id,
|
||||
|
@ -706,8 +685,8 @@ fn handle_rustc_crates(
|
|||
|
||||
fn add_target_crate_root(
|
||||
crate_graph: &mut CrateGraph,
|
||||
pkg: &cargo_workspace::PackageData,
|
||||
build_data: Option<&PackageBuildData>,
|
||||
pkg: &PackageData,
|
||||
build_data: Option<&BuildScriptOutput>,
|
||||
cfg_options: &CfgOptions,
|
||||
proc_macro_loader: &dyn Fn(&AbsPath) -> Vec<ProcMacro>,
|
||||
file_id: FileId,
|
||||
|
@ -726,6 +705,8 @@ fn add_target_crate_root(
|
|||
};
|
||||
|
||||
let mut env = Env::default();
|
||||
inject_cargo_env(pkg, &mut env);
|
||||
|
||||
if let Some(envs) = build_data.map(|it| &it.envs) {
|
||||
for (k, v) in envs {
|
||||
env.set(k, v.clone());
|
||||
|
@ -812,3 +793,40 @@ fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId)
|
|||
log::error!("{}", err)
|
||||
}
|
||||
}
|
||||
|
||||
/// Recreates the compile-time environment variables that Cargo sets.
|
||||
///
|
||||
/// Should be synced with
|
||||
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
|
||||
///
|
||||
/// FIXME: ask Cargo to provide this data instead of re-deriving.
|
||||
fn inject_cargo_env(package: &PackageData, env: &mut Env) {
|
||||
// FIXME: Missing variables:
|
||||
// CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
|
||||
|
||||
let mut manifest_dir = package.manifest.clone();
|
||||
manifest_dir.pop();
|
||||
env.set("CARGO_MANIFEST_DIR".into(), manifest_dir.as_os_str().to_string_lossy().into_owned());
|
||||
|
||||
// Not always right, but works for common cases.
|
||||
env.set("CARGO".into(), "cargo".into());
|
||||
|
||||
env.set("CARGO_PKG_VERSION".into(), package.version.to_string());
|
||||
env.set("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string());
|
||||
env.set("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string());
|
||||
env.set("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string());
|
||||
env.set("CARGO_PKG_VERSION_PRE".into(), package.version.pre.to_string());
|
||||
|
||||
env.set("CARGO_PKG_AUTHORS".into(), String::new());
|
||||
|
||||
env.set("CARGO_PKG_NAME".into(), package.name.clone());
|
||||
// FIXME: This isn't really correct (a package can have many crates with different names), but
|
||||
// it's better than leaving the variable unset.
|
||||
env.set("CARGO_CRATE_NAME".into(), CrateName::normalize_dashes(&package.name).to_string());
|
||||
env.set("CARGO_PKG_DESCRIPTION".into(), String::new());
|
||||
env.set("CARGO_PKG_HOMEPAGE".into(), String::new());
|
||||
env.set("CARGO_PKG_REPOSITORY".into(), String::new());
|
||||
env.set("CARGO_PKG_LICENSE".into(), String::new());
|
||||
|
||||
env.set("CARGO_PKG_LICENSE_FILE".into(), String::new());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue