xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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/StringSwitch.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 namespace llvm {
15 namespace MachO {
16 
17 Expected<Target> Target::create(StringRef TargetValue) {
18   auto Result = TargetValue.split('-');
19   auto ArchitectureStr = Result.first;
20   auto Architecture = getArchitectureFromName(ArchitectureStr);
21   auto PlatformStr = Result.second;
22   PlatformType Platform;
23   Platform = StringSwitch<PlatformType>(PlatformStr)
24 #define PLATFORM(platform, id, name, build_name, target, tapi_target,          \
25                  marketing)                                                    \
26   .Case(#tapi_target, PLATFORM_##platform)
27 #include "llvm/BinaryFormat/MachO.def"
28                  .Default(PLATFORM_UNKNOWN);
29 
30   if (Platform == PLATFORM_UNKNOWN) {
31     if (PlatformStr.starts_with("<") && PlatformStr.ends_with(">")) {
32       PlatformStr = PlatformStr.drop_front().drop_back();
33       unsigned long long RawValue;
34       if (!PlatformStr.getAsInteger(10, RawValue))
35         Platform = (PlatformType)RawValue;
36     }
37   }
38 
39   return Target{Architecture, Platform};
40 }
41 
42 Target::operator std::string() const {
43   auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
44 
45   return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
46           Version + ")")
47       .str();
48 }
49 
50 raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
51   OS << std::string(Target);
52   return OS;
53 }
54 
55 PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
56   PlatformVersionSet Result;
57   for (const auto &Target : Targets)
58     Result.insert({Target.Platform, Target.MinDeployment});
59   return Result;
60 }
61 
62 PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
63   PlatformSet Result;
64   for (const auto &Target : Targets)
65     Result.insert(Target.Platform);
66   return Result;
67 }
68 
69 ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
70   ArchitectureSet Result;
71   for (const auto &Target : Targets)
72     Result.set(Target.Arch);
73   return Result;
74 }
75 
76 std::string getTargetTripleName(const Target &Targ) {
77   auto Version =
78       Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
79 
80   return (getArchitectureName(Targ.Arch) + "-apple-" +
81           getOSAndEnvironmentName(Targ.Platform, Version))
82       .str();
83 }
84 
85 } // end namespace MachO.
86 } // end namespace llvm.
87