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