| //===- Multilib.cpp - Multilib Implementation -----------------------------===// |
| // |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| // See https://llvm.org/LICENSE.txt for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "clang/Driver/Multilib.h" |
| #include "clang/Basic/LLVM.h" |
| #include "llvm/ADT/SmallString.h" |
| #include "llvm/ADT/StringMap.h" |
| #include "llvm/ADT/StringRef.h" |
| #include "llvm/ADT/StringSet.h" |
| #include "llvm/Support/Compiler.h" |
| #include "llvm/Support/ErrorHandling.h" |
| #include "llvm/Support/Path.h" |
| #include "llvm/Support/Regex.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include <algorithm> |
| #include <cassert> |
| #include <string> |
| |
| using namespace clang; |
| using namespace driver; |
| using namespace llvm::sys; |
| |
| Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix, |
| StringRef IncludeSuffix, const flags_list &Flags) |
| : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix), |
| Flags(Flags) { |
| assert(GCCSuffix.empty() || |
| (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1)); |
| assert(OSSuffix.empty() || |
| (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1)); |
| assert(IncludeSuffix.empty() || |
| (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1)); |
| } |
| |
| LLVM_DUMP_METHOD void Multilib::dump() const { |
| print(llvm::errs()); |
| } |
| |
| void Multilib::print(raw_ostream &OS) const { |
| if (GCCSuffix.empty()) |
| OS << "."; |
| else { |
| OS << StringRef(GCCSuffix).drop_front(); |
| } |
| OS << ";"; |
| for (StringRef Flag : Flags) { |
| if (Flag.front() == '+') |
| OS << "@" << Flag.substr(1); |
| } |
| } |
| |
| bool Multilib::operator==(const Multilib &Other) const { |
| // Check whether the flags sets match |
| // allowing for the match to be order invariant |
| llvm::StringSet<> MyFlags; |
| for (const auto &Flag : Flags) |
| MyFlags.insert(Flag); |
| |
| for (const auto &Flag : Other.Flags) |
| if (!MyFlags.contains(Flag)) |
| return false; |
| |
| if (osSuffix() != Other.osSuffix()) |
| return false; |
| |
| if (gccSuffix() != Other.gccSuffix()) |
| return false; |
| |
| if (includeSuffix() != Other.includeSuffix()) |
| return false; |
| |
| return true; |
| } |
| |
| raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) { |
| M.print(OS); |
| return OS; |
| } |
| |
| MultilibSet &MultilibSet::FilterOut(FilterCallback F) { |
| llvm::erase_if(Multilibs, F); |
| return *this; |
| } |
| |
| void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); } |
| |
| MultilibSet::multilib_list |
| MultilibSet::select(const Multilib::flags_list &Flags) const { |
| llvm::StringSet<> FlagSet; |
| for (const auto &Flag : Flags) |
| FlagSet.insert(Flag); |
| |
| multilib_list Result; |
| llvm::copy_if(Multilibs, std::back_inserter(Result), |
| [&FlagSet](const Multilib &M) { |
| for (const std::string &F : M.flags()) |
| if (!FlagSet.contains(F)) |
| return false; |
| return true; |
| }); |
| return Result; |
| } |
| |
| bool MultilibSet::select(const Multilib::flags_list &Flags, |
| Multilib &Selected) const { |
| multilib_list Result = select(Flags); |
| if (Result.empty()) |
| return false; |
| Selected = Result.back(); |
| return true; |
| } |
| |
| LLVM_DUMP_METHOD void MultilibSet::dump() const { |
| print(llvm::errs()); |
| } |
| |
| void MultilibSet::print(raw_ostream &OS) const { |
| for (const auto &M : *this) |
| OS << M << "\n"; |
| } |
| |
| raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) { |
| MS.print(OS); |
| return OS; |
| } |