blob: a88edf7f04e3175cefcccfae2c8221deff6e6ca5 [file] [log] [blame]
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +00001//===--- 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"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000016#include "llvm/Option/Arg.h"
17#include "llvm/Option/ArgList.h"
18#include "llvm/Option/OptTable.h"
19#include "llvm/Option/Option.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000020#include "llvm/Support/Path.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000021#include "llvm/Support/Regex.h"
22#include "llvm/Support/YAMLParser.h"
23#include "llvm/Support/YAMLTraits.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000024#include "llvm/Support/raw_ostream.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000025#include <algorithm>
26
27using namespace clang::driver;
28using namespace clang;
29using namespace llvm::opt;
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000030using namespace llvm::sys;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000031
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000032/// normalize Segment to "/foo/bar" or "".
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000033static void normalizePathSegment(std::string &Segment) {
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000034 StringRef seg = Segment;
35
36 // Prune trailing "/" or "./"
37 while (1) {
Justin Bogner6bcf7242014-08-03 21:46:33 +000038 StringRef last = path::filename(seg);
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000039 if (last != ".")
40 break;
41 seg = path::parent_path(seg);
42 }
43
44 if (seg.empty() || seg == "/") {
Jonathan Roelofs4c991ef2014-02-12 06:37:27 +000045 Segment = "";
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000046 return;
47 }
48
49 // Add leading '/'
50 if (seg.front() != '/') {
51 Segment = "/" + seg.str();
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000052 } else {
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000053 Segment = seg;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000054 }
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000055}
56
57Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
58 StringRef IncludeSuffix)
59 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix) {
60 normalizePathSegment(this->GCCSuffix);
61 normalizePathSegment(this->OSSuffix);
62 normalizePathSegment(this->IncludeSuffix);
63}
64
65Multilib &Multilib::gccSuffix(StringRef S) {
66 GCCSuffix = S;
67 normalizePathSegment(GCCSuffix);
68 return *this;
69}
70
71Multilib &Multilib::osSuffix(StringRef S) {
72 OSSuffix = S;
73 normalizePathSegment(OSSuffix);
74 return *this;
75}
76
77Multilib &Multilib::includeSuffix(StringRef S) {
78 IncludeSuffix = S;
79 normalizePathSegment(IncludeSuffix);
80 return *this;
81}
82
83void Multilib::print(raw_ostream &OS) const {
84 assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
85 if (GCCSuffix.empty())
86 OS << ".";
87 else {
88 OS << StringRef(GCCSuffix).drop_front();
89 }
90 OS << ";";
Simon Atanasyan6f657c42014-05-08 19:32:46 +000091 for (StringRef Flag : Flags) {
92 if (Flag.front() == '+')
93 OS << "@" << Flag.substr(1);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000094 }
95}
96
97bool Multilib::isValid() const {
98 llvm::StringMap<int> FlagSet;
99 for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
100 StringRef Flag(Flags[I]);
101 llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
102
103 assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
104
105 if (SI == FlagSet.end())
106 FlagSet[Flag.substr(1)] = I;
107 else if (Flags[I] != Flags[SI->getValue()])
108 return false;
109 }
110 return true;
111}
112
113bool Multilib::operator==(const Multilib &Other) const {
114 // Check whether the flags sets match
115 // allowing for the match to be order invariant
116 llvm::StringSet<> MyFlags;
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000117 for (const auto &Flag : Flags)
118 MyFlags.insert(Flag);
119
120 for (const auto &Flag : Other.Flags)
121 if (MyFlags.find(Flag) == MyFlags.end())
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000122 return false;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000123
124 if (osSuffix() != Other.osSuffix())
125 return false;
126
127 if (gccSuffix() != Other.gccSuffix())
128 return false;
129
130 if (includeSuffix() != Other.includeSuffix())
131 return false;
132
133 return true;
134}
135
136raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
137 M.print(OS);
138 return OS;
139}
140
141MultilibSet &MultilibSet::Maybe(const Multilib &M) {
142 Multilib Opposite;
143 // Negate any '+' flags
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000144 for (StringRef Flag : M.flags()) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000145 if (Flag.front() == '+')
146 Opposite.flags().push_back(("-" + Flag.substr(1)).str());
147 }
148 return Either(M, Opposite);
149}
150
151MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000152 return Either({M1, M2});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000153}
154
155MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
156 const Multilib &M3) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000157 return Either({M1, M2, M3});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000158}
159
160MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
161 const Multilib &M3, const Multilib &M4) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000162 return Either({M1, M2, M3, M4});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000163}
164
165MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
166 const Multilib &M3, const Multilib &M4,
167 const Multilib &M5) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000168 return Either({M1, M2, M3, M4, M5});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000169}
170
171static Multilib compose(const Multilib &Base, const Multilib &New) {
172 SmallString<128> GCCSuffix;
173 llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
174 SmallString<128> OSSuffix;
175 llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
176 SmallString<128> IncludeSuffix;
177 llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
178 New.includeSuffix());
179
Yaron Keren92e1b622015-03-18 10:17:07 +0000180 Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000181
182 Multilib::flags_list &Flags = Composed.flags();
183
184 Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
185 Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
186
187 return Composed;
188}
189
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000190MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000191 multilib_list Composed;
192
193 if (Multilibs.empty())
194 Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
195 MultilibSegments.end());
196 else {
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000197 for (const Multilib &New : MultilibSegments) {
198 for (const Multilib &Base : *this) {
199 Multilib MO = compose(Base, New);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000200 if (MO.isValid())
201 Composed.push_back(MO);
202 }
203 }
204
205 Multilibs = Composed;
206 }
207
208 return *this;
209}
210
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000211MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000212 filterInPlace(F, Multilibs);
213 return *this;
214}
215
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000216MultilibSet &MultilibSet::FilterOut(const char *Regex) {
217 llvm::Regex R(Regex);
218#ifndef NDEBUG
219 std::string Error;
220 if (!R.isValid(Error)) {
221 llvm::errs() << Error;
222 llvm_unreachable("Invalid regex!");
223 }
224#endif
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000225
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000226 filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
227 Multilibs);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000228 return *this;
229}
230
231void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
232
233void MultilibSet::combineWith(const MultilibSet &Other) {
234 Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
235}
236
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000237static bool isFlagEnabled(StringRef Flag) {
238 char Indicator = Flag.front();
239 assert(Indicator == '+' || Indicator == '-');
240 return Indicator == '+';
241}
242
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000243bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000244 llvm::StringMap<bool> FlagSet;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000245
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000246 // Stuff all of the flags into the FlagSet such that a true mappend indicates
247 // the flag was enabled, and a false mappend indicates the flag was disabled.
248 for (StringRef Flag : Flags)
249 FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000250
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000251 multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
252 for (StringRef Flag : M.flags()) {
253 llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
254 if (SI != FlagSet.end())
255 if (SI->getValue() != isFlagEnabled(Flag))
256 return true;
257 }
258 return false;
259 }, Multilibs);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000260
Simon Atanasyanecce7e12015-10-12 14:32:57 +0000261 if (Filtered.size() == 0)
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000262 return false;
Simon Atanasyanecce7e12015-10-12 14:32:57 +0000263 if (Filtered.size() == 1) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000264 M = Filtered[0];
265 return true;
266 }
267
268 // TODO: pick the "best" multlib when more than one is suitable
269 assert(false);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000270 return false;
271}
272
273void MultilibSet::print(raw_ostream &OS) const {
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000274 for (const Multilib &M : *this)
275 OS << M << "\n";
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000276}
277
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000278MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
279 const multilib_list &Ms) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000280 multilib_list Copy(Ms);
281 filterInPlace(F, Copy);
282 return Copy;
283}
284
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000285void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
286 Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000287}
288
289raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
290 MS.print(OS);
291 return OS;
292}