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