Multilib.cpp 8.3 KB

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