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
14namespace llvm {
15namespace MachO {
16
17Expected<Target> Target::create(StringRef TargetValue) {
18 auto Result = TargetValue.split(Separator: '-');
19 auto ArchitectureStr = Result.first;
20 auto Architecture = getArchitectureFromName(Name: 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(Value: PLATFORM_UNKNOWN);
29
30 if (Platform == PLATFORM_UNKNOWN) {
31 if (PlatformStr.starts_with(Prefix: "<") && PlatformStr.ends_with(Suffix: ">")) {
32 PlatformStr = PlatformStr.drop_front().drop_back();
33 unsigned long long RawValue;
34 if (!PlatformStr.getAsInteger(Radix: 10, Result&: RawValue))
35 Platform = (PlatformType)RawValue;
36 }
37 }
38
39 return Target{Architecture, Platform};
40}
41
42Target::operator std::string() const {
43 auto Version = MinDeployment.empty() ? "" : MinDeployment.getAsString();
44
45 return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) +
46 Version + ")")
47 .str();
48}
49
50raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
51 OS << std::string(Target);
52 return OS;
53}
54
55PlatformVersionSet mapToPlatformVersionSet(ArrayRef<Target> Targets) {
56 PlatformVersionSet Result;
57 for (const auto &Target : Targets)
58 Result.insert(V: {Target.Platform, Target.MinDeployment});
59 return Result;
60}
61
62PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
63 PlatformSet Result;
64 for (const auto &Target : Targets)
65 Result.insert(V: Target.Platform);
66 return Result;
67}
68
69ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
70 ArchitectureSet Result;
71 for (const auto &Target : Targets)
72 Result.set(Target.Arch);
73 return Result;
74}
75
76std::string getTargetTripleName(const Target &Targ) {
77 auto Version =
78 Targ.MinDeployment.empty() ? "" : Targ.MinDeployment.getAsString();
79
80 return (getArchitectureName(Arch: Targ.Arch) + "-apple-" +
81 getOSAndEnvironmentName(Platform: Targ.Platform, Version))
82 .str();
83}
84
85} // end namespace MachO.
86} // end namespace llvm.
87