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/StringRef.h"
14#include "llvm/Support/Compiler.h"
15#include "llvm/Support/ErrorHandling.h"
16#include "llvm/Support/Regex.h"
17#include "llvm/Support/VersionTuple.h"
18#include "llvm/Support/YAMLParser.h"
19#include "llvm/Support/YAMLTraits.h"
20#include "llvm/Support/raw_ostream.h"
21#include <algorithm>
22#include <cassert>
23#include <string>
24
25using namespace clang;
26using namespace driver;
27using namespace llvm::sys;
28
29Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
30 StringRef IncludeSuffix, const flags_list &Flags,
31 StringRef ExclusiveGroup, std::optional<StringRef> Error)
32 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
33 Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) {
34 assert(GCCSuffix.empty() ||
35 (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
36 assert(OSSuffix.empty() ||
37 (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
38 assert(IncludeSuffix.empty() ||
39 (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
40}
41
42LLVM_DUMP_METHOD void Multilib::dump() const {
43 print(llvm::errs());
44}
45
46void Multilib::print(raw_ostream &OS) const {
47 if (GCCSuffix.empty())
48 OS << ".";
49 else {
50 OS << StringRef(GCCSuffix).drop_front();
51 }
52 OS << ";";
53 for (StringRef Flag : Flags) {
54 if (Flag.front() == '-')
55 OS << "@" << Flag.substr(1);
56 }
57}
58
60 // Check whether the flags sets match
61 // allowing for the match to be order invariant
62 llvm::StringSet<> MyFlags;
63 for (const auto &Flag : Flags)
64 MyFlags.insert(Flag);
65
66 for (const auto &Flag : Other.Flags)
67 if (!MyFlags.contains(Flag))
68 return false;
69
70 if (osSuffix() != Other.osSuffix())
71 return false;
72
73 if (gccSuffix() != Other.gccSuffix())
74 return false;
75
76 if (includeSuffix() != Other.includeSuffix())
77 return false;
78
79 return true;
80}
81
82raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
83 M.print(OS);
84 return OS;
85}
86
88 llvm::erase_if(Multilibs, F);
89 return *this;
90}
91
92void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
93
95 llvm::SmallVectorImpl<Multilib> &Selected) const {
96 llvm::StringSet<> FlagSet(expandFlags(Flags));
97 Selected.clear();
98 bool AnyErrors = false;
99
100 // Decide which multilibs we're going to select at all.
101 llvm::DenseSet<StringRef> ExclusiveGroupsSelected;
102 for (const Multilib &M : llvm::reverse(Multilibs)) {
103 // If this multilib doesn't match all our flags, don't select it.
104 if (!llvm::all_of(M.flags(), [&FlagSet](const std::string &F) {
105 return FlagSet.contains(F);
106 }))
107 continue;
108
109 const std::string &group = M.exclusiveGroup();
110 if (!group.empty()) {
111 // If this multilib has the same ExclusiveGroup as one we've already
112 // selected, skip it. We're iterating in reverse order, so the group
113 // member we've selected already is preferred.
114 //
115 // Otherwise, add the group name to the set of groups we've already
116 // selected a member of.
117 auto [It, Inserted] = ExclusiveGroupsSelected.insert(group);
118 if (!Inserted)
119 continue;
120 }
121
122 // If this multilib is actually a placeholder containing an error message
123 // written by the multilib.yaml author, then set a flag that will cause a
124 // failure return. Our caller will display the error message.
125 if (M.isError())
126 AnyErrors = true;
127
128 // Select this multilib.
129 Selected.push_back(M);
130 }
131
132 // We iterated in reverse order, so now put Selected back the right way
133 // round.
134 std::reverse(Selected.begin(), Selected.end());
135
136 return !AnyErrors && !Selected.empty();
137}
138
139llvm::StringSet<>
141 llvm::StringSet<> Result;
142 for (const auto &F : InFlags)
143 Result.insert(F);
144 for (const FlagMatcher &M : FlagMatchers) {
145 std::string RegexString(M.Match);
146
147 // Make the regular expression match the whole string.
148 if (!StringRef(M.Match).starts_with("^"))
149 RegexString.insert(RegexString.begin(), '^');
150 if (!StringRef(M.Match).ends_with("$"))
151 RegexString.push_back('$');
152
153 const llvm::Regex Regex(RegexString);
154 assert(Regex.isValid());
155 if (llvm::any_of(InFlags,
156 [&Regex](StringRef F) { return Regex.match(F); })) {
157 Result.insert(M.Flags.begin(), M.Flags.end());
158 }
159 }
160 return Result;
161}
162
163namespace {
164
165// When updating this also update MULTILIB_VERSION in MultilibTest.cpp
166static const VersionTuple MultilibVersionCurrent(1, 0);
167
168struct MultilibSerialization {
169 std::string Dir; // if this record successfully selects a library dir
170 std::string Error; // if this record reports a fatal error message
171 std::vector<std::string> Flags;
172 std::string Group;
173};
174
175enum class MultilibGroupType {
176 /*
177 * The only group type currently supported is 'Exclusive', which indicates a
178 * group of multilibs of which at most one may be selected.
179 */
180 Exclusive,
181
182 /*
183 * Future possibility: a second group type indicating a set of library
184 * directories that are mutually _dependent_ rather than mutually exclusive:
185 * if you include one you must include them all.
186 *
187 * It might also be useful to allow groups to be members of other groups, so
188 * that a mutually exclusive group could contain a mutually dependent set of
189 * library directories, or vice versa.
190 *
191 * These additional features would need changes in the implementation, but
192 * the YAML schema is set up so they can be added without requiring changes
193 * in existing users' multilib.yaml files.
194 */
195};
196
197struct MultilibGroupSerialization {
198 std::string Name;
199 MultilibGroupType Type;
200};
201
202struct MultilibSetSerialization {
203 llvm::VersionTuple MultilibVersion;
204 std::vector<MultilibGroupSerialization> Groups;
205 std::vector<MultilibSerialization> Multilibs;
206 std::vector<MultilibSet::FlagMatcher> FlagMatchers;
207};
208
209} // end anonymous namespace
210
211template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
212 static void mapping(llvm::yaml::IO &io, MultilibSerialization &V) {
213 io.mapOptional("Dir", V.Dir);
214 io.mapOptional("Error", V.Error);
215 io.mapRequired("Flags", V.Flags);
216 io.mapOptional("Group", V.Group);
217 }
218 static std::string validate(IO &io, MultilibSerialization &V) {
219 if (V.Dir.empty() && V.Error.empty())
220 return "one of the 'Dir' and 'Error' keys must be specified";
221 if (!V.Dir.empty() && !V.Error.empty())
222 return "the 'Dir' and 'Error' keys may not both be specified";
223 if (StringRef(V.Dir).starts_with("/"))
224 return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
225 return std::string{};
226 }
227};
228
229template <> struct llvm::yaml::ScalarEnumerationTraits<MultilibGroupType> {
230 static void enumeration(IO &io, MultilibGroupType &Val) {
231 io.enumCase(Val, "Exclusive", MultilibGroupType::Exclusive);
232 }
233};
234
235template <> struct llvm::yaml::MappingTraits<MultilibGroupSerialization> {
236 static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V) {
237 io.mapRequired("Name", V.Name);
238 io.mapRequired("Type", V.Type);
239 }
240};
241
242template <> struct llvm::yaml::MappingTraits<MultilibSet::FlagMatcher> {
243 static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M) {
244 io.mapRequired("Match", M.Match);
245 io.mapRequired("Flags", M.Flags);
246 }
247 static std::string validate(IO &io, MultilibSet::FlagMatcher &M) {
248 llvm::Regex Regex(M.Match);
249 std::string RegexError;
250 if (!Regex.isValid(RegexError))
251 return RegexError;
252 if (M.Flags.empty())
253 return "value required for 'Flags'";
254 return std::string{};
255 }
256};
257
258template <> struct llvm::yaml::MappingTraits<MultilibSetSerialization> {
259 static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M) {
260 io.mapRequired("MultilibVersion", M.MultilibVersion);
261 io.mapRequired("Variants", M.Multilibs);
262 io.mapOptional("Groups", M.Groups);
263 io.mapOptional("Mappings", M.FlagMatchers);
264 }
265 static std::string validate(IO &io, MultilibSetSerialization &M) {
266 if (M.MultilibVersion.empty())
267 return "missing required key 'MultilibVersion'";
268 if (M.MultilibVersion.getMajor() != MultilibVersionCurrent.getMajor())
269 return "multilib version " + M.MultilibVersion.getAsString() +
270 " is unsupported";
271 if (M.MultilibVersion.getMinor() > MultilibVersionCurrent.getMinor())
272 return "multilib version " + M.MultilibVersion.getAsString() +
273 " is unsupported";
274 for (const MultilibSerialization &Lib : M.Multilibs) {
275 if (!Lib.Group.empty()) {
276 bool Found = false;
277 for (const MultilibGroupSerialization &Group : M.Groups)
278 if (Group.Name == Lib.Group) {
279 Found = true;
280 break;
281 }
282 if (!Found)
283 return "multilib \"" + Lib.Dir +
284 "\" specifies undefined group name \"" + Lib.Group + "\"";
285 }
286 }
287 return std::string{};
288 }
289};
290
291LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSerialization)
292LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibGroupSerialization)
293LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSet::FlagMatcher)
294
295llvm::ErrorOr<MultilibSet>
296MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
297 llvm::SourceMgr::DiagHandlerTy DiagHandler,
298 void *DiagHandlerCtxt) {
299 MultilibSetSerialization MS;
300 llvm::yaml::Input YamlInput(Input, nullptr, DiagHandler, DiagHandlerCtxt);
301 YamlInput >> MS;
302 if (YamlInput.error())
303 return YamlInput.error();
304
305 multilib_list Multilibs;
306 Multilibs.reserve(MS.Multilibs.size());
307 for (const auto &M : MS.Multilibs) {
308 if (!M.Error.empty()) {
309 Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error);
310 } else {
311 std::string Dir;
312 if (M.Dir != ".")
313 Dir = "/" + M.Dir;
314 // We transfer M.Group straight into the ExclusiveGroup parameter for the
315 // Multilib constructor. If we later support more than one type of group,
316 // we'll have to look up the group name in MS.Groups, check its type, and
317 // decide what to do here.
318 Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
319 }
320 }
321
322 return MultilibSet(std::move(Multilibs), std::move(MS.FlagMatchers));
323}
324
325LLVM_DUMP_METHOD void MultilibSet::dump() const {
326 print(llvm::errs());
327}
328
329void MultilibSet::print(raw_ostream &OS) const {
330 for (const auto &M : *this)
331 OS << M << "\n";
332}
333
334raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
335 MS.print(OS);
336 return OS;
337}
#define V(N, I)
Definition: ASTContext.h:3443
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:77
See also MultilibSetBuilder for combining multilibs into a set.
Definition: Multilib.h:105
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:325
llvm::function_ref< bool(const Multilib &)> FilterCallback
Definition: Multilib.h:111
static llvm::ErrorOr< MultilibSet > parseYaml(llvm::MemoryBufferRef, llvm::SourceMgr::DiagHandlerTy=nullptr, void *DiagHandlerCtxt=nullptr)
Definition: Multilib.cpp:296
void print(raw_ostream &OS) const
Definition: Multilib.cpp:329
MultilibSet & FilterOut(FilterCallback F)
Filter out some subset of the Multilibs using a user defined callback.
Definition: Multilib.cpp:87
std::vector< Multilib > multilib_list
Definition: Multilib.h:107
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:140
void push_back(const Multilib &M)
Add a completed Multilib to the set.
Definition: Multilib.cpp:92
bool select(const Driver &D, const Multilib::flags_list &Flags, llvm::SmallVectorImpl< Multilib > &) const
Select compatible variants,.
Definition: Multilib.cpp:94
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:29
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:78
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:42
void print(raw_ostream &OS) const
print summary of the Multilib
Definition: Multilib.cpp:46
bool operator==(const Multilib &Other) const
Definition: Multilib.cpp:59
raw_ostream & operator<<(raw_ostream &OS, const Multilib &M)
Definition: Multilib.cpp:82
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.
Definition: Multilib.h:116
std::vector< std::string > Flags
Definition: Multilib.h:118
static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V)
Definition: Multilib.cpp:236
static void mapping(llvm::yaml::IO &io, MultilibSerialization &V)
Definition: Multilib.cpp:212
static std::string validate(IO &io, MultilibSerialization &V)
Definition: Multilib.cpp:218
static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M)
Definition: Multilib.cpp:259
static std::string validate(IO &io, MultilibSetSerialization &M)
Definition: Multilib.cpp:265
static std::string validate(IO &io, MultilibSet::FlagMatcher &M)
Definition: Multilib.cpp:247
static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M)
Definition: Multilib.cpp:243
static void enumeration(IO &io, MultilibGroupType &Val)
Definition: Multilib.cpp:230