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