1*e26ca418SLoGin use std::path::PathBuf; 2*e26ca418SLoGin 3*e26ca418SLoGin use cc::Build; 4*e26ca418SLoGin 5*e26ca418SLoGin use crate::utils::cargo_handler::CargoHandler; 6*e26ca418SLoGin 7*e26ca418SLoGin use self::arch::current_cfiles_arch; 8*e26ca418SLoGin 9*e26ca418SLoGin mod arch; 10*e26ca418SLoGin 11*e26ca418SLoGin /// 构建项目的c文件 12*e26ca418SLoGin pub struct CFilesBuilder; 13*e26ca418SLoGin 14*e26ca418SLoGin impl CFilesBuilder { 15*e26ca418SLoGin pub fn build() { 16*e26ca418SLoGin let mut c = cc::Build::new(); 17*e26ca418SLoGin Self::setup_global_flags(&mut c); 18*e26ca418SLoGin Self::setup_defines(&mut c); 19*e26ca418SLoGin Self::setup_global_include_dir(&mut c); 20*e26ca418SLoGin Self::setup_files(&mut c); 21*e26ca418SLoGin c.compile("dragonos_kernel_cfiles"); 22*e26ca418SLoGin } 23*e26ca418SLoGin 24*e26ca418SLoGin fn setup_global_flags(c: &mut Build) { 25*e26ca418SLoGin c.flag("-mcmodel=large") 26*e26ca418SLoGin .flag("-fno-builtin") 27*e26ca418SLoGin .flag("-nostdlib") 28*e26ca418SLoGin .flag("-fno-stack-protector") 29*e26ca418SLoGin .flag("-fno-pie") 30*e26ca418SLoGin .flag("-Wno-expansion-to-defined") 31*e26ca418SLoGin .flag("-Wno-unused-parameter") 32*e26ca418SLoGin .flag("-m64") 33*e26ca418SLoGin .flag("-O1"); 34*e26ca418SLoGin } 35*e26ca418SLoGin 36*e26ca418SLoGin fn setup_defines(c: &mut Build) { 37*e26ca418SLoGin if let Ok(k) = std::env::var("EMULATOR") { 38*e26ca418SLoGin c.define("EMULATOR", Some(k.as_str())); 39*e26ca418SLoGin } else { 40*e26ca418SLoGin c.define("EMULATOR", "__NO_EMULATION__"); 41*e26ca418SLoGin } 42*e26ca418SLoGin 43*e26ca418SLoGin current_cfiles_arch().setup_defines(c); 44*e26ca418SLoGin } 45*e26ca418SLoGin 46*e26ca418SLoGin fn setup_global_include_dir(c: &mut Build) { 47*e26ca418SLoGin c.include("src/include"); 48*e26ca418SLoGin c.include("src"); 49*e26ca418SLoGin c.include("."); 50*e26ca418SLoGin 51*e26ca418SLoGin current_cfiles_arch().setup_global_include_dir(c); 52*e26ca418SLoGin } 53*e26ca418SLoGin 54*e26ca418SLoGin /// 设置需要编译的文件 55*e26ca418SLoGin fn setup_files(c: &mut Build) { 56*e26ca418SLoGin let mut files: Vec<PathBuf> = Vec::new(); 57*e26ca418SLoGin 58*e26ca418SLoGin current_cfiles_arch().setup_files(c, &mut files); 59*e26ca418SLoGin 60*e26ca418SLoGin Self::set_rerun_if_files_changed(&files); 61*e26ca418SLoGin c.files(files.as_slice()); 62*e26ca418SLoGin } 63*e26ca418SLoGin 64*e26ca418SLoGin /// 设置Cargo对文件更改的监听 65*e26ca418SLoGin fn set_rerun_if_files_changed(files: &Vec<PathBuf>) { 66*e26ca418SLoGin CargoHandler::emit_rerun_if_files_changed(files.as_slice()); 67*e26ca418SLoGin } 68*e26ca418SLoGin } 69