12#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Compiler.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/Path.h"
19#include "llvm/Support/Regex.h"
20#include "llvm/Support/VersionTuple.h"
21#include "llvm/Support/YAMLParser.h"
22#include "llvm/Support/YAMLTraits.h"
23#include "llvm/Support/raw_ostream.h"
29using namespace driver;
30using namespace llvm::sys;
33 StringRef IncludeSuffix,
const flags_list &Flags,
34 StringRef ExclusiveGroup)
35 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
36 Flags(Flags), ExclusiveGroup(ExclusiveGroup) {
37 assert(GCCSuffix.empty() ||
38 (StringRef(GCCSuffix).front() ==
'/' && GCCSuffix.size() > 1));
39 assert(OSSuffix.empty() ||
40 (StringRef(OSSuffix).front() ==
'/' && OSSuffix.size() > 1));
41 assert(IncludeSuffix.empty() ||
42 (StringRef(IncludeSuffix).front() ==
'/' && IncludeSuffix.size() > 1));
50 if (GCCSuffix.empty())
53 OS << StringRef(GCCSuffix).drop_front();
56 for (StringRef Flag : Flags) {
57 if (Flag.front() ==
'-')
58 OS <<
"@" << Flag.substr(1);
65 llvm::StringSet<> MyFlags;
66 for (
const auto &Flag : Flags)
69 for (
const auto &Flag :
Other.Flags)
70 if (!MyFlags.contains(Flag))
91 llvm::erase_if(Multilibs, F);
103 llvm::DenseSet<StringRef> ExclusiveGroupsSelected;
104 for (
const Multilib &M : llvm::reverse(Multilibs)) {
106 if (!llvm::all_of(M.flags(), [&FlagSet](
const std::string &F) {
107 return FlagSet.contains(F);
111 const std::string &group = M.exclusiveGroup();
112 if (!group.empty()) {
119 auto [It, Inserted] = ExclusiveGroupsSelected.insert(group);
125 Selected.push_back(M);
130 std::reverse(Selected.begin(), Selected.end());
132 return !Selected.empty();
138 for (
const auto &F : InFlags)
141 std::string RegexString(M.Match);
144 if (!StringRef(M.Match).starts_with(
"^"))
145 RegexString.insert(RegexString.begin(),
'^');
146 if (!StringRef(M.Match).ends_with(
"$"))
147 RegexString.push_back(
'$');
149 const llvm::Regex Regex(RegexString);
150 assert(Regex.isValid());
151 if (llvm::any_of(InFlags,
152 [&Regex](StringRef F) {
return Regex.match(F); })) {
153 Result.insert(M.Flags.begin(), M.Flags.end());
162static const VersionTuple MultilibVersionCurrent(1, 0);
164struct MultilibSerialization {
166 std::vector<std::string> Flags;
170enum class MultilibGroupType {
192struct MultilibGroupSerialization {
194 MultilibGroupType
Type;
197struct MultilibSetSerialization {
198 llvm::VersionTuple MultilibVersion;
199 std::vector<MultilibGroupSerialization> Groups;
200 std::vector<MultilibSerialization> Multilibs;
201 std::vector<MultilibSet::FlagMatcher> FlagMatchers;
206template <>
struct llvm::yaml::MappingTraits<MultilibSerialization> {
207 static void mapping(llvm::yaml::IO &io, MultilibSerialization &
V) {
208 io.mapRequired(
"Dir",
V.Dir);
209 io.mapRequired(
"Flags",
V.Flags);
210 io.mapOptional(
"Group",
V.Group);
212 static std::string
validate(IO &io, MultilibSerialization &
V) {
213 if (StringRef(
V.Dir).starts_with(
"/"))
214 return "paths must be relative but \"" +
V.Dir +
"\" starts with \"/\"";
215 return std::string{};
219template <>
struct llvm::yaml::ScalarEnumerationTraits<MultilibGroupType> {
221 io.enumCase(Val,
"Exclusive", MultilibGroupType::Exclusive);
225template <>
struct llvm::yaml::MappingTraits<MultilibGroupSerialization> {
226 static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &
V) {
227 io.mapRequired(
"Name",
V.Name);
228 io.mapRequired(
"Type",
V.Type);
232template <>
struct llvm::yaml::MappingTraits<
MultilibSet::FlagMatcher> {
234 io.mapRequired(
"Match", M.
Match);
235 io.mapRequired(
"Flags", M.
Flags);
238 llvm::Regex Regex(M.
Match);
239 std::string RegexError;
240 if (!Regex.isValid(RegexError))
243 return "value required for 'Flags'";
244 return std::string{};
248template <>
struct llvm::yaml::MappingTraits<MultilibSetSerialization> {
249 static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M) {
250 io.mapRequired(
"MultilibVersion", M.MultilibVersion);
251 io.mapRequired(
"Variants", M.Multilibs);
252 io.mapOptional(
"Groups", M.Groups);
253 io.mapOptional(
"Mappings", M.FlagMatchers);
255 static std::string
validate(IO &io, MultilibSetSerialization &M) {
256 if (M.MultilibVersion.empty())
257 return "missing required key 'MultilibVersion'";
258 if (M.MultilibVersion.getMajor() != MultilibVersionCurrent.getMajor())
259 return "multilib version " + M.MultilibVersion.getAsString() +
261 if (M.MultilibVersion.getMinor() > MultilibVersionCurrent.getMinor())
262 return "multilib version " + M.MultilibVersion.getAsString() +
264 for (
const MultilibSerialization &Lib : M.Multilibs) {
265 if (!Lib.Group.empty()) {
267 for (
const MultilibGroupSerialization &Group : M.Groups)
268 if (Group.Name == Lib.Group) {
273 return "multilib \"" + Lib.Dir +
274 "\" specifies undefined group name \"" + Lib.Group +
"\"";
277 return std::string{};
281LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSerialization)
282LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibGroupSerialization)
285llvm::ErrorOr<MultilibSet>
287 llvm::SourceMgr::DiagHandlerTy DiagHandler,
288 void *DiagHandlerCtxt) {
289 MultilibSetSerialization MS;
290 llvm::yaml::Input YamlInput(Input,
nullptr, DiagHandler, DiagHandlerCtxt);
292 if (YamlInput.error())
293 return YamlInput.error();
296 Multilibs.reserve(MS.Multilibs.size());
297 for (
const auto &M : MS.Multilibs) {
305 Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
308 return MultilibSet(std::move(Multilibs), std::move(MS.FlagMatchers));
316 for (
const auto &M : *
this)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines version macros and version-related utility functions for Clang.
The base class of the type hierarchy.
See also MultilibSetBuilder for combining multilibs into a set.
LLVM_DUMP_METHOD void dump() const
llvm::function_ref< bool(const Multilib &)> FilterCallback
static llvm::ErrorOr< MultilibSet > parseYaml(llvm::MemoryBufferRef, llvm::SourceMgr::DiagHandlerTy=nullptr, void *DiagHandlerCtxt=nullptr)
void print(raw_ostream &OS) const
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
std::vector< Multilib > multilib_list
llvm::StringSet expandFlags(const Multilib::flags_list &) const
Get the given flags plus flags found by matching them against the FlagMatchers and choosing the Flags...
bool select(const Multilib::flags_list &Flags, llvm::SmallVectorImpl< Multilib > &) const
Select compatible variants,.
void push_back(const Multilib &M)
Add a completed Multilib to the set.
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Multilib(StringRef GCCSuffix={}, StringRef OSSuffix={}, StringRef IncludeSuffix={}, const flags_list &Flags=flags_list(), StringRef ExclusiveGroup={})
GCCSuffix, OSSuffix & IncludeSuffix will be appended directly to the sysroot string so they must eith...
std::vector< std::string > flags_list
const std::string & includeSuffix() const
Get the include directory suffix.
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &OS) const
print summary of the Multilib
bool operator==(const Multilib &Other) const
raw_ostream & operator<<(raw_ostream &OS, const Multilib &M)
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
@ Other
Other implicit parameter.
Uses regular expressions to simplify flags used for multilib selection.
std::vector< std::string > Flags
static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V)
static void mapping(llvm::yaml::IO &io, MultilibSerialization &V)
static std::string validate(IO &io, MultilibSerialization &V)
static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M)
static std::string validate(IO &io, MultilibSetSerialization &M)
static std::string validate(IO &io, MultilibSet::FlagMatcher &M)
static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M)
static void enumeration(IO &io, MultilibGroupType &Val)