Target.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===- tapi/Core/Target.cpp - Target ----------------------------*- 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/ADT/SmallString.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/ADT/StringSwitch.h"
  12. #include "llvm/Support/Format.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "llvm/TextAPI/MachO/Target.h"
  15. namespace llvm {
  16. namespace MachO {
  17. Expected<Target> Target::create(StringRef TargetValue) {
  18. auto Result = TargetValue.split('-');
  19. auto ArchitectureStr = Result.first;
  20. auto Architecture = getArchitectureFromName(ArchitectureStr);
  21. auto PlatformStr = Result.second;
  22. PlatformKind Platform;
  23. Platform = StringSwitch<PlatformKind>(PlatformStr)
  24. .Case("macos", PlatformKind::macOS)
  25. .Case("ios", PlatformKind::iOS)
  26. .Case("tvos", PlatformKind::tvOS)
  27. .Case("watchos", PlatformKind::watchOS)
  28. .Case("bridgeos", PlatformKind::bridgeOS)
  29. .Case("maccatalyst", PlatformKind::macCatalyst)
  30. .Case("ios-simulator", PlatformKind::iOSSimulator)
  31. .Case("tvos-simulator", PlatformKind::tvOSSimulator)
  32. .Case("watchos-simulator", PlatformKind::watchOSSimulator)
  33. .Default(PlatformKind::unknown);
  34. if (Platform == PlatformKind::unknown) {
  35. if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
  36. PlatformStr = PlatformStr.drop_front().drop_back();
  37. unsigned long long RawValue;
  38. if (!PlatformStr.getAsInteger(10, RawValue))
  39. Platform = (PlatformKind)RawValue;
  40. }
  41. }
  42. return Target{Architecture, Platform};
  43. }
  44. Target::operator std::string() const {
  45. return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) + ")")
  46. .str();
  47. }
  48. raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
  49. OS << std::string(Target);
  50. return OS;
  51. }
  52. PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
  53. PlatformSet Result;
  54. for (const auto &Target : Targets)
  55. Result.insert(Target.Platform);
  56. return Result;
  57. }
  58. ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
  59. ArchitectureSet Result;
  60. for (const auto &Target : Targets)
  61. Result.set(Target.Arch);
  62. return Result;
  63. }
  64. } // end namespace MachO.
  65. } // end namespace llvm.