1 //===- Multilib.h -----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_DRIVER_MULTILIB_H
10 #define LLVM_CLANG_DRIVER_MULTILIB_H
12 #include "clang/Basic/LLVM.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Compiler.h"
26 /// This corresponds to a single GCC Multilib, or a segment of one controlled
27 /// by a command line flag
30 using flags_list = std::vector<std::string>;
33 std::string GCCSuffix;
35 std::string IncludeSuffix;
40 Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
41 StringRef IncludeSuffix = {}, int Priority = 0);
43 /// Get the detected GCC installation path suffix for the multi-arch
44 /// target variant. Always starts with a '/', unless empty
45 const std::string &gccSuffix() const {
46 assert(GCCSuffix.empty() ||
47 (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
51 /// Set the GCC installation path suffix.
52 Multilib &gccSuffix(StringRef S);
54 /// Get the detected os path suffix for the multi-arch
55 /// target variant. Always starts with a '/', unless empty
56 const std::string &osSuffix() const {
57 assert(OSSuffix.empty() ||
58 (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
62 /// Set the os path suffix.
63 Multilib &osSuffix(StringRef S);
65 /// Get the include directory suffix. Always starts with a '/', unless
67 const std::string &includeSuffix() const {
68 assert(IncludeSuffix.empty() ||
69 (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
73 /// Set the include directory suffix
74 Multilib &includeSuffix(StringRef S);
76 /// Get the flags that indicate or contraindicate this multilib's use
77 /// All elements begin with either '+' or '-'
78 const flags_list &flags() const { return Flags; }
79 flags_list &flags() { return Flags; }
81 /// Returns the multilib priority. When more than one multilib matches flags,
82 /// the one with the highest priority is selected, with 0 being the default.
83 int priority() const { return Priority; }
85 /// Add a flag to the flags list
86 /// \p Flag must be a flag accepted by the driver with its leading '-' removed,
87 /// and replaced with either:
88 /// '-' which contraindicates using this multilib with that flag
90 /// '+' which promotes using this multilib in the presence of that flag
91 /// otherwise '-print-multi-lib' will not emit them correctly.
92 Multilib &flag(StringRef F) {
93 assert(F.front() == '+' || F.front() == '-');
98 LLVM_DUMP_METHOD void dump() const;
99 /// print summary of the Multilib
100 void print(raw_ostream &OS) const;
102 /// Check whether any of the 'against' flags contradict the 'for' flags.
103 bool isValid() const;
105 /// Check whether the default is selected
106 bool isDefault() const
107 { return GCCSuffix.empty() && OSSuffix.empty() && IncludeSuffix.empty(); }
109 bool operator==(const Multilib &Other) const;
112 raw_ostream &operator<<(raw_ostream &OS, const Multilib &M);
116 using multilib_list = std::vector<Multilib>;
117 using iterator = multilib_list::iterator;
118 using const_iterator = multilib_list::const_iterator;
119 using IncludeDirsFunc =
120 std::function<std::vector<std::string>(const Multilib &M)>;
121 using FilterCallback = llvm::function_ref<bool(const Multilib &)>;
124 multilib_list Multilibs;
125 IncludeDirsFunc IncludeCallback;
126 IncludeDirsFunc FilePathsCallback;
129 MultilibSet() = default;
131 /// Add an optional Multilib segment
132 MultilibSet &Maybe(const Multilib &M);
134 /// Add a set of mutually incompatible Multilib segments
135 MultilibSet &Either(const Multilib &M1, const Multilib &M2);
136 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
138 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
139 const Multilib &M3, const Multilib &M4);
140 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
141 const Multilib &M3, const Multilib &M4,
143 MultilibSet &Either(ArrayRef<Multilib> Ms);
145 /// Filter out some subset of the Multilibs using a user defined callback
146 MultilibSet &FilterOut(FilterCallback F);
148 /// Filter out those Multilibs whose gccSuffix matches the given expression
149 MultilibSet &FilterOut(const char *Regex);
151 /// Add a completed Multilib to the set
152 void push_back(const Multilib &M);
154 /// Union this set of multilibs with another
155 void combineWith(const MultilibSet &MS);
157 /// Remove all of the multilibs from the set
158 void clear() { Multilibs.clear(); }
160 iterator begin() { return Multilibs.begin(); }
161 const_iterator begin() const { return Multilibs.begin(); }
163 iterator end() { return Multilibs.end(); }
164 const_iterator end() const { return Multilibs.end(); }
166 /// Pick the best multilib in the set, \returns false if none are compatible
167 bool select(const Multilib::flags_list &Flags, Multilib &M) const;
169 unsigned size() const { return Multilibs.size(); }
171 LLVM_DUMP_METHOD void dump() const;
172 void print(raw_ostream &OS) const;
174 MultilibSet &setIncludeDirsCallback(IncludeDirsFunc F) {
175 IncludeCallback = std::move(F);
179 const IncludeDirsFunc &includeDirsCallback() const { return IncludeCallback; }
181 MultilibSet &setFilePathsCallback(IncludeDirsFunc F) {
182 FilePathsCallback = std::move(F);
186 const IncludeDirsFunc &filePathsCallback() const { return FilePathsCallback; }
189 /// Apply the filter to Multilibs and return the subset that remains
190 static multilib_list filterCopy(FilterCallback F, const multilib_list &Ms);
192 /// Apply the filter to the multilib_list, removing those that don't match
193 static void filterInPlace(FilterCallback F, multilib_list &Ms);
196 raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
198 } // namespace driver
201 #endif // LLVM_CLANG_DRIVER_MULTILIB_H