clang 23.0.0git
Haiku.cpp
Go to the documentation of this file.
1//===--- Haiku.cpp - Haiku 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 "Haiku.h"
10#include "clang/Config/config.h"
14#include "llvm/Support/Path.h"
15
16using namespace clang::driver;
17using namespace clang::driver::tools;
18using namespace clang::driver::toolchains;
19using namespace clang;
20using namespace llvm::opt;
21
23 const InputInfo &Output,
24 const InputInfoList &Inputs,
25 const ArgList &Args,
26 const char *LinkingOutput) const {
27 const auto &ToolChain = static_cast<const Haiku &>(getToolChain());
28 const Driver &D = ToolChain.getDriver();
29 const llvm::Triple &Triple = ToolChain.getTriple();
30 const bool Static = Args.hasArg(options::OPT_static);
31 const bool Shared = Args.hasArg(options::OPT_shared);
32 ArgStringList CmdArgs;
33
34 // Silence warning for "clang -g foo.o -o foo"
35 Args.ClaimAllArgs(options::OPT_g_Group);
36 // and "clang -emit-llvm foo.o -o foo"
37 Args.ClaimAllArgs(options::OPT_emit_llvm);
38 // and for "clang -w foo.o -o foo". Other warning options are already
39 // handled somewhere else.
40 Args.ClaimAllArgs(options::OPT_w);
41
42 // Silence warning for "clang -pie foo.o -o foo"
43 Args.ClaimAllArgs(options::OPT_pie);
44
45 // -rdynamic is a no-op with Haiku. Claim argument to avoid warning.
46 Args.ClaimAllArgs(options::OPT_rdynamic);
47
48 if (!D.SysRoot.empty())
49 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
50
51 CmdArgs.push_back("--eh-frame-hdr");
52 if (Static) {
53 CmdArgs.push_back("-Bstatic");
54 } else {
55 if (Shared)
56 CmdArgs.push_back("-shared");
57 CmdArgs.push_back("--enable-new-dtags");
58 }
59
60 CmdArgs.push_back("-shared");
61
62 if (!Shared)
63 CmdArgs.push_back("--no-undefined");
64
65 if (Triple.isRISCV64()) {
66 CmdArgs.push_back("-X");
67 if (Args.hasArg(options::OPT_mno_relax))
68 CmdArgs.push_back("--no-relax");
69 }
70
71 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
72 if (Output.isFilename()) {
73 CmdArgs.push_back("-o");
74 CmdArgs.push_back(Output.getFilename());
75 }
76
77 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
78 options::OPT_r)) {
79 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
80 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
81 if (!Shared)
82 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("start_dyn.o")));
83 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("init_term_dyn.o")));
84 }
85
86 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
87 options::OPT_s, options::OPT_t});
88 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
89
90 if (auto LTO = ToolChain.getLTOMode(Args); LTO != LTOK_None)
91 addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs, LTO == LTOK_Thin);
92
93 bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
95 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
96
97 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
98 options::OPT_r)) {
99 // Use the static OpenMP runtime with -static-openmp
100 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
101 addOpenMPRuntime(C, CmdArgs, ToolChain, Args, StaticOpenMP);
102
103 if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
104 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
105
106 // Silence warnings when linking C code with a C++ '-stdlib' argument.
107 Args.ClaimAllArgs(options::OPT_stdlib_EQ);
108
109 // Additional linker set-up and flags for Fortran. This is required in order
110 // to generate executables. As Fortran runtime depends on the C runtime,
111 // these dependencies need to be listed before the C runtime below (i.e.
112 // AddRunTimeLibs).
113 if (D.IsFlangMode() &&
114 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
116 ToolChain.addFortranRuntimeLibs(Args, CmdArgs);
117 }
118
119 if (NeedsSanitizerDeps)
120 linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
121
122 CmdArgs.push_back("-lgcc");
123
124 CmdArgs.push_back("--push-state");
125 CmdArgs.push_back("--as-needed");
126 CmdArgs.push_back("-lgcc_s");
127 CmdArgs.push_back("--no-as-needed");
128 CmdArgs.push_back("--pop-state");
129
130 CmdArgs.push_back("-lroot");
131
132 CmdArgs.push_back("-lgcc");
133
134 CmdArgs.push_back("--push-state");
135 CmdArgs.push_back("--as-needed");
136 CmdArgs.push_back("-lgcc_s");
137 CmdArgs.push_back("--no-as-needed");
138 CmdArgs.push_back("--pop-state");
139 }
140
141 // No need to do anything for pthreads. Claim argument to avoid warning.
142 Args.claimAllArgs(options::OPT_pthread, options::OPT_pthreads);
143
144 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
145 options::OPT_r)) {
146 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
147 CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
148 }
149
150 ToolChain.addProfileRTLibs(Args, CmdArgs);
151
152 const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
153 C.addCommand(std::make_unique<Command>(JA, *this,
155 Exec, CmdArgs, Inputs, Output));
156}
157
158/// Haiku - Haiku tool chain which can call as(1) and ld(1) directly.
159
160Haiku::Haiku(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
161 : Generic_ELF(D, Triple, Args) {
162
163 GCCInstallation.init(Triple, Args);
164
165 getFilePaths().push_back(concat(getDriver().SysRoot, "/boot/system/lib"));
166 getFilePaths().push_back(concat(getDriver().SysRoot, "/boot/system/develop/lib"));
167
168 if (GCCInstallation.isValid())
169 getFilePaths().push_back(GCCInstallation.getInstallPath().str());
170}
171
172void Haiku::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
173 llvm::opt::ArgStringList &CC1Args) const {
174 const Driver &D = getDriver();
175
176 if (DriverArgs.hasArg(options::OPT_nostdinc))
177 return;
178
179 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
180 SmallString<128> Dir(D.ResourceDir);
181 llvm::sys::path::append(Dir, "include");
182 addSystemInclude(DriverArgs, CC1Args, Dir.str());
183 }
184
185 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
186 return;
187
188 // Add dirs specified via 'configure --with-c-include-dirs'.
189 StringRef CIncludeDirs(C_INCLUDE_DIRS);
190 if (!CIncludeDirs.empty()) {
192 CIncludeDirs.split(dirs, ":");
193 for (StringRef dir : dirs) {
194 StringRef Prefix =
195 llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
196 addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
197 }
198 return;
199 }
200
201 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
202 "/boot/system/non-packaged/develop/headers"));
203 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
204 "/boot/system/develop/headers/os"));
205 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
206 "/boot/system/develop/headers/os/app"));
207 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
208 "/boot/system/develop/headers/os/device"));
209 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
210 "/boot/system/develop/headers/os/drivers"));
211 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
212 "/boot/system/develop/headers/os/game"));
213 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
214 "/boot/system/develop/headers/os/interface"));
215 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
216 "/boot/system/develop/headers/os/kernel"));
217 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
218 "/boot/system/develop/headers/os/locale"));
219 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
220 "/boot/system/develop/headers/os/mail"));
221 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
222 "/boot/system/develop/headers/os/media"));
223 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
224 "/boot/system/develop/headers/os/midi"));
225 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
226 "/boot/system/develop/headers/os/midi2"));
227 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
228 "/boot/system/develop/headers/os/net"));
229 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
230 "/boot/system/develop/headers/os/opengl"));
231 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
232 "/boot/system/develop/headers/os/storage"));
233 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
234 "/boot/system/develop/headers/os/support"));
235 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
236 "/boot/system/develop/headers/os/translation"));
237 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
238 "/boot/system/develop/headers/os/add-ons/graphics"));
239 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
240 "/boot/system/develop/headers/os/add-ons/input_server"));
241 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
242 "/boot/system/develop/headers/os/add-ons/mail_daemon"));
243 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
244 "/boot/system/develop/headers/os/add-ons/registrar"));
245 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
246 "/boot/system/develop/headers/os/add-ons/screen_saver"));
247 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
248 "/boot/system/develop/headers/os/add-ons/tracker"));
249 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
250 "/boot/system/develop/headers/os/be_apps/Deskbar"));
251 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
252 "/boot/system/develop/headers/os/be_apps/NetPositive"));
253 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
254 "/boot/system/develop/headers/os/be_apps/Tracker"));
255 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
256 "/boot/system/develop/headers/3rdparty"));
257 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
258 "/boot/system/develop/headers/bsd"));
259 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
260 "/boot/system/develop/headers/glibc"));
261 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
262 "/boot/system/develop/headers/gnu"));
263 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
264 "/boot/system/develop/headers/posix"));
265 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
266 "/boot/system/develop/headers/gcc/include"));
267 addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
268 "/boot/system/develop/headers"));
269}
270
271void Haiku::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
272 llvm::opt::ArgStringList &CC1Args) const {
273 addSystemInclude(DriverArgs, CC1Args,
274 concat(getDriver().SysRoot, "/boot/system/develop/headers/c++/v1"));
275}
276
277Tool *Haiku::buildLinker() const { return new tools::haiku::Linker(*this); }
278
279bool Haiku::HasNativeLLVMSupport() const { return true; }
280
283 Action::OffloadKind DeviceOffloadKind) const {
284 SanitizerMask Res =
285 ToolChain::getSupportedSanitizers(BoundArch, DeviceOffloadKind);
286
287 Res |= SanitizerKind::Address;
288
289 return Res;
290}
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:94
std::string SysRoot
sysroot, if present
Definition Driver.h:194
bool IsFlangMode() const
Whether the driver should invoke flang for fortran inputs.
Definition Driver.h:234
bool CCCIsCXX() const
Whether the driver should follow g++ like behavior.
Definition Driver.h:221
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:95
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.
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
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.
virtual void addFortranRuntimeLibraryPath(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Adds the path for the Fortran runtime libraries to CmdArgs.
std::string GetFilePath(const char *Name) const
virtual void addFortranRuntimeLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Adds Fortran runtime libraries to CmdArgs.
path_list & getFilePaths()
Definition ToolChain.h:325
const Driver & getDriver() const
Definition ToolChain.h:285
static std::string concat(StringRef Path, const Twine &A, const Twine &B="", const Twine &C="", const Twine &D="")
virtual LTOKind getLTOMode(const llvm::opt::ArgList &Args, Action::OffloadKind Kind=Action::OFK_None) const
Resolve the requested LTO mode for this toolchain.
virtual SanitizerMask getSupportedSanitizers(StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const
Return sanitizers which are available in this toolchain.
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...
const llvm::Triple & getTriple() const
Definition ToolChain.h:287
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 ...
virtual void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Tool - Information on a specific compilation tool.
Definition Tool.h:32
const ToolChain & getToolChain() const
Definition Tool.h:52
Generic_ELF(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition Gnu.h:439
GCCInstallationDetector GCCInstallation
Definition Gnu.h:357
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition Haiku.cpp:172
SanitizerMask getSupportedSanitizers(StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
Return sanitizers which are available in this toolchain.
Definition Haiku.cpp:282
void addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Definition Haiku.cpp:271
Tool * buildLinker() const override
Definition Haiku.cpp:277
Haiku(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Haiku - Haiku tool chain which can call as(1) and ld(1) directly.
Definition Haiku.cpp:160
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition Haiku.cpp:279
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 Haiku.cpp:22
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, const InputInfoList &Inputs, bool IsThinLTO)
void linkSanitizerRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void addLinkerCompressDebugSectionsOption(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &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 AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:50
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()
Definition Job.h:92