Skip to content

Commit

Permalink
feat: 添加target_arch字段,实现根据不同的架构来自动编译对应的包。
Browse files Browse the repository at this point in the history
  • Loading branch information
fslongjin committed Apr 27, 2024
1 parent bb3f6fb commit af65f61
Show file tree
Hide file tree
Showing 18 changed files with 925 additions and 207 deletions.
13 changes: 13 additions & 0 deletions src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::path::PathBuf;

use clap::{Parser, Subcommand};

use crate::parser::task::TargetArch;

use self::clean::CleanArg;

#[derive(Debug, Parser, Clone)]
Expand All @@ -43,6 +45,9 @@ pub struct CommandLineArgs {
/// DADK任务并行线程数量
#[arg(short, long)]
pub thread: Option<usize>,

#[arg(long, value_parser = parse_target_arch)]
pub target_arch: Option<TargetArch>,
}

/// @brief 检查目录是否存在
Expand All @@ -58,6 +63,14 @@ fn parse_check_dir_exists(path: &str) -> Result<PathBuf, String> {
return Ok(path);
}

fn parse_target_arch(s: &str) -> Result<TargetArch, String> {
let x = TargetArch::try_from(s);
if x.is_err() {
return Err(format!("Invalid target arch: {}", s));
}
return Ok(x.unwrap());
}

Check warning on line 72 in src/console/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/console/mod.rs#L66-L72

Added lines #L66 - L72 were not covered by tests

/// @brief 要执行的操作
#[derive(Debug, Subcommand, Clone, Copy, PartialEq, Eq)]
pub enum Action {
Expand Down
62 changes: 61 additions & 1 deletion src/console/new_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
parser::task::{
BuildConfig, CleanConfig, CodeSource, DADKTask, Dependency, InstallConfig, PrebuiltSource,
TaskEnv, TaskType,
TargetArch, TaskEnv, TaskType,
},
};

