Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 1 | //===--- 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" |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Regex.h" |
| 24 | #include "llvm/Support/YAMLParser.h" |
| 25 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
| 29 | using namespace clang::driver; |
| 30 | using namespace clang; |
| 31 | using namespace llvm::opt; |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 32 | using namespace llvm::sys; |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 33 | |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 34 | /// normalize Segment to "/foo/bar" or "". |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 35 | static void normalizePathSegment(std::string &Segment) { |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 36 | StringRef seg = Segment; |
| 37 | |
| 38 | // Prune trailing "/" or "./" |
| 39 | while (1) { |
Justin Bogner | 6bcf724 | 2014-08-03 21:46:33 +0000 | [diff] [blame] | 40 | StringRef last = path::filename(seg); |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 41 | if (last != ".") |
| 42 | break; |
| 43 | seg = path::parent_path(seg); |
| 44 | } |
| 45 | |
| 46 | if (seg.empty() || seg == "/") { |
Jonathan Roelofs | 4c991ef | 2014-02-12 06:37:27 +0000 | [diff] [blame] | 47 | Segment = ""; |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Add leading '/' |
| 52 | if (seg.front() != '/') { |
| 53 | Segment = "/" + seg.str(); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 54 | } else { |
NAKAMURA Takumi | 691e6ef | 2014-02-12 11:42:02 +0000 | [diff] [blame] | 55 | Segment = seg; |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 56 | } |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | Multilib::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 | |
| 67 | Multilib &Multilib::gccSuffix(StringRef S) { |
| 68 | GCCSuffix = S; |
| 69 | normalizePathSegment(GCCSuffix); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | Multilib &Multilib::osSuffix(StringRef S) { |
| 74 | OSSuffix = S; |
| 75 | normalizePathSegment(OSSuffix); |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | Multilib &Multilib::includeSuffix(StringRef S) { |
| 80 | IncludeSuffix = S; |
| 81 | normalizePathSegment(IncludeSuffix); |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | void 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 << ";"; |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 93 | for (StringRef Flag : Flags) { |
| 94 | if (Flag.front() == '+') |
| 95 | OS << "@" << Flag.substr(1); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | bool 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 | |
| 115 | bool 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; |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 119 | for (const auto &Flag : Flags) |
| 120 | MyFlags.insert(Flag); |
| 121 | |
| 122 | for (const auto &Flag : Other.Flags) |
| 123 | if (MyFlags.find(Flag) == MyFlags.end()) |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 124 | return false; |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 125 | |
| 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 | |
| 138 | raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) { |
| 139 | M.print(OS); |
| 140 | return OS; |
| 141 | } |
| 142 | |
| 143 | MultilibSet &MultilibSet::Maybe(const Multilib &M) { |
| 144 | Multilib Opposite; |
| 145 | // Negate any '+' flags |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 146 | for (StringRef Flag : M.flags()) { |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 147 | if (Flag.front() == '+') |
| 148 | Opposite.flags().push_back(("-" + Flag.substr(1)).str()); |
| 149 | } |
| 150 | return Either(M, Opposite); |
| 151 | } |
| 152 | |
| 153 | MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) { |
| 154 | std::vector<Multilib> Ms; |
| 155 | Ms.push_back(M1); |
| 156 | Ms.push_back(M2); |
| 157 | return Either(Ms); |
| 158 | } |
| 159 | |
| 160 | MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2, |
| 161 | const Multilib &M3) { |
| 162 | std::vector<Multilib> Ms; |
| 163 | Ms.push_back(M1); |
| 164 | Ms.push_back(M2); |
| 165 | Ms.push_back(M3); |
| 166 | return Either(Ms); |
| 167 | } |
| 168 | |
| 169 | MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2, |
| 170 | const Multilib &M3, const Multilib &M4) { |
| 171 | std::vector<Multilib> Ms; |
| 172 | Ms.push_back(M1); |
| 173 | Ms.push_back(M2); |
| 174 | Ms.push_back(M3); |
| 175 | Ms.push_back(M4); |
| 176 | return Either(Ms); |
| 177 | } |
| 178 | |
| 179 | MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2, |
| 180 | const Multilib &M3, const Multilib &M4, |
| 181 | const Multilib &M5) { |
| 182 | std::vector<Multilib> Ms; |
| 183 | Ms.push_back(M1); |
| 184 | Ms.push_back(M2); |
| 185 | Ms.push_back(M3); |
| 186 | Ms.push_back(M4); |
| 187 | Ms.push_back(M5); |
| 188 | return Either(Ms); |
| 189 | } |
| 190 | |
| 191 | static Multilib compose(const Multilib &Base, const Multilib &New) { |
| 192 | SmallString<128> GCCSuffix; |
| 193 | llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix()); |
| 194 | SmallString<128> OSSuffix; |
| 195 | llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix()); |
| 196 | SmallString<128> IncludeSuffix; |
| 197 | llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(), |
| 198 | New.includeSuffix()); |
| 199 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame^] | 200 | Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 201 | |
| 202 | Multilib::flags_list &Flags = Composed.flags(); |
| 203 | |
| 204 | Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end()); |
| 205 | Flags.insert(Flags.end(), New.flags().begin(), New.flags().end()); |
| 206 | |
| 207 | return Composed; |
| 208 | } |
| 209 | |
| 210 | MultilibSet & |
| 211 | MultilibSet::Either(const std::vector<Multilib> &MultilibSegments) { |
| 212 | multilib_list Composed; |
| 213 | |
| 214 | if (Multilibs.empty()) |
| 215 | Multilibs.insert(Multilibs.end(), MultilibSegments.begin(), |
| 216 | MultilibSegments.end()); |
| 217 | else { |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 218 | for (const Multilib &New : MultilibSegments) { |
| 219 | for (const Multilib &Base : *this) { |
| 220 | Multilib MO = compose(Base, New); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 221 | if (MO.isValid()) |
| 222 | Composed.push_back(MO); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Multilibs = Composed; |
| 227 | } |
| 228 | |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | MultilibSet &MultilibSet::FilterOut(const MultilibSet::FilterCallback &F) { |
| 233 | filterInPlace(F, Multilibs); |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | MultilibSet &MultilibSet::FilterOut(std::string Regex) { |
| 238 | class REFilter : public MultilibSet::FilterCallback { |
| 239 | mutable llvm::Regex R; |
| 240 | |
| 241 | public: |
| 242 | REFilter(std::string Regex) : R(Regex) {} |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 243 | bool operator()(const Multilib &M) const override { |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 244 | std::string Error; |
| 245 | if (!R.isValid(Error)) { |
| 246 | llvm::errs() << Error; |
| 247 | assert(false); |
| 248 | return false; |
| 249 | } |
| 250 | return R.match(M.gccSuffix()); |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | REFilter REF(Regex); |
| 255 | filterInPlace(REF, Multilibs); |
| 256 | return *this; |
| 257 | } |
| 258 | |
| 259 | void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); } |
| 260 | |
| 261 | void MultilibSet::combineWith(const MultilibSet &Other) { |
| 262 | Multilibs.insert(Multilibs.end(), Other.begin(), Other.end()); |
| 263 | } |
| 264 | |
| 265 | bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const { |
| 266 | class FilterFlagsMismatch : public MultilibSet::FilterCallback { |
| 267 | llvm::StringMap<bool> FlagSet; |
| 268 | |
| 269 | public: |
| 270 | FilterFlagsMismatch(const std::vector<std::string> &Flags) { |
| 271 | // Stuff all of the flags into the FlagSet such that a true mappend |
| 272 | // indicates the flag was enabled, and a false mappend indicates the |
| 273 | // flag was disabled |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 274 | for (StringRef Flag : Flags) |
| 275 | FlagSet[Flag.substr(1)] = isFlagEnabled(Flag); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 276 | } |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 277 | bool operator()(const Multilib &M) const override { |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 278 | for (StringRef Flag : M.flags()) { |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 279 | llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1)); |
| 280 | if (SI != FlagSet.end()) |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 281 | if (SI->getValue() != isFlagEnabled(Flag)) |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | return false; |
| 285 | } |
| 286 | private: |
| 287 | bool isFlagEnabled(StringRef Flag) const { |
| 288 | char Indicator = Flag.front(); |
| 289 | assert(Indicator == '+' || Indicator == '-'); |
| 290 | return Indicator == '+'; |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | FilterFlagsMismatch FlagsMismatch(Flags); |
| 295 | |
| 296 | multilib_list Filtered = filterCopy(FlagsMismatch, Multilibs); |
| 297 | |
| 298 | if (Filtered.size() == 0) { |
| 299 | return false; |
| 300 | } else if (Filtered.size() == 1) { |
| 301 | M = Filtered[0]; |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | // TODO: pick the "best" multlib when more than one is suitable |
| 306 | assert(false); |
| 307 | |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | void MultilibSet::print(raw_ostream &OS) const { |
Simon Atanasyan | 6f657c4 | 2014-05-08 19:32:46 +0000 | [diff] [blame] | 312 | for (const Multilib &M : *this) |
| 313 | OS << M << "\n"; |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | MultilibSet::multilib_list |
| 317 | MultilibSet::filterCopy(const MultilibSet::FilterCallback &F, |
| 318 | const multilib_list &Ms) { |
| 319 | multilib_list Copy(Ms); |
| 320 | filterInPlace(F, Copy); |
| 321 | return Copy; |
| 322 | } |
| 323 | |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 324 | void MultilibSet::filterInPlace(const MultilibSet::FilterCallback &F, |
| 325 | multilib_list &Ms) { |
Benjamin Kramer | 65f0a4a | 2014-03-05 13:25:00 +0000 | [diff] [blame] | 326 | Ms.erase(std::remove_if(Ms.begin(), Ms.end(), |
| 327 | [&F](const Multilib &M) { return F(M); }), |
| 328 | Ms.end()); |
Jonathan Roelofs | 2cea1be | 2014-02-12 03:21:20 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) { |
| 332 | MS.print(OS); |
| 333 | return OS; |
| 334 | } |