134 lines · cpp
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-exception6//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/TargetParser/Triple.h"17 18namespace llvm {19namespace MachO {20 21PlatformType 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 34PlatformType 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_TVOSSIMULATOR48 : PLATFORM_TVOS;49 case Triple::WatchOS:50 return Target.isSimulatorEnvironment() ? PLATFORM_WATCHOSSIMULATOR51 : PLATFORM_WATCHOS;52 case Triple::BridgeOS:53 return PLATFORM_BRIDGEOS;54 case Triple::DriverKit:55 return PLATFORM_DRIVERKIT;56 case Triple::XROS:57 return Target.isSimulatorEnvironment() ? PLATFORM_XROS_SIMULATOR58 : PLATFORM_XROS;59 }60}61 62PlatformSet mapToPlatformSet(ArrayRef<Triple> Targets) {63 PlatformSet Result;64 for (const auto &Target : Targets)65 Result.insert(mapToPlatformType(Target));66 return Result;67}68 69StringRef getPlatformName(PlatformType Platform) {70 switch (Platform) {71#define PLATFORM(platform, id, name, build_name, target, tapi_target, \72 marketing) \73 case PLATFORM_##platform: \74 return #marketing;75#include "llvm/BinaryFormat/MachO.def"76 }77 llvm_unreachable("Unknown llvm::MachO::PlatformType enum");78}79 80PlatformType getPlatformFromName(StringRef Name) {81 return StringSwitch<PlatformType>(Name)82 .Case("osx", PLATFORM_MACOS)83#define PLATFORM(platform, id, name, build_name, target, tapi_target, \84 marketing) \85 .Case(#target, PLATFORM_##platform)86#include "llvm/BinaryFormat/MachO.def"87 .Default(PLATFORM_UNKNOWN);88}89 90std::string getOSAndEnvironmentName(PlatformType Platform,91 std::string Version) {92 switch (Platform) {93 case PLATFORM_UNKNOWN:94 return "darwin" + Version;95 case PLATFORM_MACOS:96 return "macos" + Version;97 case PLATFORM_IOS:98 return "ios" + Version;99 case PLATFORM_TVOS:100 return "tvos" + Version;101 case PLATFORM_WATCHOS:102 return "watchos" + Version;103 case PLATFORM_BRIDGEOS:104 return "bridgeos" + Version;105 case PLATFORM_MACCATALYST:106 return "ios" + Version + "-macabi";107 case PLATFORM_IOSSIMULATOR:108 return "ios" + Version + "-simulator";109 case PLATFORM_TVOSSIMULATOR:110 return "tvos" + Version + "-simulator";111 case PLATFORM_WATCHOSSIMULATOR:112 return "watchos" + Version + "-simulator";113 case PLATFORM_DRIVERKIT:114 return "driverkit" + Version;115 case PLATFORM_XROS:116 return "xros" + Version;117 case PLATFORM_XROS_SIMULATOR:118 return "xros" + Version + "-simulator";119 }120 llvm_unreachable("Unknown llvm::MachO::PlatformType enum");121}122 123VersionTuple mapToSupportedOSVersion(const Triple &Triple) {124 const VersionTuple MinSupportedOS = Triple.getMinimumSupportedOSVersion();125 if (MinSupportedOS > Triple.getOSVersion())126 return MinSupportedOS;127 return Triple::getCanonicalVersionForOS(128 Triple.getOS(), Triple.getOSVersion(),129 Triple::isValidVersionForOS(Triple.getOS(), Triple.getOSVersion()));130}131 132} // end namespace MachO.133} // end namespace llvm.134