platform.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /// Try to determine the current target triple.
  2. ///
  3. /// Returns a target triple (e.g. `x86_64-unknown-linux-gnu` or `i686-pc-windows-msvc`) or an
  4. /// `Error::Config` if the current config cannot be determined or is not some combination of the
  5. /// following values:
  6. /// `linux, mac, windows` -- `i686, x86, armv7` -- `gnu, musl, msvc`
  7. ///
  8. /// * Errors:
  9. /// * Unexpected system config
  10. pub fn target_triple() -> Result<String, String> {
  11. let arch = if cfg!(target_arch = "x86") {
  12. "i686"
  13. } else if cfg!(target_arch = "x86_64") {
  14. "x86_64"
  15. } else if cfg!(target_arch = "arm") {
  16. "armv7"
  17. } else {
  18. return Err("Unable to determine target-architecture".to_string());
  19. };
  20. let os = if cfg!(target_os = "linux") {
  21. "unknown-linux"
  22. } else if cfg!(target_os = "macos") {
  23. "apple-darwin"
  24. } else if cfg!(target_os = "windows") {
  25. "pc-windows"
  26. } else if cfg!(target_os = "freebsd") {
  27. "unknown-freebsd"
  28. } else {
  29. return Err("Unable to determine target-os".to_string());
  30. };
  31. let s;
  32. let os = if cfg!(target_os = "macos") || cfg!(target_os = "freebsd") {
  33. os
  34. } else {
  35. let env = if cfg!(target_env = "gnu") {
  36. "gnu"
  37. } else if cfg!(target_env = "musl") {
  38. "musl"
  39. } else if cfg!(target_env = "msvc") {
  40. "msvc"
  41. } else {
  42. return Err("Unable to determine target-environment".to_string());
  43. };
  44. s = format!("{}-{}", os, env);
  45. &s
  46. };
  47. Ok(format!("{}-{}", arch, os))
  48. }