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