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/StringSwitch.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/raw_ostream.h"
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, \
26 .Case(#tapi_target, PLATFORM_##platform)
27 #include "llvm/BinaryFormat/MachO.def"
28 .Default(PLATFORM_UNKNOWN
);
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
;
39 return Target
{Architecture
, Platform
};
42 Target::operator std::string() const {
43 auto Version
= MinDeployment
.empty() ? "" : MinDeployment
.getAsString();
45 return (getArchitectureName(Arch
) + " (" + getPlatformName(Platform
) +
50 raw_ostream
&operator<<(raw_ostream
&OS
, const Target
&Target
) {
51 OS
<< std::string(Target
);
55 PlatformVersionSet
mapToPlatformVersionSet(ArrayRef
<Target
> Targets
) {
56 PlatformVersionSet Result
;
57 for (const auto &Target
: Targets
)
58 Result
.insert({Target
.Platform
, Target
.MinDeployment
});
62 PlatformSet
mapToPlatformSet(ArrayRef
<Target
> Targets
) {
64 for (const auto &Target
: Targets
)
65 Result
.insert(Target
.Platform
);
69 ArchitectureSet
mapToArchitectureSet(ArrayRef
<Target
> Targets
) {
70 ArchitectureSet Result
;
71 for (const auto &Target
: Targets
)
72 Result
.set(Target
.Arch
);
76 std::string
getTargetTripleName(const Target
&Targ
) {
78 Targ
.MinDeployment
.empty() ? "" : Targ
.MinDeployment
.getAsString();
80 return (getArchitectureName(Targ
.Arch
) + "-apple-" +
81 getOSAndEnvironmentName(Targ
.Platform
, Version
))
85 } // end namespace MachO.
86 } // end namespace llvm.