Revert "[Driver][AVR] Fix warn_drv_avr_stdlib_not_linked condition"

This reverts commit 16524d2f1b.

The test caused some warnings when avr-gcc was not installed.
This commit is contained in:
Fangrui Song 2022-03-28 10:11:45 -07:00
parent 8565a87fd4
commit 17ea5bf1d5
4 changed files with 59 additions and 56 deletions

View File

@ -366,20 +366,50 @@ const StringRef PossibleAVRLibcLocations[] = {
/// AVR Toolchain /// AVR Toolchain
AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple, AVRToolChain::AVRToolChain(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args) const ArgList &Args)
: Generic_ELF(D, Triple, Args) { : Generic_ELF(D, Triple, Args), LinkStdlib(false) {
GCCInstallation.init(Triple, Args); GCCInstallation.init(Triple, Args);
// Only add default libraries if the user hasn't explicitly opted out. // Only add default libraries if the user hasn't explicitly opted out.
if (!Args.hasArg(options::OPT_nostdlib) && if (!Args.hasArg(options::OPT_nostdlib) &&
!Args.hasArg(options::OPT_nodefaultlibs)) { !Args.hasArg(options::OPT_nodefaultlibs) &&
if (GCCInstallation.isValid()) { !Args.hasArg(options::OPT_c /* does not apply when not linking */)) {
GCCInstallPath = GCCInstallation.getInstallPath(); std::string CPU = getCPUName(D, Args, Triple);
std::string GCCParentPath(GCCInstallation.getParentLibPath());
getProgramPaths().push_back(GCCParentPath + "/../bin"); if (CPU.empty()) {
// We cannot link any standard libraries without an MCU specified.
D.Diag(diag::warn_drv_avr_mcu_not_specified);
} else { } else {
Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
Optional<std::string> AVRLibcRoot = findAVRLibcInstallation();
if (!FamilyName.hasValue()) {
// We do not have an entry for this CPU in the family
// mapping table yet.
D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
<< CPU;
} else if (!GCCInstallation.isValid()) {
// No avr-gcc found and so no runtime linked.
D.Diag(diag::warn_drv_avr_gcc_not_found); D.Diag(diag::warn_drv_avr_gcc_not_found);
} else if (!AVRLibcRoot.hasValue()) {
// 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(GCCInstallation.getInstallPath());
std::string GCCParentPath(GCCInstallation.getParentLibPath());
std::string LibcRoot = AVRLibcRoot.getValue();
std::string SubPath = GetMCUSubPath(CPU);
getProgramPaths().push_back(GCCParentPath + "/../bin");
getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath);
getFilePaths().push_back(GCCRoot + std::string("/") + SubPath);
LinkStdlib = true;
} }
} }
if (!LinkStdlib)
D.Diag(diag::warn_drv_avr_stdlib_not_linked);
}
} }
void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, void AVRToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
@ -415,14 +445,13 @@ void AVRToolChain::addClangTargetOptions(
} }
Tool *AVRToolChain::buildLinker() const { Tool *AVRToolChain::buildLinker() const {
return new tools::AVR::Linker(getTriple(), *this); return new tools::AVR::Linker(getTriple(), *this, LinkStdlib);
} }
void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA, void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfo &Output,
const InputInfoList &Inputs, const ArgList &Args, const InputInfoList &Inputs, const ArgList &Args,
const char *LinkingOutput) const { const char *LinkingOutput) const {
const auto &TC = static_cast<const AVRToolChain &>(getToolChain());
const Driver &D = getToolChain().getDriver(); const Driver &D = getToolChain().getDriver();
// Compute information about the target AVR. // Compute information about the target AVR.
@ -444,39 +473,6 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.AddAllArgs(CmdArgs, options::OPT_L); Args.AddAllArgs(CmdArgs, options::OPT_L);
getToolChain().AddFilePathLibArgs(Args, CmdArgs); getToolChain().AddFilePathLibArgs(Args, CmdArgs);
// Only add default libraries if the user hasn't explicitly opted out.
bool LinkStdlib = false;
if (!Args.hasArg(options::OPT_nostdlib) &&
!Args.hasArg(options::OPT_nodefaultlibs)) {
if (CPU.empty()) {
// We cannot link any standard libraries without an MCU specified.
D.Diag(diag::warn_drv_avr_mcu_not_specified);
} else {
Optional<StringRef> FamilyName = GetMCUFamilyName(CPU);
Optional<std::string> AVRLibcRoot = TC.findAVRLibcInstallation();
if (!FamilyName) {
// We do not have an entry for this CPU in the family
// mapping table yet.
D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented)
<< CPU;
} else if (!AVRLibcRoot) {
// No avr-libc found and so no runtime linked.
D.Diag(diag::warn_drv_avr_libc_not_found);
} else if (!TC.getGCCInstallPath().empty()) {
std::string SubPath = GetMCUSubPath(CPU);
CmdArgs.push_back(
Args.MakeArgString(Twine("-L") + *AVRLibcRoot + "/lib/" + SubPath));
CmdArgs.push_back(
Args.MakeArgString("-L" + TC.getGCCInstallPath() + "/" + SubPath));
LinkStdlib = true;
}
}
if (!LinkStdlib)
D.Diag(diag::warn_drv_avr_stdlib_not_linked);
}
if (SectionAddressData.hasValue()) { if (SectionAddressData.hasValue()) {
std::string DataSectionArg = std::string("-Tdata=0x") + std::string DataSectionArg = std::string("-Tdata=0x") +
llvm::utohexstr(SectionAddressData.getValue()); llvm::utohexstr(SectionAddressData.getValue());

View File

@ -31,14 +31,17 @@ public:
llvm::opt::ArgStringList &CC1Args, llvm::opt::ArgStringList &CC1Args,
Action::OffloadKind DeviceOffloadKind) const override; Action::OffloadKind DeviceOffloadKind) const override;
llvm::Optional<std::string> findAVRLibcInstallation() const;
StringRef getGCCInstallPath() const { return GCCInstallPath; }
protected: protected:
Tool *buildLinker() const override; Tool *buildLinker() const override;
private: private:
StringRef GCCInstallPath; /// Whether libgcc, libct, and friends should be linked.
///
/// This is not done if the user does not specify a
/// microcontroller on the command line.
bool LinkStdlib;
llvm::Optional<std::string> findAVRLibcInstallation() const;
}; };
} // end namespace toolchains } // end namespace toolchains
@ -47,8 +50,9 @@ namespace tools {
namespace AVR { namespace AVR {
class LLVM_LIBRARY_VISIBILITY Linker : public Tool { class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
public: public:
Linker(const llvm::Triple &Triple, const ToolChain &TC) Linker(const llvm::Triple &Triple, const ToolChain &TC, bool LinkStdlib)
: Tool("AVR::Linker", "avr-ld", TC), Triple(Triple) {} : Tool("AVR::Linker", "avr-ld", TC), Triple(Triple),
LinkStdlib(LinkStdlib) {}
bool hasIntegratedCPP() const override { return false; } bool hasIntegratedCPP() const override { return false; }
bool isLinkJob() const override { return true; } bool isLinkJob() const override { return true; }
@ -59,6 +63,7 @@ public:
protected: protected:
const llvm::Triple &Triple; const llvm::Triple &Triple;
bool LinkStdlib;
}; };
} // end namespace AVR } // end namespace AVR
} // end namespace tools } // end namespace tools

