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