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