-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathtriplet.cpp
117 lines (104 loc) · 3.5 KB
/
triplet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <vcpkg/base/strings.h>
#include <vcpkg/triplet.h>
#include <vcpkg/vcpkgcmdarguments.h>
namespace vcpkg
{
struct TripletInstance
{
TripletInstance(std::string&& s) : value(std::move(s)), hash(std::hash<std::string>()(value)) { }
const std::string value;
const size_t hash = 0;
bool operator==(const TripletInstance& o) const { return o.value == value; }
};
const TripletInstance Triplet::DEFAULT_INSTANCE({});
}
namespace std
{
template<>
struct hash<vcpkg::TripletInstance>
{
size_t operator()(const vcpkg::TripletInstance& t) const { return t.hash; }
};
}
namespace vcpkg
{
Triplet Triplet::from_canonical_name(std::string&& triplet_as_string)
{
static std::unordered_set<TripletInstance> g_triplet_instances;
std::string s(Strings::ascii_to_lowercase(std::move(triplet_as_string)));
const auto p = g_triplet_instances.emplace(std::move(s));
return &*p.first;
}
const std::string& Triplet::canonical_name() const { return this->m_instance->value; }
const std::string& Triplet::to_string() const { return this->canonical_name(); }
void Triplet::to_string(std::string& out) const { out.append(this->canonical_name()); }
size_t Triplet::hash_code() const { return m_instance->hash; }
Optional<System::CPUArchitecture> Triplet::guess_architecture() const noexcept
{
using System::CPUArchitecture;
if (Strings::starts_with(this->canonical_name(), "x86-"))
{
return CPUArchitecture::X86;
}
if (Strings::starts_with(this->canonical_name(), "x64-"))
{
return CPUArchitecture::X64;
}
if (Strings::starts_with(this->canonical_name(), "arm-"))
{
return CPUArchitecture::ARM;
}
if (Strings::starts_with(this->canonical_name(), "arm64-"))
{
return CPUArchitecture::ARM64;
}
if (Strings::starts_with(this->canonical_name(), "s390x-"))
{
return CPUArchitecture::S390X;
}
if (Strings::starts_with(this->canonical_name(), "ppc64le-"))
{
return CPUArchitecture::PPC64LE;
}
return nullopt;
}
static Triplet system_triplet()
{
#if defined(_WIN32)
StringLiteral operating_system = "windows";
#elif defined(__APPLE__)
StringLiteral operating_system = "osx";
#elif defined(__FreeBSD__)
StringLiteral operating_system = "freebsd";
#elif defined(__OpenBSD__)
StringLiteral operating_system = "openbsd";
#elif defined(__GLIBC__)
StringLiteral operating_system = "linux";
#else
StringLiteral operating_system = "linux-musl";
#endif
auto host_proc = System::get_host_processor();
auto canonical_name = Strings::format("%s-%s", to_zstring_view(host_proc), operating_system);
return Triplet::from_canonical_name(std::move(canonical_name));
}
Triplet default_triplet(const VcpkgCmdArguments& args)
{
if (args.triplet != nullptr)
{
return Triplet::from_canonical_name(std::string(*args.triplet));
}
#if defined(_WIN32)
return Triplet::from_canonical_name("x86-windows");
#else
return system_triplet();
#endif
}
Triplet default_host_triplet(const VcpkgCmdArguments& args)
{
if (args.host_triplet != nullptr)
{
return Triplet::from_canonical_name(std::string(*args.host_triplet));
}
return system_triplet();
}
}