use crate::core::compiler::unit_dependencies::IsArtifact;
use crate::core::compiler::UnitInterner;
use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::profiles::{Profiles, UnitFor};
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::core::resolver::HasDevUnits;
use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace};
use crate::ops::{self, Packages};
use crate::util::errors::CargoResult;
use crate::Config;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use super::BuildConfig;
pub fn parse_unstable_flag(value: Option<&str>) -> Vec<String> {
let value = value.unwrap_or("std");
let mut crates: HashSet<&str> = value.split(',').collect();
if crates.contains("std") {
crates.insert("core");
crates.insert("alloc");
crates.insert("proc_macro");
crates.insert("panic_unwind");
crates.insert("compiler_builtins");
} else if crates.contains("core") {
crates.insert("compiler_builtins");
}
crates.into_iter().map(|s| s.to_string()).collect()
}
pub(crate) fn std_crates(config: &Config, units: Option<&[Unit]>) -> Option<Vec<String>> {
let crates = config.cli_unstable().build_std.as_ref()?.clone();
let mut crates = crates.clone();
if let Some(units) = units {
if units
.iter()
.any(|unit| unit.mode.is_rustc_test() && unit.target.harness())
{
if crates.iter().any(|c| c == "std") && !crates.iter().any(|c| c == "test") {
crates.push("test".to_string());
}
}
} else {
if !crates.iter().any(|c| c == "test") {
crates.push("test".to_string())
}
}
Some(crates)
}
pub fn resolve_std<'cfg>(
ws: &Workspace<'cfg>,
target_data: &mut RustcTargetData<'cfg>,
build_config: &BuildConfig,
crates: &[String],
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
if build_config.build_plan {
ws.config()
.shell()
.warn("-Zbuild-std does not currently fully support --build-plan")?;
}
let src_path = detect_sysroot_src_path(target_data)?;
let to_patch = [
"rustc-std-workspace-core",
"rustc-std-workspace-alloc",
"rustc-std-workspace-std",
];
let patches = to_patch
.iter()
.map(|&name| {
let source_path = SourceId::for_path(&src_path.join("library").join(name))?;
let dep = Dependency::parse(name, None, source_path)?;
Ok(dep)
})
.collect::<CargoResult<Vec<_>>>()?;
let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
let patch = HashMap::from([(crates_io_url, patches)]);
let members = vec![
String::from("library/std"),
String::from("library/core"),
String::from("library/alloc"),
String::from("library/sysroot"),
];
let ws_config = crate::core::WorkspaceConfig::Root(crate::core::WorkspaceRootConfig::new(
&src_path,
&Some(members),
&None,
&None,
&None,
&None,
));
let virtual_manifest = crate::core::VirtualManifest::new(
Vec::new(),
patch,
ws_config,
None,
crate::core::Features::default(),
None,
);
let config = ws.config();
let current_manifest = src_path.join("library/sysroot/Cargo.toml");
let mut std_ws = Workspace::new_virtual(src_path, current_manifest, virtual_manifest, config)?;
std_ws.set_require_optional_deps(false);
let mut spec_pkgs = Vec::from(crates);
spec_pkgs.push("sysroot".to_string());
let spec = Packages::Packages(spec_pkgs);
let specs = spec.to_package_id_specs(&std_ws)?;
let features = match &config.cli_unstable().build_std_features {
Some(list) => list.clone(),
None => vec![
"panic-unwind".to_string(),
"backtrace".to_string(),
"default".to_string(),
],
};
let cli_features = CliFeatures::from_command_line(
&features, false, false,
)?;
let max_rust_version = ws.rust_version();
let resolve = ops::resolve_ws_with_opts(
&std_ws,
target_data,
&build_config.requested_kinds,
&cli_features,
&specs,
HasDevUnits::No,
crate::core::resolver::features::ForceAllTargets::No,
max_rust_version,
)?;
Ok((
resolve.pkg_set,
resolve.targeted_resolve,
resolve.resolved_features,
))
}
pub fn generate_std_roots(
crates: &[String],
std_resolve: &Resolve,
std_features: &ResolvedFeatures,
kinds: &[CompileKind],
package_set: &PackageSet<'_>,
interner: &UnitInterner,
profiles: &Profiles,
) -> CargoResult<HashMap<CompileKind, Vec<Unit>>> {
let std_ids = crates
.iter()
.map(|crate_name| std_resolve.query(crate_name))
.collect::<CargoResult<Vec<PackageId>>>()?;
let std_pkgs = package_set.get_many(std_ids)?;
let mut ret = HashMap::new();
for pkg in std_pkgs {
let lib = pkg
.targets()
.iter()
.find(|t| t.is_lib())
.expect("std has a lib");
let mode = CompileMode::Build;
let features = std_features.activated_features(pkg.package_id(), FeaturesFor::NormalOrDev);
for kind in kinds {
let list = ret.entry(*kind).or_insert_with(Vec::new);
let unit_for = UnitFor::new_normal(*kind);
let profile = profiles.get_profile(
pkg.package_id(),
false,
false,
unit_for,
*kind,
);
list.push(interner.intern(
pkg,
lib,
profile,
*kind,
mode,
features.clone(),
true,
0,
IsArtifact::No,
None,
));
}
}
Ok(ret)
}
fn detect_sysroot_src_path(target_data: &RustcTargetData<'_>) -> CargoResult<PathBuf> {
if let Some(s) = target_data.config.get_env_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
return Ok(s.into());
}
let src_path = target_data
.info(CompileKind::Host)
.sysroot
.join("lib")
.join("rustlib")
.join("src")
.join("rust");
let lock = src_path.join("Cargo.lock");
if !lock.exists() {
let msg = format!(
"{:?} does not exist, unable to build with the standard \
library, try:\n rustup component add rust-src",
lock
);
match target_data.config.get_env("RUSTUP_TOOLCHAIN") {
Ok(rustup_toolchain) => {
anyhow::bail!("{} --toolchain {}", msg, rustup_toolchain);
}
Err(_) => {
anyhow::bail!(msg);
}
}
}
Ok(src_path)
}