把内核构建脚本单独独立成一个crate (#444)

This commit is contained in:
LoGin
2023-11-17 11:26:26 +08:00
committed by GitHub
parent a0c98cd4df
commit e26ca418df
15 changed files with 374 additions and 180 deletions

View File

@ -0,0 +1,78 @@
use std::{env, path::PathBuf};
lazy_static! {
static ref CARGO_HANDLER_DATA: CargoHandlerData = CargoHandlerData::new();
}
struct CargoHandlerData {
target_arch: TargetArch,
}
impl CargoHandlerData {
fn new() -> Self {
CargoHandlerData {
target_arch: TargetArch::new(),
}
}
}
#[derive(Debug)]
pub struct CargoHandler;
impl CargoHandler {
pub fn readenv(key: &str) -> Option<String> {
if let Ok(value) = env::var(key) {
Some(value)
} else {
None
}
}
/// 获取当前编译的目标架构
pub fn target_arch() -> TargetArch {
CARGO_HANDLER_DATA.target_arch
}
/// 设置Cargo对文件更改的监听
///
/// ## Parameters
///
/// - `files` - The files to set rerun build
pub fn emit_rerun_if_files_changed(files: &[PathBuf]) {
for f in files {
println!("cargo:rerun-if-changed={}", f.to_str().unwrap());
}
}
}
/// 目标架构
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetArch {
X86_64,
Aarch64,
Riscv64,
Mips64,
Powerpc64,
S390x,
Sparc64,
Unknown,
}
impl TargetArch {
pub fn new() -> Self {
let data = CargoHandler::readenv("CARGO_CFG_TARGET_ARCH")
.expect("CARGO_CFG_TARGET_ARCH is not set")
.to_ascii_lowercase();
match data.as_str() {
"x86_64" => TargetArch::X86_64,
"aarch64" => TargetArch::Aarch64,
"riscv64" => TargetArch::Riscv64,
"mips64" => TargetArch::Mips64,
"powerpc64" => TargetArch::Powerpc64,
"s390x" => TargetArch::S390x,
"sparc64" => TargetArch::Sparc64,
_ => TargetArch::Unknown,
}
}
}

View File

@ -0,0 +1,49 @@
use std::path::PathBuf;
pub mod cargo_handler;
pub struct FileUtils;
impl FileUtils {
/// 列出指定目录下的所有文件
///
/// ## 参数
///
/// - `path` - 指定的目录
/// - `ext_name` - 文件的扩展名如果为None则列出所有文件
/// - `recursive` - 是否递归列出所有文件
pub fn list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf> {
let mut queue: Vec<PathBuf> = Vec::new();
let mut result = Vec::new();
queue.push(path.clone());
while !queue.is_empty() {
let path = queue.pop().unwrap();
let d = std::fs::read_dir(path);
if d.is_err() {
continue;
}
let d = d.unwrap();
d.for_each(|ent| {
if let Ok(ent) = ent {
if let Ok(file_type) = ent.file_type() {
if file_type.is_file() {
if let Some(e) = ext_name {
if let Some(ext) = ent.path().extension() {
if ext == e {
result.push(ent.path());
}
}
}
} else if file_type.is_dir() && recursive {
queue.push(ent.path());
}
}
}
});
}
return result;
}
}