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(); 17*11f78b73SLoGin 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) { 26e26ca418SLoGin c.flag("-mcmodel=large") 27e26ca418SLoGin .flag("-fno-builtin") 28e26ca418SLoGin .flag("-nostdlib") 29e26ca418SLoGin .flag("-fno-stack-protector") 30e26ca418SLoGin .flag("-fno-pie") 31e26ca418SLoGin .flag("-Wno-expansion-to-defined") 32e26ca418SLoGin .flag("-Wno-unused-parameter") 33e26ca418SLoGin .flag("-m64") 34e26ca418SLoGin .flag("-O1"); 35*11f78b73SLoGin 36*11f78b73SLoGin // set Arch-specific flags 37*11f78b73SLoGin current_cfiles_arch().setup_global_flags(c); 38e26ca418SLoGin } 39e26ca418SLoGin 40e26ca418SLoGin fn setup_defines(c: &mut Build) { 41e26ca418SLoGin if let Ok(k) = std::env::var("EMULATOR") { 42e26ca418SLoGin c.define("EMULATOR", Some(k.as_str())); 43e26ca418SLoGin } else { 44e26ca418SLoGin c.define("EMULATOR", "__NO_EMULATION__"); 45e26ca418SLoGin } 46e26ca418SLoGin 47e26ca418SLoGin current_cfiles_arch().setup_defines(c); 48e26ca418SLoGin } 49e26ca418SLoGin 50e26ca418SLoGin fn setup_global_include_dir(c: &mut Build) { 51e26ca418SLoGin c.include("src/include"); 52e26ca418SLoGin c.include("src"); 53e26ca418SLoGin c.include("."); 54e26ca418SLoGin 55e26ca418SLoGin current_cfiles_arch().setup_global_include_dir(c); 56e26ca418SLoGin } 57e26ca418SLoGin 58e26ca418SLoGin /// 设置需要编译的文件 59e26ca418SLoGin fn setup_files(c: &mut Build) { 60e26ca418SLoGin let mut files: Vec<PathBuf> = Vec::new(); 61e26ca418SLoGin 62e26ca418SLoGin current_cfiles_arch().setup_files(c, &mut files); 63e26ca418SLoGin 64e26ca418SLoGin Self::set_rerun_if_files_changed(&files); 65e26ca418SLoGin c.files(files.as_slice()); 66e26ca418SLoGin } 67e26ca418SLoGin 68e26ca418SLoGin /// 设置Cargo对文件更改的监听 69e26ca418SLoGin fn set_rerun_if_files_changed(files: &Vec<PathBuf>) { 70e26ca418SLoGin CargoHandler::emit_rerun_if_files_changed(files.as_slice()); 71e26ca418SLoGin } 72e26ca418SLoGin } 73