Platform.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===- llvm/TextAPI/Platform.cpp - Platform ---------------------*- 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. // Implementations of Platform Helper functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/TextAPI/Platform.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. #include "llvm/ADT/Triple.h"
  16. namespace llvm {
  17. namespace MachO {
  18. PlatformType mapToPlatformType(PlatformType Platform, bool WantSim) {
  19. switch (Platform) {
  20. default:
  21. return Platform;
  22. case PLATFORM_IOS:
  23. return WantSim ? PLATFORM_IOSSIMULATOR : PLATFORM_IOS;
  24. case PLATFORM_TVOS:
  25. return WantSim ? PLATFORM_TVOSSIMULATOR : PLATFORM_TVOS;
  26. case PLATFORM_WATCHOS:
  27. return WantSim ? PLATFORM_WATCHOSSIMULATOR : PLATFORM_WATCHOS;
  28. }
  29. }
  30. PlatformType mapToPlatformType(const Triple &Target) {
  31. switch (Target.getOS()) {
  32. default:
  33. return PLATFORM_UNKNOWN;
  34. case Triple::MacOSX:
  35. return PLATFORM_MACOS;
  36. case Triple::IOS:
  37. if (Target.isSimulatorEnvironment())
  38. return PLATFORM_IOSSIMULATOR;
  39. if (Target.getEnvironment() == Triple::MacABI)
  40. return PLATFORM_MACCATALYST;
  41. return PLATFORM_IOS;
  42. case Triple::TvOS:
  43. return Target.isSimulatorEnvironment() ? PLATFORM_TVOSSIMULATOR
  44. : PLATFORM_TVOS;
  45. case Triple::WatchOS:
  46. return Target.isSimulatorEnvironment() ? PLATFORM_WATCHOSSIMULATOR
  47. : PLATFORM_WATCHOS;
  48. // TODO: add bridgeOS & driverKit once in llvm::Triple
  49. }
  50. }
  51. PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {
  52. PlatformSet Result;
  53. for (const auto &Target : Targets)
  54. Result.insert(mapToPlatformType(Target));
  55. return Result;
  56. }
  57. StringRef getPlatformName(PlatformType Platform) {
  58. switch (Platform) {
  59. case PLATFORM_UNKNOWN:
  60. return "unknown";
  61. case PLATFORM_MACOS:
  62. return "macOS";
  63. case PLATFORM_IOS:
  64. return "iOS";
  65. case PLATFORM_TVOS:
  66. return "tvOS";
  67. case PLATFORM_WATCHOS:
  68. return "watchOS";
  69. case PLATFORM_BRIDGEOS:
  70. return "bridgeOS";
  71. case PLATFORM_MACCATALYST:
  72. return "macCatalyst";
  73. case PLATFORM_IOSSIMULATOR:
  74. return "iOS Simulator";
  75. case PLATFORM_TVOSSIMULATOR:
  76. return "tvOS Simulator";
  77. case PLATFORM_WATCHOSSIMULATOR:
  78. return "watchOS Simulator";
  79. case PLATFORM_DRIVERKIT:
  80. return "DriverKit";
  81. }
  82. llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
  83. }
  84. PlatformType getPlatformFromName(StringRef Name) {
  85. return StringSwitch<PlatformType>(Name)
  86. .Case("macos", PLATFORM_MACOS)
  87. .Case("ios", PLATFORM_IOS)
  88. .Case("tvos", PLATFORM_TVOS)
  89. .Case("watchos", PLATFORM_WATCHOS)
  90. .Case("bridgeos", PLATFORM_BRIDGEOS)
  91. .Case("ios-macabi", PLATFORM_MACCATALYST)
  92. .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
  93. .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
  94. .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
  95. .Case("driverkit", PLATFORM_DRIVERKIT)
  96. .Default(PLATFORM_UNKNOWN);
  97. }
  98. std::string getOSAndEnvironmentName(PlatformType Platform,
  99. std::string Version) {
  100. switch (Platform) {
  101. case PLATFORM_UNKNOWN:
  102. return "darwin" + Version;
  103. case PLATFORM_MACOS:
  104. return "macos" + Version;
  105. case PLATFORM_IOS:
  106. return "ios" + Version;
  107. case PLATFORM_TVOS:
  108. return "tvos" + Version;
  109. case PLATFORM_WATCHOS:
  110. return "watchos" + Version;
  111. case PLATFORM_BRIDGEOS:
  112. return "bridgeos" + Version;
  113. case PLATFORM_MACCATALYST:
  114. return "ios" + Version + "-macabi";
  115. case PLATFORM_IOSSIMULATOR:
  116. return "ios" + Version + "-simulator";
  117. case PLATFORM_TVOSSIMULATOR:
  118. return "tvos" + Version + "-simulator";
  119. case PLATFORM_WATCHOSSIMULATOR:
  120. return "watchos" + Version + "-simulator";
  121. case PLATFORM_DRIVERKIT:
  122. return "driverkit" + Version;
  123. }
  124. llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
  125. }
  126. } // end namespace MachO.
  127. } // end namespace llvm.