xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Platform.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 //===- llvm/TextAPI/Platform.cpp - Platform ---------------------*- 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 // Implementations of Platform Helper functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/TextAPI/Platform.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Triple.h"
17 
18 namespace llvm {
19 namespace MachO {
20 
21 PlatformKind mapToPlatformKind(PlatformKind Platform, bool WantSim) {
22   switch (Platform) {
23   default:
24     return Platform;
25   case PlatformKind::iOS:
26     return WantSim ? PlatformKind::iOSSimulator : PlatformKind::iOS;
27   case PlatformKind::tvOS:
28     return WantSim ? PlatformKind::tvOSSimulator : PlatformKind::tvOS;
29   case PlatformKind::watchOS:
30     return WantSim ? PlatformKind::watchOSSimulator : PlatformKind::watchOS;
31   }
32   llvm_unreachable("Unknown llvm::MachO::PlatformKind enum");
33 }
34 
35 PlatformKind mapToPlatformKind(const Triple &Target) {
36   switch (Target.getOS()) {
37   default:
38     return PlatformKind::unknown;
39   case Triple::MacOSX:
40     return PlatformKind::macOS;
41   case Triple::IOS:
42     if (Target.isSimulatorEnvironment())
43       return PlatformKind::iOSSimulator;
44     if (Target.getEnvironment() == Triple::MacABI)
45       return PlatformKind::macCatalyst;
46     return PlatformKind::iOS;
47   case Triple::TvOS:
48     return Target.isSimulatorEnvironment() ? PlatformKind::tvOSSimulator
49                                            : PlatformKind::tvOS;
50   case Triple::WatchOS:
51     return Target.isSimulatorEnvironment() ? PlatformKind::watchOSSimulator
52                                            : PlatformKind::watchOS;
53     // TODO: add bridgeOS & driverKit once in llvm::Triple
54   }
55   llvm_unreachable("Unknown Target Triple");
56 }
57 
58 PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {
59   PlatformSet Result;
60   for (const auto &Target : Targets)
61     Result.insert(mapToPlatformKind(Target));
62   return Result;
63 }
64 
65 StringRef getPlatformName(PlatformKind Platform) {
66   switch (Platform) {
67   case PlatformKind::unknown:
68     return "unknown";
69   case PlatformKind::macOS:
70     return "macOS";
71   case PlatformKind::iOS:
72     return "iOS";
73   case PlatformKind::tvOS:
74     return "tvOS";
75   case PlatformKind::watchOS:
76     return "watchOS";
77   case PlatformKind::bridgeOS:
78     return "bridgeOS";
79   case PlatformKind::macCatalyst:
80     return "macCatalyst";
81   case PlatformKind::iOSSimulator:
82     return "iOS Simulator";
83   case PlatformKind::tvOSSimulator:
84     return "tvOS Simulator";
85   case PlatformKind::watchOSSimulator:
86     return "watchOS Simulator";
87   case PlatformKind::driverKit:
88     return "DriverKit";
89   }
90   llvm_unreachable("Unknown llvm::MachO::PlatformKind enum");
91 }
92 
93 PlatformKind getPlatformFromName(StringRef Name) {
94   return StringSwitch<PlatformKind>(Name)
95       .Case("macos", PlatformKind::macOS)
96       .Case("ios", PlatformKind::iOS)
97       .Case("tvos", PlatformKind::tvOS)
98       .Case("watchos", PlatformKind::watchOS)
99       .Case("bridgeos", PlatformKind::macOS)
100       .Case("ios-macabi", PlatformKind::macCatalyst)
101       .Case("ios-simulator", PlatformKind::iOSSimulator)
102       .Case("tvos-simulator", PlatformKind::tvOSSimulator)
103       .Case("watchos-simulator", PlatformKind::watchOSSimulator)
104       .Case("driverkit", PlatformKind::driverKit)
105       .Default(PlatformKind::unknown);
106 }
107 
108 std::string getOSAndEnvironmentName(PlatformKind Platform,
109                                     std::string Version) {
110   switch (Platform) {
111   case PlatformKind::unknown:
112     return "darwin" + Version;
113   case PlatformKind::macOS:
114     return "macos" + Version;
115   case PlatformKind::iOS:
116     return "ios" + Version;
117   case PlatformKind::tvOS:
118     return "tvos" + Version;
119   case PlatformKind::watchOS:
120     return "watchos" + Version;
121   case PlatformKind::bridgeOS:
122     return "bridgeos" + Version;
123   case PlatformKind::macCatalyst:
124     return "ios" + Version + "-macabi";
125   case PlatformKind::iOSSimulator:
126     return "ios" + Version + "-simulator";
127   case PlatformKind::tvOSSimulator:
128     return "tvos" + Version + "-simulator";
129   case PlatformKind::watchOSSimulator:
130     return "watchos" + Version + "-simulator";
131   case PlatformKind::driverKit:
132     return "driverkit" + Version;
133   }
134   llvm_unreachable("Unknown llvm::MachO::PlatformKind enum");
135 }
136 
137 } // end namespace MachO.
138 } // end namespace llvm.
139