build.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #[cfg(target_os = "windows")]
  2. fn main() {}
  3. #[cfg(target_os = "macos")]
  4. fn main() {}
  5. #[cfg(target_os = "linux")]
  6. use pkg_config;
  7. #[cfg(target_os = "linux")]
  8. use std::env;
  9. #[cfg(target_os = "linux")]
  10. use std::fs::File;
  11. #[cfg(target_os = "linux")]
  12. use std::io::Write;
  13. #[cfg(target_os = "linux")]
  14. use std::path::Path;
  15. #[cfg(target_os = "linux")]
  16. fn main() {
  17. let libraries = [
  18. "xext",
  19. "gl",
  20. "xcursor",
  21. "xxf86vm",
  22. "xft",
  23. "xinerama",
  24. "xi",
  25. "x11",
  26. "xlib_xcb",
  27. "xmu",
  28. "xrandr",
  29. "xtst",
  30. "xrender",
  31. "xscrnsaver",
  32. "xt",
  33. ];
  34. let mut config = String::new();
  35. for lib in libraries.iter() {
  36. let libdir = match pkg_config::get_variable(lib, "libdir") {
  37. Ok(libdir) => format!("Some(\"{}\")", libdir),
  38. Err(_) => "None".to_string(),
  39. };
  40. config.push_str(&format!(
  41. "pub const {}: Option<&'static str> = {};\n",
  42. lib, libdir
  43. ));
  44. }
  45. let config = format!("pub mod config {{ pub mod libdir {{\n{}}}\n}}", config);
  46. let out_dir = env::var("OUT_DIR").unwrap();
  47. let dest_path = Path::new(&out_dir).join("config.rs");
  48. let mut f = File::create(&dest_path).unwrap();
  49. f.write_all(&config.into_bytes()).unwrap();
  50. let target = env::var("TARGET").unwrap();
  51. if target.contains("linux") {
  52. println!("cargo:rustc-link-lib=dl");
  53. } else if target.contains("freebsd") || target.contains("dragonfly") {
  54. println!("cargo:rustc-link-lib=c");
  55. }
  56. }