Index: clang/lib/Driver/ToolChains/AVR.cpp =================================================================== --- clang/lib/Driver/ToolChains/AVR.cpp +++ clang/lib/Driver/ToolChains/AVR.cpp @@ -297,8 +297,8 @@ } const StringRef PossibleAVRLibcLocations[] = { - "/usr/avr", - "/usr/lib/avr", + "/avr", + "/lib/avr", }; } // end anonymous namespace @@ -334,10 +334,17 @@ // No avr-libc found and so no runtime linked. D.Diag(diag::warn_drv_avr_libc_not_found); } else { // We have enough information to link stdlibs - std::string GCCRoot = std::string(GCCInstallation.getInstallPath()); + std::string GCCRoot(GCCInstallation.getInstallPath()); + std::string ParentLibPath(GCCInstallation.getParentLibPath()); std::string LibcRoot = AVRLibcRoot.getValue(); std::string SubPath = GetMCUSubPath(CPU); + // Use user specified program path (via --sysroot) as the first choice, + // and use GCCInstallation's as an alternative. + if (!getDriver().SysRoot.empty()) + getProgramPaths().push_back(getDriver().SysRoot + "/bin"); + getProgramPaths().push_back(ParentLibPath + "/../bin"); + getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath); getFilePaths().push_back(GCCRoot + std::string("/") + SubPath); @@ -419,9 +426,14 @@ llvm::Optional AVRToolChain::findAVRLibcInstallation() const { for (StringRef PossiblePath : PossibleAVRLibcLocations) { + // Use user specified avr-libc path (via --sysroot) if available, otherwise + // search '/usr'. + std::string Path(getDriver().SysRoot.empty() ? "/usr" + : getDriver().SysRoot); + Path += PossiblePath.str(); // Return the first avr-libc installation that exists. - if (llvm::sys::fs::is_directory(PossiblePath)) - return Optional(std::string(PossiblePath)); + if (llvm::sys::fs::is_directory(Path)) + return Optional(Path); } return llvm::None; Index: clang/test/Driver/avr-ld.c =================================================================== --- /dev/null +++ clang/test/Driver/avr-ld.c @@ -0,0 +1,2 @@ +// RUN: %clang -### --target=avr -mmcu=atmega328 --sysroot %S/Inputs/basic_avr_tree %s 2>&1 | FileCheck -check-prefix LINK %s +// LINK: {{".*ld.*"}} {{.*}} {{"-L.*avr5"}} {{.*}} "-Tdata=0x800100" {{.*}} "-latmega328" "-mavr5"