Host.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===- llvm/Support/Unix/Host.inc -------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the UNIX Host support.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. //===----------------------------------------------------------------------===//
  13. //=== WARNING: Implementation here must contain only generic UNIX code that
  14. //=== is guaranteed to work on *all* UNIX variants.
  15. //===----------------------------------------------------------------------===//
  16. #include "Unix.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Config/config.h"
  19. #include <cctype>
  20. #include <string>
  21. #include <sys/utsname.h>
  22. using namespace llvm;
  23. static std::string getOSVersion() {
  24. struct utsname info;
  25. if (uname(&info))
  26. return "";
  27. return info.release;
  28. }
  29. static std::string updateTripleOSVersion(std::string TargetTripleString) {
  30. // On darwin, we want to update the version to match that of the target.
  31. std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
  32. if (DarwinDashIdx != std::string::npos) {
  33. TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
  34. TargetTripleString += getOSVersion();
  35. return TargetTripleString;
  36. }
  37. std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos");
  38. if (MacOSDashIdx != std::string::npos) {
  39. TargetTripleString.resize(MacOSDashIdx);
  40. // Reset the OS to darwin as the OS version from `uname` doesn't use the
  41. // macOS version scheme.
  42. TargetTripleString += "-darwin";
  43. TargetTripleString += getOSVersion();
  44. }
  45. // On AIX, the AIX version and release should be that of the current host
  46. // unless if the version has already been specified.
  47. if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) {
  48. Triple TT(TargetTripleString);
  49. if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) {
  50. struct utsname name;
  51. if (uname(&name) != -1) {
  52. std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX));
  53. NewOSName += name.version;
  54. NewOSName += '.';
  55. NewOSName += name.release;
  56. NewOSName += ".0.0";
  57. TT.setOSName(NewOSName);
  58. return TT.str();
  59. }
  60. }
  61. }
  62. return TargetTripleString;
  63. }
  64. std::string sys::getDefaultTargetTriple() {
  65. std::string TargetTripleString =
  66. updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
  67. // Override the default target with an environment variable named by
  68. // LLVM_TARGET_TRIPLE_ENV.
  69. #if defined(LLVM_TARGET_TRIPLE_ENV)
  70. if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
  71. TargetTripleString = EnvTriple;
  72. #endif
  73. return TargetTripleString;
  74. }