clang  20.0.0git
DragonFly.cpp
Go to the documentation of this file.
1 //===--- DragonFly.cpp - DragonFly ToolChain Implementations ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DragonFly.h"
10 #include "CommonArgs.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Option/ArgList.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::tools;
19 using namespace clang::driver::toolchains;
20 using namespace clang;
21 using namespace llvm::opt;
22 
24  const InputInfo &Output,
25  const InputInfoList &Inputs,
26  const ArgList &Args,
27  const char *LinkingOutput) const {
28  const auto &ToolChain = static_cast<const DragonFly &>(getToolChain());
29  ArgStringList CmdArgs;
30 
31  claimNoWarnArgs(Args);
32 
33  // When building 32-bit code on DragonFly/pc64, we have to explicitly
34  // instruct as in the base system to assemble 32-bit code.
35  if (ToolChain.getArch() == llvm::Triple::x86)
36  CmdArgs.push_back("--32");
37 
38  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
39 
40  CmdArgs.push_back("-o");
41  CmdArgs.push_back(Output.getFilename());
42 
43  for (const auto &II : Inputs)
44  CmdArgs.push_back(II.getFilename());
45 
46  const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("as"));
47  C.addCommand(std::make_unique<Command>(JA, *this,
49  Exec, CmdArgs, Inputs, Output));
50 }
51 
53  const InputInfo &Output,
54  const InputInfoList &Inputs,
55  const ArgList &Args,
56  const char *LinkingOutput) const {
57  const auto &ToolChain = static_cast<const DragonFly &>(getToolChain());
58  const Driver &D = ToolChain.getDriver();
59  const llvm::Triple::ArchType Arch = ToolChain.getArch();
60  const bool Static = Args.hasArg(options::OPT_static);
61  const bool Shared = Args.hasArg(options::OPT_shared);
62  const bool Profiling = Args.hasArg(options::OPT_pg);
63  const bool Pie = Args.hasArg(options::OPT_pie);
64  ArgStringList CmdArgs;
65 
66  if (!D.SysRoot.empty())
67  CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
68 
69  CmdArgs.push_back("--eh-frame-hdr");
70  if (Static) {
71  CmdArgs.push_back("-Bstatic");
72  } else {
73  if (Args.hasArg(options::OPT_rdynamic))
74  CmdArgs.push_back("-export-dynamic");
75  if (Shared)
76  CmdArgs.push_back("-shared");
77  else if (!Args.hasArg(options::OPT_r)) {
78  CmdArgs.push_back("-dynamic-linker");
79  CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
80  }
81  CmdArgs.push_back("--hash-style=gnu");
82  CmdArgs.push_back("--enable-new-dtags");
83  }
84 
85  // When building 32-bit code on DragonFly/pc64, we have to explicitly
86  // instruct ld in the base system to link 32-bit code.
87  if (Arch == llvm::Triple::x86) {
88  CmdArgs.push_back("-m");
89  CmdArgs.push_back("elf_i386");
90  }
91 
92  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
93  if (Output.isFilename()) {
94  CmdArgs.push_back("-o");
95  CmdArgs.push_back(Output.getFilename());
96  }
97 
98  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
99  options::OPT_r)) {
100  const char *crt1 = nullptr;
101  const char *crtbegin = nullptr;
102  if (!Shared) {
103  if (Profiling)
104  crt1 = "gcrt1.o";
105  else {
106  if (Pie)
107  crt1 = "Scrt1.o";
108  else
109  crt1 = "crt1.o";
110  }
111  }
112 
113  if (Shared || Pie)
114  crtbegin = "crtbeginS.o";
115  else
116  crtbegin = "crtbegin.o";
117 
118  if (crt1)
119  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
120  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
121  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
122  }
123 
124  Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
125  options::OPT_s, options::OPT_t});
126  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
127 
128  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
129 
130  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
131  options::OPT_r)) {
132  if (!Static) {
133  CmdArgs.push_back("-rpath");
134  CmdArgs.push_back("/usr/lib/gcc80");
135  }
136 
137  // Use the static OpenMP runtime with -static-openmp
138  bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
139  addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
140 
141  if (D.CCCIsCXX()) {
142  if (ToolChain.ShouldLinkCXXStdlib(Args))
143  ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
144  CmdArgs.push_back("-lm");
145  }
146 
147  // Silence warnings when linking C code with a C++ '-stdlib' argument.
148  Args.ClaimAllArgs(options::OPT_stdlib_EQ);
149 
150  // Additional linker set-up and flags for Fortran. This is required in order
151  // to generate executables. As Fortran runtime depends on the C runtime,
152  // these dependencies need to be listed before the C runtime below (i.e.
153  // AddRunTimeLibs).
154  if (D.IsFlangMode()) {
155  addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
156  addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
157  CmdArgs.push_back("-lm");
158  }
159 
160  if (Args.hasArg(options::OPT_pthread))
161  CmdArgs.push_back("-lpthread");
162 
163  if (!Args.hasArg(options::OPT_nolibc))
164  CmdArgs.push_back("-lc");
165 
166  if (Static || Args.hasArg(options::OPT_static_libgcc)) {
167  CmdArgs.push_back("-lgcc");
168  CmdArgs.push_back("-lgcc_eh");
169  } else {
170  if (Args.hasArg(options::OPT_shared_libgcc)) {
171  CmdArgs.push_back("-lgcc_pic");
172  if (!Shared)
173  CmdArgs.push_back("-lgcc");
174  } else {
175  CmdArgs.push_back("-lgcc");
176  CmdArgs.push_back("--as-needed");
177  CmdArgs.push_back("-lgcc_pic");
178  CmdArgs.push_back("--no-as-needed");
179  }
180  }
181  }
182 
183  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
184  options::OPT_r)) {
185  const char *crtend = nullptr;
186  if (Shared || Pie)
187  crtend ="crtendS.o";
188  else
189  crtend = "crtend.o";
190 
191  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtend)));
192  CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
193  }
194 
195  ToolChain.addProfileRTLibs(Args, CmdArgs);
196 
197  const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
198  C.addCommand(std::make_unique<Command>(JA, *this,
200  Exec, CmdArgs, Inputs, Output));
201 }
202 
203 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
204 
205 DragonFly::DragonFly(const Driver &D, const llvm::Triple &Triple,
206  const ArgList &Args)
207  : Generic_ELF(D, Triple, Args) {
208  // Path mangling to find libexec
209  getProgramPaths().push_back(getDriver().Dir);
210 
211  getFilePaths().push_back(getDriver().Dir + "/../lib");
212  getFilePaths().push_back(concat(getDriver().SysRoot, "/usr/lib"));
213  getFilePaths().push_back(concat(getDriver().SysRoot, "/usr/lib/gcc80"));
214 }
215 
217  const llvm::opt::ArgList &DriverArgs,
218  llvm::opt::ArgStringList &CC1Args) const {
219  const Driver &D = getDriver();
220 
221  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
222  return;
223 
224  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
225  SmallString<128> Dir(D.ResourceDir);
226  llvm::sys::path::append(Dir, "include");
227  addSystemInclude(DriverArgs, CC1Args, Dir.str());
228  }
229 
230  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
231  return;
232 
233  addExternCSystemInclude(DriverArgs, CC1Args,
234  concat(D.SysRoot, "/usr/include"));
235 }
236 
237 void DragonFly::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
238  llvm::opt::ArgStringList &CC1Args) const {
239  addLibStdCXXIncludePaths(concat(getDriver().SysRoot, "/usr/include/c++/8.0"), "", "",
240  DriverArgs, CC1Args);
241 }
242 
244  return new tools::dragonfly::Assembler(*this);
245 }
246 
248  return new tools::dragonfly::Linker(*this);
249 }
const Decl * D
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
bool isNothing() const
Definition: InputInfo.h:74
bool isFilename() const
Definition: InputInfo.h:75
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
const Driver & getDriver() const
Definition: ToolChain.h:269
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1287
path_list & getFilePaths()
Definition: ToolChain.h:311
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1389
static void addExternCSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory with extern "C" semantics to CC1 arguments.
Definition: ToolChain.cpp:1302
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:977
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:285
static std::string concat(StringRef Path, const Twine &A, const Twine &B="", const Twine &C="", const Twine &D="")
Definition: ToolChain.cpp:1326
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:1395
virtual void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
addProfileRTLibs - When -fprofile-instr-profile is specified, try to pass a suitable profile runtime ...
Definition: ToolChain.cpp:1191
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:985
std::string GetProgramPath(const char *Name) const
Definition: ToolChain.cpp:981
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:1414
path_list & getProgramPaths()
Definition: ToolChain.h:314
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
Tool * buildLinker() const override
Definition: DragonFly.cpp:247
void addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition: DragonFly.cpp:237
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: DragonFly.cpp:216
Tool * buildAssembler() const override
Definition: DragonFly.cpp:243
bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple, Twine IncludeSuffix, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, bool DetectDebian=false) const
Definition: Gnu.cpp:3464
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: DragonFly.cpp:23
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: DragonFly.cpp:52
void addFortranRuntimeLibraryPath(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Adds the path for the Fortran runtime libraries to CmdArgs.
bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, const llvm::opt::ArgList &Args, bool ForceStaticHostRuntime=false, bool IsOffloadingHost=false, bool GompNeedsRT=false)
Returns true, if an OpenMP runtime has been added.
void claimNoWarnArgs(const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Adds Fortran runtime libraries to CmdArgs.
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()
Definition: Job.h:93