Supporting running OSDK commands in workspace root

This commit is contained in:
Jianfeng Jiang
2024-03-06 09:40:48 +00:00
committed by Tate, Hongliang Tian
parent c7b7e2473f
commit aaf101a53e
8 changed files with 167 additions and 29 deletions

View File

@ -2,16 +2,19 @@
use std::{
ffi::OsStr,
fs,
path::{Path, PathBuf},
process::Command,
};
use crate::{error::Errno, error_msg};
use quote::ToTokens;
/// FIXME: We should publish the asterinas crates to a public registry
/// and use the published version in the generated Cargo.toml.
pub const ASTER_GIT_LINK: &str = "https://github.com/asterinas/asterinas";
pub const ASTER_GIT_REV: &str = "7d0ea99";
pub const ASTER_GIT_REV: &str = "437ab80";
pub fn aster_crate_dep(crate_name: &str) -> String {
format!(
"{} = {{ git = \"{}\", rev = \"{}\" }}",
@ -78,16 +81,90 @@ pub struct CrateInfo {
pub path: String,
}
/// Retrieve the default member in the workspace.
///
/// If there is only one kernel crate, return that crate;
/// If there are multiple kernel crates or no kernel crates in the workspace,
/// this function will exit with an error.
///
/// A crate is considered a kernel crate if it utilizes the `aster_main` macro.
fn get_default_member(metadata: &serde_json::Value) -> &str {
let default_members = metadata
.get("workspace_default_members")
.unwrap()
.as_array()
.unwrap();
if default_members.len() == 1 {
return default_members[0].as_str().unwrap();
}
let packages: Vec<_> = {
let packages = metadata.get("packages").unwrap().as_array().unwrap();
packages
.iter()
.filter(|package| {
let id = package.get("id").unwrap();
if !default_members.contains(&id) {
return false;
}
let src_path = {
let targets = package.get("targets").unwrap().as_array().unwrap();
if targets.len() != 1 {
return false;
}
targets[0].get("src_path").unwrap().as_str().unwrap()
};
let file = {
let content = fs::read_to_string(src_path).unwrap();
syn::parse_file(&content).unwrap()
};
contains_aster_main_macro(&file)
})
.collect()
};
if packages.len() == 0 {
error_msg!("OSDK requires there's at least one kernel package. Please navigate to the kernel package directory or the workspace root and run the command.");
std::process::exit(Errno::BuildCrate as _);
}
if packages.len() >= 2 {
error_msg!("OSDK requires there's at most one kernel package in the workspace. Please navigate to the kernel package directory and run the command.");
std::process::exit(Errno::BuildCrate as _);
}
packages[0].get("id").unwrap().as_str().unwrap()
}
fn contains_aster_main_macro(file: &syn::File) -> bool {
for item in &file.items {
let syn::Item::Fn(item_fn) = item else {
continue;
};
for attr in &item_fn.attrs {
let attr = format!("{}", attr.to_token_stream());
if attr.as_str() == "# [aster_main]" {
return true;
}
}
}
false
}
pub fn get_current_crate_info() -> CrateInfo {
let metadata = get_cargo_metadata(None::<&str>, None::<&[&str]>).unwrap();
let default_members = metadata.get("workspace_default_members").unwrap();
assert_eq!(default_members.as_array().unwrap().len(), 1);
let default_member = get_default_member(&metadata);
// The default member string here is in the form of "<crate_name> <crate_version> (path+file://<crate_path>)"
let default_member = default_members[0]
.as_str()
.unwrap()
.split(' ')
.collect::<Vec<&str>>();
let default_member = default_member.split(' ').collect::<Vec<&str>>();
let name = default_member[0].to_string();
let version = default_member[1].to_string();
let path = default_member[2]