1 //===--- DragonFly.cpp - DragonFly ToolChain Implementations ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #include "CommonArgs.h"
11 #include "clang/Driver/Compilation.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"
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
;
23 void dragonfly::Assembler::ConstructJob(Compilation
&C
, const JobAction
&JA
,
24 const InputInfo
&Output
,
25 const InputInfoList
&Inputs
,
27 const char *LinkingOutput
) const {
28 const auto &ToolChain
= static_cast<const DragonFly
&>(getToolChain());
29 ArgStringList CmdArgs
;
31 claimNoWarnArgs(Args
);
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");
38 Args
.AddAllArgValues(CmdArgs
, options::OPT_Wa_COMMA
, options::OPT_Xassembler
);
40 CmdArgs
.push_back("-o");
41 CmdArgs
.push_back(Output
.getFilename());
43 for (const auto &II
: Inputs
)
44 CmdArgs
.push_back(II
.getFilename());
46 const char *Exec
= Args
.MakeArgString(ToolChain
.GetProgramPath("as"));
47 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
48 ResponseFileSupport::AtFileCurCP(),
49 Exec
, CmdArgs
, Inputs
, Output
));
52 void dragonfly::Linker::ConstructJob(Compilation
&C
, const JobAction
&JA
,
53 const InputInfo
&Output
,
54 const InputInfoList
&Inputs
,
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
;
66 if (!D
.SysRoot
.empty())
67 CmdArgs
.push_back(Args
.MakeArgString("--sysroot=" + D
.SysRoot
));
69 CmdArgs
.push_back("--eh-frame-hdr");
71 CmdArgs
.push_back("-Bstatic");
73 if (Args
.hasArg(options::OPT_rdynamic
))
74 CmdArgs
.push_back("-export-dynamic");
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");
81 CmdArgs
.push_back("--hash-style=gnu");
82 CmdArgs
.push_back("--enable-new-dtags");
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");
92 assert((Output
.isFilename() || Output
.isNothing()) && "Invalid output.");
93 if (Output
.isFilename()) {
94 CmdArgs
.push_back("-o");
95 CmdArgs
.push_back(Output
.getFilename());
98 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
100 const char *crt1
= nullptr;
101 const char *crtbegin
= nullptr;
114 crtbegin
= "crtbeginS.o";
116 crtbegin
= "crtbegin.o";
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
)));
124 Args
.addAllArgs(CmdArgs
, {options::OPT_L
, options::OPT_T_Group
,
125 options::OPT_s
, options::OPT_t
, options::OPT_r
});
126 ToolChain
.AddFilePathLibArgs(Args
, CmdArgs
);
128 AddLinkerInputs(ToolChain
, Inputs
, Args
, CmdArgs
, JA
);
130 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nodefaultlibs
,
133 CmdArgs
.push_back("-rpath");
134 CmdArgs
.push_back("/usr/lib/gcc80");
137 // Use the static OpenMP runtime with -static-openmp
138 bool StaticOpenMP
= Args
.hasArg(options::OPT_static_openmp
) && !Static
;
139 addOpenMPRuntime(CmdArgs
, ToolChain
, Args
, StaticOpenMP
);
142 if (ToolChain
.ShouldLinkCXXStdlib(Args
))
143 ToolChain
.AddCXXStdlibLibArgs(Args
, CmdArgs
);
144 CmdArgs
.push_back("-lm");
147 // Silence warnings when linking C code with a C++ '-stdlib' argument.
148 Args
.ClaimAllArgs(options::OPT_stdlib_EQ
);
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.
154 if (D
.IsFlangMode()) {
155 addFortranRuntimeLibraryPath(ToolChain
, Args
, CmdArgs
);
156 addFortranRuntimeLibs(ToolChain
, Args
, CmdArgs
);
157 CmdArgs
.push_back("-lm");
160 if (Args
.hasArg(options::OPT_pthread
))
161 CmdArgs
.push_back("-lpthread");
163 if (!Args
.hasArg(options::OPT_nolibc
))
164 CmdArgs
.push_back("-lc");
166 if (Static
|| Args
.hasArg(options::OPT_static_libgcc
)) {
167 CmdArgs
.push_back("-lgcc");
168 CmdArgs
.push_back("-lgcc_eh");
170 if (Args
.hasArg(options::OPT_shared_libgcc
)) {
171 CmdArgs
.push_back("-lgcc_pic");
173 CmdArgs
.push_back("-lgcc");
175 CmdArgs
.push_back("-lgcc");
176 CmdArgs
.push_back("--as-needed");
177 CmdArgs
.push_back("-lgcc_pic");
178 CmdArgs
.push_back("--no-as-needed");
183 if (!Args
.hasArg(options::OPT_nostdlib
, options::OPT_nostartfiles
,
185 const char *crtend
= nullptr;
191 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath(crtend
)));
192 CmdArgs
.push_back(Args
.MakeArgString(ToolChain
.GetFilePath("crtn.o")));
195 ToolChain
.addProfileRTLibs(Args
, CmdArgs
);
197 const char *Exec
= Args
.MakeArgString(ToolChain
.GetLinkerPath());
198 C
.addCommand(std::make_unique
<Command
>(JA
, *this,
199 ResponseFileSupport::AtFileCurCP(),
200 Exec
, CmdArgs
, Inputs
, Output
));
203 /// DragonFly - DragonFly tool chain which can call as(1) and ld(1) directly.
205 DragonFly::DragonFly(const Driver
&D
, const llvm::Triple
&Triple
,
207 : Generic_ELF(D
, Triple
, Args
) {
209 // Path mangling to find libexec
210 getProgramPaths().push_back(getDriver().getInstalledDir());
211 if (getDriver().getInstalledDir() != getDriver().Dir
)
212 getProgramPaths().push_back(getDriver().Dir
);
214 getFilePaths().push_back(getDriver().Dir
+ "/../lib");
215 getFilePaths().push_back(concat(getDriver().SysRoot
, "/usr/lib"));
216 getFilePaths().push_back(concat(getDriver().SysRoot
, "/usr/lib/gcc80"));
219 void DragonFly::AddClangSystemIncludeArgs(
220 const llvm::opt::ArgList
&DriverArgs
,
221 llvm::opt::ArgStringList
&CC1Args
) const {
222 const Driver
&D
= getDriver();
224 if (DriverArgs
.hasArg(clang::driver::options::OPT_nostdinc
))
227 if (!DriverArgs
.hasArg(options::OPT_nobuiltininc
)) {
228 SmallString
<128> Dir(D
.ResourceDir
);
229 llvm::sys::path::append(Dir
, "include");
230 addSystemInclude(DriverArgs
, CC1Args
, Dir
.str());
233 if (DriverArgs
.hasArg(options::OPT_nostdlibinc
))
236 addExternCSystemInclude(DriverArgs
, CC1Args
,
237 concat(D
.SysRoot
, "/usr/include"));
240 void DragonFly::addLibStdCxxIncludePaths(const llvm::opt::ArgList
&DriverArgs
,
241 llvm::opt::ArgStringList
&CC1Args
) const {
242 addLibStdCXXIncludePaths(concat(getDriver().SysRoot
, "/usr/include/c++/8.0"), "", "",
243 DriverArgs
, CC1Args
);
246 Tool
*DragonFly::buildAssembler() const {
247 return new tools::dragonfly::Assembler(*this);
250 Tool
*DragonFly::buildLinker() const {
251 return new tools::dragonfly::Linker(*this);