xref: /DragonOS/build-scripts/kernel_build/src/cfiles/arch/x86_64.rs (revision 01c18c64b14b4ebabd98fa92c587c26874275eb1)
1 use std::{collections::HashSet, path::PathBuf};
2 
3 use cc::Build;
4 
5 use crate::{constant::ARCH_DIR_X86_64, utils::FileUtils};
6 
7 use super::CFilesArch;
8 
9 pub(super) struct X86_64CFilesArch;
10 
11 impl CFilesArch for X86_64CFilesArch {
12     fn setup_defines(&self, c: &mut cc::Build) {
13         c.define("__x86_64__", None);
14     }
15 
16     fn setup_global_include_dir(&self, include_dirs: &mut HashSet<PathBuf>) {
17         include_dirs.insert("src/arch/x86_64/include".into());
18     }
19 
20     fn setup_files(&self, _c: &mut Build, files: &mut HashSet<PathBuf>) {
21         const DIRS: [&str; 4] = ["driver/apic", "init", "asm", "interrupt"];
22         DIRS.iter().for_each(|dir| {
23             FileUtils::list_all_files(&arch_path(dir), Some("c"), true)
24                 .into_iter()
25                 .for_each(|f| {
26                     files.insert(f);
27                 });
28         });
29 
30         // setup asm files
31         files.insert(PathBuf::from("src/arch/x86_64/asm/head.S"));
32         files.insert(PathBuf::from("src/arch/x86_64/asm/entry.S"));
33         files.insert(PathBuf::from("src/arch/x86_64/asm/apu_boot.S"));
34     }
35 
36     fn setup_global_flags(&self, c: &mut Build) {
37         c.asm_flag("-m64");
38         c.flag("-mcmodel=large").flag("-m64");
39     }
40 }
41 
42 fn arch_path(relative_path: &str) -> PathBuf {
43     PathBuf::from(format!("{}/{}", ARCH_DIR_X86_64, relative_path))
44 }
45