xref: /freebsd-src/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1fe6060f1SDimitry Andric //===- Target.cpp -----------------------------------------------*- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #include "llvm/TextAPI/Target.h"
10fe6060f1SDimitry Andric #include "llvm/ADT/StringSwitch.h"
1104eeddc0SDimitry Andric #include "llvm/ADT/Twine.h"
12fe6060f1SDimitry Andric #include "llvm/Support/raw_ostream.h"
13fe6060f1SDimitry Andric 
14fe6060f1SDimitry Andric namespace llvm {
15fe6060f1SDimitry Andric namespace MachO {
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric Expected<Target> Target::create(StringRef TargetValue) {
18fe6060f1SDimitry Andric   auto Result = TargetValue.split('-');
19fe6060f1SDimitry Andric   auto ArchitectureStr = Result.first;
20fe6060f1SDimitry Andric   auto Architecture = getArchitectureFromName(ArchitectureStr);
21fe6060f1SDimitry Andric   auto PlatformStr = Result.second;
2204eeddc0SDimitry Andric   PlatformType Platform;
2304eeddc0SDimitry Andric   Platform = StringSwitch<PlatformType>(PlatformStr)
2404eeddc0SDimitry Andric                  .Case("macos", PLATFORM_MACOS)
2504eeddc0SDimitry Andric                  .Case("ios", PLATFORM_IOS)
2604eeddc0SDimitry Andric                  .Case("tvos", PLATFORM_TVOS)
2704eeddc0SDimitry Andric                  .Case("watchos", PLATFORM_WATCHOS)
2804eeddc0SDimitry Andric                  .Case("bridgeos", PLATFORM_BRIDGEOS)
2904eeddc0SDimitry Andric                  .Case("maccatalyst", PLATFORM_MACCATALYST)
3004eeddc0SDimitry Andric                  .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
3104eeddc0SDimitry Andric                  .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
3204eeddc0SDimitry Andric                  .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
3304eeddc0SDimitry Andric                  .Case("driverkit", PLATFORM_DRIVERKIT)
3404eeddc0SDimitry Andric                  .Default(PLATFORM_UNKNOWN);
35fe6060f1SDimitry Andric 
3604eeddc0SDimitry Andric   if (Platform == PLATFORM_UNKNOWN) {
37fe6060f1SDimitry Andric     if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
38fe6060f1SDimitry Andric       PlatformStr = PlatformStr.drop_front().drop_back();
39fe6060f1SDimitry Andric       unsigned long long RawValue;
40fe6060f1SDimitry Andric       if (!PlatformStr.getAsInteger(10, RawValue))
4104eeddc0SDimitry Andric         Platform = (PlatformType)RawValue;
42fe6060f1SDimitry Andric     }
43fe6060f1SDimitry Andric   }
44fe6060f1SDimitry Andric 
45fe6060f1SDimitry Andric   return Target{Architecture, Platform};
46fe6060f1SDimitry Andric }
47fe6060f1SDimitry Andric 
48fe6060f1SDimitry Andric Target::operator std::string() const {
49*06c3fb27SDimitry Andric   auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
50*06c3fb27SDimitry Andric 
51*06c3fb27SDimitry Andric   return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
52*06c3fb27SDimitry Andric           Version + ")")
53fe6060f1SDimitry Andric       .str();
54fe6060f1SDimitry Andric }
55fe6060f1SDimitry Andric 
56fe6060f1SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
57fe6060f1SDimitry Andric   OS << std::string(Target);
58fe6060f1SDimitry Andric   return OS;
59fe6060f1SDimitry Andric }
60fe6060f1SDimitry Andric 
61*06c3fb27SDimitry Andric PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
62*06c3fb27SDimitry Andric   PlatformVersionSet Result;
63*06c3fb27SDimitry Andric   for (const auto &Target : Targets)
64*06c3fb27SDimitry Andric     Result.insert({Target.Platform, Target.MinDeployment});
65*06c3fb27SDimitry Andric   return Result;
66*06c3fb27SDimitry Andric }
67*06c3fb27SDimitry Andric 
68fe6060f1SDimitry Andric PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
69fe6060f1SDimitry Andric   PlatformSet Result;
70fe6060f1SDimitry Andric   for (const auto &Target : Targets)
71fe6060f1SDimitry Andric     Result.insert(Target.Platform);
72fe6060f1SDimitry Andric   return Result;
73fe6060f1SDimitry Andric }
74fe6060f1SDimitry Andric 
75fe6060f1SDimitry Andric ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
76fe6060f1SDimitry Andric   ArchitectureSet Result;
77fe6060f1SDimitry Andric   for (const auto &Target : Targets)
78fe6060f1SDimitry Andric     Result.set(Target.Arch);
79fe6060f1SDimitry Andric   return Result;
80fe6060f1SDimitry Andric }
81fe6060f1SDimitry Andric 
82fe6060f1SDimitry Andric std::string getTargetTripleName(const Target &Targ) {
83*06c3fb27SDimitry Andric   auto Version =
84*06c3fb27SDimitry Andric       Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
85*06c3fb27SDimitry Andric 
86fe6060f1SDimitry Andric   return (getArchitectureName(Targ.Arch) + "-apple-" +
87*06c3fb27SDimitry Andric           getOSAndEnvironmentName(Targ.Platform, Version))
88fe6060f1SDimitry Andric       .str();
89fe6060f1SDimitry Andric }
90fe6060f1SDimitry Andric 
91fe6060f1SDimitry Andric } // end namespace MachO.
92fe6060f1SDimitry Andric } // end namespace llvm.
93