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/ADT/StringSet.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/SourceMgr.h"
28 /// This corresponds to a single GCC Multilib, or a segment of one controlled
29 /// by a command line flag.
30 /// See also MultilibBuilder for building a multilib by mutating it
34 using flags_list = std::vector<std::string>;
37 std::string GCCSuffix;
39 std::string IncludeSuffix;
43 /// GCCSuffix, OSSuffix & IncludeSuffix will be appended directly to the
44 /// sysroot string so they must either be empty or begin with a '/' character.
45 /// This is enforced with an assert in the constructor.
46 Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
47 StringRef IncludeSuffix = {},
48 const flags_list &Flags = flags_list());
50 /// Get the detected GCC installation path suffix for the multi-arch
51 /// target variant. Always starts with a '/', unless empty
52 const std::string &gccSuffix() const { return GCCSuffix; }
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 { return OSSuffix; }
58 /// Get the include directory suffix. Always starts with a '/', unless
60 const std::string &includeSuffix() const { return IncludeSuffix; }
62 /// Get the flags that indicate or contraindicate this multilib's use
63 /// All elements begin with either '-' or '!'
64 const flags_list &flags() const { return Flags; }
66 LLVM_DUMP_METHOD void dump() const;
67 /// print summary of the Multilib
68 void print(raw_ostream &OS) const;
70 /// Check whether the default is selected
71 bool isDefault() const
72 { return GCCSuffix.empty() && OSSuffix.empty() && IncludeSuffix.empty(); }
74 bool operator==(const Multilib &Other) const;
77 raw_ostream &operator<<(raw_ostream &OS, const Multilib &M);
79 /// See also MultilibSetBuilder for combining multilibs into a set.
82 using multilib_list = std::vector<Multilib>;
83 using const_iterator = multilib_list::const_iterator;
84 using IncludeDirsFunc =
85 std::function<std::vector<std::string>(const Multilib &M)>;
86 using FilterCallback = llvm::function_ref<bool(const Multilib &)>;
88 /// Uses regular expressions to simplify flags used for multilib selection.
89 /// For example, we may wish both -mfloat-abi=soft and -mfloat-abi=softfp to
90 /// be treated as -mfloat-abi=soft.
93 std::vector<std::string> Flags;
97 multilib_list Multilibs;
98 std::vector<FlagMatcher> FlagMatchers;
99 IncludeDirsFunc IncludeCallback;
100 IncludeDirsFunc FilePathsCallback;
103 MultilibSet() = default;
104 MultilibSet(multilib_list &&Multilibs,
105 std::vector<FlagMatcher> &&FlagMatchers = {})
106 : Multilibs(Multilibs), FlagMatchers(FlagMatchers) {}
108 const multilib_list &getMultilibs() { return Multilibs; }
110 /// Filter out some subset of the Multilibs using a user defined callback
111 MultilibSet &FilterOut(FilterCallback F);
113 /// Add a completed Multilib to the set
114 void push_back(const Multilib &M);
116 const_iterator begin() const { return Multilibs.begin(); }
117 const_iterator end() const { return Multilibs.end(); }
119 /// Select compatible variants, \returns false if none are compatible
120 bool select(const Multilib::flags_list &Flags,
121 llvm::SmallVector<Multilib> &) const;
123 unsigned size() const { return Multilibs.size(); }
125 /// Get the given flags plus flags found by matching them against the
126 /// FlagMatchers and choosing the Flags of each accordingly. The select method
127 /// calls this method so in most cases it's not necessary to call it directly.
128 llvm::StringSet<> expandFlags(const Multilib::flags_list &) const;
130 LLVM_DUMP_METHOD void dump() const;
131 void print(raw_ostream &OS) const;
133 MultilibSet &setIncludeDirsCallback(IncludeDirsFunc F) {
134 IncludeCallback = std::move(F);
138 const IncludeDirsFunc &includeDirsCallback() const { return IncludeCallback; }
140 MultilibSet &setFilePathsCallback(IncludeDirsFunc F) {
141 FilePathsCallback = std::move(F);
145 const IncludeDirsFunc &filePathsCallback() const { return FilePathsCallback; }
147 static llvm::ErrorOr<MultilibSet>
148 parseYaml(llvm::MemoryBufferRef, llvm::SourceMgr::DiagHandlerTy = nullptr,
149 void *DiagHandlerCtxt = nullptr);
152 raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
154 } // namespace driver
157 #endif // LLVM_CLANG_DRIVER_MULTILIB_H