clang 10.0.0git

Multilib.cpp
Go to the documentation of this file.
1 //===- Multilib.cpp - Multilib Implementation -----------------------------===//
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 "clang/Driver/Multilib.h"
10 #include "clang/Basic/LLVM.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringMap.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <algorithm>
21 #include <cassert>
22 #include <string>
23
24 using namespace clang;
25 using namespace driver;
26 using namespace llvm::sys;
27
28 /// normalize Segment to "/foo/bar" or "".
29 static void normalizePathSegment(std::string &Segment) {
30 StringRef seg = Segment;
31
32 // Prune trailing "/" or "./"
33 while (true) {
34 StringRef last = path::filename(seg);
35 if (last != ".")
36 break;
37 seg = path::parent_path(seg);
38 }
39
40 if (seg.empty() || seg == "/") {
41 Segment.clear();
42 return;
43 }
44
45 // Add leading '/'
46 if (seg.front() != '/') {
47 Segment = "/" + seg.str();
48 } else {
49 Segment = seg;
50 }
51 }
52
53 Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
54 StringRef IncludeSuffix, int Priority)
55 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
56 Priority(Priority) {
57 normalizePathSegment(this->GCCSuffix);
58 normalizePathSegment(this->OSSuffix);
59 normalizePathSegment(this->IncludeSuffix);
60 }
61
62 Multilib &Multilib::gccSuffix(StringRef S) {
63 GCCSuffix = S;
64 normalizePathSegment(GCCSuffix);
65 return *this;
66 }
67
68 Multilib &Multilib::osSuffix(StringRef S) {
69 OSSuffix = S;
70 normalizePathSegment(OSSuffix);
71 return *this;
72 }
73
74 Multilib &Multilib::includeSuffix(StringRef S) {
75 IncludeSuffix = S;
76 normalizePathSegment(IncludeSuffix);
77 return *this;
78 }
79
80 LLVM_DUMP_METHOD void Multilib::dump() const {
81 print(llvm::errs());
82 }
83
84 void Multilib::print(raw_ostream &OS) const {
85 assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
86 if (GCCSuffix.empty())
87 OS << ".";
88 else {
89 OS << StringRef(GCCSuffix).drop_front();
90 }
91 OS << ";";
92 for (StringRef Flag : Flags) {
93 if (Flag.front() == '+')
94 OS << "@" << Flag.substr(1);
95 }
96 }
97
98 bool Multilib::isValid() const {
99 llvm::StringMap<int> FlagSet;
100 for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
101 StringRef Flag(Flags[I]);
102 llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
103
104 assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
105
106 if (SI == FlagSet.end())
107 FlagSet[Flag.substr(1)] = I;
108 else if (Flags[I] != Flags[SI->getValue()])
109 return false;
110 }
111 return true;
112 }
113
114 bool Multilib::operator==(const Multilib &Other) const {
115 // Check whether the flags sets match
116 // allowing for the match to be order invariant
117 llvm::StringSet<> MyFlags;
118 for (const auto &Flag : Flags)
119 MyFlags.insert(Flag);
120
121 for (const auto &Flag : Other.Flags)
122 if (MyFlags.find(Flag) == MyFlags.end())
123 return false;
124
125 if (osSuffix() != Other.osSuffix())
126 return false;
127
128 if (gccSuffix() != Other.gccSuffix())
129 return false;
130
131 if (includeSuffix() != Other.includeSuffix())
132 return false;
133
134 return true;
135 }
136
137 raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
138 M.print(OS);
139 return OS;
140 }
141
142 MultilibSet &MultilibSet::Maybe(const Multilib &M) {
143 Multilib Opposite;
144 // Negate any '+' flags
145 for (StringRef Flag : M.flags()) {
146 if (Flag.front() == '+')
147 Opposite.flags().push_back(("-" + Flag.substr(1)).str());
148 }
149 return Either(M, Opposite);
150 }
151
152 MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
153 return Either({M1, M2});
154 }
155
156 MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
157 const Multilib &M3) {
158 return Either({M1, M2, M3});
159 }
160
161 MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
162 const Multilib &M3, const Multilib &M4) {
163 return Either({M1, M2, M3, M4});
164 }
165
166 MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
167 const Multilib &M3, const Multilib &M4,
168 const Multilib &M5) {
169 return Either({M1, M2, M3, M4, M5});
170 }
171
172 static Multilib compose(const Multilib &Base, const Multilib &New) {
173 SmallString<128> GCCSuffix;
174 llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
175 SmallString<128> OSSuffix;
176 llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
177 SmallString<128> IncludeSuffix;
178 llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
179 New.includeSuffix());
180
181 Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
182
183 Multilib::flags_list &Flags = Composed.flags();
184
185 Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
186 Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
187
188 return Composed;
189 }
190
191 MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
192 multilib_list Composed;
193
194 if (Multilibs.empty())
195 Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
196 MultilibSegments.end());
197 else {
198 for (const auto &New : MultilibSegments) {
199 for (const auto &Base : *this) {
200 Multilib MO = compose(Base, New);
201 if (MO.isValid())
202 Composed.push_back(MO);
203 }
204 }
205
206 Multilibs = Composed;
207 }
208
209 return *this;
210 }
211
212 MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
213 filterInPlace(F, Multilibs);
214 return *this;
215 }
216
217 MultilibSet &MultilibSet::FilterOut(const char *Regex) {
218 llvm::Regex R(Regex);
219 #ifndef NDEBUG
220 std::string Error;
221 if (!R.isValid(Error)) {
222 llvm::errs() << Error;
223 llvm_unreachable("Invalid regex!");
224 }
225 #endif
226
227 filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
228 Multilibs);
229 return *this;
230 }
231
232 void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
233
234 void MultilibSet::combineWith(const MultilibSet &Other) {
235 Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
236 }
237
238 static bool isFlagEnabled(StringRef Flag) {
239 char Indicator = Flag.front();
240 assert(Indicator == '+' || Indicator == '-');
241 return Indicator == '+';
242 }
243
244 bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
245 llvm::StringMap<bool> FlagSet;
246
247 // Stuff all of the flags into the FlagSet such that a true mappend indicates
248 // the flag was enabled, and a false mappend indicates the flag was disabled.
249 for (StringRef Flag : Flags)
250 FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
251
252 multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
253 for (StringRef Flag : M.flags()) {
254 llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
255 if (SI != FlagSet.end())
256 if (SI->getValue() != isFlagEnabled(Flag))
257 return true;
258 }
259 return false;
260 }, Multilibs);
261
262 if (Filtered.empty())
263 return false;
264 if (Filtered.size() == 1) {
265 M = Filtered[0];
266 return true;
267 }
268
269 // Sort multilibs by priority and select the one with the highest priority.
270 llvm::sort(Filtered.begin(), Filtered.end(),
271 [](const Multilib &a, const Multilib &b) -> bool {
272 return a.priority() > b.priority();
273 });
274
275 if (Filtered[0].priority() > Filtered[1].priority()) {
276 M = Filtered[0];
277 return true;
278 }
279
280 // TODO: We should consider returning llvm::Error rather than aborting.
281 assert(false && "More than one multilib with the same priority");
282 return false;
283 }
284
285 LLVM_DUMP_METHOD void MultilibSet::dump() const {
286 print(llvm::errs());
287 }
288
289 void MultilibSet::print(raw_ostream &OS) const {
290 for (const auto &M : *this)
291 OS << M << "\n";
292 }
293
294 MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
295 const multilib_list &Ms) {
296 multilib_list Copy(Ms);
297 filterInPlace(F, Copy);
298 return Copy;
299 }
300
301 void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
302 Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
303 }
304
305 raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
306 MS.print(OS);
307 return OS;
308 }
clang::driver::MultilibSet::Either
MultilibSet & Either(const Multilib &M1, const Multilib &M2)
Add a set of mutually incompatible Multilib segments.
Definition: Multilib.cpp:152
clang::driver::MultilibSet::Maybe
MultilibSet & Maybe(const Multilib &M)
Add an optional Multilib segment.
Definition: Multilib.cpp:142
clang::driver::Multilib::operator==
bool operator==(const Multilib &Other) const
Definition: Multilib.cpp:114
compose
static Multilib compose(const Multilib &Base, const Multilib &New)
Definition: Multilib.cpp:172
Base
clang::driver::MultilibSet::end
iterator end()
Definition: Multilib.h:163
clang::driver::MultilibSet::multilib_list
std::vector< Multilib > multilib_list
Definition: Multilib.h:116
sys
clang::driver::MultilibSet::select
bool select(const Multilib::flags_list &Flags, Multilib &M) const
Pick the best multilib in the set,.
Definition: Multilib.cpp:244
clang::driver::Multilib::includeSuffix
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:67
clang::driver::operator<<
raw_ostream & operator<<(raw_ostream &OS, const Multilib &M)
Definition: Multilib.cpp:137
clang::driver::Multilib::gccSuffix
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:45
clang::driver::MultilibSet::combineWith
void combineWith(const MultilibSet &MS)
Union this set of multilibs with another.
Definition: Multilib.cpp:234
normalizePathSegment
static void normalizePathSegment(std::string &Segment)
normalize Segment to "/foo/bar" or "".
Definition: Multilib.cpp:29
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::driver::Multilib::osSuffix
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:56
clang::driver::MultilibSet
Definition: Multilib.h:114
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:19
llvm::ArrayRef
Definition: LLVM.h:31
llvm::SmallString< 128 >
clang::driver::MultilibSet::print
void print(raw_ostream &OS) const
Definition: Multilib.cpp:289
clang::driver::Multilib::Multilib
Multilib(StringRef GCCSuffix={}, StringRef OSSuffix={}, StringRef IncludeSuffix={}, int Priority=0)
Definition: Multilib.cpp:53
clang::driver::MultilibSet::begin
iterator begin()
Definition: Multilib.h:160
clang::driver::MultilibSet::dump
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:285
clang::driver::Multilib::flags_list
std::vector< std::string > flags_list
Definition: Multilib.h:30
clang::driver::MultilibSet::push_back
void push_back(const Multilib &M)
Add a completed Multilib to the set.
Definition: Multilib.cpp:232
clang::driver::Multilib::flags
const flags_list & flags() const
Get the flags that indicate or contraindicate this multilib&#39;s use All elements begin with either &#39;+&#39; ...
Definition: Multilib.h:78
clang::driver::Multilib
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
clang::driver::Multilib::priority
int priority() const
Returns the multilib priority.
Definition: Multilib.h:83
clang::driver::Multilib::isValid
bool isValid() const
Check whether any of the &#39;against&#39; flags contradict the &#39;for&#39; flags.
Definition: Multilib.cpp:98
Multilib.h
clang::driver::Multilib::print
void print(raw_ostream &OS) const
print summary of the Multilib
Definition: Multilib.cpp:84
clang::driver::MultilibSet::FilterCallback
llvm::function_ref< bool(const Multilib &)> FilterCallback
Definition: Multilib.h:121
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::driver::MultilibSet::FilterOut
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:212
Priority
int Priority
Definition: Format.cpp:1829
isFlagEnabled
static bool isFlagEnabled(StringRef Flag)
Definition: Multilib.cpp:238
clang::driver::Multilib::dump
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:80