You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.4 KiB

  1. extern crate serde;
  2. extern crate toml;
  3. use serde::Deserialize;
  4. use std::env;
  5. use std::fs::File;
  6. use std::io::prelude::*;
  7. use std::path::PathBuf;
  8. macro_rules! set_rustc_env_var {
  9. ($name: expr, $value: expr) => {{
  10. println!("cargo:rustc-env={}={}", $name, $value);
  11. }};
  12. }
  13. #[derive(Deserialize)]
  14. pub struct LockFile {
  15. package: Vec<Package>,
  16. }
  17. #[derive(Deserialize)]
  18. struct Package {
  19. name: String,
  20. version: String,
  21. }
  22. struct Error;
  23. impl From<std::io::Error> for Error {
  24. fn from(_error: std::io::Error) -> Self {
  25. Error {}
  26. }
  27. }
  28. impl From<toml::de::Error> for Error {
  29. fn from(_error: toml::de::Error) -> Self {
  30. Error {}
  31. }
  32. }
  33. fn get_lock_file() -> Result<LockFile, Error> {
  34. let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
  35. path.push("Cargo.lock");
  36. let mut file = File::open(path)?;
  37. let mut contents = String::new();
  38. file.read_to_string(&mut contents)?;
  39. let ret: LockFile = toml::from_str(&contents)?;
  40. Ok(ret)
  41. }
  42. fn set_http_agent() {
  43. let lock = match get_lock_file() {
  44. Ok(l) => l,
  45. Err(_) => {
  46. return;
  47. }
  48. };
  49. for p in lock.package.iter() {
  50. if p.name == "reqwest" {
  51. let agent = format!("{}/{}", p.name, p.version);
  52. set_rustc_env_var!("ACMED_HTTP_LIB_AGENT", agent);
  53. return;
  54. }
  55. }
  56. }
  57. fn set_target() {
  58. if let Ok(target) = env::var("TARGET") {
  59. set_rustc_env_var!("ACMED_TARGET", target);
  60. };
  61. }
  62. fn main() {
  63. set_target();
  64. set_http_agent();
  65. }