]> granicus.if.org Git - clang/blob - include/clang/Driver/Multilib.h
0b64456d22aaa1ea82f0a5b67b785370712a4546
[clang] / include / clang / Driver / Multilib.h
1 //===--- Multilib.h ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef CLANG_LIB_DRIVER_MULTILIB_H_
11 #define CLANG_LIB_DRIVER_MULTILIB_H_
12
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Option/Option.h"
16
17 #include <functional>
18 #include <string>
19 #include <vector>
20
21 namespace clang {
22 namespace driver {
23
24 /// This corresponds to a single GCC Multilib, or a segment of one controlled
25 /// by a command line flag
26 class Multilib {
27 public:
28   typedef std::vector<std::string> flags_list;
29
30 private:
31   std::string GCCSuffix;
32   std::string OSSuffix;
33   std::string IncludeSuffix;
34   flags_list Flags;
35
36 public:
37   Multilib(StringRef GCCSuffix = "", StringRef OSSuffix = "",
38            StringRef IncludeSuffix = "");
39
40   /// \brief Get the detected GCC installation path suffix for the multi-arch
41   /// target variant. Always starts with a '/', unless empty
42   const std::string &gccSuffix() const {
43     assert(GCCSuffix.empty() ||
44            (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
45     return GCCSuffix;
46   }
47   /// Set the GCC installation path suffix.
48   Multilib &gccSuffix(StringRef S);
49
50   /// \brief Get the detected os path suffix for the multi-arch
51   /// target variant. Always starts with a '/', unless empty
52   const std::string &osSuffix() const {
53     assert(OSSuffix.empty() ||
54            (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
55     return OSSuffix;
56   }
57   /// Set the os path suffix.
58   Multilib &osSuffix(StringRef S);
59
60   /// \brief Get the include directory suffix. Always starts with a '/', unless
61   /// empty
62   const std::string &includeSuffix() const {
63     assert(IncludeSuffix.empty() ||
64            (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
65     return IncludeSuffix;
66   }
67   /// Set the include directory suffix
68   Multilib &includeSuffix(StringRef S);
69
70   /// \brief Get the flags that indicate or contraindicate this multilib's use
71   /// All elements begin with either '+' or '-'
72   const flags_list &flags() const { return Flags; }
73   flags_list &flags() { return Flags; }
74   /// Add a flag to the flags list
75   Multilib &flag(StringRef F) {
76     assert(F.front() == '+' || F.front() == '-');
77     Flags.push_back(F);
78     return *this;
79   }
80
81   /// \brief print summary of the Multilib
82   void print(raw_ostream &OS) const;
83
84   /// Check whether any of the 'against' flags contradict the 'for' flags.
85   bool isValid() const;
86
87   /// Check whether the default is selected
88   bool isDefault() const
89   { return GCCSuffix.empty() && OSSuffix.empty() && IncludeSuffix.empty(); }
90
91   bool operator==(const Multilib &Other) const;
92 };
93
94 raw_ostream &operator<<(raw_ostream &OS, const Multilib &M);
95
96 class MultilibSet {
97 public:
98   typedef std::vector<Multilib> multilib_list;
99   typedef multilib_list::iterator iterator;
100   typedef multilib_list::const_iterator const_iterator;
101
102   typedef std::function<std::vector<std::string>(
103       StringRef InstallDir, StringRef Triple, const Multilib &M)>
104   IncludeDirsFunc;
105
106   struct FilterCallback {
107     virtual ~FilterCallback() {};
108     /// \return true iff the filter should remove the Multilib from the set
109     virtual bool operator()(const Multilib &M) const = 0;
110   };
111
112 private:
113   multilib_list Multilibs;
114   IncludeDirsFunc IncludeCallback;
115
116 public:
117   MultilibSet() {}
118
119   /// Add an optional Multilib segment
120   MultilibSet &Maybe(const Multilib &M);
121
122   /// Add a set of mutually incompatible Multilib segments
123   MultilibSet &Either(const Multilib &M1, const Multilib &M2);
124   MultilibSet &Either(const Multilib &M1, const Multilib &M2,
125                       const Multilib &M3);
126   MultilibSet &Either(const Multilib &M1, const Multilib &M2,
127                       const Multilib &M3, const Multilib &M4);
128   MultilibSet &Either(const Multilib &M1, const Multilib &M2,
129                       const Multilib &M3, const Multilib &M4,
130                       const Multilib &M5);
131   MultilibSet &Either(const std::vector<Multilib> &Ms);
132
133   /// Filter out some subset of the Multilibs using a user defined callback
134   MultilibSet &FilterOut(const FilterCallback &F);
135   /// Filter out those Multilibs whose gccSuffix matches the given expression
136   MultilibSet &FilterOut(std::string Regex);
137
138   /// Add a completed Multilib to the set
139   void push_back(const Multilib &M);
140
141   /// Union this set of multilibs with another
142   void combineWith(const MultilibSet &MS);
143
144   /// Remove all of thie multilibs from the set
145   void clear() { Multilibs.clear(); }
146
147   iterator begin() { return Multilibs.begin(); }
148   const_iterator begin() const { return Multilibs.begin(); }
149
150   iterator end() { return Multilibs.end(); }
151   const_iterator end() const { return Multilibs.end(); }
152
153   /// Pick the best multilib in the set, \returns false if none are compatible
154   bool select(const Multilib::flags_list &Flags, Multilib &M) const;
155
156   unsigned size() const { return Multilibs.size(); }
157
158   void print(raw_ostream &OS) const;
159
160   MultilibSet &setIncludeDirsCallback(IncludeDirsFunc F) {
161     IncludeCallback = F;
162     return *this;
163   }
164   IncludeDirsFunc includeDirsCallback() const { return IncludeCallback; }
165
166 private:
167   /// Apply the filter to Multilibs and return the subset that remains
168   static multilib_list filterCopy(const FilterCallback &F,
169                                   const multilib_list &Ms);
170
171   /// Apply the filter to the multilib_list, removing those that don't match
172   static void filterInPlace(const FilterCallback &F, multilib_list &Ms);
173 };
174
175 raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
176 }
177 }
178
179 #endif
180