Target.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===- Target.cpp -----------------------------------------------*- 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. #include "llvm/TextAPI/Target.h"
  9. #include "llvm/ADT/StringSwitch.h"
  10. #include "llvm/ADT/Twine.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. namespace llvm {
  13. namespace MachO {
  14. Expected<Target> Target::create(StringRef TargetValue) {
  15. auto Result = TargetValue.split('-');
  16. auto ArchitectureStr = Result.first;
  17. auto Architecture = getArchitectureFromName(ArchitectureStr);
  18. auto PlatformStr = Result.second;
  19. PlatformType Platform;
  20. Platform = StringSwitch<PlatformType>(PlatformStr)
  21. .Case("macos", PLATFORM_MACOS)
  22. .Case("ios", PLATFORM_IOS)
  23. .Case("tvos", PLATFORM_TVOS)
  24. .Case("watchos", PLATFORM_WATCHOS)
  25. .Case("bridgeos", PLATFORM_BRIDGEOS)
  26. .Case("maccatalyst", PLATFORM_MACCATALYST)
  27. .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
  28. .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
  29. .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
  30. .Case("driverkit", PLATFORM_DRIVERKIT)
  31. .Default(PLATFORM_UNKNOWN);
  32. if (Platform == PLATFORM_UNKNOWN) {
  33. if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
  34. PlatformStr = PlatformStr.drop_front().drop_back();
  35. unsigned long long RawValue;
  36. if (!PlatformStr.getAsInteger(10, RawValue))
  37. Platform = (PlatformType)RawValue;
  38. }
  39. }
  40. return Target{Architecture, Platform};
  41. }
  42. Target::operator std::string() const {
  43. return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) + ")")
  44. .str();
  45. }
  46. raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
  47. OS << std::string(Target);
  48. return OS;
  49. }
  50. PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
  51. PlatformSet Result;
  52. for (const auto &Target : Targets)
  53. Result.insert(Target.Platform);
  54. return Result;
  55. }
  56. ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
  57. ArchitectureSet Result;
  58. for (const auto &Target : Targets)
  59. Result.set(Target.Arch);
  60. return Result;
  61. }
  62. std::string getTargetTripleName(const Target &Targ) {
  63. return (getArchitectureName(Targ.Arch) + "-apple-" +
  64. getOSAndEnvironmentName(Targ.Platform))
  65. .str();
  66. }
  67. } // end namespace MachO.
  68. } // end namespace llvm.