Multilib.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //===--- Multilib.cpp - Multilib Implementation ---------------------------===//
  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. #include "clang/Driver/Multilib.h"
  10. #include "Tools.h"
  11. #include "clang/Driver/Options.h"
  12. #include "llvm/ADT/StringMap.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/ADT/StringSet.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/Option/Arg.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Option/OptTable.h"
  19. #include "llvm/Option/Option.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/Regex.h"
  23. #include "llvm/Support/YAMLParser.h"
  24. #include "llvm/Support/YAMLTraits.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <algorithm>
  27. using namespace clang::driver;
  28. using namespace clang;
  29. using namespace llvm::opt;
  30. using namespace llvm::sys;
  31. /// normalize Segment to "/foo/bar" or "".
  32. static void normalizePathSegment(std::string &Segment) {
  33. StringRef seg = Segment;
  34. // Prune trailing "/" or "./"
  35. while (1) {
  36. StringRef last = path::filename(seg);
  37. if (last != ".")
  38. break;
  39. seg = path::parent_path(seg);
  40. }
  41. if (seg.empty() || seg == "/") {
  42. Segment = "";
  43. return;
  44. }
  45. // Add leading '/'
  46. if (seg.front() != '/') {
  47. Segment = "/" + seg.str();
  48. } else {
  49. Segment = seg;
  50. }
  51. }
  52. Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
  53. StringRef IncludeSuffix)
  54. : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix) {
  55. normalizePathSegment(this->GCCSuffix);
  56. normalizePathSegment(this->OSSuffix);
  57. normalizePathSegment(this->IncludeSuffix);
  58. }
  59. Multilib &Multilib::gccSuffix(StringRef S) {
  60. GCCSuffix = S;
  61. normalizePathSegment(GCCSuffix);
  62. return *this;
  63. }
  64. Multilib &Multilib::osSuffix(StringRef S) {
  65. OSSuffix = S;
  66. normalizePathSegment(OSSuffix);
  67. return *this;
  68. }
  69. Multilib &Multilib::includeSuffix(StringRef S) {
  70. IncludeSuffix = S;
  71. normalizePathSegment(IncludeSuffix);
  72. return *this;
  73. }
  74. void Multilib::print(raw_ostream &OS) const {
  75. assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
  76. if (GCCSuffix.empty())
  77. OS << ".";
  78. else {
  79. OS << StringRef(GCCSuffix).drop_front();
  80. }
  81. OS << ";";
  82. for (StringRef Flag : Flags) {
  83. if (Flag.front() == '+')
  84. OS << "@" << Flag.substr(1);
  85. }
  86. }
  87. bool Multilib::isValid() const {
  88. llvm::StringMap<int> FlagSet;
  89. for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
  90. StringRef Flag(Flags[I]);
  91. llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
  92. assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
  93. if (SI == FlagSet.end())
  94. FlagSet[Flag.substr(1)] = I;
  95. else if (Flags[I] != Flags[SI->getValue()])
  96. return false;
  97. }
  98. return true;
  99. }
  100. bool Multilib::operator==(const Multilib &Other) const {
  101. // Check whether the flags sets match
  102. // allowing for the match to be order invariant
  103. llvm::StringSet<> MyFlags;
  104. for (const auto &Flag : Flags)
  105. MyFlags.insert(Flag);
  106. for (const auto &Flag : Other.Flags)
  107. if (MyFlags.find(Flag) == MyFlags.end())
  108. return false;
  109. if (osSuffix() != Other.osSuffix())
  110. return false;
  111. if (gccSuffix() != Other.gccSuffix())
  112. return false;
  113. if (includeSuffix() != Other.includeSuffix())
  114. return false;
  115. return true;
  116. }
  117. raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
  118. M.print(OS);
  119. return OS;
  120. }
  121. MultilibSet &MultilibSet::Maybe(const Multilib &M) {
  122. Multilib Opposite;
  123. // Negate any '+' flags
  124. for (StringRef Flag : M.flags()) {
  125. if (Flag.front() == '+')
  126. Opposite.flags().push_back(("-" + Flag.substr(1)).str());
  127. }
  128. return Either(M, Opposite);
  129. }
  130. MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
  131. return Either({M1, M2});
  132. }
  133. MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
  134. const Multilib &M3) {
  135. return Either({M1, M2, M3});
  136. }
  137. MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
  138. const Multilib &M3, const Multilib &M4) {
  139. return Either({M1, M2, M3, M4});
  140. }
  141. MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
  142. const Multilib &M3, const Multilib &M4,
  143. const Multilib &M5) {
  144. return Either({M1, M2, M3, M4, M5});
  145. }
  146. static Multilib compose(const Multilib &Base, const Multilib &New) {
  147. SmallString<128> GCCSuffix;
  148. llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
  149. SmallString<128> OSSuffix;
  150. llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
  151. SmallString<128> IncludeSuffix;
  152. llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
  153. New.includeSuffix());
  154. Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
  155. Multilib::flags_list &Flags = Composed.flags();
  156. Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
  157. Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
  158. return Composed;
  159. }
  160. MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
  161. multilib_list Composed;
  162. if (Multilibs.empty())
  163. Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
  164. MultilibSegments.end());
  165. else {
  166. for (const Multilib &New : MultilibSegments) {
  167. for (const Multilib &Base : *this) {
  168. Multilib MO = compose(Base, New);
  169. if (MO.isValid())
  170. Composed.push_back(MO);
  171. }
  172. }
  173. Multilibs = Composed;
  174. }
  175. return *this;
  176. }
  177. MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
  178. filterInPlace(F, Multilibs);
  179. return *this;
  180. }
  181. MultilibSet &MultilibSet::FilterOut(const char *Regex) {
  182. llvm::Regex R(Regex);
  183. #ifndef NDEBUG
  184. std::string Error;
  185. if (!R.isValid(Error)) {
  186. llvm::errs() << Error;
  187. llvm_unreachable("Invalid regex!");
  188. }
  189. #endif
  190. filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
  191. Multilibs);
  192. return *this;
  193. }
  194. void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
  195. void MultilibSet::combineWith(const MultilibSet &Other) {
  196. Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
  197. }
  198. static bool isFlagEnabled(StringRef Flag) {
  199. char Indicator = Flag.front();
  200. assert(Indicator == '+' || Indicator == '-');
  201. return Indicator == '+';
  202. }
  203. bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
  204. llvm::StringMap<bool> FlagSet;
  205. // Stuff all of the flags into the FlagSet such that a true mappend indicates
  206. // the flag was enabled, and a false mappend indicates the flag was disabled.
  207. for (StringRef Flag : Flags)
  208. FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
  209. multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
  210. for (StringRef Flag : M.flags()) {
  211. llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
  212. if (SI != FlagSet.end())
  213. if (SI->getValue() != isFlagEnabled(Flag))
  214. return true;
  215. }
  216. return false;
  217. }, Multilibs);
  218. if (Filtered.size() == 0) {
  219. return false;
  220. } else if (Filtered.size() == 1) {
  221. M = Filtered[0];
  222. return true;
  223. }
  224. // TODO: pick the "best" multlib when more than one is suitable
  225. assert(false);
  226. return false;
  227. }
  228. void MultilibSet::print(raw_ostream &OS) const {
  229. for (const Multilib &M : *this)
  230. OS << M << "\n";
  231. }
  232. MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
  233. const multilib_list &Ms) {
  234. multilib_list Copy(Ms);
  235. filterInPlace(F, Copy);
  236. return Copy;
  237. }
  238. void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
  239. Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
  240. }
  241. raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
  242. MS.print(OS);
  243. return OS;
  244. }