blob: 8acda6794d7296de201cabd72467e9ff65d820bf [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) {
Stephen Hines176edba2014-12-01 14:53:08 -080040 StringRef last = path::filename(seg);
Stephen Hines651f13c2014-04-23 16:59:28 -070041 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 << ";";
Stephen Hines6bcf27b2014-05-29 04:14:42 -070093 for (StringRef Flag : Flags) {
94 if (Flag.front() == '+')
95 OS << "@" << Flag.substr(1);
Stephen Hines651f13c2014-04-23 16:59:28 -070096 }
97}
98
99bool Multilib::isValid() const {
100 llvm::StringMap<int> FlagSet;
101 for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
102 StringRef Flag(Flags[I]);
103 llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
104
105 assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
106
107 if (SI == FlagSet.end())
108 FlagSet[Flag.substr(1)] = I;
109 else if (Flags[I] != Flags[SI->getValue()])
110 return false;
111 }
112 return true;
113}
114
115bool Multilib::operator==(const Multilib &Other) const {
116 // Check whether the flags sets match
117 // allowing for the match to be order invariant
118 llvm::StringSet<> MyFlags;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700119 for (const auto &Flag : Flags)
120 MyFlags.insert(Flag);
121
122 for (const auto &Flag : Other.Flags)
123 if (MyFlags.find(Flag) == MyFlags.end())
Stephen Hines651f13c2014-04-23 16:59:28 -0700124 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700125
126 if (osSuffix() != Other.osSuffix())
127 return false;
128
129 if (gccSuffix() != Other.gccSuffix())
130 return false;
131
132 if (includeSuffix() != Other.includeSuffix())
133 return false;
134
135 return true;
136}
137
138raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
139 M.print(OS);
140 return OS;
141}
142
143MultilibSet &MultilibSet::Maybe(const Multilib &M) {
144 Multilib Opposite;
145 // Negate any '+' flags
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700146 for (StringRef Flag : M.flags()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700147 if (Flag.front() == '+')
148 Opposite.flags().push_back(("-" + Flag.substr(1)).str());
149 }
150 return Either(M, Opposite);
151}
152
153MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700154 return Either({M1, M2});
Stephen Hines651f13c2014-04-23 16:59:28 -0700155}
156
157MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
158 const Multilib &M3) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700159 return Either({M1, M2, M3});
Stephen Hines651f13c2014-04-23 16:59:28 -0700160}
161
162MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
163 const Multilib &M3, const Multilib &M4) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700164 return Either({M1, M2, M3, M4});
Stephen Hines651f13c2014-04-23 16:59:28 -0700165}
166
167MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
168 const Multilib &M3, const Multilib &M4,
169 const Multilib &M5) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700170 return Either({M1, M2, M3, M4, M5});
Stephen Hines651f13c2014-04-23 16:59:28 -0700171}
172
173static Multilib compose(const Multilib &Base, const Multilib &New) {
174 SmallString<128> GCCSuffix;
175 llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
176 SmallString<128> OSSuffix;
177 llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
178 SmallString<128> IncludeSuffix;
179 llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
180 New.includeSuffix());
181
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700182 Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
Stephen Hines651f13c2014-04-23 16:59:28 -0700183
184 Multilib::flags_list &Flags = Composed.flags();
185
186 Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
187 Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
188
189 return Composed;
190}
191
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700192MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700193 multilib_list Composed;
194
195 if (Multilibs.empty())
196 Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
197 MultilibSegments.end());
198 else {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700199 for (const Multilib &New : MultilibSegments) {
200 for (const Multilib &Base : *this) {
201 Multilib MO = compose(Base, New);
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 if (MO.isValid())
203 Composed.push_back(MO);
204 }
205 }
206
207 Multilibs = Composed;
208 }
209
210 return *this;
211}
212
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700213MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 filterInPlace(F, Multilibs);
215 return *this;
216}
217
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700218MultilibSet &MultilibSet::FilterOut(const char *Regex) {
219 llvm::Regex R(Regex);
220#ifndef NDEBUG
221 std::string Error;
222 if (!R.isValid(Error)) {
223 llvm::errs() << Error;
224 llvm_unreachable("Invalid regex!");
225 }
226#endif
Stephen Hines651f13c2014-04-23 16:59:28 -0700227
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700228 filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
229 Multilibs);
Stephen Hines651f13c2014-04-23 16:59:28 -0700230 return *this;
231}
232
233void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
234
235void MultilibSet::combineWith(const MultilibSet &Other) {
236 Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
237}
238
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700239static bool isFlagEnabled(StringRef Flag) {
240 char Indicator = Flag.front();
241 assert(Indicator == '+' || Indicator == '-');
242 return Indicator == '+';
243}
244
Stephen Hines651f13c2014-04-23 16:59:28 -0700245bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700246 llvm::StringMap<bool> FlagSet;
Stephen Hines651f13c2014-04-23 16:59:28 -0700247
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700248 // Stuff all of the flags into the FlagSet such that a true mappend indicates
249 // the flag was enabled, and a false mappend indicates the flag was disabled.
250 for (StringRef Flag : Flags)
251 FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
Stephen Hines651f13c2014-04-23 16:59:28 -0700252
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700253 multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
254 for (StringRef Flag : M.flags()) {
255 llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
256 if (SI != FlagSet.end())
257 if (SI->getValue() != isFlagEnabled(Flag))
258 return true;
259 }
260 return false;
261 }, Multilibs);
Stephen Hines651f13c2014-04-23 16:59:28 -0700262
263 if (Filtered.size() == 0) {
264 return false;
265 } else if (Filtered.size() == 1) {
266 M = Filtered[0];
267 return true;
268 }
269
270 // TODO: pick the "best" multlib when more than one is suitable
271 assert(false);
272
273 return false;
274}
275
276void MultilibSet::print(raw_ostream &OS) const {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700277 for (const Multilib &M : *this)
278 OS << M << "\n";
Stephen Hines651f13c2014-04-23 16:59:28 -0700279}
280
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700281MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
282 const multilib_list &Ms) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700283 multilib_list Copy(Ms);
284 filterInPlace(F, Copy);
285 return Copy;
286}
287
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700288void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
289 Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
Stephen Hines651f13c2014-04-23 16:59:28 -0700290}
291
292raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
293 MS.print(OS);
294 return OS;
295}