1 //===- Multilib.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_DRIVER_MULTILIB_H
11 #define LLVM_CLANG_DRIVER_MULTILIB_H
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Compiler.h"
27 /// This corresponds to a single GCC Multilib, or a segment of one controlled
28 /// by a command line flag
31 using flags_list = std::vector<std::string>;
34 std::string GCCSuffix;
36 std::string IncludeSuffix;
40 Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
41 StringRef IncludeSuffix = {});
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 /// Add a flag to the flags list
82 /// \p Flag must be a flag accepted by the driver with its leading '-' removed,
83 /// and replaced with either:
84 /// '-' which contraindicates using this multilib with that flag
86 /// '+' which promotes using this multilib in the presence of that flag
87 /// otherwise '-print-multi-lib' will not emit them correctly.
88 Multilib &flag(StringRef F) {
89 assert(F.front() == '+' || F.front() == '-');
94 LLVM_DUMP_METHOD void dump() const;
95 /// print summary of the Multilib
96 void print(raw_ostream &OS) const;
98 /// Check whether any of the 'against' flags contradict the 'for' flags.
101 /// Check whether the default is selected
102 bool isDefault() const
103 { return GCCSuffix.empty() && OSSuffix.empty() && IncludeSuffix.empty(); }
105 bool operator==(const Multilib &Other) const;
108 raw_ostream &operator<<(raw_ostream &OS, const Multilib &M);
112 using multilib_list = std::vector<Multilib>;
113 using iterator = multilib_list::iterator;
114 using const_iterator = multilib_list::const_iterator;
115 using IncludeDirsFunc =
116 std::function<std::vector<std::string>(const Multilib &M)>;
117 using FilterCallback = llvm::function_ref<bool(const Multilib &)>;
120 multilib_list Multilibs;
121 IncludeDirsFunc IncludeCallback;
122 IncludeDirsFunc FilePathsCallback;
125 MultilibSet() = default;
127 /// Add an optional Multilib segment
128 MultilibSet &Maybe(const Multilib &M);
130 /// Add a set of mutually incompatible Multilib segments
131 MultilibSet &Either(const Multilib &M1, const Multilib &M2);
132 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
134 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
135 const Multilib &M3, const Multilib &M4);
136 MultilibSet &Either(const Multilib &M1, const Multilib &M2,
137 const Multilib &M3, const Multilib &M4,
139 MultilibSet &Either(ArrayRef<Multilib> Ms);
141 /// Filter out some subset of the Multilibs using a user defined callback
142 MultilibSet &FilterOut(FilterCallback F);
144 /// Filter out those Multilibs whose gccSuffix matches the given expression
145 MultilibSet &FilterOut(const char *Regex);
147 /// Add a completed Multilib to the set
148 void push_back(const Multilib &M);
150 /// Union this set of multilibs with another
151 void combineWith(const MultilibSet &MS);
153 /// Remove all of the multilibs from the set
154 void clear() { Multilibs.clear(); }
156 iterator begin() { return Multilibs.begin(); }
157 const_iterator begin() const { return Multilibs.begin(); }
159 iterator end() { return Multilibs.end(); }
160 const_iterator end() const { return Multilibs.end(); }
162 /// Pick the best multilib in the set, \returns false if none are compatible
163 bool select(const Multilib::flags_list &Flags, Multilib &M) const;
165 unsigned size() const { return Multilibs.size(); }
167 LLVM_DUMP_METHOD void dump() const;
168 void print(raw_ostream &OS) const;
170 MultilibSet &setIncludeDirsCallback(IncludeDirsFunc F) {
171 IncludeCallback = std::move(F);
175 const IncludeDirsFunc &includeDirsCallback() const { return IncludeCallback; }
177 MultilibSet &setFilePathsCallback(IncludeDirsFunc F) {
178 FilePathsCallback = std::move(F);
182 const IncludeDirsFunc &filePathsCallback() const { return FilePathsCallback; }
185 /// Apply the filter to Multilibs and return the subset that remains
186 static multilib_list filterCopy(FilterCallback F, const multilib_list &Ms);
188 /// Apply the filter to the multilib_list, removing those that don't match
189 static void filterInPlace(FilterCallback F, multilib_list &Ms);
192 raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
194 } // namespace driver
197 #endif // LLVM_CLANG_DRIVER_MULTILIB_H