xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/TextAPI/Platform.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1*82d56013Sjoerg //===- llvm/TextAPI/Platform.cpp - Platform ---------------------*- C++ -*-===//
2*82d56013Sjoerg //
3*82d56013Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*82d56013Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*82d56013Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*82d56013Sjoerg //
7*82d56013Sjoerg //===----------------------------------------------------------------------===//
8*82d56013Sjoerg //
9*82d56013Sjoerg // Implementations of Platform Helper functions.
10*82d56013Sjoerg //
11*82d56013Sjoerg //===----------------------------------------------------------------------===//
12*82d56013Sjoerg 
13*82d56013Sjoerg #include "llvm/TextAPI/Platform.h"
14*82d56013Sjoerg #include "llvm/ADT/ArrayRef.h"
15*82d56013Sjoerg #include "llvm/ADT/StringSwitch.h"
16*82d56013Sjoerg #include "llvm/ADT/Triple.h"
17*82d56013Sjoerg 
18*82d56013Sjoerg namespace llvm {
19*82d56013Sjoerg namespace MachO {
20*82d56013Sjoerg 
mapToPlatformKind(PlatformKind Platform,bool WantSim)21*82d56013Sjoerg PlatformKind mapToPlatformKind(PlatformKind Platform, bool WantSim) {
22*82d56013Sjoerg   switch (Platform) {
23*82d56013Sjoerg   default:
24*82d56013Sjoerg     return Platform;
25*82d56013Sjoerg   case PlatformKind::iOS:
26*82d56013Sjoerg     return WantSim ? PlatformKind::iOSSimulator : PlatformKind::iOS;
27*82d56013Sjoerg   case PlatformKind::tvOS:
28*82d56013Sjoerg     return WantSim ? PlatformKind::tvOSSimulator : PlatformKind::tvOS;
29*82d56013Sjoerg   case PlatformKind::watchOS:
30*82d56013Sjoerg     return WantSim ? PlatformKind::watchOSSimulator : PlatformKind::watchOS;
31*82d56013Sjoerg   }
32*82d56013Sjoerg   llvm_unreachable("Unknown llvm::MachO::PlatformKind enum");
33*82d56013Sjoerg }
34*82d56013Sjoerg 
mapToPlatformKind(const Triple & Target)35*82d56013Sjoerg PlatformKind mapToPlatformKind(const Triple &Target) {
36*82d56013Sjoerg   switch (Target.getOS()) {
37*82d56013Sjoerg   default:
38*82d56013Sjoerg     return PlatformKind::unknown;
39*82d56013Sjoerg   case Triple::MacOSX:
40*82d56013Sjoerg     return PlatformKind::macOS;
41*82d56013Sjoerg   case Triple::IOS:
42*82d56013Sjoerg     if (Target.isSimulatorEnvironment())
43*82d56013Sjoerg       return PlatformKind::iOSSimulator;
44*82d56013Sjoerg     if (Target.getEnvironment() == Triple::MacABI)
45*82d56013Sjoerg       return PlatformKind::macCatalyst;
46*82d56013Sjoerg     return PlatformKind::iOS;
47*82d56013Sjoerg   case Triple::TvOS:
48*82d56013Sjoerg     return Target.isSimulatorEnvironment() ? PlatformKind::tvOSSimulator
49*82d56013Sjoerg                                            : PlatformKind::tvOS;
50*82d56013Sjoerg   case Triple::WatchOS:
51*82d56013Sjoerg     return Target.isSimulatorEnvironment() ? PlatformKind::watchOSSimulator
52*82d56013Sjoerg                                            : PlatformKind::watchOS;
53*82d56013Sjoerg     // TODO: add bridgeOS & driverKit once in llvm::Triple
54*82d56013Sjoerg   }
55*82d56013Sjoerg   llvm_unreachable("Unknown Target Triple");
56*82d56013Sjoerg }
57*82d56013Sjoerg 
mapToPlatformSet(ArrayRef<Triple> Targets)58*82d56013Sjoerg PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {
59*82d56013Sjoerg   PlatformSet Result;
60*82d56013Sjoerg   for (const auto &Target : Targets)
61*82d56013Sjoerg     Result.insert(mapToPlatformKind(Target));
62*82d56013Sjoerg   return Result;
63*82d56013Sjoerg }
64*82d56013Sjoerg 
getPlatformName(PlatformKind Platform)65*82d56013Sjoerg StringRef getPlatformName(PlatformKind Platform) {
66*82d56013Sjoerg   switch (Platform) {
67*82d56013Sjoerg   case PlatformKind::unknown:
68*82d56013Sjoerg     return "unknown";
69*82d56013Sjoerg   case PlatformKind::macOS:
70*82d56013Sjoerg     return "macOS";
71*82d56013Sjoerg   case PlatformKind::iOS:
72*82d56013Sjoerg     return "iOS";
73*82d56013Sjoerg   case PlatformKind::tvOS:
74*82d56013Sjoerg     return "tvOS";
75*82d56013Sjoerg   case PlatformKind::watchOS:
76*82d56013Sjoerg     return "watchOS";
77*82d56013Sjoerg   case PlatformKind::bridgeOS:
78*82d56013Sjoerg     return "bridgeOS";
79*82d56013Sjoerg   case PlatformKind::macCatalyst:
80*82d56013Sjoerg     return "macCatalyst";
81*82d56013Sjoerg   case PlatformKind::iOSSimulator:
82*82d56013Sjoerg     return "iOS Simulator";
83*82d56013Sjoerg   case PlatformKind::tvOSSimulator:
84*82d56013Sjoerg     return "tvOS Simulator";
85*82d56013Sjoerg   case PlatformKind::watchOSSimulator:
86*82d56013Sjoerg     return "watchOS Simulator";
87*82d56013Sjoerg   case PlatformKind::driverKit:
88*82d56013Sjoerg     return "DriverKit";
89*82d56013Sjoerg   }
90*82d56013Sjoerg   llvm_unreachable("Unknown llvm::MachO::PlatformKind enum");
91*82d56013Sjoerg }
92*82d56013Sjoerg 
getPlatformFromName(StringRef Name)93*82d56013Sjoerg PlatformKind getPlatformFromName(StringRef Name) {
94*82d56013Sjoerg   return StringSwitch<PlatformKind>(Name)
95*82d56013Sjoerg       .Case("macos", PlatformKind::macOS)
96*82d56013Sjoerg       .Case("ios", PlatformKind::iOS)
97*82d56013Sjoerg       .Case("tvos", PlatformKind::tvOS)
98*82d56013Sjoerg       .Case("watchos", PlatformKind::watchOS)
99*82d56013Sjoerg       .Case("bridgeos", PlatformKind::macOS)
100*82d56013Sjoerg       .Case("ios-macabi", PlatformKind::macCatalyst)
101*82d56013Sjoerg       .Case("ios-simulator", PlatformKind::iOSSimulator)
102*82d56013Sjoerg       .Case("tvos-simulator", PlatformKind::tvOSSimulator)
103*82d56013Sjoerg       .Case("watchos-simulator", PlatformKind::watchOSSimulator)
104*82d56013Sjoerg       .Case("driverkit", PlatformKind::driverKit)
105*82d56013Sjoerg       .Default(PlatformKind::unknown);
106*82d56013Sjoerg }
107*82d56013Sjoerg 
108*82d56013Sjoerg } // end namespace MachO.
109*82d56013Sjoerg } // end namespace llvm.
110