Fix OSDK feature selection

This commit is contained in:
Zhang Junyang 2024-04-13 20:11:32 +08:00 committed by Tate, Hongliang Tian
parent faacb68210
commit f9ac2ee498
6 changed files with 72 additions and 16 deletions

View File

@ -26,7 +26,7 @@ pub fn new_base_crate(
std::fs::remove_dir_all(&base_crate_path).unwrap(); std::fs::remove_dir_all(&base_crate_path).unwrap();
} }
let dep_crate_version = { let (dep_crate_version, dep_crate_features) = {
let cargo_toml = dep_crate_path.as_ref().join("Cargo.toml"); let cargo_toml = dep_crate_path.as_ref().join("Cargo.toml");
let cargo_toml = fs::read_to_string(cargo_toml).unwrap(); let cargo_toml = fs::read_to_string(cargo_toml).unwrap();
let cargo_toml: toml::Value = toml::from_str(&cargo_toml).unwrap(); let cargo_toml: toml::Value = toml::from_str(&cargo_toml).unwrap();
@ -38,8 +38,13 @@ pub fn new_base_crate(
.get("version") .get("version")
.unwrap() .unwrap()
.as_str() .as_str()
.unwrap(); .unwrap()
dep_version.to_string() .to_string();
let dep_features = cargo_toml
.get("features")
.map(|f| f.as_table().unwrap().clone())
.unwrap_or_default();
(dep_version, dep_features)
}; };
// Create the directory // Create the directory
@ -82,6 +87,9 @@ pub fn new_base_crate(
// Copy the manifest configurations from the target crate to the base crate // Copy the manifest configurations from the target crate to the base crate
copy_profile_configurations(workspace_root); copy_profile_configurations(workspace_root);
// Generate the features by copying the features from the target crate
add_feature_entries(dep_crate_name, &dep_crate_features);
// Get back to the original directory // Get back to the original directory
std::env::set_current_dir(original_dir).unwrap(); std::env::set_current_dir(original_dir).unwrap();
} }
@ -105,7 +113,7 @@ fn add_manifest_dependency(crate_name: &str, crate_path: impl AsRef<Path>) {
let dependencies = manifest.get_mut("dependencies").unwrap(); let dependencies = manifest.get_mut("dependencies").unwrap();
let dep = toml::Table::from_str(&format!( let dep = toml::Table::from_str(&format!(
"{} = {{ path = \"{}\"}}", "{} = {{ path = \"{}\", default-features = false }}",
crate_name, crate_name,
crate_path.as_ref().display() crate_path.as_ref().display()
)) ))
@ -133,9 +141,38 @@ fn copy_profile_configurations(workspace_root: impl AsRef<Path>) {
// Copy the profile configurations // Copy the profile configurations
let profile = target_manifest.get("profile"); let profile = target_manifest.get("profile");
if let Some(profile) = profile { if let Some(profile) = profile {
manifest.insert("profile".to_string(), profile.clone()); manifest.insert(
"profile".to_string(),
toml::Value::Table(profile.as_table().unwrap().clone()),
);
} }
let content = toml::to_string(&manifest).unwrap(); let content = toml::to_string(&manifest).unwrap();
fs::write(manifest_path, content).unwrap(); fs::write(manifest_path, content).unwrap();
} }
fn add_feature_entries(dep_crate_name: &str, features: &toml::Table) {
let manifest_path = "Cargo.toml";
let mut manifest: toml::Table = {
let content = fs::read_to_string(manifest_path).unwrap();
toml::from_str(&content).unwrap()
};
let mut table = toml::Table::new();
for (feature, value) in features.iter() {
let value = if feature != &"default".to_string() {
vec![toml::Value::String(format!(
"{}/{}",
dep_crate_name, feature
))]
} else {
value.as_array().unwrap().clone()
};
table.insert(feature.clone(), toml::Value::Array(value));
}
manifest.insert("features".to_string(), toml::Value::Table(table));
let content = toml::to_string(&manifest).unwrap();
fs::write(manifest_path, content).unwrap();
}

View File