View File

@ -0,0 +1,10 @@
// RUN: %clang -### -target avr -no-canonical-prefixes -save-temps %s 2>&1 | FileCheck --check-prefix=WARN %s
// RUN: %clang -### -target avr -no-canonical-prefixes -save-temps -mmcu=atmega328 %s 2>&1 | FileCheck --check-prefix=NOWARN %s
// WARN: warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name>
// WARN: warning: standard library not linked and so no interrupt vector table or compiler runtime routines will be linked
// NOWARN: main
int main() { return 0; }

View File

@ -35,11 +35,3 @@
// RUN: %clang %s -### -target avr --sysroot %S/Inputs/basic_avr_tree 2>&1 -nostdinc | FileCheck --check-prefix=NOSTDINC %s // RUN: %clang %s -### -target avr --sysroot %S/Inputs/basic_avr_tree 2>&1 -nostdinc | FileCheck --check-prefix=NOSTDINC %s
// RUN: %clang %s -### -target avr --sysroot %S/Inputs/basic_avr_tree 2>&1 -nostdlibinc | FileCheck --check-prefix=NOSTDINC %s // RUN: %clang %s -### -target avr --sysroot %S/Inputs/basic_avr_tree 2>&1 -nostdlibinc | FileCheck --check-prefix=NOSTDINC %s
// NOSTDINC-NOT: "-internal-isystem" {{".*avr/include"}} // NOSTDINC-NOT: "-internal-isystem" {{".*avr/include"}}
// RUN: %clang -### --target=avr %s 2>&1 | FileCheck --check-prefix=WARN_STDLIB %s
// RUN: %clang -### --target=avr -mmcu=atmega328 %s 2>&1 | FileCheck --check-prefix=NOWARN_STDLIB %s
// RUN: %clang -### --target=avr -c %s 2>&1 | FileCheck --check-prefix=NOWARN_STDLIB %s
// WARN_STDLIB: warning: no target microcontroller specified on command line, cannot link standard libraries, please pass -mmcu=<mcu name>
// WARN_STDLIB: warning: standard library not linked and so no interrupt vector table or compiler runtime routines will be linked
// NOWARN_STDLIB-NOT: warning: