2018-03-21 05:08:59 +08:00
|
|
|
//===- Multilib.cpp - Multilib Implementation -----------------------------===//
|
2014-02-12 11:21:20 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-02-12 11:21:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Multilib.h"
|
2018-03-21 05:08:59 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-02-12 11:21:20 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
2018-03-21 05:08:59 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-02-12 11:21:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
2014-03-04 18:05:20 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-02-12 11:21:20 +08:00
|
|
|
#include <algorithm>
|
2018-03-21 05:08:59 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
2014-02-12 11:21:20 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2018-03-21 05:08:59 +08:00
|
|
|
using namespace driver;
|
2014-02-12 19:42:02 +08:00
|
|
|
using namespace llvm::sys;
|
2014-02-12 11:21:20 +08:00
|
|
|
|
2014-02-12 19:42:02 +08:00
|
|
|
/// normalize Segment to "/foo/bar" or "".
|
2014-02-12 11:21:20 +08:00
|
|
|
static void normalizePathSegment(std::string &Segment) {
|
2014-02-12 19:42:02 +08:00
|
|
|
StringRef seg = Segment;
|
|
|
|
|
|
|
|
// Prune trailing "/" or "./"
|
2018-03-21 05:08:59 +08:00
|
|
|
while (true) {
|
2014-08-04 05:46:33 +08:00
|
|
|
StringRef last = path::filename(seg);
|
2014-02-12 19:42:02 +08:00
|
|
|
if (last != ".")
|
|
|
|
break;
|
|
|
|
seg = path::parent_path(seg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seg.empty() || seg == "/") {
|
2018-03-21 05:08:59 +08:00
|
|
|
Segment.clear();
|
2014-02-12 19:42:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add leading '/'
|
|
|
|
if (seg.front() != '/') {
|
|
|
|
Segment = "/" + seg.str();
|
2014-02-12 11:21:20 +08:00
|
|
|
} else {
|
2014-02-12 19:42:02 +08:00
|
|
|
Segment = seg;
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
|
2019-04-27 08:25:11 +08:00
|
|
|
StringRef IncludeSuffix, int Priority)
|
|
|
|
: GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
|
|
|
|
Priority(Priority) {
|
2014-02-12 11:21:20 +08:00
|
|
|
normalizePathSegment(this->GCCSuffix);
|
|
|
|
normalizePathSegment(this->OSSuffix);
|
|
|
|
normalizePathSegment(this->IncludeSuffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
Multilib &Multilib::gccSuffix(StringRef S) {
|
|
|
|
GCCSuffix = S;
|
|
|
|
normalizePathSegment(GCCSuffix);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Multilib &Multilib::osSuffix(StringRef S) {
|
|
|
|
OSSuffix = S;
|
|
|
|
normalizePathSegment(OSSuffix);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Multilib &Multilib::includeSuffix(StringRef S) {
|
|
|
|
IncludeSuffix = S;
|
|
|
|
normalizePathSegment(IncludeSuffix);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-05-06 05:30:13 +08:00
|
|
|
LLVM_DUMP_METHOD void Multilib::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:21:20 +08:00
|
|
|
void Multilib::print(raw_ostream &OS) const {
|
|
|
|
assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
|
|
|
|
if (GCCSuffix.empty())
|
|
|
|
OS << ".";
|
|
|
|
else {
|
|
|
|
OS << StringRef(GCCSuffix).drop_front();
|
|
|
|
}
|
|
|
|
OS << ";";
|
2014-05-09 03:32:46 +08:00
|
|
|
for (StringRef Flag : Flags) {
|
|
|
|
if (Flag.front() == '+')
|
|
|
|
OS << "@" << Flag.substr(1);
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Multilib::isValid() const {
|
|
|
|
llvm::StringMap<int> FlagSet;
|
|
|
|
for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
|
|
|
|
StringRef Flag(Flags[I]);
|
|
|
|
llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
|
|
|
|
|
|
|
|
assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
|
|
|
|
|
|
|
|
if (SI == FlagSet.end())
|
|
|
|
FlagSet[Flag.substr(1)] = I;
|
|
|
|
else if (Flags[I] != Flags[SI->getValue()])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Multilib::operator==(const Multilib &Other) const {
|
|
|
|
// Check whether the flags sets match
|
|
|
|
// allowing for the match to be order invariant
|
|
|
|
llvm::StringSet<> MyFlags;
|
2014-05-09 03:32:46 +08:00
|
|
|
for (const auto &Flag : Flags)
|
|
|
|
MyFlags.insert(Flag);
|
|
|
|
|
|
|
|
for (const auto &Flag : Other.Flags)
|
|
|
|
if (MyFlags.find(Flag) == MyFlags.end())
|
2014-02-12 11:21:20 +08:00
|
|
|
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::Maybe(const Multilib &M) {
|
|
|
|
Multilib Opposite;
|
|
|
|
// Negate any '+' flags
|
2014-05-09 03:32:46 +08:00
|
|
|
for (StringRef Flag : M.flags()) {
|
2014-02-12 11:21:20 +08:00
|
|
|
if (Flag.front() == '+')
|
|
|
|
Opposite.flags().push_back(("-" + Flag.substr(1)).str());
|
|
|
|
}
|
|
|
|
return Either(M, Opposite);
|
|
|
|
}
|
|
|
|
|
|
|
|
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
|
2015-03-22 23:56:12 +08:00
|
|
|
return Either({M1, M2});
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
|
|
|
|
const Multilib &M3) {
|
2015-03-22 23:56:12 +08:00
|
|
|
return Either({M1, M2, M3});
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
|
|
|
|
const Multilib &M3, const Multilib &M4) {
|
2015-03-22 23:56:12 +08:00
|
|
|
return Either({M1, M2, M3, M4});
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
|
|
|
|
const Multilib &M3, const Multilib &M4,
|
|
|
|
const Multilib &M5) {
|
2015-03-22 23:56:12 +08:00
|
|
|
return Either({M1, M2, M3, M4, M5});
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Multilib compose(const Multilib &Base, const Multilib &New) {
|
|
|
|
SmallString<128> GCCSuffix;
|
|
|
|
llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
|
|
|
|
SmallString<128> OSSuffix;
|
|
|
|
llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
|
|
|
|
SmallString<128> IncludeSuffix;
|
|
|
|
llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
|
|
|
|
New.includeSuffix());
|
|
|
|
|
2015-03-18 18:17:07 +08:00
|
|
|
Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
|
2014-02-12 11:21:20 +08:00
|
|
|
|
|
|
|
Multilib::flags_list &Flags = Composed.flags();
|
|
|
|
|
|
|
|
Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
|
|
|
|
Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
|
|
|
|
|
|
|
|
return Composed;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
|
2014-02-12 11:21:20 +08:00
|
|
|
multilib_list Composed;
|
|
|
|
|
|
|
|
if (Multilibs.empty())
|
|
|
|
Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
|
|
|
|
MultilibSegments.end());
|
|
|
|
else {
|
2018-03-21 05:08:59 +08:00
|
|
|
for (const auto &New : MultilibSegments) {
|
|
|
|
for (const auto &Base : *this) {
|
2014-05-09 03:32:46 +08:00
|
|
|
Multilib MO = compose(Base, New);
|
2014-02-12 11:21:20 +08:00
|
|
|
if (MO.isValid())
|
|
|
|
Composed.push_back(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Multilibs = Composed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
|
2014-02-12 11:21:20 +08:00
|
|
|
filterInPlace(F, Multilibs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
MultilibSet &MultilibSet::FilterOut(const char *Regex) {
|
|
|
|
llvm::Regex R(Regex);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::string Error;
|
|
|
|
if (!R.isValid(Error)) {
|
|
|
|
llvm::errs() << Error;
|
|
|
|
llvm_unreachable("Invalid regex!");
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-12 11:21:20 +08:00
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
|
|
|
|
Multilibs);
|
2014-02-12 11:21:20 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
|
|
|
|
|
|
|
|
void MultilibSet::combineWith(const MultilibSet &Other) {
|
|
|
|
Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
static bool isFlagEnabled(StringRef Flag) {
|
|
|
|
char Indicator = Flag.front();
|
|
|
|
assert(Indicator == '+' || Indicator == '-');
|
|
|
|
return Indicator == '+';
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:21:20 +08:00
|
|
|
bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
|
2015-03-22 23:56:12 +08:00
|
|
|
llvm::StringMap<bool> FlagSet;
|
|
|
|
|
|
|
|
// Stuff all of the flags into the FlagSet such that a true mappend indicates
|
|
|
|
// the flag was enabled, and a false mappend indicates the flag was disabled.
|
|
|
|
for (StringRef Flag : Flags)
|
|
|
|
FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
|
|
|
|
|
|
|
|
multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
|
|
|
|
for (StringRef Flag : M.flags()) {
|
|
|
|
llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
|
|
|
|
if (SI != FlagSet.end())
|
|
|
|
if (SI->getValue() != isFlagEnabled(Flag))
|
|
|
|
return true;
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
2015-03-22 23:56:12 +08:00
|
|
|
return false;
|
|
|
|
}, Multilibs);
|
2014-02-12 11:21:20 +08:00
|
|
|
|
2018-03-21 05:08:59 +08:00
|
|
|
if (Filtered.empty())
|
2014-02-12 11:21:20 +08:00
|
|
|
return false;
|
2015-10-12 22:32:57 +08:00
|
|
|
if (Filtered.size() == 1) {
|
2014-02-12 11:21:20 +08:00
|
|
|
M = Filtered[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-27 08:25:11 +08:00
|
|
|
// Sort multilibs by priority and select the one with the highest priority.
|
|
|
|
llvm::sort(Filtered.begin(), Filtered.end(),
|
|
|
|
[](const Multilib &a, const Multilib &b) -> bool {
|
|
|
|
return a.priority() > b.priority();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Filtered[0].priority() > Filtered[1].priority()) {
|
|
|
|
M = Filtered[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should consider returning llvm::Error rather than aborting.
|
|
|
|
assert(false && "More than one multilib with the same priority");
|
2014-02-12 11:21:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-06 05:30:13 +08:00
|
|
|
LLVM_DUMP_METHOD void MultilibSet::dump() const {
|
|
|
|
print(llvm::errs());
|
|
|
|
}
|
|
|
|
|
2014-02-12 11:21:20 +08:00
|
|
|
void MultilibSet::print(raw_ostream &OS) const {
|
2018-03-21 05:08:59 +08:00
|
|
|
for (const auto &M : *this)
|
2014-05-09 03:32:46 +08:00
|
|
|
OS << M << "\n";
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
|
|
|
|
const multilib_list &Ms) {
|
2014-02-12 11:21:20 +08:00
|
|
|
multilib_list Copy(Ms);
|
|
|
|
filterInPlace(F, Copy);
|
|
|
|
return Copy;
|
|
|
|
}
|
|
|
|
|
2015-03-22 23:56:12 +08:00
|
|
|
void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
|
|
|
|
Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
|
2014-02-12 11:21:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
|
|
|
|
MS.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|