Index: include/clang/Driver/Multilib.h =================================================================== --- include/clang/Driver/Multilib.h +++ include/clang/Driver/Multilib.h @@ -0,0 +1,154 @@ +//===--- Multilib.h ---------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef CLANG_LIB_DRIVER_MULTILIB_H_ +#define CLANG_LIB_DRIVER_MULTILIB_H_ + +#include "clang/Basic/LLVM.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Option/Option.h" +#include +#include + +namespace clang { +namespace driver { + +/// This corresponds to a single GCC Multilib, or a segment of one controlled +/// by a command line flag +class Multilib { +public: + typedef std::vector flags_list; + +private: + std::string GCCSuffix; + std::string OSSuffix; + std::string IncludeSuffix; + flags_list Flags; + +public: + Multilib(); + Multilib(std::string GCCSuffix); + Multilib(std::string GCCSuffix, std::string OSSuffix); + Multilib(std::string GCCSuffix, std::string OSSuffix, std::string IncludeSuffix); + + /// \brief Get the detected GCC installation path suffix for the multi-arch + /// target variant. + const std::string &gccSuffix() const { return GCCSuffix; } + std::string &gccSuffix() { return GCCSuffix; } + /// Set the GCC installation path suffix + Multilib &gccSuffix(std::string S) { GCCSuffix = S; return *this; } + + /// \brief Get the detected os path suffix for the multi-arch + /// target variant. + const std::string &osSuffix() const { return OSSuffix; } + std::string &osSuffix() { return OSSuffix; } + /// Set the os path suffix + Multilib &osSuffix(std::string S) { OSSuffix = S; return *this; } + + /// \brief Get the flags that indicate or contraindicate this multilib's use + const flags_list &flags() const { return Flags; } + flags_list &flags() { return Flags; } + /// Add a flag to the flags list + Multilib &flag(std::string F) { Flags.push_back(F); return *this; } + + /// \brief Get the include directory suffix + const std::string &includeSuffix() const { return IncludeSuffix; } + std::string &includeSuffix() { return IncludeSuffix; } + /// Set the include directory suffix + Multilib &includeSuffix(std::string S) { IncludeSuffix = S; return *this; } + + /// \brief print summary of the Multilib + void print(raw_ostream &OS) const; + + /// Check whether any of the 'against' flags contradict the 'for' flags. + bool isValid() const; + + bool operator== (Multilib &Other) const; +}; + +raw_ostream &operator<< (raw_ostream &OS, const Multilib &M); + +class MultilibSet { +public: + typedef std::vector multilib_list; + typedef multilib_list::iterator iterator; + typedef multilib_list::const_iterator const_iterator; + + struct FilterCallback { + /// \return true iff the filter should remove the Multilib from the set + virtual bool operator() (const Multilib &M) const = 0; + }; + +private: + multilib_list Multilibs; + +public: + MultilibSet(); + + /// Add an optional Multilib segment + MultilibSet &Maybe(const Multilib &M); + + /// Add a set of mutually incompatible Multilib segments + MultilibSet &Either(const Multilib &M1, + const Multilib &M2); + MultilibSet &Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3); + MultilibSet &Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3, + const Multilib &M4); + MultilibSet &Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3, + const Multilib &M4, + const Multilib &M5); + MultilibSet &Either(const std::vector &Ms); + + /// Filter out some subset of the Multilibs using a user defined callback + MultilibSet &FilterOut(const FilterCallback &F); + /// Filter out those Multilibs whose gccSuffix matches the given expression + MultilibSet &FilterOut(std::string Regex); + + /// Add a completed Multilib to the set + void push_back(const Multilib &M); + + /// Union this set of multilibs with another + void combineWith(const MultilibSet &MS); + + /// Remove all of thie multilibs from the set + void clear() { Multilibs.clear(); } + + iterator begin() { return Multilibs.begin(); } + const_iterator begin() const { return Multilibs.begin(); } + + iterator end() { return Multilibs.end(); } + const_iterator end() const { return Multilibs.end(); } + + /// Pick the best multilib in the set, \returns false if none are compatible + bool select(const llvm::Triple &TargetTriple, + const llvm::opt::ArgList &Args, + Multilib &M) const; + + unsigned size() const { return Multilibs.size(); } +private: + + /// Apply the filter to Multilibs and return the subset that remains + static multilib_list filterCopy(const FilterCallback &F, + const multilib_list &Ms); + + /// Apply the filter to the multilib_list, removing those that don't match + static void filterInPlace(const FilterCallback &F, multilib_list &Ms); +}; + +} +} + +#endif + Index: include/clang/Driver/ToolChain.h =================================================================== --- include/clang/Driver/ToolChain.h +++ include/clang/Driver/ToolChain.h @@ -11,6 +11,7 @@ #define CLANG_DRIVER_TOOLCHAIN_H_ #include "clang/Driver/Action.h" +#include "clang/Driver/Multilib.h" #include "clang/Driver/Types.h" #include "clang/Driver/Util.h" #include "llvm/ADT/OwningPtr.h" @@ -76,6 +77,8 @@ mutable OwningPtr SanitizerArguments; protected: + MultilibSet Multilibs; + ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args); @@ -130,6 +133,8 @@ path_list &getProgramPaths() { return ProgramPaths; } const path_list &getProgramPaths() const { return ProgramPaths; } + const MultilibSet &getMultilibs() const { return Multilibs; } + const SanitizerArgs& getSanitizerArgs() const; // Tool access. Index: lib/Driver/ArgParse.h =================================================================== --- lib/Driver/ArgParse.h +++ lib/Driver/ArgParse.h @@ -0,0 +1,53 @@ +//===--- ArgParse.h - Argument Parsing Utilities ----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Triple.h" + +namespace llvm { +namespace opt { + class ArgList; +} +} + +namespace clang { +namespace driver { + +bool isMipsArch(llvm::Triple::ArchType Arch); + +bool isMips32(llvm::Triple::ArchType Arch); + +bool isMips64(llvm::Triple::ArchType Arch); + +bool isMipsEL(llvm::Triple::ArchType Arch); + +bool isMipsEB(llvm::Triple::ArchType Arch); + +bool isMips16(const llvm::opt::ArgList &Args); + +bool isMips32r2(const llvm::opt::ArgList &Args); + +bool isMips64r2(const llvm::opt::ArgList &Args); + +bool isMicroMips(const llvm::opt::ArgList &Args); + +bool isMipsFP64(const llvm::opt::ArgList &Args); + +bool isMipsNan2008(const llvm::opt::ArgList &Args); + +bool hasMipsN32ABIArg(const llvm::opt::ArgList &Args); + +bool hasMips64ABIArg(const llvm::opt::ArgList &Args); + +bool isSoftFloatABI(const llvm::opt::ArgList &Args); + +llvm::StringRef getGCCToolchainDir(const llvm::opt::ArgList &Args); + +} // end namespace driver +} // end namespace clang Index: lib/Driver/ArgParse.cpp =================================================================== --- lib/Driver/ArgParse.cpp +++ lib/Driver/ArgParse.cpp @@ -0,0 +1,118 @@ +//===--- ArgParse.cpp - Argument Parsing Implementations ------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ArgParse.h" +#include "clang/Basic/LLVM.h" +#include "clang/Driver/Options.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" + +// FIXME: This needs to be listed last until we fix the broken include guards +// in these files and the LLVM config.h files. +#include "clang/Config/config.h" // for GCC_INSTALL_PREFIX + +using namespace clang; +using namespace clang::driver; +using namespace llvm::opt; + +namespace clang { +namespace driver { + +bool isMipsArch(llvm::Triple::ArchType Arch) { + return Arch == llvm::Triple::mips || + Arch == llvm::Triple::mipsel || + Arch == llvm::Triple::mips64 || + Arch == llvm::Triple::mips64el; +} + +bool isMips32(llvm::Triple::ArchType Arch) { + return Arch == llvm::Triple::mips || + Arch == llvm::Triple::mipsel; +} + +bool isMips64(llvm::Triple::ArchType Arch) { + return Arch == llvm::Triple::mips64 || + Arch == llvm::Triple::mips64el; +} + +bool isMipsEL(llvm::Triple::ArchType Arch) { + return Arch == llvm::Triple::mipsel || + Arch == llvm::Triple::mips64el; +} + +bool isMipsEB(llvm::Triple::ArchType Arch) { + return Arch == llvm::Triple::mips || + Arch == llvm::Triple::mips64; +} + +bool isMips16(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mips16, + options::OPT_mno_mips16); + return A && A->getOption().matches(options::OPT_mips16); +} + +bool isMips32r2(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_march_EQ, + options::OPT_mcpu_EQ); + return A && A->getValue() == StringRef("mips32r2"); +} + +bool isMips64r2(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_march_EQ, + options::OPT_mcpu_EQ); + + return A && A->getValue() == StringRef("mips64r2"); +} + +bool isMicroMips(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mmicromips, + options::OPT_mno_micromips); + return A && A->getOption().matches(options::OPT_mmicromips); +} + +bool isMipsFP64(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mfp64, options::OPT_mfp32); + return A && A->getOption().matches(options::OPT_mfp64); +} + +bool isMipsNan2008(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mnan_EQ); + return A && A->getValue() == StringRef("2008"); +} + +bool hasMipsN32ABIArg(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mabi_EQ); + return A && (A->getValue() == StringRef("n32")); +} + +bool hasMips64ABIArg(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_mabi_EQ); + return A && (A->getValue() == StringRef("64")); +} + +bool isSoftFloatABI(const ArgList &Args) { + Arg *A = Args.getLastArg(options::OPT_msoft_float, + options::OPT_mhard_float, + options::OPT_mfloat_abi_EQ); + if (!A) return false; + + return A->getOption().matches(options::OPT_msoft_float) || + (A->getOption().matches(options::OPT_mfloat_abi_EQ) && + A->getValue() == StringRef("soft")); +} + +llvm::StringRef getGCCToolchainDir(const ArgList &Args) { + const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain); + if (A) + return A->getValue(); + return GCC_INSTALL_PREFIX; +} + +} // end namespace driver +} // end namespace clang Index: lib/Driver/CMakeLists.txt =================================================================== --- lib/Driver/CMakeLists.txt +++ lib/Driver/CMakeLists.txt @@ -6,11 +6,13 @@ add_clang_library(clangDriver Action.cpp + ArgParse.cpp CC1AsOptions.cpp Compilation.cpp Driver.cpp DriverOptions.cpp Job.cpp + Multilib.cpp Phases.cpp SanitizerArgs.cpp Tool.cpp Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -747,51 +747,39 @@ } if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { - // FIXME: We need tool chain support for this. - llvm::outs() << ".;\n"; + const MultilibSet &Multilibs = TC.getMultilibs(); - switch (C.getDefaultToolChain().getTriple().getArch()) { - default: - break; + for (MultilibSet::const_iterator I = Multilibs.begin(), + E = Multilibs.end(); I != E; ++I) { + llvm::outs() << *I << "\n"; + } - case llvm::Triple::x86_64: - llvm::outs() << "x86_64;@m64" << "\n"; - break; + return false; + } - case llvm::Triple::ppc64: - llvm::outs() << "ppc64;@m64" << "\n"; - break; + if (C.getArgs().hasArg(options::OPT_print_multi_directory)) { - case llvm::Triple::ppc64le: - llvm::outs() << "ppc64le;@m64" << "\n"; - break; + const MultilibSet &Multilibs = TC.getMultilibs(); + + for (MultilibSet::const_iterator I = Multilibs.begin(), + E = Multilibs.end(); I != E; ++I) { + if (I->gccSuffix().empty()) + llvm::outs() << ".\n"; + else { + StringRef Suffix(I->gccSuffix()); + assert(Suffix.front() == '/'); + llvm::outs() << Suffix.substr(1) << "\n"; + } } + return false; } - // FIXME: What is the difference between print-multi-directory and - // print-multi-os-directory? - if (C.getArgs().hasArg(options::OPT_print_multi_directory) || - C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { - switch (C.getDefaultToolChain().getTriple().getArch()) { - default: - case llvm::Triple::x86: - case llvm::Triple::ppc: - llvm::outs() << "." << "\n"; - break; - - case llvm::Triple::x86_64: - llvm::outs() << "x86_64" << "\n"; - break; - - case llvm::Triple::ppc64: - llvm::outs() << "ppc64" << "\n"; - break; + if (C.getArgs().hasArg(options::OPT_print_multi_os_directory)) { + // This should print out "lib/../lib", "lib/../lib64", or "lib/../lib32" + // as appropriate for the toolchain. For now, print nothing because it's + // not supported yet. - case llvm::Triple::ppc64le: - llvm::outs() << "ppc64le" << "\n"; - break; - } return false; } Index: lib/Driver/Multilib.cpp =================================================================== --- lib/Driver/Multilib.cpp +++ lib/Driver/Multilib.cpp @@ -0,0 +1,378 @@ +//===--- Multilib.cpp - Multilib Implementation ---------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Driver/Multilib.h" +#include "ArgParse.h" +#include "Tools.h" +#include "clang/Driver/Options.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/OptTable.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Regex.h" +#include "llvm/Support/YAMLParser.h" +#include "llvm/Support/YAMLTraits.h" +#include + +using namespace clang::driver; +using namespace clang; +using namespace llvm::opt; + +Multilib::Multilib() { +} + +Multilib::Multilib(std::string GCCSuffix) + : GCCSuffix(GCCSuffix) { +} + +Multilib::Multilib(std::string GCCSuffix, std::string OSSuffix) + : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix) { +} + +Multilib::Multilib(std::string GCCSuffix, std::string OSSuffix, + std::string IncludeSuffix) + : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix) { +} + +void Multilib::print(raw_ostream &OS) const { + StringRef Suffix(GCCSuffix); + if (GCCSuffix.empty()) + OS << "."; + else { + assert(Suffix.front() == '/'); + OS << Suffix.substr(1); + } + + OS << ";"; + + for (flags_list::const_iterator I = Flags.begin(), E = Flags.end(); + I != E; ++I) { + if (StringRef(*I).startswith("+")) + OS << "@" << I->substr(1); + } +} + +bool Multilib::isValid() const { + llvm::StringMap FlagSet; + for (unsigned I = 0, N = Flags.size(); I != N; ++I) { + StringRef Flag(Flags[I]); + llvm::StringMap::iterator SI = FlagSet.find(Flag.substr(1)); + + assert(Flag.front() == '+' || Flag.front() == '-'); + + if (SI == FlagSet.end()) + FlagSet[Flag.substr(1)] = I; + else if (Flags[I] != Flags[SI->getValue()]) + return false; + } + return true; +} + +bool Multilib::operator== (Multilib &Other) const { + // Check whether the flags sets match + // allowing for the match to be order invariant + llvm::StringSet<> MyFlags; + for (flags_list::const_iterator + I = Flags.begin(), + E = Flags.end(); + I != E; ++I) { + MyFlags.insert(*I); + } + for (flags_list::const_iterator + I = Other.Flags.begin(), + E = Other.Flags.end(); + I != E; ++I) { + if (MyFlags.find(*I) == MyFlags.end()) + return false; + } + + if (osSuffix() != Other.osSuffix()) + return false; + + if (gccSuffix() != Other.gccSuffix()) + return false; + + return true; +} + +raw_ostream &clang::driver::operator<< (raw_ostream &OS, const Multilib &M) { + M.print(OS); + return OS; +} + +MultilibSet::MultilibSet() { +} + +MultilibSet &MultilibSet::Maybe(const Multilib &M) { + Multilib Opposite("",""); + // Negate any '+' flags + for (Multilib::flags_list::const_iterator I = M.flags().begin(), + E = M.flags().end(); I != E; ++I) { + StringRef Flag(*I); + if (Flag.front() == '+') + Opposite.flags().push_back(("-" + Flag.substr(1)).str()); + } + return Either(M, Opposite); +} + +MultilibSet &MultilibSet::Either(const Multilib &M1, + const Multilib &M2) { + std::vector Ms; + Ms.push_back(M1); + Ms.push_back(M2); + return Either(Ms); +} + +MultilibSet &MultilibSet::Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3) { + std::vector Ms; + Ms.push_back(M1); + Ms.push_back(M2); + Ms.push_back(M3); + return Either(Ms); +} + +MultilibSet &MultilibSet::Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3, + const Multilib &M4) { + std::vector Ms; + Ms.push_back(M1); + Ms.push_back(M2); + Ms.push_back(M3); + Ms.push_back(M4); + return Either(Ms); +} + +MultilibSet &MultilibSet::Either(const Multilib &M1, + const Multilib &M2, + const Multilib &M3, + const Multilib &M4, + const Multilib &M5) { + std::vector Ms; + Ms.push_back(M1); + Ms.push_back(M2); + Ms.push_back(M3); + Ms.push_back(M4); + Ms.push_back(M5); + return Either(Ms); +} + +static Multilib compose(const Multilib &Base, const Multilib &New) { + Multilib Composed(Base.gccSuffix() + New.gccSuffix(), + Base.osSuffix() + New.osSuffix(), + Base.includeSuffix() + New.includeSuffix()); + + Multilib::flags_list &Flags = Composed.flags(); + + Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end()); + Flags.insert(Flags.end(), New.flags().begin(), New.flags().end()); + + return Composed; +} + +MultilibSet &MultilibSet::Either( + const std::vector &MultilibSegments) { + multilib_list Composed; + + if (Multilibs.empty()) + Multilibs.insert(Multilibs.end(), MultilibSegments.begin(), + MultilibSegments.end()); + else { + for (std::vector::const_iterator + NewI = MultilibSegments.begin(), + NewE = MultilibSegments.end(); + NewI != NewE; ++NewI) { + for (const_iterator + BaseI = begin(), + BaseE = end(); + BaseI != BaseE; ++BaseI) { + Multilib MO = compose(*BaseI, *NewI); + if (MO.isValid()) + Composed.push_back(MO); + } + } + + Multilibs = Composed; + } + + return *this; +} + +MultilibSet &MultilibSet::FilterOut(const MultilibSet::FilterCallback &F) { + filterInPlace(F, Multilibs); + return *this; +} + +MultilibSet &MultilibSet::FilterOut(std::string Regex) { + class REFilter : public MultilibSet::FilterCallback { + mutable llvm::Regex R; + public: + REFilter(std::string Regex) : R(Regex) {} + bool operator() (const Multilib &M) const LLVM_OVERRIDE { + std::string Error; + if (!R.isValid(Error)) { + llvm::errs() << Error; + assert(false); + return false; + } + return R.match(M.gccSuffix()); + } + }; + + REFilter REF(Regex); + filterInPlace(REF, Multilibs); + return *this; +} + +void MultilibSet::push_back(const Multilib &M) { + Multilibs.push_back(M); +} + +void MultilibSet::combineWith(const MultilibSet &Other) { + Multilibs.insert(Multilibs.end(), Other.begin(), Other.end()); +} + +static void addMultilibFlag(bool Enabled, const char *const Flag, + std::vector &Flags) { + if (Enabled) + Flags.push_back(std::string("+") + Flag); + else + Flags.push_back(std::string("-") + Flag); +} + +static void getMipsMultilibFlags(llvm::Triple::ArchType TargetArch, + const llvm::opt::ArgList &Args, + std::vector &Flags) { + addMultilibFlag(isMips32(TargetArch), "m32", Flags); + addMultilibFlag(isMips64(TargetArch), "m64", Flags); + addMultilibFlag(isMips16(Args), "mips16", Flags); + addMultilibFlag(isMips32r2(Args), "march=mips32r2", Flags); + addMultilibFlag(isMips64r2(Args), "march=mips64r2", Flags); + addMultilibFlag(isMicroMips(Args), "mmicromips", Flags); + addMultilibFlag(isMipsFP64(Args), "mfp64", Flags); + addMultilibFlag(!isMipsFP64(Args), "mfp32", Flags); + addMultilibFlag(isMipsNan2008(Args), "mnan=2008", Flags); + addMultilibFlag(hasMipsN32ABIArg(Args), "mabi=n32", Flags); + // Default is to assume mabi=64 + bool IsMABI64 = hasMips64ABIArg(Args) || + (!hasMipsN32ABIArg(Args) && isMips64(TargetArch)); + addMultilibFlag(IsMABI64, "mabi=64", Flags); + addMultilibFlag(isSoftFloatABI(Args), "msoft-float", Flags); + addMultilibFlag(isSoftFloatABI(Args), "mfloat-abi=soft", Flags); + addMultilibFlag(!isSoftFloatABI(Args), "mhard-float", Flags); + addMultilibFlag(!isSoftFloatABI(Args), "mfloat-abi=hard", Flags); + addMultilibFlag(isMipsEL(TargetArch), "EL", Flags); + addMultilibFlag(isMipsEB(TargetArch), "EB", Flags); +} + +static void getDefaultBiarchMultilibFlags(const llvm::Triple &TargetTriple, + const llvm::opt::ArgList &Args, + std::vector &Flags) { + addMultilibFlag(TargetTriple.isArch64Bit(), "m64", Flags); + addMultilibFlag(TargetTriple.isArch32Bit(), "m32", Flags); +} + +bool MultilibSet::select(const llvm::Triple &TargetTriple, + const llvm::opt::ArgList &Args, + Multilib &M) const { + Multilib::flags_list MultilibFlags; + + llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); + + switch (TargetArch) { + case llvm::Triple::mips: + case llvm::Triple::mipsel: + case llvm::Triple::mips64: + case llvm::Triple::mips64el: + getMipsMultilibFlags(TargetArch, Args, MultilibFlags); + break; + default: + getDefaultBiarchMultilibFlags(TargetTriple, Args, MultilibFlags); + break; + } + + class FilterFlagsMismatch : public MultilibSet::FilterCallback { + llvm::StringMap FlagSet; + public: + FilterFlagsMismatch(std::vector &Flags) { + // Stuff all of the flags into the FlagSet such that a true mappend + // indicates the flag was enabled, and a false mappend indicates the + // flag was disabled + for (Multilib::flags_list::const_iterator + I = Flags.begin(), E = Flags.end(); + I != E; ++I) { + FlagSet[StringRef(*I).substr(1)] = isFlagEnabled(*I); + } + } + bool operator() (const Multilib &M) const LLVM_OVERRIDE { + for (Multilib::flags_list::const_iterator I = M.flags().begin(), + E = M.flags().end(); I != E; ++I) { + StringRef Flag(*I); + llvm::StringMap::const_iterator SI = FlagSet.find(Flag.substr(1)); + if (SI != FlagSet.end()) + if ((*SI).getValue() != isFlagEnabled(Flag)) + return true; + } + return false; + } + private: + bool isFlagEnabled(StringRef Flag) const { + char Indicator = Flag.front(); + assert( Indicator == '+' || Indicator == '-' ); + return Indicator == '+'; + } + }; + + FilterFlagsMismatch FlagsMismatch(MultilibFlags); + + multilib_list Filtered = filterCopy(FlagsMismatch, Multilibs); + + if (Filtered.size() == 0) { + return false; + } else if (Filtered.size() == 1) { + M = Filtered[0]; + return true; + } + + // TODO: pick the "best" multlib when more than one is suitable + assert(false); + + return false; +} + +MultilibSet::multilib_list MultilibSet::filterCopy( + const MultilibSet::FilterCallback &F, const multilib_list &Ms) { + multilib_list Copy(Ms); + filterInPlace(F, Copy); + return Copy; +} + +namespace { + // Wrapper for FilterCallback to make operator() nonvirtual so it + // can be passed by value to std::remove_if + class FilterWrapper { + const MultilibSet::FilterCallback &F; + public: + FilterWrapper(const MultilibSet::FilterCallback &F) : F(F) {} + bool operator()(const Multilib &M) const LLVM_OVERRIDE { return F(M); } + }; +} // end anonymous namespace + +void MultilibSet::filterInPlace(const MultilibSet::FilterCallback &F, + multilib_list &Ms) { + Ms.erase(std::remove_if(Ms.begin(), Ms.end(), FilterWrapper(F)), Ms.end()); +} Index: lib/Driver/ToolChains.h =================================================================== --- lib/Driver/ToolChains.h +++ lib/Driver/ToolChains.h @@ -13,8 +13,10 @@ #include "Tools.h" #include "clang/Basic/VersionTuple.h" #include "clang/Driver/Action.h" +#include "clang/Driver/Multilib.h" #include "clang/Driver/ToolChain.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" #include "llvm/Support/Compiler.h" #include #include @@ -67,7 +69,6 @@ bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); } }; - /// \brief This is a class to find a viable GCC installation for Clang to /// use. /// @@ -80,9 +81,13 @@ // FIXME: These might be better as path objects. std::string GCCInstallPath; - std::string GCCBiarchSuffix; std::string GCCParentLibPath; - std::string GCCMIPSABIDirSuffix; + + /// The primary multilib appropriate for the given flags. + Multilib SelectedMultilib; + /// On Biarch systems, this corresponds to the default multilib when + /// targeting the non-default multilib. Otherwise, it is empty. + llvm::Optional BiarchSibling; GCCVersion Version; @@ -90,6 +95,9 @@ // order to print out detailed information in verbose mode. std::set CandidateGCCInstallPaths; + /// The set of multilibs that the detected installation supports. + MultilibSet Multilibs; + public: GCCInstallationDetector() : IsValid(false) {} void init(const Driver &D, const llvm::Triple &TargetTriple, @@ -104,26 +112,18 @@ /// \brief Get the detected GCC installation path. StringRef getInstallPath() const { return GCCInstallPath; } - /// \brief Get the detected GCC installation path suffix for the bi-arch - /// target variant. - StringRef getBiarchSuffix() const { return GCCBiarchSuffix; } - /// \brief Get the detected GCC parent lib path. StringRef getParentLibPath() const { return GCCParentLibPath; } - /// \brief Get the detected GCC MIPS ABI directory suffix. - /// - /// This is used as a suffix both to the install directory of GCC and as - /// a suffix to its parent lib path in order to select a MIPS ABI-specific - /// subdirectory. - /// - /// This will always be empty for any non-MIPS target. - /// - // FIXME: This probably shouldn't exist at all, and should be factored - // into the multiarch and/or biarch support. Please don't add more uses of - // this interface, it is meant as a legacy crutch for the MIPS driver - // logic. - StringRef getMIPSABIDirSuffix() const { return GCCMIPSABIDirSuffix; } + /// \brief Get the detected Multilib + const Multilib &getMultilib() const { return SelectedMultilib; } + + /// \brief Get the whole MultilibSet + const MultilibSet &getMultilibs() const { return Multilibs; } + + /// Get the biarch sibling multilib (if it exists). + /// \return true iff such a sibling exists + bool getBiarchSibling(Multilib &M) const; /// \brief Get the detected GCC version string. const GCCVersion &getVersion() const { return Version; } @@ -140,15 +140,18 @@ SmallVectorImpl &BiarchLibDirs, SmallVectorImpl &BiarchTripleAliases); - void ScanLibDirForGCCTriple(llvm::Triple::ArchType TargetArch, + void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch, const llvm::opt::ArgList &Args, const std::string &LibDir, StringRef CandidateTriple, bool NeedsBiarchSuffix = false); - void findMIPSABIDirSuffix(std::string &Suffix, - llvm::Triple::ArchType TargetArch, StringRef Path, - const llvm::opt::ArgList &Args); + bool findMIPSMultilibs(const llvm::Triple &TargetArch, StringRef Path, + const llvm::opt::ArgList &Args); + + bool findBiarchMultilibs(const llvm::Triple &TargetArch, + StringRef Path, const llvm::opt::ArgList &Args, + bool NeedsBiarchSuffix); }; GCCInstallationDetector GCCInstallation; @@ -596,8 +599,7 @@ private: static bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, Twine TargetArchDir, - Twine BiarchSuffix, - Twine MIPSABIDirSuffix, + Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args); static bool addLibStdCXXIncludePaths(Twine Base, Twine TargetArchDir, Index: lib/Driver/ToolChains.cpp =================================================================== --- lib/Driver/ToolChains.cpp +++ lib/Driver/ToolChains.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "ToolChains.h" +#include "ArgParse.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Version.h" #include "clang/Driver/Compilation.h" @@ -138,17 +139,6 @@ .Default(0); } -static bool isSoftFloatABI(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_msoft_float, - options::OPT_mhard_float, - options::OPT_mfloat_abi_EQ); - if (!A) return false; - - return A->getOption().matches(options::OPT_msoft_float) || - (A->getOption().matches(options::OPT_mfloat_abi_EQ) && - A->getValue() == StringRef("soft")); -} - StringRef Darwin::getDarwinArchName(const ArgList &Args) const { switch (getTriple().getArch()) { default: @@ -1047,13 +1037,6 @@ return false; } -static StringRef getGCCToolchainDir(const ArgList &Args) { - const Arg *A = Args.getLastArg(options::OPT_gcc_toolchain); - if (A) - return A->getValue(); - return GCC_INSTALL_PREFIX; -} - /// \brief Initialize a GCCInstallationDetector from the driver. /// /// This performs all of the autodetection and sets up the various paths. @@ -1069,7 +1052,6 @@ llvm::Triple BiarchVariantTriple = TargetTriple.isArch32Bit() ? TargetTriple.get64BitArchVariant() : TargetTriple.get32BitArchVariant(); - llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); // The library directories which may contain GCC installations. SmallVector CandidateLibDirs, CandidateBiarchLibDirs; // The compatible GCC triples for this particular architecture. @@ -1115,7 +1097,7 @@ if (!llvm::sys::fs::exists(LibDir)) continue; for (unsigned k = 0, ke = CandidateTripleAliases.size(); k < ke; ++k) - ScanLibDirForGCCTriple(TargetArch, Args, LibDir, + ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, CandidateTripleAliases[k]); } for (unsigned j = 0, je = CandidateBiarchLibDirs.size(); j < je; ++j) { @@ -1124,7 +1106,7 @@ continue; for (unsigned k = 0, ke = CandidateBiarchTripleAliases.size(); k < ke; ++k) - ScanLibDirForGCCTriple(TargetArch, Args, LibDir, + ScanLibDirForGCCTriple(TargetTriple, Args, LibDir, CandidateBiarchTripleAliases[k], /*NeedsBiarchSuffix=*/ true); } @@ -1139,6 +1121,21 @@ OS << "Found candidate GCC installation: " << *I << "\n"; OS << "Selected GCC installation: " << GCCInstallPath << "\n"; + for (MultilibSet::const_iterator + I = Multilibs.begin(), + E = Multilibs.end(); + I != E; ++I) { + OS << "Candidate multiilb: " << *I << "\n"; + } + OS << "Selected multilib: " << SelectedMultilib << "\n"; +} + +bool Generic_GCC::GCCInstallationDetector::getBiarchSibling(Multilib &M) const { + if (BiarchSibling.hasValue()) { + M = BiarchSibling.getValue(); + return true; + } + return false; } /*static*/ void Generic_GCC::GCCInstallationDetector::CollectLibDirsAndTriples( @@ -1345,81 +1342,24 @@ BiarchTripleAliases.push_back(BiarchTriple.str()); } -static bool isMipsArch(llvm::Triple::ArchType Arch) { - return Arch == llvm::Triple::mips || - Arch == llvm::Triple::mipsel || - Arch == llvm::Triple::mips64 || - Arch == llvm::Triple::mips64el; -} - -static bool isMips16(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mips16, - options::OPT_mno_mips16); - return A && A->getOption().matches(options::OPT_mips16); -} - -static bool isMips32r2(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_march_EQ, - options::OPT_mcpu_EQ); - - return A && A->getValue() == StringRef("mips32r2"); -} - -static bool isMips64r2(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_march_EQ, - options::OPT_mcpu_EQ); - - return A && A->getValue() == StringRef("mips64r2"); -} - -static bool isMicroMips(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mmicromips, - options::OPT_mno_micromips); - return A && A->getOption().matches(options::OPT_mmicromips); -} - -static bool isMipsFP64(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mfp64, options::OPT_mfp32); - return A && A->getOption().matches(options::OPT_mfp64); -} - -static bool isMipsNan2008(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mnan_EQ); - return A && A->getValue() == StringRef("2008"); -} - -// FIXME: There is the same routine in the Tools.cpp. -static bool hasMipsN32ABIArg(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mabi_EQ); - return A && (A->getValue() == StringRef("n32")); -} - static bool hasCrtBeginObj(Twine Path) { return llvm::sys::fs::exists(Path + "/crtbegin.o"); } -static bool findTargetBiarchSuffix(std::string &Suffix, StringRef Path, - llvm::Triple::ArchType TargetArch, - const ArgList &Args) { - // FIXME: This routine was only intended to model bi-arch toolchains which - // use -m32 and -m64 to swap between variants of a target. It shouldn't be - // doing ABI-based builtin location for MIPS. - if (hasMipsN32ABIArg(Args)) - Suffix = "/n32"; - else if (TargetArch == llvm::Triple::x86_64 || - TargetArch == llvm::Triple::ppc64 || - TargetArch == llvm::Triple::systemz || - TargetArch == llvm::Triple::mips64 || - TargetArch == llvm::Triple::mips64el) - Suffix = "/64"; - else - Suffix = "/32"; - - return hasCrtBeginObj(Path + Suffix); -} +namespace { + // Filter to remove Multilibs that don't exist as a suffix to Path + class FilterNonExistant : public MultilibSet::FilterCallback { + std::string Base; + public: + FilterNonExistant(std::string Base) : Base(Base) {} + bool operator() (const Multilib &M) const LLVM_OVERRIDE { + return !hasCrtBeginObj(Base + M.gccSuffix()); + } + }; +} // end anonymous namespace -void Generic_GCC::GCCInstallationDetector::findMIPSABIDirSuffix( - std::string &Suffix, llvm::Triple::ArchType TargetArch, StringRef Path, +bool Generic_GCC::GCCInstallationDetector::findMIPSMultilibs( + const llvm::Triple &TargetTriple, StringRef Path, const llvm::opt::ArgList &Args) { // Some MIPS toolchains put libraries and object files compiled // using different options in to the sub-directoris which names @@ -1446,69 +1386,255 @@ // /usr // /lib <= crt*.o files compiled with '-mips32' - // Check FSF Toolchain path - Suffix.clear(); - if (TargetArch == llvm::Triple::mips || - TargetArch == llvm::Triple::mipsel) { - if (isMicroMips(Args)) - Suffix += "/micromips"; - else if (isMips32r2(Args)) - Suffix += ""; - else - Suffix += "/mips32"; + FilterNonExistant NonExistant(Path); - if (isMips16(Args)) - Suffix += "/mips16"; - } else { - if (isMips64r2(Args)) - Suffix += hasMipsN32ABIArg(Args) ? "/mips64r2" : "/mips64r2/64"; - else - Suffix += hasMipsN32ABIArg(Args) ? "/mips64" : "/mips64/64"; - } + // Check for FSF toolchain multilibs + MultilibSet FSFMipsMultilibs; + { + Multilib MArchMips32 = Multilib() + .gccSuffix("/mips32") + .osSuffix("/mips32") + .includeSuffix("/mips32") + .flag("+m32").flag("-m64").flag("-mmicromips").flag("-march=mips32r2"); + + Multilib MArchMicroMips = Multilib() + .gccSuffix("/micromips") + .osSuffix("/micromips") + .includeSuffix("/micromips") + .flag("+m32").flag("-m64").flag("+mmicromips"); + + Multilib MArchMips64r2 = Multilib() + .gccSuffix("/mips64r2") + .osSuffix("/mips64r2") + .includeSuffix("/mips64r2") + .flag("-m32").flag("+m64").flag("+march=mips64r2"); + + Multilib MArchMips64 = Multilib() + .gccSuffix("/mips64") + .osSuffix("/mips64") + .includeSuffix("/mips64") + .flag("-m32").flag("+m64").flag("-march=mips64r2"); + + Multilib MArchDefault = Multilib() + .flag("+m32").flag("-m64").flag("+march=mips32r2"); + + Multilib Mips16 = Multilib() + .gccSuffix("/mips16") + .osSuffix("/mips16") + .includeSuffix("/mips16") + .flag("+mips16"); + + Multilib MAbi64 = Multilib() + .gccSuffix("/64") + .osSuffix("/64") + .includeSuffix("/64") + .flag("+mabi=64").flag("-mabi=n32").flag("-m32"); + + Multilib LittleEndian = Multilib() + .gccSuffix("/el") + .osSuffix("/el") + .includeSuffix("/el") + .flag("+EL").flag("-EB"); + + Multilib SoftFloat = Multilib() + .gccSuffix("/sof") + .osSuffix("/sof") + .includeSuffix("/sof") + .flag("+msoft-float"); + + Multilib FP64 = Multilib() + .gccSuffix("/fp64") + .osSuffix("/fp64") + .includeSuffix("/fp64") + .flag("+mfp64"); + + Multilib Nan2008 = Multilib() + .gccSuffix("/nan2008") + .osSuffix("/nan2008") + .includeSuffix("/nan2008") + .flag("+mnan=2008"); + + FSFMipsMultilibs = MultilibSet() + .Either(MArchMips32, MArchMicroMips, + MArchMips64r2, MArchMips64, MArchDefault) + .Maybe(Mips16) + .FilterOut("/mips64/mips16") + .FilterOut("/mips64r2/mips16") + .FilterOut("/micromips/mips16") + .Maybe(MAbi64) + .FilterOut("/micromips/64") + .FilterOut("/mips32/64") + .FilterOut("^/64") + .FilterOut("/mips16/64") + .Maybe(LittleEndian) + .Maybe(SoftFloat) + .Maybe(FP64) + .Maybe(Nan2008) + .FilterOut(".*sof/nan2008") + .FilterOut(".*sof/fp64") + .FilterOut(NonExistant); + } + + // Check for Code Sourcery toolchain multilibs + MultilibSet CSMipsMultilibs; + { + Multilib MArchMips16 = Multilib() + .gccSuffix("/mips16") + .osSuffix("/mips16") + .includeSuffix("/mips16") + .flag("+m32").flag("+mips16"); + + Multilib MArchMicroMips = Multilib() + .gccSuffix("/micromips") + .osSuffix("/micromips") + .includeSuffix("/micromips") + .flag("+m32").flag("+mmicromips"); + + Multilib MArchDefault = Multilib() + .flag("-mips16").flag("-mmicromips"); + + Multilib SoftFloat = Multilib() + .gccSuffix("/soft-float") + .osSuffix("/soft-float") + .includeSuffix("/soft-float") + .flag("+msoft-float"); + + Multilib Nan2008 = Multilib() + .gccSuffix("/nan2008") + .osSuffix("/nan2008") + .includeSuffix("/nan2008") + .flag("+mnan=2008"); + + Multilib DefaultFloat = Multilib() + .flag("-msoft-float").flag("-mnan=2008"); + + Multilib LittleEndian = Multilib() + .gccSuffix("/el") + .osSuffix("/el") + .includeSuffix("/el") + .flag("+EL").flag("-EB"); + + // Note that this one's osSuffix is "" + Multilib MAbi64 = Multilib() + .gccSuffix("/64") + .includeSuffix("/64") + .flag("+mabi=64").flag("-mabi=n32").flag("-m32"); + + CSMipsMultilibs = MultilibSet() + .Either(MArchMips16, MArchMicroMips, MArchDefault) + .Either(SoftFloat, Nan2008, DefaultFloat) + .FilterOut("/micromips/nan2008") + .FilterOut("/mips16/nan2008") + .Maybe(LittleEndian) + .Maybe(MAbi64) + .FilterOut("/mips16.*/64") + .FilterOut("/micromips.*/64") + .FilterOut(NonExistant); + } + + MultilibSet AndroidMipsMultilibs = MultilibSet() + .Maybe(Multilib("/mips-r2").flag("+march=mips32r2")) + .FilterOut(NonExistant); + + MultilibSet DebianMipsMultilibs; + { + Multilib MAbiN32 = Multilib() + .gccSuffix("/n32") + .includeSuffix("/n32") + .flag("+mabi=n32"); + + Multilib M64 = Multilib() + .gccSuffix("/64") + .includeSuffix("/64") + .flag("+m64").flag("-m32").flag("-mabi=n32"); + + Multilib M32 = Multilib() + .flag("-m64").flag("+m32").flag("-mabi=n32"); + + DebianMipsMultilibs = MultilibSet() + .Either(M32, M64, MAbiN32) + .FilterOut(NonExistant); + } + + Multilibs.clear(); + + if (TargetTriple.getEnvironment() == llvm::Triple::Android) + Multilibs.combineWith(AndroidMipsMultilibs); + else if (DebianMipsMultilibs.size() == 3) { + Multilibs.combineWith(DebianMipsMultilibs); + BiarchSibling = Multilib(); + } else if (FSFMipsMultilibs.size() > CSMipsMultilibs.size()) + Multilibs.combineWith(FSFMipsMultilibs); + else + Multilibs.combineWith(CSMipsMultilibs); - if (TargetArch == llvm::Triple::mipsel || - TargetArch == llvm::Triple::mips64el) - Suffix += "/el"; + return Multilibs.select(TargetTriple, Args, SelectedMultilib); +} - if (isSoftFloatABI(Args)) - Suffix += "/sof"; +bool Generic_GCC::GCCInstallationDetector::findBiarchMultilibs( + const llvm::Triple &TargetTriple, StringRef Path, const ArgList &Args, + bool NeedsBiarchSuffix) { + + // Some versions of SUSE and Fedora on ppc64 put 32-bit libs + // in what would normally be GCCInstallPath and put the 64-bit + // libs in a subdirectory named 64. The simple logic we follow is that + // *if* there is a subdirectory of the right name with crtbegin.o in it, + // we use that. If not, and if not a biarch triple alias, we look for + // crtbegin.o without the subdirectory. + + Multilib Default; + Multilib Alt64 = Multilib() + .gccSuffix("/64") + .includeSuffix("/64") + .flag("-m32").flag("+m64"); + Multilib Alt32 = Multilib() + .gccSuffix("/32") + .includeSuffix("/32") + .flag("+m32").flag("-m64"); + + FilterNonExistant NonExistant(Path); + + // Decide whether the default multilib is 32bit, correcting for + // when the default multilib and the alternate appear backwards + bool DefaultIs32Bit; + if (TargetTriple.isArch32Bit() && !NonExistant(Alt32)) + DefaultIs32Bit = false; + else if (TargetTriple.isArch64Bit() && !NonExistant(Alt64)) + DefaultIs32Bit = true; else { - if (isMipsFP64(Args)) - Suffix += "/fp64"; - - if (isMipsNan2008(Args)) - Suffix += "/nan2008"; + if (NeedsBiarchSuffix) + DefaultIs32Bit = TargetTriple.isArch64Bit(); + else + DefaultIs32Bit = TargetTriple.isArch32Bit(); } - if (hasCrtBeginObj(Path + Suffix)) - return; + if (DefaultIs32Bit) + Default.flag("+m32").flag("-m64"); + else + Default.flag("-m32").flag("+m64"); - // Check Code Sourcery Toolchain path - Suffix.clear(); - if (isMips16(Args)) - Suffix += "/mips16"; - else if (isMicroMips(Args)) - Suffix += "/micromips"; - - if (isSoftFloatABI(Args)) - Suffix += "/soft-float"; - else if (isMipsNan2008(Args)) - Suffix += "/nan2008"; - - if (TargetArch == llvm::Triple::mipsel || - TargetArch == llvm::Triple::mips64el) - Suffix += "/el"; + Multilibs.push_back(Default); + Multilibs.push_back(Alt64); + Multilibs.push_back(Alt32); - if (hasCrtBeginObj(Path + Suffix)) - return; + Multilibs.FilterOut(NonExistant); + + if (!Multilibs.select(TargetTriple, Args, SelectedMultilib)) + return false; - Suffix.clear(); + if (SelectedMultilib == Alt64 || + SelectedMultilib == Alt32) { + BiarchSibling = Default; + } + + return true; } void Generic_GCC::GCCInstallationDetector::ScanLibDirForGCCTriple( - llvm::Triple::ArchType TargetArch, const ArgList &Args, + const llvm::Triple &TargetTriple, const ArgList &Args, const std::string &LibDir, StringRef CandidateTriple, bool NeedsBiarchSuffix) { + llvm::Triple::ArchType TargetArch = TargetTriple.getArch(); // There are various different suffixes involving the triple we // check for. We also record what is necessary to walk from each back // up to the lib directory. @@ -1553,28 +1679,18 @@ if (CandidateVersion <= Version) continue; - std::string MIPSABIDirSuffix; - if (isMipsArch(TargetArch)) - findMIPSABIDirSuffix(MIPSABIDirSuffix, TargetArch, LI->path(), Args); - - // Some versions of SUSE and Fedora on ppc64 put 32-bit libs - // in what would normally be GCCInstallPath and put the 64-bit - // libs in a subdirectory named 64. The simple logic we follow is that - // *if* there is a subdirectory of the right name with crtbegin.o in it, - // we use that. If not, and if not a biarch triple alias, we look for - // crtbegin.o without the subdirectory. - - std::string BiarchSuffix; - if (findTargetBiarchSuffix(BiarchSuffix, - LI->path() + MIPSABIDirSuffix, - TargetArch, Args)) { - GCCBiarchSuffix = BiarchSuffix; - } else if (NeedsBiarchSuffix || - !hasCrtBeginObj(LI->path() + MIPSABIDirSuffix)) { + Multilibs.clear(); + SelectedMultilib = Multilib(); + BiarchSibling.reset(); + + // Debian mips multilibs behave more like the rest of the biarch ones, + // so handle them there + if (isMipsArch(TargetArch)) { + if (!findMIPSMultilibs(TargetTriple, LI->path(), Args)) + continue; + } else if (!findBiarchMultilibs(TargetTriple, LI->path(), Args, + NeedsBiarchSuffix)) continue; - } else { - GCCBiarchSuffix.clear(); - } Version = CandidateVersion; GCCTriple.setTriple(CandidateTriple); @@ -1583,7 +1699,6 @@ // Linux. GCCInstallPath = LibDir + LibSuffixes[i] + "/" + VersionText.str(); GCCParentLibPath = GCCInstallPath + InstallSuffixes[i]; - GCCMIPSABIDirSuffix = MIPSABIDirSuffix; IsValid = true; } } @@ -2362,8 +2477,8 @@ if (llvm::sys::fs::exists(Path)) Paths.push_back(Path.str()); } -static StringRef getMultilibDir(const llvm::Triple &Triple, - const ArgList &Args) { +static StringRef getOSLibDir(const llvm::Triple &Triple, + const ArgList &Args) { if (isMipsArch(Triple.getArch())) { // lib32 directory has a special meaning on MIPS targets. // It contains N32 ABI binaries. Use this folder if produce @@ -2373,14 +2488,14 @@ return Triple.isArch32Bit() ? "lib" : "lib64"; } - // It happens that only x86 and PPC use the 'lib32' variant of multilib, and + // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and // using that variant while targeting other architectures causes problems // because the libraries are laid out in shared system roots that can't cope - // with a 'lib32' multilib search path being considered. So we only enable + // with a 'lib32' library search path being considered. So we only enable // them when we know we may need it. // // FIXME: This is a bit of a hack. We should really unify this code for - // reasoning about multilib spellings with the lib dir spellings in the + // reasoning about oslibdir spellings with the lib dir spellings in the // GCCInstallationDetector, but that is a more significant refactoring. if (Triple.getArch() == llvm::Triple::x86 || Triple.getArch() == llvm::Triple::ppc) @@ -2392,6 +2507,7 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) : Generic_ELF(D, Triple, Args) { GCCInstallation.init(D, Triple, Args); + Multilibs = GCCInstallation.getMultilibs(); llvm::Triple::ArchType Arch = Triple.getArch(); std::string SysRoot = computeSysRoot(); @@ -2459,29 +2575,20 @@ // to the link paths. path_list &Paths = getFilePaths(); - const std::string Multilib = getMultilibDir(Triple, Args); + const std::string OSLibDir = getOSLibDir(Triple, Args); const std::string MultiarchTriple = getMultiarchTriple(Triple, SysRoot); // Add the multilib suffixed paths where they are available. if (GCCInstallation.isValid()) { const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); const std::string &LibPath = GCCInstallation.getParentLibPath(); + const Multilib &Multilib = GCCInstallation.getMultilib(); // Sourcery CodeBench MIPS toolchain holds some libraries under // a biarch-like suffix of the GCC installation. - // - // FIXME: It would be cleaner to model this as a variant of bi-arch. IE, - // instead of a '64' biarch suffix it would be 'el' or something. - if (IsAndroid && IsMips && isMips32r2(Args)) { - assert(GCCInstallation.getBiarchSuffix().empty() && - "Unexpected bi-arch suffix"); - addPathIfExists(GCCInstallation.getInstallPath() + "/mips-r2", Paths); - } else { - addPathIfExists((GCCInstallation.getInstallPath() + - GCCInstallation.getMIPSABIDirSuffix() + - GCCInstallation.getBiarchSuffix()), - Paths); - } + addPathIfExists((GCCInstallation.getInstallPath() + + Multilib.gccSuffix()), + Paths); // GCC cross compiling toolchains will install target libraries which ship // as part of the toolchain under // rather than as @@ -2501,8 +2608,8 @@ // // Note that this matches the GCC behavior. See the below comment for where // Clang diverges from GCC's behavior. - addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + Multilib + - GCCInstallation.getMIPSABIDirSuffix(), + addPathIfExists(LibPath + "/../" + GCCTriple.str() + "/lib/../" + OSLibDir + + Multilib.osSuffix(), Paths); // If the GCC installation we found is inside of the sysroot, we want to @@ -2516,31 +2623,34 @@ // a bug. if (StringRef(LibPath).startswith(SysRoot)) { addPathIfExists(LibPath + "/" + MultiarchTriple, Paths); - addPathIfExists(LibPath + "/../" + Multilib, Paths); + addPathIfExists(LibPath + "/../" + OSLibDir, Paths); } } addPathIfExists(SysRoot + "/lib/" + MultiarchTriple, Paths); - addPathIfExists(SysRoot + "/lib/../" + Multilib, Paths); + addPathIfExists(SysRoot + "/lib/../" + OSLibDir, Paths); addPathIfExists(SysRoot + "/usr/lib/" + MultiarchTriple, Paths); - addPathIfExists(SysRoot + "/usr/lib/../" + Multilib, Paths); + addPathIfExists(SysRoot + "/usr/lib/../" + OSLibDir, Paths); // Try walking via the GCC triple path in case of biarch or multiarch GCC // installations with strange symlinks. if (GCCInstallation.isValid()) { addPathIfExists(SysRoot + "/usr/lib/" + GCCInstallation.getTriple().str() + - "/../../" + Multilib, Paths); + "/../../" + OSLibDir, Paths); - // Add the non-multilib suffixed paths (if potentially different). - const std::string &LibPath = GCCInstallation.getParentLibPath(); - const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); - if (!GCCInstallation.getBiarchSuffix().empty()) + // Add the 'other' biarch variant path + Multilib BiarchSibling; + if (GCCInstallation.getBiarchSibling(BiarchSibling)) { addPathIfExists(GCCInstallation.getInstallPath() + - GCCInstallation.getMIPSABIDirSuffix(), Paths); + BiarchSibling.gccSuffix(), Paths); + } // See comments above on the multilib variant for details of why this is // included even from outside the sysroot. + const std::string &LibPath = GCCInstallation.getParentLibPath(); + const llvm::Triple &GCCTriple = GCCInstallation.getTriple(); + const Multilib &Multilib = GCCInstallation.getMultilib(); addPathIfExists(LibPath + "/../" + GCCTriple.str() + - "/lib" + GCCInstallation.getMIPSABIDirSuffix(), Paths); + "/lib" + Multilib.osSuffix(), Paths); // See comments above on the multilib variant for details of why this is // only included from within the sysroot. @@ -2593,15 +2703,15 @@ const StringRef InstallDir = GCCInstallation.getInstallPath(); const StringRef TripleStr = GCCInstallation.getTriple().str(); - const StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix(); + const Multilib &Multilib = GCCInstallation.getMultilib(); std::string Path = (InstallDir + "/../../../../" + TripleStr + "/libc" + - MIPSABIDirSuffix).str(); + Multilib.osSuffix()).str(); if (llvm::sys::fs::exists(Path)) return Path; - Path = (InstallDir + "/../../../../sysroot" + MIPSABIDirSuffix).str(); + Path = (InstallDir + "/../../../../sysroot" + Multilib.osSuffix()).str(); if (llvm::sys::fs::exists(Path)) return Path; @@ -2757,17 +2867,16 @@ /// libstdc++ installation. /*static*/ bool Linux::addLibStdCXXIncludePaths(Twine Base, Twine Suffix, Twine TargetArchDir, - Twine BiarchSuffix, - Twine MIPSABIDirSuffix, + Twine IncludeSuffix, const ArgList &DriverArgs, ArgStringList &CC1Args) { if (!addLibStdCXXIncludePaths(Base + Suffix, - TargetArchDir + MIPSABIDirSuffix + BiarchSuffix, + TargetArchDir + IncludeSuffix, DriverArgs, CC1Args)) return false; addSystemInclude(DriverArgs, CC1Args, Base + "/" + TargetArchDir + Suffix - + MIPSABIDirSuffix + BiarchSuffix); + + IncludeSuffix); return true; } @@ -2796,13 +2905,12 @@ StringRef LibDir = GCCInstallation.getParentLibPath(); StringRef InstallDir = GCCInstallation.getInstallPath(); StringRef TripleStr = GCCInstallation.getTriple().str(); - StringRef MIPSABIDirSuffix = GCCInstallation.getMIPSABIDirSuffix(); - StringRef BiarchSuffix = GCCInstallation.getBiarchSuffix(); + const Multilib &Multilib = GCCInstallation.getMultilib(); const GCCVersion &Version = GCCInstallation.getVersion(); if (addLibStdCXXIncludePaths(LibDir.str() + "/../include", - "/c++/" + Version.Text, TripleStr, BiarchSuffix, - MIPSABIDirSuffix, DriverArgs, CC1Args)) + "/c++/" + Version.Text, TripleStr, + Multilib.includeSuffix(), DriverArgs, CC1Args)) return; const std::string IncludePathCandidates[] = { @@ -2820,7 +2928,7 @@ for (unsigned i = 0; i < llvm::array_lengthof(IncludePathCandidates); ++i) { if (addLibStdCXXIncludePaths(IncludePathCandidates[i], - TripleStr + MIPSABIDirSuffix + BiarchSuffix, + TripleStr + Multilib.includeSuffix(), DriverArgs, CC1Args)) break; } Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -10,6 +10,7 @@ #include "Tools.h" #include "InputInfo.h" #include "ToolChains.h" +#include "ArgParse.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/Version.h" #include "clang/Driver/Action.h" @@ -6427,11 +6428,6 @@ CmdArgs.push_back("-ldl"); } -static bool hasMipsN32ABIArg(const ArgList &Args) { - Arg *A = Args.getLastArg(options::OPT_mabi_EQ); - return A && (A->getValue() == StringRef("n32")); -} - static StringRef getLinuxDynamicLinker(const ArgList &Args, const toolchains::Linux &ToolChain) { if (ToolChain.getTriple().getEnvironment() == llvm::Triple::Android)