Host.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===- llvm/TargetParser/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 "llvm/ADT/StringRef.h"
  17. #include "llvm/Config/config.h"
  18. #include <cctype>
  19. #include <string>
  20. #include <sys/utsname.h>
  21. #ifdef HAVE_UNISTD_H
  22. #include <unistd.h>
  23. #endif
  24. using namespace llvm;
  25. static std::string getOSVersion() {
  26. struct utsname info;
  27. if (uname(&info))
  28. return "";
  29. return info.release;
  30. }
  31. static std::string updateTripleOSVersion(std::string TargetTripleString) {
  32. // On darwin, we want to update the version to match that of the target.
  33. std::string::size_type DarwinDashIdx = TargetTripleString.find("-darwin");
  34. if (DarwinDashIdx != std::string::npos) {
  35. TargetTripleString.resize(DarwinDashIdx + strlen("-darwin"));
  36. TargetTripleString += getOSVersion();
  37. return TargetTripleString;
  38. }
  39. std::string::size_type MacOSDashIdx = TargetTripleString.find("-macos");
  40. if (MacOSDashIdx != std::string::npos) {
  41. TargetTripleString.resize(MacOSDashIdx);
  42. // Reset the OS to darwin as the OS version from `uname` doesn't use the
  43. // macOS version scheme.
  44. TargetTripleString += "-darwin";
  45. TargetTripleString += getOSVersion();
  46. }
  47. // On AIX, the AIX version and release should be that of the current host
  48. // unless if the version has already been specified.
  49. if (Triple(LLVM_HOST_TRIPLE).getOS() == Triple::AIX) {
  50. Triple TT(TargetTripleString);
  51. if (TT.getOS() == Triple::AIX && !TT.getOSMajorVersion()) {
  52. struct utsname name;
  53. if (uname(&name) != -1) {
  54. std::string NewOSName = std::string(Triple::getOSTypeName(Triple::AIX));
  55. NewOSName += name.version;
  56. NewOSName += '.';
  57. NewOSName += name.release;
  58. NewOSName += ".0.0";
  59. TT.setOSName(NewOSName);
  60. return TT.str();
  61. }
  62. }
  63. }
  64. return TargetTripleString;
  65. }
  66. std::string sys::getDefaultTargetTriple() {
  67. std::string TargetTripleString =
  68. updateTripleOSVersion(LLVM_DEFAULT_TARGET_TRIPLE);
  69. // Override the default target with an environment variable named by
  70. // LLVM_TARGET_TRIPLE_ENV.
  71. #if defined(LLVM_TARGET_TRIPLE_ENV)
  72. if (const char *EnvTriple = std::getenv(LLVM_TARGET_TRIPLE_ENV))
  73. TargetTripleString = EnvTriple;
  74. #endif
  75. return TargetTripleString;
  76. }