blob: 178a60db60e5d9716c41a5df46572e19ec2f6169 [file] [log] [blame]
Eugene Zelenko5e4511c2018-03-20 21:08:59 +00001//===- Multilib.cpp - Multilib Implementation -----------------------------===//
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +00002//
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"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000011#include "clang/Basic/LLVM.h"
12#include "llvm/ADT/SmallString.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000013#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/StringSet.h"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000016#include "llvm/Support/Compiler.h"
17#include "llvm/Support/ErrorHandling.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000018#include "llvm/Support/Path.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000019#include "llvm/Support/Regex.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000020#include "llvm/Support/raw_ostream.h"
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000021#include <algorithm>
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000022#include <cassert>
23#include <string>
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000024
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000025using namespace clang;
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000026using namespace driver;
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000027using namespace llvm::sys;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000028
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000029/// normalize Segment to "/foo/bar" or "".
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000030static void normalizePathSegment(std::string &Segment) {
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000031 StringRef seg = Segment;
32
33 // Prune trailing "/" or "./"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000034 while (true) {
Justin Bogner6bcf7242014-08-03 21:46:33 +000035 StringRef last = path::filename(seg);
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000036 if (last != ".")
37 break;
38 seg = path::parent_path(seg);
39 }
40
41 if (seg.empty() || seg == "/") {
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000042 Segment.clear();
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000043 return;
44 }
45
46 // Add leading '/'
47 if (seg.front() != '/') {
48 Segment = "/" + seg.str();
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000049 } else {
NAKAMURA Takumi691e6ef2014-02-12 11:42:02 +000050 Segment = seg;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000051 }
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000052}
53
54Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
55 StringRef IncludeSuffix)
56 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix) {
57 normalizePathSegment(this->GCCSuffix);
58 normalizePathSegment(this->OSSuffix);
59 normalizePathSegment(this->IncludeSuffix);
60}
61
62Multilib &Multilib::gccSuffix(StringRef S) {
63 GCCSuffix = S;
64 normalizePathSegment(GCCSuffix);
65 return *this;
66}
67
68Multilib &Multilib::osSuffix(StringRef S) {
69 OSSuffix = S;
70 normalizePathSegment(OSSuffix);
71 return *this;
72}
73
74Multilib &Multilib::includeSuffix(StringRef S) {
75 IncludeSuffix = S;
76 normalizePathSegment(IncludeSuffix);
77 return *this;
78}
79
Jonathan Roelofsc8e377c2017-05-05 21:30:13 +000080LLVM_DUMP_METHOD void Multilib::dump() const {
81 print(llvm::errs());
82}
83
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000084void 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 << ";";
Simon Atanasyan6f657c42014-05-08 19:32:46 +000092 for (StringRef Flag : Flags) {
93 if (Flag.front() == '+')
94 OS << "@" << Flag.substr(1);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +000095 }
96}
97
98bool 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
114bool 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;
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000118 for (const auto &Flag : Flags)
119 MyFlags.insert(Flag);
120
121 for (const auto &Flag : Other.Flags)
122 if (MyFlags.find(Flag) == MyFlags.end())
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000123 return false;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000124
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
137raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
138 M.print(OS);
139 return OS;
140}
141
142MultilibSet &MultilibSet::Maybe(const Multilib &M) {
143 Multilib Opposite;
144 // Negate any '+' flags
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000145 for (StringRef Flag : M.flags()) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000146 if (Flag.front() == '+')
147 Opposite.flags().push_back(("-" + Flag.substr(1)).str());
148 }
149 return Either(M, Opposite);
150}
151
152MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000153 return Either({M1, M2});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000154}
155
156MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
157 const Multilib &M3) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000158 return Either({M1, M2, M3});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000159}
160
161MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
162 const Multilib &M3, const Multilib &M4) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000163 return Either({M1, M2, M3, M4});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000164}
165
166MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
167 const Multilib &M3, const Multilib &M4,
168 const Multilib &M5) {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000169 return Either({M1, M2, M3, M4, M5});
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000170}
171
172static 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
Yaron Keren92e1b622015-03-18 10:17:07 +0000181 Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000182
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
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000191MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000192 multilib_list Composed;
193
194 if (Multilibs.empty())
195 Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
196 MultilibSegments.end());
197 else {
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000198 for (const auto &New : MultilibSegments) {
199 for (const auto &Base : *this) {
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000200 Multilib MO = compose(Base, New);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000201 if (MO.isValid())
202 Composed.push_back(MO);
203 }
204 }
205
206 Multilibs = Composed;
207 }
208
209 return *this;
210}
211
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000212MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000213 filterInPlace(F, Multilibs);
214 return *this;
215}
216
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000217MultilibSet &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
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000226
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000227 filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
228 Multilibs);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000229 return *this;
230}
231
232void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
233
234void MultilibSet::combineWith(const MultilibSet &Other) {
235 Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
236}
237
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000238static bool isFlagEnabled(StringRef Flag) {
239 char Indicator = Flag.front();
240 assert(Indicator == '+' || Indicator == '-');
241 return Indicator == '+';
242}
243
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000244bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000245 llvm::StringMap<bool> FlagSet;
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000246
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000247 // 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);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000251
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000252 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);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000261
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000262 if (Filtered.empty())
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000263 return false;
Simon Atanasyanecce7e12015-10-12 14:32:57 +0000264 if (Filtered.size() == 1) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000265 M = Filtered[0];
266 return true;
267 }
268
269 // TODO: pick the "best" multlib when more than one is suitable
270 assert(false);
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000271 return false;
272}
273
Jonathan Roelofsc8e377c2017-05-05 21:30:13 +0000274LLVM_DUMP_METHOD void MultilibSet::dump() const {
275 print(llvm::errs());
276}
277
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000278void MultilibSet::print(raw_ostream &OS) const {
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000279 for (const auto &M : *this)
Simon Atanasyan6f657c42014-05-08 19:32:46 +0000280 OS << M << "\n";
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000281}
282
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000283MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
284 const multilib_list &Ms) {
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000285 multilib_list Copy(Ms);
286 filterInPlace(F, Copy);
287 return Copy;
288}
289
Benjamin Kramerac75baa2015-03-22 15:56:12 +0000290void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
291 Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
Jonathan Roelofs2cea1be2014-02-12 03:21:20 +0000292}
293
294raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
295 MS.print(OS);
296 return OS;
297}