xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision e9e8876a4d6afc1ad5315faaa191b25121a813d7)
1 //===- Target.cpp -----------------------------------------------*- 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 #include "llvm/TextAPI/Target.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 namespace llvm {
18 namespace MachO {
19 
20 Expected<Target> Target::create(StringRef TargetValue) {
21   auto Result = TargetValue.split('-');
22   auto ArchitectureStr = Result.first;
23   auto Architecture = getArchitectureFromName(ArchitectureStr);
24   auto PlatformStr = Result.second;
25   PlatformKind Platform;
26   Platform = StringSwitch<PlatformKind>(PlatformStr)
27                  .Case("macos", PlatformKind::macOS)
28                  .Case("ios", PlatformKind::iOS)
29                  .Case("tvos", PlatformKind::tvOS)
30                  .Case("watchos", PlatformKind::watchOS)
31                  .Case("bridgeos", PlatformKind::bridgeOS)
32                  .Case("maccatalyst", PlatformKind::macCatalyst)
33                  .Case("ios-simulator", PlatformKind::iOSSimulator)
34                  .Case("tvos-simulator", PlatformKind::tvOSSimulator)
35                  .Case("watchos-simulator", PlatformKind::watchOSSimulator)
36                  .Case("driverkit", PlatformKind::driverKit)
37                  .Default(PlatformKind::unknown);
38 
39   if (Platform == PlatformKind::unknown) {
40     if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
41       PlatformStr = PlatformStr.drop_front().drop_back();
42       unsigned long long RawValue;
43       if (!PlatformStr.getAsInteger(10, RawValue))
44         Platform = (PlatformKind)RawValue;
45     }
46   }
47 
48   return Target{Architecture, Platform};
49 }
50 
51 Target::operator std::string() const {
52   return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) + ")")
53       .str();
54 }
55 
56 raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
57   OS << std::string(Target);
58   return OS;
59 }
60 
61 PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
62   PlatformSet Result;
63   for (const auto &Target : Targets)
64     Result.insert(Target.Platform);
65   return Result;
66 }
67 
68 ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
69   ArchitectureSet Result;
70   for (const auto &Target : Targets)
71     Result.set(Target.Arch);
72   return Result;
73 }
74 
75 std::string getTargetTripleName(const Target &Targ) {
76   return (getArchitectureName(Targ.Arch) + "-apple-" +
77           getOSAndEnvironmentName(Targ.Platform))
78       .str();
79 }
80 
81 } // end namespace MachO.
82 } // end namespace llvm.
83