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