build.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. extern crate bindgen;
  2. extern crate cc;
  3. // use ::std::env;
  4. use std::path::PathBuf;
  5. use cc::Build;
  6. fn main() {
  7. // Tell cargo to look for shared libraries in the specified directory
  8. println!("cargo:rustc-link-search=src");
  9. println!("cargo:rerun-if-changed=src/include/bindings/wrapper.h");
  10. generate_bindings();
  11. CFilesBuilder::build();
  12. }
  13. fn generate_bindings() {
  14. // let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
  15. let out_path = PathBuf::from(String::from("src/include/bindings/"));
  16. // The bindgen::Builder is the main entry point
  17. // to bindgen, and lets you build up options for
  18. // the resulting bindings.
  19. {
  20. let bindings = bindgen::Builder::default()
  21. .clang_arg("-I./src")
  22. .clang_arg("-I./src/include")
  23. .clang_arg("-I./src/arch/x86_64/include") // todo: 当引入多种架构之后,需要修改这里,对于不同的架构编译时,include不同的路径
  24. // The input header we would like to generate
  25. // bindings for.
  26. .header("src/include/bindings/wrapper.h")
  27. .blocklist_file("src/include/bindings/bindings.h")
  28. .clang_arg("--target=x86_64-none-none")
  29. .clang_arg("-v")
  30. // 使用core,并将c语言的类型改为core::ffi,而不是使用std库。
  31. .use_core()
  32. .ctypes_prefix("::core::ffi")
  33. .generate_inline_functions(true)
  34. .raw_line("#![allow(dead_code)]")
  35. .raw_line("#![allow(non_upper_case_globals)]")
  36. .raw_line("#![allow(non_camel_case_types)]")
  37. // Tell cargo to invalidate the built crate whenever any of the
  38. // included header files changed.
  39. .parse_callbacks(Box::new(bindgen::CargoCallbacks))
  40. // Finish the builder and generate the bindings.
  41. .generate()
  42. // Unwrap the Result and panic on failure.
  43. .expect("Unable to generate bindings");
  44. bindings
  45. .write_to_file(out_path.join("bindings.rs"))
  46. .expect("Couldn't write bindings!");
  47. }
  48. }
  49. /// 构建项目的c文件
  50. struct CFilesBuilder;
  51. impl CFilesBuilder {
  52. fn build() {
  53. let mut c = cc::Build::new();
  54. Self::setup_global_flags(&mut c);
  55. Self::setup_defines(&mut c);
  56. Self::setup_global_include_dir(&mut c);
  57. Self::setup_files(&mut c);
  58. c.compile("dragonos_kernel_cfiles");
  59. }
  60. fn setup_global_flags(c: &mut Build) {
  61. c.flag("-mcmodel=large")
  62. .flag("-fno-builtin")
  63. .flag("-nostdlib")
  64. .flag("-fno-stack-protector")
  65. .flag("-fno-pie")
  66. .flag("-Wno-expansion-to-defined")
  67. .flag("-Wno-unused-parameter")
  68. .flag("-m64")
  69. .flag("-O1");
  70. }
  71. fn setup_defines(c: &mut Build) {
  72. if let Ok(k) = std::env::var("EMULATOR") {
  73. c.define("EMULATOR", Some(k.as_str()));
  74. } else {
  75. c.define("EMULATOR", "__NO_EMULATION__");
  76. }
  77. {
  78. #[cfg(target_arch = "x86_64")]
  79. c.define("__x86_64__", None);
  80. }
  81. }
  82. fn setup_global_include_dir(c: &mut Build) {
  83. c.include("src/include");
  84. c.include("src");
  85. c.include(".");
  86. #[cfg(target_arch = "x86_64")]
  87. c.include("src/arch/x86_64/include");
  88. }
  89. /// 设置需要编译的文件
  90. fn setup_files(c: &mut Build) {
  91. let mut files = Vec::new();
  92. #[cfg(target_arch = "x86_64")]
  93. Self::setup_files_x86_64(&mut files);
  94. Self::set_rerun_if_files_changed(&files);
  95. c.files(files.as_slice());
  96. }
  97. /// 设置x86_64架构下需要编译的C文件
  98. fn setup_files_x86_64(files: &mut Vec<PathBuf>) {
  99. files.push(PathBuf::from("src/arch/x86_64/driver/hpet.c"));
  100. // 获取`kernel/src/arch/x86_64/driver/apic`下的所有C文件
  101. files.append(&mut FileUtils::list_all_files(
  102. &PathBuf::from("src/arch/x86_64/driver/apic"),
  103. Some("c"),
  104. true,
  105. ));
  106. }
  107. /// 设置Cargo对文件更改的监听
  108. fn set_rerun_if_files_changed(files: &Vec<PathBuf>) {
  109. for f in files {
  110. println!("cargo:rerun-if-changed={}", f.to_str().unwrap());
  111. }
  112. }
  113. }
  114. struct FileUtils;
  115. impl FileUtils {
  116. /// 列出指定目录下的所有文件
  117. ///
  118. /// ## 参数
  119. ///
  120. /// - `path` - 指定的目录
  121. /// - `ext_name` - 文件的扩展名,如果为None,则列出所有文件
  122. /// - `recursive` - 是否递归列出所有文件
  123. pub fn list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf> {
  124. let mut queue: Vec<PathBuf> = Vec::new();
  125. let mut result = Vec::new();
  126. queue.push(path.clone());
  127. while !queue.is_empty() {
  128. let path = queue.pop().unwrap();
  129. let d = std::fs::read_dir(path);
  130. if d.is_err() {
  131. continue;
  132. }
  133. let d = d.unwrap();
  134. d.for_each(|ent| {
  135. if let Ok(ent) = ent {
  136. if let Ok(file_type) = ent.file_type() {
  137. if file_type.is_file() {
  138. if let Some(e) = ext_name {
  139. if let Some(ext) = ent.path().extension() {
  140. if ext == e {
  141. result.push(ent.path());
  142. }
  143. }
  144. }
  145. } else if file_type.is_dir() && recursive {
  146. queue.push(ent.path());
  147. }
  148. }
  149. }
  150. });
  151. }
  152. return result;
  153. }
  154. }