173779f3dSLoGin use std::{
273779f3dSLoGin path::PathBuf,
373779f3dSLoGin process::exit,
473779f3dSLoGin sync::{Arc, Mutex, Weak},
573779f3dSLoGin };
673779f3dSLoGin
7*eaa67f3cSLoGin use dadk_config::{common::target_arch::TargetArch, user::UserCleanLevel};
873779f3dSLoGin use derive_builder::Builder;
973779f3dSLoGin use log::error;
1073779f3dSLoGin #[cfg(test)]
11e2cc487bSLoGin use test_base::{global::BaseGlobalTestContext, test_context::TestContext};
1273779f3dSLoGin
13*eaa67f3cSLoGin use crate::{executor::cache::cache_root_init, scheduler::task_deque::TASK_DEQUE};
1473779f3dSLoGin
1573779f3dSLoGin #[derive(Debug, Builder)]
1673779f3dSLoGin #[builder(setter(into))]
1773779f3dSLoGin pub struct DadkUserExecuteContext {
1873779f3dSLoGin /// DragonOS sysroot在主机上的路径
1973779f3dSLoGin sysroot_dir: Option<PathBuf>,
2073779f3dSLoGin /// DADK任务配置文件所在目录
2173779f3dSLoGin config_dir: Option<PathBuf>,
2273779f3dSLoGin /// 要执行的操作
2373779f3dSLoGin action: Action,
2473779f3dSLoGin /// 并行线程数量
2573779f3dSLoGin thread_num: Option<usize>,
2673779f3dSLoGin /// dadk缓存根目录
2773779f3dSLoGin cache_dir: Option<PathBuf>,
2873779f3dSLoGin
2973779f3dSLoGin /// 目标架构
3073779f3dSLoGin #[builder(default = "crate::DADKTask::default_target_arch()")]
3173779f3dSLoGin target_arch: TargetArch,
3273779f3dSLoGin
3373779f3dSLoGin #[cfg(test)]
34e2cc487bSLoGin base_test_context: Option<BaseGlobalTestContext>,
3573779f3dSLoGin
3673779f3dSLoGin #[builder(setter(skip), default = "Mutex::new(Weak::new())")]
3773779f3dSLoGin self_ref: Mutex<Weak<Self>>,
3873779f3dSLoGin }
3973779f3dSLoGin
4073779f3dSLoGin impl DadkUserExecuteContext {
4173779f3dSLoGin pub fn init(&self, self_arc: Arc<Self>) {
4273779f3dSLoGin self.set_self_ref(Arc::downgrade(&self_arc));
init(&self, self_arc: Arc<Self>)4373779f3dSLoGin
4473779f3dSLoGin // 初始化缓存目录
4573779f3dSLoGin let r: Result<(), crate::executor::ExecutorError> =
4673779f3dSLoGin cache_root_init(self.cache_dir().cloned());
4773779f3dSLoGin if r.is_err() {
4873779f3dSLoGin error!("Failed to init cache root: {:?}", r.unwrap_err());
4973779f3dSLoGin exit(1);
5073779f3dSLoGin }
5173779f3dSLoGin
5273779f3dSLoGin if let Some(thread) = self.thread_num() {
5373779f3dSLoGin TASK_DEQUE.lock().unwrap().set_thread(thread);
5473779f3dSLoGin }
5573779f3dSLoGin
5673779f3dSLoGin if self.config_dir().is_none() {
5773779f3dSLoGin error!("Config dir is required for action: {:?}", self.action());
5873779f3dSLoGin exit(1);
5973779f3dSLoGin }
6073779f3dSLoGin
6173779f3dSLoGin if self.sysroot_dir().is_none() {
6273779f3dSLoGin error!(
6373779f3dSLoGin "dragonos sysroot dir is required for action: {:?}",
6473779f3dSLoGin self.action()
6573779f3dSLoGin );
6673779f3dSLoGin exit(1);
6773779f3dSLoGin }
6873779f3dSLoGin }
6973779f3dSLoGin
7073779f3dSLoGin #[allow(dead_code)]
7173779f3dSLoGin pub fn self_ref(&self) -> Option<Arc<Self>> {
7273779f3dSLoGin self.self_ref.lock().unwrap().upgrade()
7373779f3dSLoGin }
7473779f3dSLoGin
7573779f3dSLoGin fn set_self_ref(&self, self_ref: Weak<Self>) {
7673779f3dSLoGin *self.self_ref.lock().unwrap() = self_ref;
self_ref(&self) -> Option<Arc<Self>>7773779f3dSLoGin }
7873779f3dSLoGin
7973779f3dSLoGin pub fn target_arch(&self) -> &TargetArch {
8073779f3dSLoGin &self.target_arch
8173779f3dSLoGin }
8273779f3dSLoGin
8373779f3dSLoGin pub fn sysroot_dir(&self) -> Option<&PathBuf> {
8473779f3dSLoGin self.sysroot_dir.as_ref()
target_arch(&self) -> &TargetArch8573779f3dSLoGin }
8673779f3dSLoGin
8773779f3dSLoGin pub fn config_dir(&self) -> Option<&PathBuf> {
8873779f3dSLoGin self.config_dir.as_ref()
8973779f3dSLoGin }
9073779f3dSLoGin
9173779f3dSLoGin pub fn action(&self) -> &Action {
9273779f3dSLoGin &self.action
config_dir(&self) -> Option<&PathBuf>9373779f3dSLoGin }
9473779f3dSLoGin
9573779f3dSLoGin pub fn thread_num(&self) -> Option<usize> {
9673779f3dSLoGin self.thread_num
9773779f3dSLoGin }
9873779f3dSLoGin
9973779f3dSLoGin pub fn cache_dir(&self) -> Option<&PathBuf> {
10073779f3dSLoGin self.cache_dir.as_ref()
thread_num(&self) -> Option<usize>10173779f3dSLoGin }
10273779f3dSLoGin }
10373779f3dSLoGin
10473779f3dSLoGin #[cfg(test)]
10573779f3dSLoGin pub trait TestContextExt: TestContext {
106e2cc487bSLoGin fn base_context(&self) -> &BaseGlobalTestContext;
10773779f3dSLoGin
10873779f3dSLoGin fn execute_context(&self) -> &DadkUserExecuteContext;
10973779f3dSLoGin }
11073779f3dSLoGin
11173779f3dSLoGin impl DadkUserExecuteContextBuilder {
base_context(&self) -> &BaseTestContext11273779f3dSLoGin /// 用于测试的默认构建器
11373779f3dSLoGin #[cfg(test)]
114e2cc487bSLoGin fn default_test_execute_context_builder(base_context: &BaseGlobalTestContext) -> Self {
11573779f3dSLoGin Self::default()
11673779f3dSLoGin .sysroot_dir(Some(base_context.fake_dragonos_sysroot()))
11773779f3dSLoGin .action(Action::Build)
11873779f3dSLoGin .thread_num(None)
11973779f3dSLoGin .cache_dir(Some(base_context.fake_dadk_cache_root()))
12073779f3dSLoGin .base_test_context(Some(base_context.clone()))
12173779f3dSLoGin .clone()
12273779f3dSLoGin }
12373779f3dSLoGin }
12473779f3dSLoGin
125*eaa67f3cSLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq)]
126*eaa67f3cSLoGin pub enum Action {
127*eaa67f3cSLoGin /// 构建所有项目
128*eaa67f3cSLoGin Build,
129*eaa67f3cSLoGin /// 清理缓存
130*eaa67f3cSLoGin Clean(UserCleanLevel),
131*eaa67f3cSLoGin /// 安装到DragonOS sysroot
132*eaa67f3cSLoGin Install,
133*eaa67f3cSLoGin }
134*eaa67f3cSLoGin
13573779f3dSLoGin #[cfg(test)]
13673779f3dSLoGin pub struct DadkExecuteContextTestBuildX86_64V1 {
13773779f3dSLoGin context: Arc<DadkUserExecuteContext>,
setup() -> Self13873779f3dSLoGin }
13973779f3dSLoGin
14073779f3dSLoGin #[cfg(test)]
14173779f3dSLoGin impl TestContext for DadkExecuteContextTestBuildX86_64V1 {
14273779f3dSLoGin fn setup() -> Self {
143e2cc487bSLoGin let base_context = BaseGlobalTestContext::setup();
14473779f3dSLoGin let context =
14573779f3dSLoGin DadkUserExecuteContextBuilder::default_test_execute_context_builder(&base_context)
14673779f3dSLoGin .target_arch(TargetArch::X86_64)
14773779f3dSLoGin .config_dir(Some(base_context.config_v1_dir()))
14873779f3dSLoGin .build()
14973779f3dSLoGin .expect("Failed to build DadkExecuteContextTestBuildX86_64V1");
15073779f3dSLoGin let context = Arc::new(context);
15173779f3dSLoGin context.init(context.clone());
15273779f3dSLoGin DadkExecuteContextTestBuildX86_64V1 { context }
15373779f3dSLoGin }
15473779f3dSLoGin }
15573779f3dSLoGin
15673779f3dSLoGin #[cfg(test)]
15773779f3dSLoGin pub struct DadkExecuteContextTestBuildRiscV64V1 {
15873779f3dSLoGin context: Arc<DadkUserExecuteContext>,
setup() -> Self15973779f3dSLoGin }
16073779f3dSLoGin
16173779f3dSLoGin #[cfg(test)]
16273779f3dSLoGin impl TestContext for DadkExecuteContextTestBuildRiscV64V1 {
16373779f3dSLoGin fn setup() -> Self {
164e2cc487bSLoGin let base_context = BaseGlobalTestContext::setup();
16573779f3dSLoGin let context =
16673779f3dSLoGin DadkUserExecuteContextBuilder::default_test_execute_context_builder(&base_context)
16773779f3dSLoGin .target_arch(TargetArch::RiscV64)
16873779f3dSLoGin .config_dir(Some(base_context.config_v1_dir()))
16973779f3dSLoGin .build()
17073779f3dSLoGin .expect("Failed to build DadkExecuteContextTestBuildRiscV64V1");
17173779f3dSLoGin let context = Arc::new(context);
17273779f3dSLoGin context.init(context.clone());
17373779f3dSLoGin DadkExecuteContextTestBuildRiscV64V1 { context }
17473779f3dSLoGin }
17573779f3dSLoGin }
17673779f3dSLoGin
17773779f3dSLoGin macro_rules! impl_for_test_context {
17873779f3dSLoGin ($context:ty) => {
17973779f3dSLoGin #[cfg(test)]
18073779f3dSLoGin impl std::ops::Deref for $context {
18173779f3dSLoGin type Target = DadkUserExecuteContext;
18273779f3dSLoGin
18373779f3dSLoGin fn deref(&self) -> &Self::Target {
18473779f3dSLoGin &self.context
18573779f3dSLoGin }
18673779f3dSLoGin }
18773779f3dSLoGin
18873779f3dSLoGin #[cfg(test)]
18973779f3dSLoGin impl TestContextExt for $context {
190e2cc487bSLoGin fn base_context(&self) -> &BaseGlobalTestContext {
19173779f3dSLoGin self.base_test_context.as_ref().unwrap()
19273779f3dSLoGin }
19373779f3dSLoGin
19473779f3dSLoGin fn execute_context(&self) -> &DadkUserExecuteContext {
19573779f3dSLoGin &self.context
19673779f3dSLoGin }
19773779f3dSLoGin }
19873779f3dSLoGin };
19973779f3dSLoGin }
20073779f3dSLoGin
20173779f3dSLoGin impl_for_test_context!(DadkExecuteContextTestBuildX86_64V1);
20273779f3dSLoGin impl_for_test_context!(DadkExecuteContextTestBuildRiscV64V1);
203