Expand Down Expand Up @@ -122,6 +122,9 @@ impl NewConfigCommand {
let install_once = BoolInput::new("Install this task once?".to_string(), None).input()?;
debug!("install_once: {:?}", install_once);

let target_arch = TargetArchInput::new().input()?;
debug!("target_arch: {:?}", target_arch);

Check warning on line 126 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L125-L126

Added lines #L125 - L126 were not covered by tests

let mut dadk: DADKTask = DADKTask::new(
name,
version,
Expand All @@ -135,6 +138,7 @@ impl NewConfigCommand {
task_env,
build_once,
install_once,
target_arch,

Check warning on line 141 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L141

Added line #L141 was not covered by tests
);

dadk.trim();
Expand Down Expand Up @@ -743,3 +747,59 @@ impl InputFunc<TaskEnv> for TaskEnvInputOne {
return Ok(env);
}
}

/// # 输入目标架构
///
/// 可选值参考:[TargetArch](crate::parser::task::TargetArch::EXPECTED)
#[derive(Debug)]
struct TargetArchInput;

impl TargetArchInput {
pub fn new() -> Self {
Self {}
}

Check warning on line 760 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L758-L760

Added lines #L758 - L760 were not covered by tests
}

impl InputFunc<Option<Vec<TargetArch>>> for TargetArchInput {
fn input(&mut self) -> Result<Option<Vec<TargetArch>>, ConsoleError> {
const TIPS: &str = "Please configure the [ available target arch ] of the task:";
println!();
println!("{TIPS}");
let env_reader: Rc<RefCell<TargetArchInputOne>> =
Rc::new(RefCell::new(TargetArchInputOne::new()));
let mut vecinput: VecInput<TargetArch> = VecInput::new(Some(TIPS.to_string()), env_reader);
vecinput.input()?;
let result = vecinput.results()?.clone();

if result.is_empty() {
return Ok(None);
}

return Ok(Some(result));
}

Check warning on line 779 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L764-L779

Added lines #L764 - L779 were not covered by tests
}

#[derive(Debug)]
struct TargetArchInputOne;

impl TargetArchInputOne {
pub fn new() -> Self {
Self
}

Check warning on line 788 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L786-L788

Added lines #L786 - L788 were not covered by tests

fn input_one(&self) -> Result<TargetArch, ConsoleError> {
let s = Input::new(Some("Please input one target arch:".to_string()), None).input()?;

Check warning on line 791 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L790-L791

Added lines #L790 - L791 were not covered by tests

let target_arch = TargetArch::try_from(s.as_str()).map_err(|e| {
ConsoleError::InvalidInput(format!("Invalid target arch: {}", e.to_string()))
})?;
return Ok(target_arch);
}

Check warning on line 797 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L793-L797

Added lines #L793 - L797 were not covered by tests
}

impl InputFunc<TargetArch> for TargetArchInputOne {
fn input(&mut self) -> Result<TargetArch, ConsoleError> {
let env = self.input_one()?;
return Ok(env);
}

Check warning on line 804 in src/console/new_config.rs

View check run for this annotation

Codecov / codecov/patch

src/console/new_config.rs#L801-L804

Added lines #L801 - L804 were not covered by tests
}
115 changes: 87 additions & 28 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::{path::PathBuf, process::exit};
use std::{
path::PathBuf,
process::exit,
sync::{Arc, Mutex, Weak},
};

use derive_builder::Builder;
use log::error;
#[cfg(test)]
use test_base::{test_context::TestContext, BaseTestContext};

use crate::{console::Action, executor::cache::cache_root_init, scheduler::task_deque::TASK_DEQUE};
use crate::{
console::Action, executor::cache::cache_root_init, parser::task::TargetArch,
scheduler::task_deque::TASK_DEQUE,
};

#[derive(Debug, Builder)]
#[builder(setter(into))]
Expand All @@ -21,12 +28,21 @@ pub struct DadkExecuteContext {
/// dadk缓存根目录
cache_dir: Option<PathBuf>,

/// 目标架构
#[builder(default = "crate::DADKTask::default_target_arch()")]
target_arch: TargetArch,

#[cfg(test)]
base_test_context: Option<BaseTestContext>,

#[builder(setter(skip), default = "Mutex::new(Weak::new())")]
self_ref: Mutex<Weak<Self>>,
}

impl DadkExecuteContext {
pub fn init(&self) {
pub fn init(&self, self_arc: Arc<Self>) {
self.set_self_ref(Arc::downgrade(&self_arc));

// 初始化缓存目录
let r: Result<(), crate::executor::ExecutorError> =
cache_root_init(self.cache_dir().cloned());
Expand Down Expand Up @@ -56,6 +72,20 @@ impl DadkExecuteContext {
exit(1);
}
}

#[allow(dead_code)]
pub fn self_ref(&self) -> Option<Arc<Self>> {
self.self_ref.lock().unwrap().upgrade()
}

fn set_self_ref(&self, self_ref: Weak<Self>) {
*self.self_ref.lock().unwrap() = self_ref;
}

pub fn target_arch(&self) -> &TargetArch {
&self.target_arch
}

pub fn sysroot_dir(&self) -> Option<&PathBuf> {
self.sysroot_dir.as_ref()
}
Expand Down Expand Up @@ -84,41 +114,65 @@ pub trait TestContextExt: TestContext {
fn execute_context(&self) -> &DadkExecuteContext;
}

impl DadkExecuteContextBuilder {
/// 用于测试的默认构建器
#[cfg(test)]
fn default_test_execute_context_builder(
base_context: &BaseTestContext,
) -> DadkExecuteContextBuilder {
DadkExecuteContextBuilder::default()
.sysroot_dir(Some(base_context.fake_dragonos_sysroot()))
.action(Action::Build)
.thread_num(None)
.cache_dir(Some(base_context.fake_dadk_cache_root()))
.base_test_context(Some(base_context.clone()))
.clone()
}
}

#[cfg(test)]
pub struct DadkExecuteContextTestBuildV1 {
context: DadkExecuteContext,
pub struct DadkExecuteContextTestBuildX86_64V1 {
context: Arc<DadkExecuteContext>,
}

#[cfg(test)]
impl TestContext for DadkExecuteContextTestBuildV1 {
impl TestContext for DadkExecuteContextTestBuildX86_64V1 {
fn setup() -> Self {
let base_context = BaseTestContext::setup();
let context = DadkExecuteContextBuilder::default()
.sysroot_dir(Some(base_context.fake_dragonos_sysroot()))
.config_dir(Some(base_context.config_v1_dir()))
.action(Action::Build)
.thread_num(None)
.cache_dir(Some(base_context.fake_dadk_cache_root()))
.base_test_context(Some(base_context))
.build()
.expect("Failed to build DadkExecuteContextTestBuildV1");
context.init();
DadkExecuteContextTestBuildV1 { context }
let context =
DadkExecuteContextBuilder::default_test_execute_context_builder(&base_context)
.target_arch(TargetArch::X86_64)
.config_dir(Some(base_context.config_v1_dir()))
.build()
.expect("Failed to build DadkExecuteContextTestBuildX86_64V1");
let context = Arc::new(context);
context.init(context.clone());
DadkExecuteContextTestBuildX86_64V1 { context }
}
}

#[cfg(test)]
impl TestContextExt for DadkExecuteContextTestBuildV1 {
fn base_context(&self) -> &BaseTestContext {
self.base_test_context.as_ref().unwrap()
}
pub struct DadkExecuteContextTestBuildRiscV64V1 {
context: Arc<DadkExecuteContext>,
}

fn execute_context(&self) -> &DadkExecuteContext {
&self.context
#[cfg(test)]
impl TestContext for DadkExecuteContextTestBuildRiscV64V1 {
fn setup() -> Self {
let base_context = BaseTestContext::setup();
let context =
DadkExecuteContextBuilder::default_test_execute_context_builder(&base_context)
.target_arch(TargetArch::RiscV64)
.config_dir(Some(base_context.config_v1_dir()))
.build()
.expect("Failed to build DadkExecuteContextTestBuildRiscV64V1");
let context = Arc::new(context);
context.init(context.clone());
DadkExecuteContextTestBuildRiscV64V1 { context }
}
}

macro_rules! impl_deref_for_test_context {
macro_rules! impl_for_test_context {
($context:ty) => {
#[cfg(test)]
impl std::ops::Deref for $context {
Expand All @@ -130,12 +184,17 @@ macro_rules! impl_deref_for_test_context {
}

#[cfg(test)]
impl std::ops::DerefMut for $context {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.context
impl TestContextExt for $context {
fn base_context(&self) -> &BaseTestContext {
self.base_test_context.as_ref().unwrap()
}

fn execute_context(&self) -> &DadkExecuteContext {
&self.context
}
}
};
}

impl_deref_for_test_context!(DadkExecuteContextTestBuildV1);
impl_for_test_context!(DadkExecuteContextTestBuildX86_64V1);
impl_for_test_context!(DadkExecuteContextTestBuildRiscV64V1);
Loading

0 comments on commit af65f61

Please sign in to comment.