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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
//===- Multilib.cpp - Multilib Implementation -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Driver/Multilib.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <string>
using namespace clang;
using namespace driver;
using namespace llvm::sys;
/// normalize Segment to "/foo/bar" or "".
static void normalizePathSegment(std::string &Segment) {
StringRef seg = Segment;
// Prune trailing "/" or "./"
while (true) {
StringRef last = path::filename(seg);
if (last != ".")
break;
seg = path::parent_path(seg);
}
if (seg.empty() || seg == "/") {
Segment.clear();
return;
}
// Add leading '/'
if (seg.front() != '/') {
Segment = "/" + seg.str();
} else {
Segment = std::string(seg);
}
}
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
StringRef IncludeSuffix, int Priority)
: GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
Priority(Priority) {
normalizePathSegment(this->GCCSuffix);
normalizePathSegment(this->OSSuffix);
normalizePathSegment(this->IncludeSuffix);
}
Multilib &Multilib::gccSuffix(StringRef S) {
GCCSuffix = std::string(S);
normalizePathSegment(GCCSuffix);
return *this;
}
Multilib &Multilib::osSuffix(StringRef S) {
OSSuffix = std::string(S);
normalizePathSegment(OSSuffix);
return *this;
}
Multilib &Multilib::includeSuffix(StringRef S) {
IncludeSuffix = std::string(S);
normalizePathSegment(IncludeSuffix);
return *this;
}
LLVM_DUMP_METHOD void Multilib::dump() const {
print(llvm::errs());
}
void Multilib::print(raw_ostream &OS) const {
assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
if (GCCSuffix.empty())
OS << ".";
else {
OS << StringRef(GCCSuffix).drop_front();
}
OS << ";";
for (StringRef Flag : Flags) {
if (Flag.front() == '+')
OS << "@" << Flag.substr(1);
}
}
bool Multilib::isValid() const {
llvm::StringMap<int> FlagSet;
for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
StringRef Flag(Flags[I]);
llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
if (SI == FlagSet.end())
FlagSet[Flag.substr(1)] = I;
else if (Flags[I] != Flags[SI->getValue()])
return false;
}
return true;
}
bool Multilib::operator==(const Multilib &Other) const {
// Check whether the flags sets match
// allowing for the match to be order invariant
llvm::StringSet<> MyFlags;
for (const auto &Flag : Flags)
MyFlags.insert(Flag);
for (const auto &Flag : Other.Flags)
if (MyFlags.find(Flag) == MyFlags.end())
return false;
if (osSuffix() != Other.osSuffix())
return false;
if (gccSuffix() != Other.gccSuffix())
return false;
if (includeSuffix() != Other.includeSuffix())
return false;
return true;
}
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
M.print(OS);
return OS;
}
MultilibSet &MultilibSet::Maybe(const Multilib &M) {
Multilib Opposite;
// Negate any '+' flags
for (StringRef Flag : M.flags()) {
if (Flag.front() == '+')
Opposite.flags().push_back(("-" + Flag.substr(1)).str());
}
return Either(M, Opposite);
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
return Either({M1, M2});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3) {
return Either({M1, M2, M3});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3, const Multilib &M4) {
return Either({M1, M2, M3, M4});
}
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
const Multilib &M3, const Multilib &M4,
const Multilib &M5) {
return Either({M1, M2, M3, M4, M5});
}
static Multilib compose(const Multilib &Base, const Multilib &New) {
SmallString<128> GCCSuffix;
llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
SmallString<128> OSSuffix;
llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
SmallString<128> IncludeSuffix;
llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
New.includeSuffix());
Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
Multilib::flags_list &Flags = Composed.flags();
Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
return Composed;
}
MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
multilib_list Composed;
if (Multilibs.empty())
Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
MultilibSegments.end());
else {
for (const auto &New : MultilibSegments) {
for (const auto &Base : *this) {
Multilib MO = compose(Base, New);
if (MO.isValid())
Composed.push_back(MO);
}
}
Multilibs = Composed;
}
return *this;
}
MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
filterInPlace(F, Multilibs);
return *this;
}
MultilibSet &MultilibSet::FilterOut(const char *Regex) {
llvm::Regex R(Regex);
#ifndef NDEBUG
std::string Error;
if (!R.isValid(Error)) {
llvm::errs() << Error;
llvm_unreachable("Invalid regex!");
}
#endif
filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
Multilibs);
return *this;
}
void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
void MultilibSet::combineWith(const MultilibSet &Other) {
Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
}
static bool isFlagEnabled(StringRef Flag) {
char Indicator = Flag.front();
assert(Indicator == '+' || Indicator == '-');
return Indicator == '+';
}
bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
llvm::StringMap<bool> FlagSet;
// Stuff all of the flags into the FlagSet such that a true mappend indicates
// the flag was enabled, and a false mappend indicates the flag was disabled.
for (StringRef Flag : Flags)
FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
for (StringRef Flag : M.flags()) {
llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
if (SI != FlagSet.end())
if (SI->getValue() != isFlagEnabled(Flag))
return true;
}
return false;
}, Multilibs);
if (Filtered.empty())
return false;
if (Filtered.size() == 1) {
M = Filtered[0];
return true;
}
// Sort multilibs by priority and select the one with the highest priority.
llvm::sort(Filtered.begin(), Filtered.end(),
[](const Multilib &a, const Multilib &b) -> bool {
return a.priority() > b.priority();
});
if (Filtered[0].priority() > Filtered[1].priority()) {
M = Filtered[0];
return true;
}
// TODO: We should consider returning llvm::Error rather than aborting.
assert(false && "More than one multilib with the same priority");
return false;
}
LLVM_DUMP_METHOD void MultilibSet::dump() const {
print(llvm::errs());
}
void MultilibSet::print(raw_ostream &OS) const {
for (const auto &M : *this)
OS << M << "\n";
}
MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
const multilib_list &Ms) {
multilib_list Copy(Ms);
filterInPlace(F, Copy);
return Copy;
}
void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
llvm::erase_if(Ms, F);
}
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
MS.print(OS);
return OS;
}
|