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