[clang] [Driver] Implement ToolChain on Haiku (PR #66038)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 20 22:15:14 PDT 2023


================
@@ -9,20 +9,142 @@
 #include "Haiku.h"
 #include "CommonArgs.h"
 #include "clang/Config/config.h"
+#include "clang/Driver/Compilation.h"
 #include "llvm/Support/Path.h"
 
 using namespace clang::driver;
+using namespace clang::driver::tools;
 using namespace clang::driver::toolchains;
 using namespace clang;
 using namespace llvm::opt;
 
+void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
+                                   const InputInfo &Output,
+                                   const InputInfoList &Inputs,
+                                   const ArgList &Args,
+                                   const char *LinkingOutput) const {
+  const toolchains::Haiku &ToolChain =
+      static_cast<const toolchains::Haiku &>(getToolChain());
+  const Driver &D = ToolChain.getDriver();
+  const llvm::Triple::ArchType Arch = ToolChain.getArch();
+  const bool Static = Args.hasArg(options::OPT_static);
+  const bool Shared = Args.hasArg(options::OPT_shared);
+  ArgStringList CmdArgs;
+
+  // Silence warning for "clang -g foo.o -o foo"
+  Args.ClaimAllArgs(options::OPT_g_Group);
+  // and "clang -emit-llvm foo.o -o foo"
+  Args.ClaimAllArgs(options::OPT_emit_llvm);
+  // and for "clang -w foo.o -o foo". Other warning options are already
+  // handled somewhere else.
+  Args.ClaimAllArgs(options::OPT_w);
+
+  // Silence warning for "clang -pie foo.o -o foo"
+  Args.ClaimAllArgs(options::OPT_pie);
+
+  if (!D.SysRoot.empty())
+    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
+
+  CmdArgs.push_back("--eh-frame-hdr");
+  if (Static) {
+    CmdArgs.push_back("-Bstatic");
+  } else {
+    if (Args.hasArg(options::OPT_rdynamic))
+      CmdArgs.push_back("-export-dynamic");
+    if (Shared)
+      CmdArgs.push_back("-shared");
+    CmdArgs.push_back("--enable-new-dtags");
+  }
+
+  CmdArgs.push_back("-shared");
+
+  if (!Shared)
+    CmdArgs.push_back("--no-undefined");
+
+  if (Arch == llvm::Triple::riscv64)
+    CmdArgs.push_back("-X");
+
+  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
+  if (Output.isFilename()) {
+    CmdArgs.push_back("-o");
+    CmdArgs.push_back(Output.getFilename());
+  }
+
+  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
+                   options::OPT_r)) {
+    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
+    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
+    if (!Shared)
+      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("start_dyn.o")));
+    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("init_term_dyn.o")));
+  }
+
+  Args.AddAllArgs(CmdArgs,
+                  {options::OPT_L, options::OPT_T_Group, options::OPT_s,
+                   options::OPT_t, options::OPT_Z_Flag, options::OPT_r});
+  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
+
+  addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
+  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
+
+  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
+                   options::OPT_r)) {
+    // Use the static OpenMP runtime with -static-openmp
+    bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
+    addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
+
+    if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
+      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
+
+    CmdArgs.push_back("-lgcc");
+
+    CmdArgs.push_back("--push-state");
+    CmdArgs.push_back("--as-needed");
+    CmdArgs.push_back("-lgcc_s");
+    CmdArgs.push_back("--no-as-needed");
+    CmdArgs.push_back("--pop-state");
+
+    CmdArgs.push_back("-lroot");
+
+    CmdArgs.push_back("-lgcc");
+
+    CmdArgs.push_back("--push-state");
+    CmdArgs.push_back("--as-needed");
+    CmdArgs.push_back("-lgcc_s");
+    CmdArgs.push_back("--no-as-needed");
+    CmdArgs.push_back("--pop-state");
+  }
+
+  // No need to do anything for pthreads. Claim argument to avoid warning.
+  Args.ClaimAllArgs(options::OPT_pthread);
----------------
MaskRay wrote:

You can use `claimAllArgs({...})` to claim multiple options.

https://github.com/llvm/llvm-project/pull/66038


More information about the cfe-commits mailing list