blob: 910e6383f28e33afe2c413ff7f5ffa5388fdfd36 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===--- Multilib.cpp - Multilib Implementation ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Driver/Multilib.h"
11#include "Tools.h"
12#include "clang/Driver/Options.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/ADT/Triple.h"
17#include "llvm/Option/Arg.h"
18#include "llvm/Option/ArgList.h"
19#include "llvm/Option/OptTable.h"
20#include "llvm/Option/Option.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/Regex.h"
24#include "llvm/Support/YAMLParser.h"
25#include "llvm/Support/YAMLTraits.h"
26#include "llvm/Support/raw_ostream.h"
27#include <algorithm>
28
29using namespace clang::driver;
30using namespace clang;
31using namespace llvm::opt;
32using namespace llvm::sys;
33
34/// normalize Segment to "/foo/bar" or "".
35static void normalizePathSegment(std::string &Segment) {
36 StringRef seg = Segment;
37
38 // Prune trailing "/" or "./"
39 while (1) {
40 StringRef last = *--path::end(seg);
41 if (last != ".")
42 break;
43 seg = path::parent_path(seg);
44 }
45
46 if (seg.empty() || seg == "/") {
47 Segment = "";
48 return;
49 }
50
51 // Add leading '/'
52 if (seg.front() != '/') {
53 Segment = "/" + seg.str();
54 } else {
55 Segment = seg;
56 }
57}
58
59Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
60 StringRef IncludeSuffix)
61 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix) {
62 normalizePathSegment(this->GCCSuffix);
63 normalizePathSegment(this->OSSuffix);
64 normalizePathSegment(this->IncludeSuffix);
65}
66
67Multilib &Multilib::gccSuffix(StringRef S) {
68 GCCSuffix = S;
69 normalizePathSegment(GCCSuffix);
70 return *this;
71}
72
73Multilib &Multilib::osSuffix(StringRef S) {
74 OSSuffix = S;
75 normalizePathSegment(OSSuffix);
76 return *this;
77}
78
79Multilib &Multilib::includeSuffix(StringRef S) {
80 IncludeSuffix = S;
81 normalizePathSegment(IncludeSuffix);
82 return *this;
83}
84
85void Multilib::print(raw_ostream &OS) const {
86 assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
87 if (GCCSuffix.empty())
88 OS << ".";
89 else {
90 OS << StringRef(GCCSuffix).drop_front();
91 }
92 OS << ";";
93 for (flags_list::const_iterator I = Flags.begin(), E = Flags.end(); I != E;
94 ++I) {
95 if (StringRef(*I).front() == '+')
96 OS << "@" << I->substr(1);
97 }
98}
99
100bool Multilib::isValid() const {
101 llvm::StringMap<int> FlagSet;
102 for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
103 StringRef Flag(Flags[I]);
104 llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
105
106 assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
107
108 if (SI == FlagSet.end())
109 FlagSet[Flag.substr(1)] = I;
110 else if (Flags[I] != Flags[SI->getValue()])
111 return false;
112 }
113 return true;
114}
115
116bool Multilib::operator==(const Multilib &Other) const {
117 // Check whether the flags sets match
118 // allowing for the match to be order invariant
119 llvm::StringSet<> MyFlags;
120 for (flags_list::const_iterator I = Flags.begin(), E = Flags.end(); I != E;
121 ++I) {
122 MyFlags.insert(*I);
123 }
124 for (flags_list::const_iterator I = Other.Flags.begin(),
125 E = Other.Flags.end();
126 I != E; ++I) {
127 if (MyFlags.find(*I) == MyFlags.end())
128 return false;
129 }
130
131 if (osSuffix() != Other.osSuffix())
132 return false;
133
134 if (gccSuffix() != Other.gccSuffix())
135 return false;
136
137 if (includeSuffix() != Other.includeSuffix())
138 return false;
139
140 return true;
141}
142
143raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
144 M.print(OS);
145 return OS;
146}
147
148MultilibSet &MultilibSet::Maybe(const Multilib &M) {
149 Multilib Opposite;
150 // Negate any '+' flags
151 for (Multilib::flags_list::const_iterator I = M.flags().begin(),
152 E = M.flags().end();
153 I != E; ++I) {
154 StringRef Flag(*I);
155 if (Flag.front() == '+')
156 Opposite.flags().push_back(("-" + Flag.substr(1)).str());
157 }
158 return Either(M, Opposite);
159}
160
161MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
162 std::vector<Multilib> Ms;
163 Ms.push_back(M1);
164 Ms.push_back(M2);
165 return Either(Ms);
166}
167
168MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
169 const Multilib &M3) {
170 std::vector<Multilib> Ms;
171 Ms.push_back(M1);
172 Ms.push_back(M2);
173 Ms.push_back(M3);
174 return Either(Ms);
175}
176
177MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
178 const Multilib &M3, const Multilib &M4) {
179 std::vector<Multilib> Ms;
180 Ms.push_back(M1);
181 Ms.push_back(M2);
182 Ms.push_back(M3);
183 Ms.push_back(M4);
184 return Either(Ms);
185}
186
187MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
188 const Multilib &M3, const Multilib &M4,
189 const Multilib &M5) {
190 std::vector<Multilib> Ms;
191 Ms.push_back(M1);
192 Ms.push_back(M2);
193 Ms.push_back(M3);
194 Ms.push_back(M4);
195 Ms.push_back(M5);
196 return Either(Ms);
197}
198
199static Multilib compose(const Multilib &Base, const Multilib &New) {
200 SmallString<128> GCCSuffix;
201 llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
202 SmallString<128> OSSuffix;
203 llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
204 SmallString<128> IncludeSuffix;
205 llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
206 New.includeSuffix());
207
208 Multilib Composed(GCCSuffix.str(), OSSuffix.str(), IncludeSuffix.str());
209
210 Multilib::flags_list &Flags = Composed.flags();
211
212 Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
213 Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
214
215 return Composed;
216}
217
218MultilibSet &
219MultilibSet::Either(const std::vector<Multilib> &MultilibSegments) {
220 multilib_list Composed;
221
222 if (Multilibs.empty())
223 Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
224 MultilibSegments.end());
225 else {
226 for (std::vector<Multilib>::const_iterator NewI = MultilibSegments.begin(),
227 NewE = MultilibSegments.end();
228 NewI != NewE; ++NewI) {
229 for (const_iterator BaseI = begin(), BaseE = end(); BaseI != BaseE;
230 ++BaseI) {
231 Multilib MO = compose(*BaseI, *NewI);
232 if (MO.isValid())
233 Composed.push_back(MO);
234 }
235 }
236
237 Multilibs = Composed;
238 }
239
240 return *this;
241}
242
243MultilibSet &MultilibSet::FilterOut(const MultilibSet::FilterCallback &F) {
244 filterInPlace(F, Multilibs);
245 return *this;
246}
247
248MultilibSet &MultilibSet::FilterOut(std::string Regex) {
249 class REFilter : public MultilibSet::FilterCallback {
250 mutable llvm::Regex R;
251
252 public:
253 REFilter(std::string Regex) : R(Regex) {}
254 bool operator()(const Multilib &M) const override {
255 std::string Error;
256 if (!R.isValid(Error)) {
257 llvm::errs() << Error;
258 assert(false);
259 return false;
260 }
261 return R.match(M.gccSuffix());
262 }
263 };
264
265 REFilter REF(Regex);
266 filterInPlace(REF, Multilibs);
267 return *this;
268}
269
270void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
271
272void MultilibSet::combineWith(const MultilibSet &Other) {
273 Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
274}
275
276bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
277 class FilterFlagsMismatch : public MultilibSet::FilterCallback {
278 llvm::StringMap<bool> FlagSet;
279
280 public:
281 FilterFlagsMismatch(const std::vector<std::string> &Flags) {
282 // Stuff all of the flags into the FlagSet such that a true mappend
283 // indicates the flag was enabled, and a false mappend indicates the
284 // flag was disabled
285 for (Multilib::flags_list::const_iterator I = Flags.begin(),
286 E = Flags.end();
287 I != E; ++I) {
288 FlagSet[StringRef(*I).substr(1)] = isFlagEnabled(*I);
289 }
290 }
291 bool operator()(const Multilib &M) const override {
292 for (Multilib::flags_list::const_iterator I = M.flags().begin(),
293 E = M.flags().end();
294 I != E; ++I) {
295 StringRef Flag(*I);
296 llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
297 if (SI != FlagSet.end())
298 if ((*SI).getValue() != isFlagEnabled(Flag))
299 return true;
300 }
301 return false;
302 }
303 private:
304 bool isFlagEnabled(StringRef Flag) const {
305 char Indicator = Flag.front();
306 assert(Indicator == '+' || Indicator == '-');
307 return Indicator == '+';
308 }
309 };
310
311 FilterFlagsMismatch FlagsMismatch(Flags);
312
313 multilib_list Filtered = filterCopy(FlagsMismatch, Multilibs);
314
315 if (Filtered.size() == 0) {
316 return false;
317 } else if (Filtered.size() == 1) {
318 M = Filtered[0];
319 return true;
320 }
321
322 // TODO: pick the "best" multlib when more than one is suitable
323 assert(false);
324
325 return false;
326}
327
328void MultilibSet::print(raw_ostream &OS) const {
329 for (const_iterator I = begin(), E = end(); I != E; ++I)
330 OS << *I << "\n";
331}
332
333MultilibSet::multilib_list
334MultilibSet::filterCopy(const MultilibSet::FilterCallback &F,
335 const multilib_list &Ms) {
336 multilib_list Copy(Ms);
337 filterInPlace(F, Copy);
338 return Copy;
339}
340
341void MultilibSet::filterInPlace(const MultilibSet::FilterCallback &F,
342 multilib_list &Ms) {
343 Ms.erase(std::remove_if(Ms.begin(), Ms.end(),
344 [&F](const Multilib &M) { return F(M); }),
345 Ms.end());
346}
347
348raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
349 MS.print(OS);
350 return OS;
351}