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 PlatformType mapToPlatformType(PlatformType Platform, bool WantSim) { 22 switch (Platform) { 23 default: 24 return Platform; 25 case PLATFORM_IOS: 26 return WantSim ? PLATFORM_IOSSIMULATOR : PLATFORM_IOS; 27 case PLATFORM_TVOS: 28 return WantSim ? PLATFORM_TVOSSIMULATOR : PLATFORM_TVOS; 29 case PLATFORM_WATCHOS: 30 return WantSim ? PLATFORM_WATCHOSSIMULATOR : PLATFORM_WATCHOS; 31 } 32 } 33 34 PlatformType mapToPlatformType(const Triple &Target) { 35 switch (Target.getOS()) { 36 default: 37 return PLATFORM_UNKNOWN; 38 case Triple::MacOSX: 39 return PLATFORM_MACOS; 40 case Triple::IOS: 41 if (Target.isSimulatorEnvironment()) 42 return PLATFORM_IOSSIMULATOR; 43 if (Target.getEnvironment() == Triple::MacABI) 44 return PLATFORM_MACCATALYST; 45 return PLATFORM_IOS; 46 case Triple::TvOS: 47 return Target.isSimulatorEnvironment() ? PLATFORM_TVOSSIMULATOR 48 : PLATFORM_TVOS; 49 case Triple::WatchOS: 50 return Target.isSimulatorEnvironment() ? PLATFORM_WATCHOSSIMULATOR 51 : PLATFORM_WATCHOS; 52 // TODO: add bridgeOS & driverKit once in llvm::Triple 53 } 54 } 55 56 PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) { 57 PlatformSet Result; 58 for (const auto &Target : Targets) 59 Result.insert(mapToPlatformType(Target)); 60 return Result; 61 } 62 63 StringRef getPlatformName(PlatformType Platform) { 64 switch (Platform) { 65 case PLATFORM_UNKNOWN: 66 return "unknown"; 67 case PLATFORM_MACOS: 68 return "macOS"; 69 case PLATFORM_IOS: 70 return "iOS"; 71 case PLATFORM_TVOS: 72 return "tvOS"; 73 case PLATFORM_WATCHOS: 74 return "watchOS"; 75 case PLATFORM_BRIDGEOS: 76 return "bridgeOS"; 77 case PLATFORM_MACCATALYST: 78 return "macCatalyst"; 79 case PLATFORM_IOSSIMULATOR: 80 return "iOS Simulator"; 81 case PLATFORM_TVOSSIMULATOR: 82 return "tvOS Simulator"; 83 case PLATFORM_WATCHOSSIMULATOR: 84 return "watchOS Simulator"; 85 case PLATFORM_DRIVERKIT: 86 return "DriverKit"; 87 } 88 } 89 90 PlatformType getPlatformFromName(StringRef Name) { 91 return StringSwitch<PlatformType>(Name) 92 .Case("macos", PLATFORM_MACOS) 93 .Case("ios", PLATFORM_IOS) 94 .Case("tvos", PLATFORM_TVOS) 95 .Case("watchos", PLATFORM_WATCHOS) 96 .Case("bridgeos", PLATFORM_BRIDGEOS) 97 .Case("ios-macabi", PLATFORM_MACCATALYST) 98 .Case("ios-simulator", PLATFORM_IOSSIMULATOR) 99 .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR) 100 .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR) 101 .Case("driverkit", PLATFORM_DRIVERKIT) 102 .Default(PLATFORM_UNKNOWN); 103 } 104 105 std::string getOSAndEnvironmentName(PlatformType Platform, 106 std::string Version) { 107 switch (Platform) { 108 case PLATFORM_UNKNOWN: 109 return "darwin" + Version; 110 case PLATFORM_MACOS: 111 return "macos" + Version; 112 case PLATFORM_IOS: 113 return "ios" + Version; 114 case PLATFORM_TVOS: 115 return "tvos" + Version; 116 case PLATFORM_WATCHOS: 117 return "watchos" + Version; 118 case PLATFORM_BRIDGEOS: 119 return "bridgeos" + Version; 120 case PLATFORM_MACCATALYST: 121 return "ios" + Version + "-macabi"; 122 case PLATFORM_IOSSIMULATOR: 123 return "ios" + Version + "-simulator"; 124 case PLATFORM_TVOSSIMULATOR: 125 return "tvos" + Version + "-simulator"; 126 case PLATFORM_WATCHOSSIMULATOR: 127 return "watchos" + Version + "-simulator"; 128 case PLATFORM_DRIVERKIT: 129 return "driverkit" + Version; 130 } 131 } 132 133 } // end namespace MachO. 134 } // end namespace llvm. 135