xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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)
24*5f757f3fSDimitry Andric #define PLATFORM(platform, id, name, build_name, target, tapi_target,          \
25*5f757f3fSDimitry Andric                  marketing)                                                    \
26*5f757f3fSDimitry Andric   .Case(#tapi_target, PLATFORM_##platform)
27*5f757f3fSDimitry Andric #include "llvm/BinaryFormat/MachO.def"
2804eeddc0SDimitry Andric                  .Default(PLATFORM_UNKNOWN);
29fe6060f1SDimitry Andric 
3004eeddc0SDimitry Andric   if (Platform == PLATFORM_UNKNOWN) {
31*5f757f3fSDimitry Andric     if (PlatformStr.starts_with("<") && PlatformStr.ends_with(">")) {
32fe6060f1SDimitry Andric       PlatformStr = PlatformStr.drop_front().drop_back();
33fe6060f1SDimitry Andric       unsigned long long RawValue;
34fe6060f1SDimitry Andric       if (!PlatformStr.getAsInteger(10, RawValue))
3504eeddc0SDimitry Andric         Platform = (PlatformType)RawValue;
36fe6060f1SDimitry Andric     }
37fe6060f1SDimitry Andric   }
38fe6060f1SDimitry Andric 
39fe6060f1SDimitry Andric   return Target{Architecture, Platform};
40fe6060f1SDimitry Andric }
41fe6060f1SDimitry Andric 
42fe6060f1SDimitry Andric Target::operator std::string() const {
4306c3fb27SDimitry Andric   auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
4406c3fb27SDimitry Andric 
4506c3fb27SDimitry Andric   return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
4606c3fb27SDimitry Andric           Version + ")")
47fe6060f1SDimitry Andric       .str();
48fe6060f1SDimitry Andric }
49fe6060f1SDimitry Andric 
50fe6060f1SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
51fe6060f1SDimitry Andric   OS << std::string(Target);
52fe6060f1SDimitry Andric   return OS;
53fe6060f1SDimitry Andric }
54fe6060f1SDimitry Andric 
5506c3fb27SDimitry Andric PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
5606c3fb27SDimitry Andric   PlatformVersionSet Result;
5706c3fb27SDimitry Andric   for (const auto &Target : Targets)
5806c3fb27SDimitry Andric     Result.insert({Target.Platform, Target.MinDeployment});
5906c3fb27SDimitry Andric   return Result;
6006c3fb27SDimitry Andric }
6106c3fb27SDimitry Andric 
62fe6060f1SDimitry Andric PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
63fe6060f1SDimitry Andric   PlatformSet Result;
64fe6060f1SDimitry Andric   for (const auto &Target : Targets)
65fe6060f1SDimitry Andric     Result.insert(Target.Platform);
66fe6060f1SDimitry Andric   return Result;
67fe6060f1SDimitry Andric }
68fe6060f1SDimitry Andric 
69fe6060f1SDimitry Andric ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
70fe6060f1SDimitry Andric   ArchitectureSet Result;
71fe6060f1SDimitry Andric   for (const auto &Target : Targets)
72fe6060f1SDimitry Andric     Result.set(Target.Arch);
73fe6060f1SDimitry Andric   return Result;
74fe6060f1SDimitry Andric }
75fe6060f1SDimitry Andric 
76fe6060f1SDimitry Andric std::string getTargetTripleName(const Target &Targ) {
7706c3fb27SDimitry Andric   auto Version =
7806c3fb27SDimitry Andric       Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
7906c3fb27SDimitry Andric 
80fe6060f1SDimitry Andric   return (getArchitectureName(Targ.Arch) + "-apple-" +
8106c3fb27SDimitry Andric           getOSAndEnvironmentName(Targ.Platform, Version))
82fe6060f1SDimitry Andric       .str();
83fe6060f1SDimitry Andric }
84fe6060f1SDimitry Andric 
85fe6060f1SDimitry Andric } // end namespace MachO.
86fe6060f1SDimitry Andric } // end namespace llvm.
87