@ -26,7 +26,7 @@ pub fn main() {
}; };
let load_config = || { let load_config = || {
let manifest = TomlManifest::load(&common_args.build_args.features); let manifest = TomlManifest::load();
let scheme = manifest.get_scheme(common_args.scheme.as_ref()); let scheme = manifest.get_scheme(common_args.scheme.as_ref());
Config::new(scheme, &common_args) Config::new(scheme, &common_args)
}; };
@ -233,6 +233,8 @@ pub struct CargoArgs {
global = true, global = true,
)] )]
pub features: Vec<String>, pub features: Vec<String>,
#[arg(long, help = "Do not activate the `default` features", global = true)]
pub no_default_features: bool,
} }
impl CargoArgs { impl CargoArgs {

View File

@ -143,20 +143,20 @@ pub fn do_build(
} }
let mut bundle = Bundle::new(&bundle_path, config, action); let mut bundle = Bundle::new(&bundle_path, config, action);
info!("Building kernel ELF"); let (build, boot) = match action {
ActionChoice::Run => (&config.run.build, &config.run.boot),
ActionChoice::Test => (&config.test.build, &config.test.boot),
};
let aster_elf = build_kernel_elf( let aster_elf = build_kernel_elf(
&config.target_arch, &config.target_arch,
&config.build.profile, &build.profile,
&config.build.features[..], &build.features[..],
build.no_default_features,
&cargo_target_directory, &cargo_target_directory,
rustflags, rustflags,
); );
let boot = match action {
ActionChoice::Run => &config.run.boot,
ActionChoice::Test => &config.test.boot,
};
match boot.method { match boot.method {
BootMethod::GrubRescueIso => { BootMethod::GrubRescueIso => {
info!("Building boot device image"); info!("Building boot device image");
@ -185,6 +185,7 @@ fn build_kernel_elf(
arch: &Arch, arch: &Arch,
profile: &str, profile: &str,
features: &[String], features: &[String],
no_default_features: bool,
cargo_target_directory: impl AsRef<Path>, cargo_target_directory: impl AsRef<Path>,
rustflags: &[&str], rustflags: &[&str],
) -> AsterBin { ) -> AsterBin {
@ -209,12 +210,18 @@ fn build_kernel_elf(
command.env("RUSTFLAGS", rustflags.join(" ")); command.env("RUSTFLAGS", rustflags.join(" "));
command.arg("build"); command.arg("build");
command.arg("--features").arg(features.join(" ")); command.arg("--features").arg(features.join(" "));
if no_default_features {
command.arg("--no-default-features");
}
command.arg("--target").arg(&target_os_string); command.arg("--target").arg(&target_os_string);
command command
.arg("--target-dir") .arg("--target-dir")
.arg(cargo_target_directory.as_ref()); .arg(cargo_target_directory.as_ref());
command.args(COMMON_CARGO_ARGS); command.args(COMMON_CARGO_ARGS);
command.arg("--profile=".to_string() + profile); command.arg("--profile=".to_string() + profile);
info!("Building kernel ELF using command: {:#?}", command);
let status = command.status().unwrap(); let status = command.status().unwrap();
if !status.success() { if !status.success() {
error_msg!("Cargo build failed"); error_msg!("Cargo build failed");

View File

@ -38,9 +38,9 @@ pub struct TomlManifest {
} }
impl TomlManifest { impl TomlManifest {
pub fn load(feature_strings: &Vec<String>) -> Self { pub fn load() -> Self {
let workspace_root = { let workspace_root = {
let cargo_metadata = get_cargo_metadata(None::<&str>, Some(feature_strings)).unwrap(); let cargo_metadata = get_cargo_metadata(None::<&str>, None::<&[&str]>).unwrap();
PathBuf::from( PathBuf::from(
cargo_metadata cargo_metadata
.get("workspace_root") .get("workspace_root")

View File

@ -42,6 +42,9 @@ fn apply_args_before_finalize(action_scheme: &mut ActionScheme, args: &CommonArg
build.profile = Some(profile.clone()); build.profile = Some(profile.clone());
} }
build.features.extend(args.build_args.features.clone()); build.features.extend(args.build_args.features.clone());
if args.build_args.no_default_features {
build.no_default_features = true;
}
if args.linux_x86_legacy_boot { if args.linux_x86_legacy_boot {
build.linux_x86_legacy_boot = true; build.linux_x86_legacy_boot = true;
} }

View File

@ -14,6 +14,8 @@ pub enum ActionChoice {
pub struct BuildScheme { pub struct BuildScheme {
pub profile: Option<String>, pub profile: Option<String>,
pub features: Vec<String>, pub features: Vec<String>,
#[serde(default)]
pub no_default_features: bool,
/// Whether to turn on the support for the /// Whether to turn on the support for the
/// [Linux legacy x86 32-bit boot protocol](https://www.kernel.org/doc/html/v5.6/x86/boot.html) /// [Linux legacy x86 32-bit boot protocol](https://www.kernel.org/doc/html/v5.6/x86/boot.html)
#[serde(default)] #[serde(default)]
@ -25,6 +27,8 @@ pub struct Build {
pub profile: String, pub profile: String,
pub features: Vec<String>, pub features: Vec<String>,
#[serde(default)] #[serde(default)]
pub no_default_features: bool,
#[serde(default)]
pub linux_x86_legacy_boot: bool, pub linux_x86_legacy_boot: bool,
} }
@ -33,6 +37,7 @@ impl Default for Build {
Self { Self {
profile: "dev".to_string(), profile: "dev".to_string(),
features: Vec::new(), features: Vec::new(),
no_default_features: false,
linux_x86_legacy_boot: false, linux_x86_legacy_boot: false,
} }
} }
@ -48,6 +53,7 @@ impl BuildScheme {
features.extend(self.features.clone()); features.extend(self.features.clone());
features features
}; };
// no_default_features is not inherited
if parent.linux_x86_legacy_boot { if parent.linux_x86_legacy_boot {
self.linux_x86_legacy_boot = true; self.linux_x86_legacy_boot = true;
} }
@ -57,6 +63,7 @@ impl BuildScheme {
Build { Build {
profile: self.profile.unwrap_or_else(|| "dev".to_string()), profile: self.profile.unwrap_or_else(|| "dev".to_string()),
features: self.features, features: self.features,
no_default_features: self.no_default_features,
linux_x86_legacy_boot: self.linux_x86_legacy_boot, linux_x86_legacy_boot: self.linux_x86_legacy_boot,
} }
} }