The new algorithm is: 1. Find all multilibs with flags that are a subset of the requested flags. 2. If more than one multilib matches, choose the last. In addition a new selection mechanism is permitted via an overload of MultilibSet::select() for which multiple multilibs are returned. This allows layering multilibs on top of each other. Since multilibs are now ordered within a list, they no longer need a Priority field. The new algorithm is different to the old algorithm, but in practise the old algorithm was always used in such a way that the effect is the same. The old algorithm was to find the set intersection of the requested flags (with the first character of each removed) with each multilib's flags (ditto), and for that intersection check whether the first character matched. However, ignoring the first characters, the requested flags were always a superset of all the multilibs flags. Therefore the new algorithm can be used as a drop-in replacement. The exception is Fuchsia, which needs adjusting slightly to set both fexceptions and fno-exceptions flags. Differential Revision: https://reviews.llvm.org/D142905
131 lines
3.5 KiB
C++
131 lines
3.5 KiB
C++
//===- 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;
|
|
}
|