1 //===- Target.cpp -----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
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
);
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
;
48 return Target
{Architecture
, Platform
};
51 Target::operator std::string() const {
52 return (getArchitectureName(Arch
) + " (" + getPlatformName(Platform
) + ")")
56 raw_ostream
&operator<<(raw_ostream
&OS
, const Target
&Target
) {
57 OS
<< std::string(Target
);
61 PlatformSet
mapToPlatformSet(ArrayRef
<Target
> Targets
) {
63 for (const auto &Target
: Targets
)
64 Result
.insert(Target
.Platform
);
68 ArchitectureSet
mapToArchitectureSet(ArrayRef
<Target
> Targets
) {
69 ArchitectureSet Result
;
70 for (const auto &Target
: Targets
)
71 Result
.set(Target
.Arch
);
75 std::string
getTargetTripleName(const Target
&Targ
) {
76 return (getArchitectureName(Targ
.Arch
) + "-apple-" +
77 getOSAndEnvironmentName(Targ
.Platform
))
81 } // end namespace MachO.
82 } // end namespace llvm.