clang 20.0.0git
Multilib.cpp
Go to the documentation of this file.
1//===- Multilib.cpp - Multilib Implementation -----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/Basic/LLVM.h"
11#include "clang/Driver/Driver.h"
12#include "llvm/ADT/DenseSet.h"
13#include "llvm/ADT/SmallSet.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/Support/Compiler.h"
16#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/Regex.h"
18#include "llvm/Support/VersionTuple.h"
19#include "llvm/Support/YAMLParser.h"
20#include "llvm/Support/YAMLTraits.h"
21#include "llvm/Support/raw_ostream.h"
22#include <algorithm>
23#include <cassert>
24#include <string>
25
26using namespace clang;
27using namespace driver;
28using namespace llvm::sys;
29
30Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
31 StringRef IncludeSuffix, const flags_list &Flags,
32 StringRef ExclusiveGroup, std::optional<StringRef> Error)
33 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
34 Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) {
35 assert(GCCSuffix.empty() ||
36 (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
37 assert(OSSuffix.empty() ||
38 (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
39 assert(IncludeSuffix.empty() ||
40 (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
41}
42
43LLVM_DUMP_METHOD void Multilib::dump() const {
44 print(llvm::errs());
45}
46
47void Multilib::print(raw_ostream &OS) const {
48 if (GCCSuffix.empty())
49 OS << ".";
50 else {
51 OS << StringRef(GCCSuffix).drop_front();
52 }
53 OS << ";";
54 for (StringRef Flag : Flags) {
55 if (Flag.front() == '-')
56 OS << "@" << Flag.substr(1);
57 }
58}
59
61 // Check whether the flags sets match
62 // allowing for the match to be order invariant
63 llvm::StringSet<> MyFlags;
64 for (const auto &Flag : Flags)
65 MyFlags.insert(Flag);
66
67 for (const auto &Flag : Other.Flags)
68 if (!MyFlags.contains(Flag))
69 return false;
70
71 if (osSuffix() != Other.osSuffix())
72 return false;
73
74 if (gccSuffix() != Other.gccSuffix())
75 return false;
76
77 if (includeSuffix() != Other.includeSuffix())
78 return false;
79
80 return true;
81}
82
83raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
84 M.print(OS);
85 return OS;
86}
87
89 llvm::erase_if(Multilibs, F);
90 return *this;
91}
92
93void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
94
96 llvm::SmallVectorImpl<Multilib> &Selected) const {
97 llvm::StringSet<> FlagSet(expandFlags(Flags));
98 Selected.clear();
99 bool AnyErrors = false;
100
101 // Decide which multilibs we're going to select at all.
102 llvm::DenseSet<StringRef> ExclusiveGroupsSelected;
103 for (const Multilib &M : llvm::reverse(Multilibs)) {
104 // If this multilib doesn't match all our flags, don't select it.
105 if (!llvm::all_of(M.flags(), [&FlagSet](const std::string &F) {
106 return FlagSet.contains(F);
107 }))
108 continue;
109
110 const std::string &group = M.exclusiveGroup();
111 if (!group.empty()) {
112 // If this multilib has the same ExclusiveGroup as one we've already
113 // selected, skip it. We're iterating in reverse order, so the group
114 // member we've selected already is preferred.
115 //
116 // Otherwise, add the group name to the set of groups we've already
117 // selected a member of.
118 auto [It, Inserted] = ExclusiveGroupsSelected.insert(group);
119 if (!Inserted)
120 continue;
121 }
122
123 // If this multilib is actually a placeholder containing an error message
124 // written by the multilib.yaml author, then set a flag that will cause a
125 // failure return. Our caller will display the error message.
126 if (M.isError())
127 AnyErrors = true;
128
129 // Select this multilib.
130 Selected.push_back(M);
131 }
132
133 // We iterated in reverse order, so now put Selected back the right way
134 // round.
135 std::reverse(Selected.begin(), Selected.end());
136
137 return !AnyErrors && !Selected.empty();
138}
139
140llvm::StringSet<>
142 llvm::StringSet<> Result;
143 for (const auto &F : InFlags)
144 Result.insert(F);
145 for (const FlagMatcher &M : FlagMatchers) {
146 std::string RegexString(M.Match);
147
148 // Make the regular expression match the whole string.
149 if (!StringRef(M.Match).starts_with("^"))
150 RegexString.insert(RegexString.begin(), '^');
151 if (!StringRef(M.Match).ends_with("$"))
152 RegexString.push_back('$');
153
154 const llvm::Regex Regex(RegexString);
155 assert(Regex.isValid());
156 if (llvm::any_of(InFlags,
157 [&Regex](StringRef F) { return Regex.match(F); })) {
158 Result.insert(M.Flags.begin(), M.Flags.end());
159 }
160 }
161 return Result;
162}
163
164namespace {
165
166// When updating this also update MULTILIB_VERSION in MultilibTest.cpp
167static const VersionTuple MultilibVersionCurrent(1, 0);
168
169struct MultilibSerialization {
170 std::string Dir; // if this record successfully selects a library dir
171 std::string Error; // if this record reports a fatal error message
172 std::vector<std::string> Flags;
173 std::string Group;
174};
175
176enum class MultilibGroupType {
177 /*
178 * The only group type currently supported is 'Exclusive', which indicates a
179 * group of multilibs of which at most one may be selected.
180 */
181 Exclusive,
182
183 /*
184 * Future possibility: a second group type indicating a set of library
185 * directories that are mutually _dependent_ rather than mutually exclusive:
186 * if you include one you must include them all.
187 *
188 * It might also be useful to allow groups to be members of other groups, so
189 * that a mutually exclusive group could contain a mutually dependent set of
190 * library directories, or vice versa.
191 *
192 * These additional features would need changes in the implementation, but
193 * the YAML schema is set up so they can be added without requiring changes
194 * in existing users' multilib.yaml files.
195 */
196};
197
198struct MultilibGroupSerialization {
199 std::string Name;
200 MultilibGroupType Type;
201};
202
203struct MultilibSetSerialization {
204 llvm::VersionTuple MultilibVersion;
208 SmallVector<custom_flag::Declaration> CustomFlagDeclarations;
209};
210
211} // end anonymous namespace
212
213LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSerialization)
214LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibGroupSerialization)
215LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSet::FlagMatcher)
216LLVM_YAML_IS_SEQUENCE_VECTOR(custom_flag::ValueDetail)
217LLVM_YAML_IS_SEQUENCE_VECTOR(custom_flag::Declaration)
218
219template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
220 static void mapping(llvm::yaml::IO &io, MultilibSerialization &V) {
221 io.mapOptional("Dir", V.Dir);
222 io.mapOptional("Error", V.Error);
223 io.mapRequired("Flags", V.Flags);
224 io.mapOptional("Group", V.Group);
225 }
226 static std::string validate(IO &io, MultilibSerialization &V) {
227 if (V.Dir.empty() && V.Error.empty())
228 return "one of the 'Dir' and 'Error' keys must be specified";
229 if (!V.Dir.empty() && !V.Error.empty())
230 return "the 'Dir' and 'Error' keys may not both be specified";
231 if (StringRef(V.Dir).starts_with("/"))
232 return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
233 return std::string{};
234 }
235};
236
237template <> struct llvm::yaml::ScalarEnumerationTraits<MultilibGroupType> {
238 static void enumeration(IO &io, MultilibGroupType &Val) {
239 io.enumCase(Val, "Exclusive", MultilibGroupType::Exclusive);
240 }
241};
242
243template <> struct llvm::yaml::MappingTraits<MultilibGroupSerialization> {
244 static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V) {
245 io.mapRequired("Name", V.Name);
246 io.mapRequired("Type", V.Type);
247 }
248};
249
250template <> struct llvm::yaml::MappingTraits<MultilibSet::FlagMatcher> {
251 static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M) {
252 io.mapRequired("Match", M.Match);
253 io.mapRequired("Flags", M.Flags);
254 }
255 static std::string validate(IO &io, MultilibSet::FlagMatcher &M) {
256 llvm::Regex Regex(M.Match);
257 std::string RegexError;
258 if (!Regex.isValid(RegexError))
259 return RegexError;
260 if (M.Flags.empty())
261 return "value required for 'Flags'";
262 return std::string{};
263 }
264};
265
266template <>
267struct llvm::yaml::MappingContextTraits<custom_flag::ValueDetail,
268 llvm::SmallSet<std::string, 32>> {
269 static void mapping(llvm::yaml::IO &io, custom_flag::ValueDetail &V,
270 llvm::SmallSet<std::string, 32> &) {
271 io.mapRequired("Name", V.Name);
272 io.mapOptional("MacroDefines", V.MacroDefines);
273 }
274 static std::string validate(IO &io, custom_flag::ValueDetail &V,
275 llvm::SmallSet<std::string, 32> &NameSet) {
276 if (V.Name.empty())
277 return "custom flag value requires a name";
278 if (!NameSet.insert(V.Name).second)
279 return "duplicate custom flag value name: \"" + V.Name + "\"";
280 return {};
281 }
282};
283
284template <>
285struct llvm::yaml::MappingContextTraits<custom_flag::Declaration,
286 llvm::SmallSet<std::string, 32>> {
287 static void mapping(llvm::yaml::IO &io, custom_flag::Declaration &V,
288 llvm::SmallSet<std::string, 32> &NameSet) {
289 io.mapRequired("Name", V.Name);
290 io.mapRequired("Values", V.ValueList, NameSet);
291 std::string DefaultValueName;
292 io.mapRequired("Default", DefaultValueName);
293
294 for (auto [Idx, Value] : llvm::enumerate(V.ValueList)) {
295 Value.Decl = &V;
296 if (Value.Name == DefaultValueName) {
297 assert(!V.DefaultValueIdx);
298 V.DefaultValueIdx = Idx;
299 }
300 }
301 }
302 static std::string validate(IO &io, custom_flag::Declaration &V,
303 llvm::SmallSet<std::string, 32> &) {
304 if (V.Name.empty())
305 return "custom flag requires a name";
306 if (V.ValueList.empty())
307 return "custom flag must have at least one value";
308 if (!V.DefaultValueIdx)
309 return "custom flag must have a default value";
310 return {};
311 }
312};
313
314template <> struct llvm::yaml::MappingTraits<MultilibSetSerialization> {
315 static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M) {
316 io.mapRequired("MultilibVersion", M.MultilibVersion);
317 io.mapRequired("Variants", M.Multilibs);
318 io.mapOptional("Groups", M.Groups);
319 llvm::SmallSet<std::string, 32> NameSet;
320 io.mapOptionalWithContext("Flags", M.CustomFlagDeclarations, NameSet);
321 io.mapOptional("Mappings", M.FlagMatchers);
322 }
323 static std::string validate(IO &io, MultilibSetSerialization &M) {
324 if (M.MultilibVersion.empty())
325 return "missing required key 'MultilibVersion'";
326 if (M.MultilibVersion.getMajor() != MultilibVersionCurrent.getMajor())
327 return "multilib version " + M.MultilibVersion.getAsString() +
328 " is unsupported";
329 if (M.MultilibVersion.getMinor() > MultilibVersionCurrent.getMinor())
330 return "multilib version " + M.MultilibVersion.getAsString() +
331 " is unsupported";
332 for (const MultilibSerialization &Lib : M.Multilibs) {
333 if (!Lib.Group.empty()) {
334 bool Found = false;
335 for (const MultilibGroupSerialization &Group : M.Groups)
336 if (Group.Name == Lib.Group) {
337 Found = true;
338 break;
339 }
340 if (!Found)
341 return "multilib \"" + Lib.Dir +
342 "\" specifies undefined group name \"" + Lib.Group + "\"";
343 }
344 }
345 return std::string{};
346 }
347};
348
349llvm::ErrorOr<MultilibSet>
350MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
351 llvm::SourceMgr::DiagHandlerTy DiagHandler,
352 void *DiagHandlerCtxt) {
353 MultilibSetSerialization MS;
354 llvm::yaml::Input YamlInput(Input, nullptr, DiagHandler, DiagHandlerCtxt);
355 YamlInput >> MS;
356 if (YamlInput.error())
357 return YamlInput.error();
358
359 multilib_list Multilibs;
360 Multilibs.reserve(MS.Multilibs.size());
361 for (const auto &M : MS.Multilibs) {
362 if (!M.Error.empty()) {
363 Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error);
364 } else {
365 std::string Dir;
366 if (M.Dir != ".")
367 Dir = "/" + M.Dir;
368 // We transfer M.Group straight into the ExclusiveGroup parameter for the
369 // Multilib constructor. If we later support more than one type of group,
370 // we'll have to look up the group name in MS.Groups, check its type, and
371 // decide what to do here.
372 Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
373 }
374 }
375
376 return MultilibSet(std::move(Multilibs), std::move(MS.FlagMatchers),
377 std::move(MS.CustomFlagDeclarations));
378}
379
380LLVM_DUMP_METHOD void MultilibSet::dump() const {
381 print(llvm::errs());
382}
383
384void MultilibSet::print(raw_ostream &OS) const {
385 for (const auto &M : *this)
386 OS << M << "\n";
387}
388
389raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
390 MS.print(OS);
391 return OS;
392}
393
396 : Name(Other.Name), ValueList(Other.ValueList),
397 DefaultValueIdx(Other.DefaultValueIdx) {
398 for (ValueDetail &Detail : ValueList)
399 Detail.Decl = this;
400}
401
403 : Name(std::move(Other.Name)), ValueList(std::move(Other.ValueList)),
404 DefaultValueIdx(std::move(Other.DefaultValueIdx)) {
405 for (ValueDetail &Detail : ValueList)
406 Detail.Decl = this;
407}
408
410 if (this == &Other)
411 return *this;
412 Name = Other.Name;
413 ValueList = Other.ValueList;
414 DefaultValueIdx = Other.DefaultValueIdx;
415 for (ValueDetail &Detail : ValueList)
416 Detail.Decl = this;
417 return *this;
418}
419
421 if (this == &Other)
422 return *this;
423 Name = std::move(Other.Name);
424 ValueList = std::move(Other.ValueList);
425 DefaultValueIdx = std::move(Other.DefaultValueIdx);
426 for (ValueDetail &Detail : ValueList)
427 Detail.Decl = this;
428 return *this;
429}
430} // namespace clang::driver::custom_flag
#define V(N, I)
Definition: ASTContext.h:3453
const Decl * D
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
The base class of the type hierarchy.
Definition: Type.h:1828
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:99
See also MultilibSetBuilder for combining multilibs into a set.
Definition: Multilib.h:129
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:380
llvm::function_ref< bool(const Multilib &)> FilterCallback
Definition: Multilib.h:135
static llvm::ErrorOr< MultilibSet > parseYaml(llvm::MemoryBufferRef, llvm::SourceMgr::DiagHandlerTy=nullptr, void *DiagHandlerCtxt=nullptr)
Definition: Multilib.cpp:350
void print(raw_ostream &OS) const
Definition: Multilib.cpp:384
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:88
std::vector< Multilib > multilib_list
Definition: Multilib.h:131
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...
Definition: Multilib.cpp:141
void push_back(const Multilib &M)
Add a completed Multilib to the set.
Definition: Multilib.cpp:93
bool select(const Driver &D, const Multilib::flags_list &Flags, llvm::SmallVectorImpl< Multilib > &) const
Select compatible variants,.
Definition: Multilib.cpp:95
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:35
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:70
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:74
std::vector< std::string > flags_list
Definition: Multilib.h:37
Multilib(StringRef GCCSuffix={}, StringRef OSSuffix={}, StringRef IncludeSuffix={}, const flags_list &Flags=flags_list(), StringRef ExclusiveGroup={}, std::optional< StringRef > Error=std::nullopt)
GCCSuffix, OSSuffix & IncludeSuffix will be appended directly to the sysroot string so they must eith...
Definition: Multilib.cpp:30
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:78
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:43
void print(raw_ostream &OS) const
print summary of the Multilib
Definition: Multilib.cpp:47
bool operator==(const Multilib &Other) const
Definition: Multilib.cpp:60
raw_ostream & operator<<(raw_ostream &OS, const Multilib &M)
Definition: Multilib.cpp:83
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.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Uses regular expressions to simplify flags used for multilib selection.
Definition: Multilib.h:140
std::vector< std::string > Flags
Definition: Multilib.h:142
Declaration & operator=(const Declaration &)
Definition: Multilib.cpp:409
std::optional< size_t > DefaultValueIdx
Definition: Multilib.h:116
static std::string validate(IO &io, custom_flag::Declaration &V, llvm::SmallSet< std::string, 32 > &)
Definition: Multilib.cpp:302
static void mapping(llvm::yaml::IO &io, custom_flag::Declaration &V, llvm::SmallSet< std::string, 32 > &NameSet)
Definition: Multilib.cpp:287
static void mapping(llvm::yaml::IO &io, custom_flag::ValueDetail &V, llvm::SmallSet< std::string, 32 > &)
Definition: Multilib.cpp:269
static std::string validate(IO &io, custom_flag::ValueDetail &V, llvm::SmallSet< std::string, 32 > &NameSet)
Definition: Multilib.cpp:274
static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V)
Definition: Multilib.cpp:244
static void mapping(llvm::yaml::IO &io, MultilibSerialization &V)
Definition: Multilib.cpp:220
static std::string validate(IO &io, MultilibSerialization &V)
Definition: Multilib.cpp:226
static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M)
Definition: Multilib.cpp:315
static std::string validate(IO &io, MultilibSetSerialization &M)
Definition: Multilib.cpp:323
static std::string validate(IO &io, MultilibSet::FlagMatcher &M)
Definition: Multilib.cpp:255
static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M)
Definition: Multilib.cpp:251
static void enumeration(IO &io, MultilibGroupType &Val)
Definition: Multilib.cpp:238