xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Driver/ToolChains/Myriad.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- Myriad.cpp - Myriad ToolChain Implementations ----------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "Myriad.h"
107330f729Sjoerg #include "CommonArgs.h"
117330f729Sjoerg #include "clang/Driver/Compilation.h"
127330f729Sjoerg #include "clang/Driver/Driver.h"
137330f729Sjoerg #include "clang/Driver/DriverDiagnostic.h"
147330f729Sjoerg #include "clang/Driver/Options.h"
157330f729Sjoerg #include "llvm/Option/ArgList.h"
167330f729Sjoerg 
177330f729Sjoerg using namespace clang::driver;
187330f729Sjoerg using namespace clang::driver::toolchains;
197330f729Sjoerg using namespace clang;
207330f729Sjoerg using namespace llvm::opt;
217330f729Sjoerg 
227330f729Sjoerg using tools::addPathIfExists;
237330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const247330f729Sjoerg void tools::SHAVE::Compiler::ConstructJob(Compilation &C, const JobAction &JA,
257330f729Sjoerg                                           const InputInfo &Output,
267330f729Sjoerg                                           const InputInfoList &Inputs,
277330f729Sjoerg                                           const ArgList &Args,
287330f729Sjoerg                                           const char *LinkingOutput) const {
297330f729Sjoerg   ArgStringList CmdArgs;
307330f729Sjoerg   assert(Inputs.size() == 1);
317330f729Sjoerg   const InputInfo &II = Inputs[0];
327330f729Sjoerg   assert(II.getType() == types::TY_C || II.getType() == types::TY_CXX ||
337330f729Sjoerg          II.getType() == types::TY_PP_CXX);
347330f729Sjoerg 
357330f729Sjoerg   if (JA.getKind() == Action::PreprocessJobClass) {
367330f729Sjoerg     Args.ClaimAllArgs();
377330f729Sjoerg     CmdArgs.push_back("-E");
387330f729Sjoerg   } else {
397330f729Sjoerg     assert(Output.getType() == types::TY_PP_Asm); // Require preprocessed asm.
407330f729Sjoerg     CmdArgs.push_back("-S");
417330f729Sjoerg     CmdArgs.push_back("-fno-exceptions"); // Always do this even if unspecified.
427330f729Sjoerg   }
437330f729Sjoerg   CmdArgs.push_back("-DMYRIAD2");
447330f729Sjoerg 
457330f729Sjoerg   // Append all -I, -iquote, -isystem paths, defines/undefines, 'f'
467330f729Sjoerg   // flags, 'g' flags, 'M' flags, optimize flags, warning options,
477330f729Sjoerg   // mcpu flags, mllvm flags, and Xclang flags.
487330f729Sjoerg   // These are spelled the same way in clang and moviCompile.
497330f729Sjoerg   Args.AddAllArgsExcept(
507330f729Sjoerg       CmdArgs,
517330f729Sjoerg       {options::OPT_I_Group, options::OPT_clang_i_Group, options::OPT_std_EQ,
527330f729Sjoerg        options::OPT_D, options::OPT_U, options::OPT_f_Group,
537330f729Sjoerg        options::OPT_f_clang_Group, options::OPT_g_Group, options::OPT_M_Group,
547330f729Sjoerg        options::OPT_O_Group, options::OPT_W_Group, options::OPT_mcpu_EQ,
557330f729Sjoerg        options::OPT_mllvm, options::OPT_Xclang},
567330f729Sjoerg       {options::OPT_fno_split_dwarf_inlining});
577330f729Sjoerg   Args.hasArg(options::OPT_fno_split_dwarf_inlining); // Claim it if present.
587330f729Sjoerg 
597330f729Sjoerg   // If we're producing a dependency file, and assembly is the final action,
607330f729Sjoerg   // then the name of the target in the dependency file should be the '.o'
617330f729Sjoerg   // file, not the '.s' file produced by this step. For example, instead of
627330f729Sjoerg   //  /tmp/mumble.s: mumble.c .../someheader.h
637330f729Sjoerg   // the filename on the lefthand side should be "mumble.o"
647330f729Sjoerg   if (Args.getLastArg(options::OPT_MF) && !Args.getLastArg(options::OPT_MT) &&
657330f729Sjoerg       C.getActions().size() == 1 &&
667330f729Sjoerg       C.getActions()[0]->getKind() == Action::AssembleJobClass) {
677330f729Sjoerg     Arg *A = Args.getLastArg(options::OPT_o);
687330f729Sjoerg     if (A) {
697330f729Sjoerg       CmdArgs.push_back("-MT");
707330f729Sjoerg       CmdArgs.push_back(Args.MakeArgString(A->getValue()));
717330f729Sjoerg     }
727330f729Sjoerg   }
737330f729Sjoerg 
747330f729Sjoerg   CmdArgs.push_back(II.getFilename());
757330f729Sjoerg   CmdArgs.push_back("-o");
767330f729Sjoerg   CmdArgs.push_back(Output.getFilename());
777330f729Sjoerg 
787330f729Sjoerg   std::string Exec =
797330f729Sjoerg       Args.MakeArgString(getToolChain().GetProgramPath("moviCompile"));
80*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
81*e038c9c4Sjoerg                                          Args.MakeArgString(Exec), CmdArgs,
82*e038c9c4Sjoerg                                          Inputs, Output));
837330f729Sjoerg }
847330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const857330f729Sjoerg void tools::SHAVE::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
867330f729Sjoerg                                            const InputInfo &Output,
877330f729Sjoerg                                            const InputInfoList &Inputs,
887330f729Sjoerg                                            const ArgList &Args,
897330f729Sjoerg                                            const char *LinkingOutput) const {
907330f729Sjoerg   ArgStringList CmdArgs;
917330f729Sjoerg 
927330f729Sjoerg   assert(Inputs.size() == 1);
937330f729Sjoerg   const InputInfo &II = Inputs[0];
947330f729Sjoerg   assert(II.getType() == types::TY_PP_Asm); // Require preprocessed asm input.
957330f729Sjoerg   assert(Output.getType() == types::TY_Object);
967330f729Sjoerg 
977330f729Sjoerg   CmdArgs.push_back("-no6thSlotCompression");
987330f729Sjoerg   const Arg *CPUArg = Args.getLastArg(options::OPT_mcpu_EQ);
997330f729Sjoerg   if (CPUArg)
1007330f729Sjoerg     CmdArgs.push_back(
1017330f729Sjoerg         Args.MakeArgString("-cv:" + StringRef(CPUArg->getValue())));
1027330f729Sjoerg   CmdArgs.push_back("-noSPrefixing");
1037330f729Sjoerg   CmdArgs.push_back("-a"); // Mystery option.
1047330f729Sjoerg   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
1057330f729Sjoerg   for (const Arg *A : Args.filtered(options::OPT_I, options::OPT_isystem)) {
1067330f729Sjoerg     A->claim();
1077330f729Sjoerg     CmdArgs.push_back(
1087330f729Sjoerg         Args.MakeArgString(std::string("-i:") + A->getValue(0)));
1097330f729Sjoerg   }
1107330f729Sjoerg   CmdArgs.push_back(II.getFilename());
1117330f729Sjoerg   CmdArgs.push_back(
1127330f729Sjoerg       Args.MakeArgString(std::string("-o:") + Output.getFilename()));
1137330f729Sjoerg 
1147330f729Sjoerg   std::string Exec =
1157330f729Sjoerg       Args.MakeArgString(getToolChain().GetProgramPath("moviAsm"));
116*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
117*e038c9c4Sjoerg                                          Args.MakeArgString(Exec), CmdArgs,
118*e038c9c4Sjoerg                                          Inputs, Output));
1197330f729Sjoerg }
1207330f729Sjoerg 
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const1217330f729Sjoerg void tools::Myriad::Linker::ConstructJob(Compilation &C, const JobAction &JA,
1227330f729Sjoerg                                          const InputInfo &Output,
1237330f729Sjoerg                                          const InputInfoList &Inputs,
1247330f729Sjoerg                                          const ArgList &Args,
1257330f729Sjoerg                                          const char *LinkingOutput) const {
1267330f729Sjoerg   const auto &TC =
1277330f729Sjoerg       static_cast<const toolchains::MyriadToolChain &>(getToolChain());
1287330f729Sjoerg   const llvm::Triple &T = TC.getTriple();
1297330f729Sjoerg   ArgStringList CmdArgs;
1307330f729Sjoerg   bool UseStartfiles =
1317330f729Sjoerg       !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
1327330f729Sjoerg   bool UseDefaultLibs =
1337330f729Sjoerg       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs);
1347330f729Sjoerg   // Silence warning if the args contain both -nostdlib and -stdlib=.
1357330f729Sjoerg   Args.getLastArg(options::OPT_stdlib_EQ);
1367330f729Sjoerg 
1377330f729Sjoerg   if (T.getArch() == llvm::Triple::sparc)
1387330f729Sjoerg     CmdArgs.push_back("-EB");
1397330f729Sjoerg   else // SHAVE assumes little-endian, and sparcel is expressly so.
1407330f729Sjoerg     CmdArgs.push_back("-EL");
1417330f729Sjoerg 
1427330f729Sjoerg   // The remaining logic is mostly like gnutools::Linker::ConstructJob,
1437330f729Sjoerg   // but we never pass through a --sysroot option and various other bits.
1447330f729Sjoerg   // For example, there are no sanitizers (yet) nor gold linker.
1457330f729Sjoerg 
1467330f729Sjoerg   // Eat some arguments that may be present but have no effect.
1477330f729Sjoerg   Args.ClaimAllArgs(options::OPT_g_Group);
1487330f729Sjoerg   Args.ClaimAllArgs(options::OPT_w);
1497330f729Sjoerg   Args.ClaimAllArgs(options::OPT_static_libgcc);
1507330f729Sjoerg 
1517330f729Sjoerg   if (Args.hasArg(options::OPT_s)) // Pass the 'strip' option.
1527330f729Sjoerg     CmdArgs.push_back("-s");
1537330f729Sjoerg 
1547330f729Sjoerg   CmdArgs.push_back("-o");
1557330f729Sjoerg   CmdArgs.push_back(Output.getFilename());
1567330f729Sjoerg 
1577330f729Sjoerg   if (UseStartfiles) {
1587330f729Sjoerg     // If you want startfiles, it means you want the builtin crti and crtbegin,
1597330f729Sjoerg     // but not crt0. Myriad link commands provide their own crt0.o as needed.
1607330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crti.o")));
1617330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtbegin.o")));
1627330f729Sjoerg   }
1637330f729Sjoerg 
1647330f729Sjoerg   Args.AddAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
1657330f729Sjoerg                             options::OPT_e, options::OPT_s, options::OPT_t,
1667330f729Sjoerg                             options::OPT_Z_Flag, options::OPT_r});
1677330f729Sjoerg 
1687330f729Sjoerg   TC.AddFilePathLibArgs(Args, CmdArgs);
1697330f729Sjoerg 
1707330f729Sjoerg   bool NeedsSanitizerDeps = addSanitizerRuntimes(TC, Args, CmdArgs);
1717330f729Sjoerg   AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
1727330f729Sjoerg 
1737330f729Sjoerg   if (UseDefaultLibs) {
1747330f729Sjoerg     if (NeedsSanitizerDeps)
1757330f729Sjoerg       linkSanitizerRuntimeDeps(TC, CmdArgs);
1767330f729Sjoerg     if (C.getDriver().CCCIsCXX()) {
1777330f729Sjoerg       if (TC.GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) {
1787330f729Sjoerg         CmdArgs.push_back("-lc++");
1797330f729Sjoerg         CmdArgs.push_back("-lc++abi");
1807330f729Sjoerg       } else
1817330f729Sjoerg         CmdArgs.push_back("-lstdc++");
1827330f729Sjoerg     }
1837330f729Sjoerg     if (T.getOS() == llvm::Triple::RTEMS) {
1847330f729Sjoerg       CmdArgs.push_back("--start-group");
1857330f729Sjoerg       CmdArgs.push_back("-lc");
1867330f729Sjoerg       CmdArgs.push_back("-lgcc"); // circularly dependent on rtems
1877330f729Sjoerg       // You must provide your own "-L" option to enable finding these.
1887330f729Sjoerg       CmdArgs.push_back("-lrtemscpu");
1897330f729Sjoerg       CmdArgs.push_back("-lrtemsbsp");
1907330f729Sjoerg       CmdArgs.push_back("--end-group");
1917330f729Sjoerg     } else {
1927330f729Sjoerg       CmdArgs.push_back("-lc");
1937330f729Sjoerg       CmdArgs.push_back("-lgcc");
1947330f729Sjoerg     }
1957330f729Sjoerg   }
1967330f729Sjoerg   if (UseStartfiles) {
1977330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtend.o")));
1987330f729Sjoerg     CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crtn.o")));
1997330f729Sjoerg   }
2007330f729Sjoerg 
2017330f729Sjoerg   std::string Exec =
2027330f729Sjoerg       Args.MakeArgString(TC.GetProgramPath("sparc-myriad-rtems-ld"));
203*e038c9c4Sjoerg   C.addCommand(std::make_unique<Command>(
204*e038c9c4Sjoerg       JA, *this, ResponseFileSupport::AtFileCurCP(), Args.MakeArgString(Exec),
205*e038c9c4Sjoerg       CmdArgs, Inputs, Output));
2067330f729Sjoerg }
2077330f729Sjoerg 
MyriadToolChain(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)2087330f729Sjoerg MyriadToolChain::MyriadToolChain(const Driver &D, const llvm::Triple &Triple,
2097330f729Sjoerg                                  const ArgList &Args)
2107330f729Sjoerg     : Generic_ELF(D, Triple, Args) {
2117330f729Sjoerg   // If a target of 'sparc-myriad-elf' is specified to clang, it wants to use
2127330f729Sjoerg   // 'sparc-myriad--elf' (note the unknown OS) as the canonical triple.
2137330f729Sjoerg   // This won't work to find gcc. Instead we give the installation detector an
2147330f729Sjoerg   // extra triple, which is preferable to further hacks of the logic that at
2157330f729Sjoerg   // present is based solely on getArch(). In particular, it would be wrong to
2167330f729Sjoerg   // choose the myriad installation when targeting a non-myriad sparc install.
2177330f729Sjoerg   switch (Triple.getArch()) {
2187330f729Sjoerg   default:
2197330f729Sjoerg     D.Diag(clang::diag::err_target_unsupported_arch)
2207330f729Sjoerg         << Triple.getArchName() << "myriad";
2217330f729Sjoerg     LLVM_FALLTHROUGH;
2227330f729Sjoerg   case llvm::Triple::shave:
2237330f729Sjoerg     return;
2247330f729Sjoerg   case llvm::Triple::sparc:
2257330f729Sjoerg   case llvm::Triple::sparcel:
2267330f729Sjoerg     GCCInstallation.init(Triple, Args, {"sparc-myriad-rtems"});
2277330f729Sjoerg   }
2287330f729Sjoerg 
2297330f729Sjoerg   if (GCCInstallation.isValid()) {
2307330f729Sjoerg     // This directory contains crt{i,n,begin,end}.o as well as libgcc.
2317330f729Sjoerg     // These files are tied to a particular version of gcc.
2327330f729Sjoerg     SmallString<128> CompilerSupportDir(GCCInstallation.getInstallPath());
2337330f729Sjoerg     addPathIfExists(D, CompilerSupportDir, getFilePaths());
2347330f729Sjoerg   }
2357330f729Sjoerg   // libstd++ and libc++ must both be found in this one place.
2367330f729Sjoerg   addPathIfExists(D, D.Dir + "/../sparc-myriad-rtems/lib", getFilePaths());
2377330f729Sjoerg }
2387330f729Sjoerg 
~MyriadToolChain()2397330f729Sjoerg MyriadToolChain::~MyriadToolChain() {}
2407330f729Sjoerg 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const2417330f729Sjoerg void MyriadToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
2427330f729Sjoerg                                                 ArgStringList &CC1Args) const {
2437330f729Sjoerg   if (!DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
2447330f729Sjoerg     addSystemInclude(DriverArgs, CC1Args, getDriver().SysRoot + "/include");
2457330f729Sjoerg }
2467330f729Sjoerg 
addLibCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const2477330f729Sjoerg void MyriadToolChain::addLibCxxIncludePaths(
2487330f729Sjoerg     const llvm::opt::ArgList &DriverArgs,
2497330f729Sjoerg     llvm::opt::ArgStringList &CC1Args) const {
2507330f729Sjoerg   std::string Path(getDriver().getInstalledDir());
2517330f729Sjoerg   addSystemInclude(DriverArgs, CC1Args, Path + "/../include/c++/v1");
2527330f729Sjoerg }
2537330f729Sjoerg 
addLibStdCxxIncludePaths(const llvm::opt::ArgList & DriverArgs,llvm::opt::ArgStringList & CC1Args) const2547330f729Sjoerg void MyriadToolChain::addLibStdCxxIncludePaths(
2557330f729Sjoerg     const llvm::opt::ArgList &DriverArgs,
2567330f729Sjoerg     llvm::opt::ArgStringList &CC1Args) const {
2577330f729Sjoerg   StringRef LibDir = GCCInstallation.getParentLibPath();
2587330f729Sjoerg   const GCCVersion &Version = GCCInstallation.getVersion();
2597330f729Sjoerg   StringRef TripleStr = GCCInstallation.getTriple().str();
2607330f729Sjoerg   const Multilib &Multilib = GCCInstallation.getMultilib();
2617330f729Sjoerg   addLibStdCXXIncludePaths(
2627330f729Sjoerg       LibDir.str() + "/../" + TripleStr.str() + "/include/c++/" + Version.Text,
263*e038c9c4Sjoerg       TripleStr, Multilib.includeSuffix(), DriverArgs, CC1Args);
2647330f729Sjoerg }
2657330f729Sjoerg 
2667330f729Sjoerg // MyriadToolChain handles several triples:
2677330f729Sjoerg //  {shave,sparc{,el}}-myriad-{rtems,unknown}-elf
SelectTool(const JobAction & JA) const2687330f729Sjoerg Tool *MyriadToolChain::SelectTool(const JobAction &JA) const {
2697330f729Sjoerg   // The inherited method works fine if not targeting the SHAVE.
2707330f729Sjoerg   if (!isShaveCompilation(getTriple()))
2717330f729Sjoerg     return ToolChain::SelectTool(JA);
2727330f729Sjoerg   switch (JA.getKind()) {
2737330f729Sjoerg   case Action::PreprocessJobClass:
2747330f729Sjoerg   case Action::CompileJobClass:
2757330f729Sjoerg     if (!Compiler)
2767330f729Sjoerg       Compiler.reset(new tools::SHAVE::Compiler(*this));
2777330f729Sjoerg     return Compiler.get();
2787330f729Sjoerg   case Action::AssembleJobClass:
2797330f729Sjoerg     if (!Assembler)
2807330f729Sjoerg       Assembler.reset(new tools::SHAVE::Assembler(*this));
2817330f729Sjoerg     return Assembler.get();
2827330f729Sjoerg   default:
2837330f729Sjoerg     return ToolChain::getTool(JA.getKind());
2847330f729Sjoerg   }
2857330f729Sjoerg }
2867330f729Sjoerg 
buildLinker() const2877330f729Sjoerg Tool *MyriadToolChain::buildLinker() const {
2887330f729Sjoerg   return new tools::Myriad::Linker(*this);
2897330f729Sjoerg }
2907330f729Sjoerg 
getSupportedSanitizers() const2917330f729Sjoerg SanitizerMask MyriadToolChain::getSupportedSanitizers() const {
2927330f729Sjoerg   return SanitizerKind::Address;
2937330f729Sjoerg }
294