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 9 #include "clang/Driver/Multilib.h" 10 #include "clang/Basic/LLVM.h" 11 #include "clang/Basic/Version.h" 12 #include "clang/Driver/Driver.h" 13 #include "llvm/ADT/DenseSet.h" 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Support/Compiler.h" 17 #include "llvm/Support/Error.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/Path.h" 20 #include "llvm/Support/Regex.h" 21 #include "llvm/Support/VersionTuple.h" 22 #include "llvm/Support/YAMLParser.h" 23 #include "llvm/Support/YAMLTraits.h" 24 #include "llvm/Support/raw_ostream.h" 25 #include <algorithm> 26 #include <cassert> 27 #include <string> 28 29 using namespace clang; 30 using namespace driver; 31 using namespace llvm::sys; 32 33 Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix, 34 StringRef IncludeSuffix, const flags_list &Flags, 35 StringRef ExclusiveGroup, std::optional<StringRef> Error) 36 : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix), 37 Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) { 38 assert(GCCSuffix.empty() || 39 (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1)); 40 assert(OSSuffix.empty() || 41 (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1)); 42 assert(IncludeSuffix.empty() || 43 (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1)); 44 } 45 46 LLVM_DUMP_METHOD void Multilib::dump() const { 47 print(llvm::errs()); 48 } 49 50 void Multilib::print(raw_ostream &OS) const { 51 if (GCCSuffix.empty()) 52 OS << "."; 53 else { 54 OS << StringRef(GCCSuffix).drop_front(); 55 } 56 OS << ";"; 57 for (StringRef Flag : Flags) { 58 if (Flag.front() == '-') 59 OS << "@" << Flag.substr(1); 60 } 61 } 62 63 bool Multilib::operator==(const Multilib &Other) const { 64 // Check whether the flags sets match 65 // allowing for the match to be order invariant 66 llvm::StringSet<> MyFlags; 67 for (const auto &Flag : Flags) 68 MyFlags.insert(Flag); 69 70 for (const auto &Flag : Other.Flags) 71 if (!MyFlags.contains(Flag)) 72 return false; 73 74 if (osSuffix() != Other.osSuffix()) 75 return false; 76 77 if (gccSuffix() != Other.gccSuffix()) 78 return false; 79 80 if (includeSuffix() != Other.includeSuffix()) 81 return false; 82 83 return true; 84 } 85 86 raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) { 87 M.print(OS); 88 return OS; 89 } 90 91 MultilibSet &MultilibSet::FilterOut(FilterCallback F) { 92 llvm::erase_if(Multilibs, F); 93 return *this; 94 } 95 96 void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); } 97 98 bool MultilibSet::select(const Driver &D, const Multilib::flags_list &Flags, 99 llvm::SmallVectorImpl<Multilib> &Selected) const { 100 llvm::StringSet<> FlagSet(expandFlags(Flags)); 101 Selected.clear(); 102 103 // Decide which multilibs we're going to select at all. 104 llvm::DenseSet<StringRef> ExclusiveGroupsSelected; 105 for (const Multilib &M : llvm::reverse(Multilibs)) { 106 // If this multilib doesn't match all our flags, don't select it. 107 if (!llvm::all_of(M.flags(), [&FlagSet](const std::string &F) { 108 return FlagSet.contains(F); 109 })) 110 continue; 111 112 const std::string &group = M.exclusiveGroup(); 113 if (!group.empty()) { 114 // If this multilib has the same ExclusiveGroup as one we've already 115 // selected, skip it. We're iterating in reverse order, so the group 116 // member we've selected already is preferred. 117 // 118 // Otherwise, add the group name to the set of groups we've already 119 // selected a member of. 120 auto [It, Inserted] = ExclusiveGroupsSelected.insert(group); 121 if (!Inserted) 122 continue; 123 } 124 125 // If this multilib is actually a placeholder containing an error message 126 // written by the multilib.yaml author, display that error message, and 127 // return failure. 128 if (M.isError()) { 129 D.Diag(clang::diag::err_drv_multilib_custom_error) << M.getErrorMessage(); 130 return false; 131 } 132 133 // Select this multilib. 134 Selected.push_back(M); 135 } 136 137 // We iterated in reverse order, so now put Selected back the right way 138 // round. 139 std::reverse(Selected.begin(), Selected.end()); 140 141 return !Selected.empty(); 142 } 143 144 llvm::StringSet<> 145 MultilibSet::expandFlags(const Multilib::flags_list &InFlags) const { 146 llvm::StringSet<> Result; 147 for (const auto &F : InFlags) 148 Result.insert(F); 149 for (const FlagMatcher &M : FlagMatchers) { 150 std::string RegexString(M.Match); 151 152 // Make the regular expression match the whole string. 153 if (!StringRef(M.Match).starts_with("^")) 154 RegexString.insert(RegexString.begin(), '^'); 155 if (!StringRef(M.Match).ends_with("$")) 156 RegexString.push_back('$'); 157 158 const llvm::Regex Regex(RegexString); 159 assert(Regex.isValid()); 160 if (llvm::any_of(InFlags, 161 [&Regex](StringRef F) { return Regex.match(F); })) { 162 Result.insert(M.Flags.begin(), M.Flags.end()); 163 } 164 } 165 return Result; 166 } 167 168 namespace { 169 170 // When updating this also update MULTILIB_VERSION in MultilibTest.cpp 171 static const VersionTuple MultilibVersionCurrent(1, 0); 172 173 struct MultilibSerialization { 174 std::string Dir; // if this record successfully selects a library dir 175 std::string Error; // if this record reports a fatal error message 176 std::vector<std::string> Flags; 177 std::string Group; 178 }; 179 180 enum class MultilibGroupType { 181 /* 182 * The only group type currently supported is 'Exclusive', which indicates a 183 * group of multilibs of which at most one may be selected. 184 */ 185 Exclusive, 186 187 /* 188 * Future possibility: a second group type indicating a set of library 189 * directories that are mutually _dependent_ rather than mutually exclusive: 190 * if you include one you must include them all. 191 * 192 * It might also be useful to allow groups to be members of other groups, so 193 * that a mutually exclusive group could contain a mutually dependent set of 194 * library directories, or vice versa. 195 * 196 * These additional features would need changes in the implementation, but 197 * the YAML schema is set up so they can be added without requiring changes 198 * in existing users' multilib.yaml files. 199 */ 200 }; 201 202 struct MultilibGroupSerialization { 203 std::string Name; 204 MultilibGroupType Type; 205 }; 206 207 struct MultilibSetSerialization { 208 llvm::VersionTuple MultilibVersion; 209 std::vector<MultilibGroupSerialization> Groups; 210 std::vector<MultilibSerialization> Multilibs; 211 std::vector<MultilibSet::FlagMatcher> FlagMatchers; 212 }; 213 214 } // end anonymous namespace 215 216 template <> struct llvm::yaml::MappingTraits<MultilibSerialization> { 217 static void mapping(llvm::yaml::IO &io, MultilibSerialization &V) { 218 io.mapOptional("Dir", V.Dir); 219 io.mapOptional("Error", V.Error); 220 io.mapRequired("Flags", V.Flags); 221 io.mapOptional("Group", V.Group); 222 } 223 static std::string validate(IO &io, MultilibSerialization &V) { 224 if (V.Dir.empty() && V.Error.empty()) 225 return "one of the 'Dir' and 'Error' keys must be specified"; 226 if (!V.Dir.empty() && !V.Error.empty()) 227 return "the 'Dir' and 'Error' keys may not both be specified"; 228 if (StringRef(V.Dir).starts_with("/")) 229 return "paths must be relative but \"" + V.Dir + "\" starts with \"/\""; 230 return std::string{}; 231 } 232 }; 233 234 template <> struct llvm::yaml::ScalarEnumerationTraits<MultilibGroupType> { 235 static void enumeration(IO &io, MultilibGroupType &Val) { 236 io.enumCase(Val, "Exclusive", MultilibGroupType::Exclusive); 237 } 238 }; 239 240 template <> struct llvm::yaml::MappingTraits<MultilibGroupSerialization> { 241 static void mapping(llvm::yaml::IO &io, MultilibGroupSerialization &V) { 242 io.mapRequired("Name", V.Name); 243 io.mapRequired("Type", V.Type); 244 } 245 }; 246 247 template <> struct llvm::yaml::MappingTraits<MultilibSet::FlagMatcher> { 248 static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M) { 249 io.mapRequired("Match", M.Match); 250 io.mapRequired("Flags", M.Flags); 251 } 252 static std::string validate(IO &io, MultilibSet::FlagMatcher &M) { 253 llvm::Regex Regex(M.Match); 254 std::string RegexError; 255 if (!Regex.isValid(RegexError)) 256 return RegexError; 257 if (M.Flags.empty()) 258 return "value required for 'Flags'"; 259 return std::string{}; 260 } 261 }; 262 263 template <> struct llvm::yaml::MappingTraits<MultilibSetSerialization> { 264 static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M) { 265 io.mapRequired("MultilibVersion", M.MultilibVersion); 266 io.mapRequired("Variants", M.Multilibs); 267 io.mapOptional("Groups", M.Groups); 268 io.mapOptional("Mappings", M.FlagMatchers); 269 } 270 static std::string validate(IO &io, MultilibSetSerialization &M) { 271 if (M.MultilibVersion.empty()) 272 return "missing required key 'MultilibVersion'"; 273 if (M.MultilibVersion.getMajor() != MultilibVersionCurrent.getMajor()) 274 return "multilib version " + M.MultilibVersion.getAsString() + 275 " is unsupported"; 276 if (M.MultilibVersion.getMinor() > MultilibVersionCurrent.getMinor()) 277 return "multilib version " + M.MultilibVersion.getAsString() + 278 " is unsupported"; 279 for (const MultilibSerialization &Lib : M.Multilibs) { 280 if (!Lib.Group.empty()) { 281 bool Found = false; 282 for (const MultilibGroupSerialization &Group : M.Groups) 283 if (Group.Name == Lib.Group) { 284 Found = true; 285 break; 286 } 287 if (!Found) 288 return "multilib \"" + Lib.Dir + 289 "\" specifies undefined group name \"" + Lib.Group + "\""; 290 } 291 } 292 return std::string{}; 293 } 294 }; 295 296 LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSerialization) 297 LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibGroupSerialization) 298 LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSet::FlagMatcher) 299 300 llvm::ErrorOr<MultilibSet> 301 MultilibSet::parseYaml(llvm::MemoryBufferRef Input, 302 llvm::SourceMgr::DiagHandlerTy DiagHandler, 303 void *DiagHandlerCtxt) { 304 MultilibSetSerialization MS; 305 llvm::yaml::Input YamlInput(Input, nullptr, DiagHandler, DiagHandlerCtxt); 306 YamlInput >> MS; 307 if (YamlInput.error()) 308 return YamlInput.error(); 309 310 multilib_list Multilibs; 311 Multilibs.reserve(MS.Multilibs.size()); 312 for (const auto &M : MS.Multilibs) { 313 if (!M.Error.empty()) { 314 Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error); 315 } else { 316 std::string Dir; 317 if (M.Dir != ".") 318 Dir = "/" + M.Dir; 319 // We transfer M.Group straight into the ExclusiveGroup parameter for the 320 // Multilib constructor. If we later support more than one type of group, 321 // we'll have to look up the group name in MS.Groups, check its type, and 322 // decide what to do here. 323 Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group); 324 } 325 } 326 327 return MultilibSet(std::move(Multilibs), std::move(MS.FlagMatchers)); 328 } 329 330 LLVM_DUMP_METHOD void MultilibSet::dump() const { 331 print(llvm::errs()); 332 } 333 334 void MultilibSet::print(raw_ostream &OS) const { 335 for (const auto &M : *this) 336 OS << M << "\n"; 337 } 338 339 raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) { 340 MS.print(OS); 341 return OS; 342 } 343