xref: /freebsd/contrib/llvm-project/llvm/lib/TextAPI/Target.cpp (revision 73ff7384e025033abc98fd5437a48beb8077a90b)
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                   .Case("macos", PLATFORM_MACOS)
25                   .Case("ios", PLATFORM_IOS)
26                   .Case("tvos", PLATFORM_TVOS)
27                   .Case("watchos", PLATFORM_WATCHOS)
28                   .Case("bridgeos", PLATFORM_BRIDGEOS)
29                   .Case("maccatalyst", PLATFORM_MACCATALYST)
30                   .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
31                   .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
32                   .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
33                   .Case("driverkit", PLATFORM_DRIVERKIT)
34                   .Default(PLATFORM_UNKNOWN);
35  
36    if (Platform == PLATFORM_UNKNOWN) {
37      if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
38        PlatformStr = PlatformStr.drop_front().drop_back();
39        unsigned long long RawValue;
40        if (!PlatformStr.getAsInteger(10, RawValue))
41          Platform = (PlatformType)RawValue;
42      }
43    }
44  
45    return Target{Architecture, Platform};
46  }
47  
48  Target::operator std::string() const {
49    auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
50  
51    return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
52            Version + ")")
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  PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
62    PlatformVersionSet Result;
63    for (const auto &Target : Targets)
64      Result.insert({Target.Platform, Target.MinDeployment});
65    return Result;
66  }
67  
68  PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
69    PlatformSet Result;
70    for (const auto &Target : Targets)
71      Result.insert(Target.Platform);
72    return Result;
73  }
74  
75  ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
76    ArchitectureSet Result;
77    for (const auto &Target : Targets)
78      Result.set(Target.Arch);
79    return Result;
80  }
81  
82  std::string getTargetTripleName(const Target &Targ) {
83    auto Version =
84        Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
85  
86    return (getArchitectureName(Targ.Arch) + "-apple-" +
87            getOSAndEnvironmentName(Targ.Platform, Version))
88        .str();
89  }
90  
91  } // end namespace MachO.
92  } // end namespace llvm.
93