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.

72 lines
2.1 KiB

  1. use std::env;
  2. use std::fs::File;
  3. use std::io::prelude::*;
  4. use std::io::BufReader;
  5. use std::path::PathBuf;
  6. macro_rules! set_rustc_env_var {
  7. ($name: expr, $value: expr) => {{
  8. println!("cargo:rustc-env={}={}", $name, $value);
  9. }};
  10. }
  11. fn get_openssl_version_unit(n: u64, pos: u32) -> u64 {
  12. let p = 0x000f_f000_0000 >> (8 * pos);
  13. let n = n & p;
  14. n >> (8 * (3 - pos) + 4)
  15. }
  16. fn get_openssl_version(v: &str) -> String {
  17. let v = u64::from_str_radix(&v, 16).unwrap();
  18. let mut version = vec![];
  19. for i in 0..3 {
  20. let n = get_openssl_version_unit(v, i);
  21. version.push(format!("{}", n));
  22. }
  23. let version = version.join(".");
  24. let p = get_openssl_version_unit(v, 3);
  25. if p != 0 {
  26. let p = p + 0x60;
  27. let p = std::char::from_u32(p as u32).unwrap();
  28. format!("{}{}", version, p)
  29. } else {
  30. version
  31. }
  32. }
  33. fn get_lib_version(lib: &str) -> Option<String> {
  34. let pat = format!("\"checksum {} ", lib);
  35. let mut lock_file = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
  36. lock_file.push("../Cargo.lock");
  37. let file = File::open(lock_file).unwrap();
  38. for line in BufReader::new(file).lines() {
  39. let line = line.unwrap();
  40. if line.starts_with(&pat) {
  41. let v: Vec<&str> = line.split(' ').collect();
  42. return Some(String::from(v[2]));
  43. }
  44. }
  45. None
  46. }
  47. fn set_tls() {
  48. if let Ok(v) = env::var("DEP_OPENSSL_VERSION_NUMBER") {
  49. let version = get_openssl_version(&v);
  50. set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version);
  51. set_rustc_env_var!("ACMED_TLS_LIB_NAME", "OpenSSL");
  52. }
  53. if let Ok(v) = env::var("DEP_OPENSSL_LIBRESSL_VERSION_NUMBER") {
  54. let version = get_openssl_version(&v);
  55. set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version);
  56. set_rustc_env_var!("ACMED_TLS_LIB_NAME", "LibreSSL");
  57. }
  58. if env::var("CARGO_FEATURE_STANDALONE").is_ok() {
  59. let version = get_lib_version("ring").unwrap();
  60. set_rustc_env_var!("ACMED_TLS_LIB_VERSION", version);
  61. set_rustc_env_var!("ACMED_TLS_LIB_NAME", "ring");
  62. }
  63. }
  64. fn main() {
  65. set_tls();
  66. }