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"
11#include "clang/Driver/CommonArgs.h"
12#include "clang/Driver/Compilation.h"
13#include "clang/Driver/SanitizerArgs.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
22void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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(Ids: options::OPT_static);
31 const bool Shared = Args.hasArg(Ids: options::OPT_shared);
32 ArgStringList CmdArgs;
33
34 // Silence warning for "clang -g foo.o -o foo"
35 Args.ClaimAllArgs(Id0: options::OPT_g_Group);
36 // and "clang -emit-llvm foo.o -o foo"
37 Args.ClaimAllArgs(Id0: 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(Id0: options::OPT_w);
41
42 // Silence warning for "clang -pie foo.o -o foo"
43 Args.ClaimAllArgs(Id0: options::OPT_pie);
44
45 // -rdynamic is a no-op with Haiku. Claim argument to avoid warning.
46 Args.ClaimAllArgs(Id0: options::OPT_rdynamic);
47
48 if (!D.SysRoot.empty())
49 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--sysroot=" + D.SysRoot));
50
51 CmdArgs.push_back(Elt: "--eh-frame-hdr");
52 if (Static) {
53 CmdArgs.push_back(Elt: "-Bstatic");
54 } else {
55 if (Shared)
56 CmdArgs.push_back(Elt: "-shared");
57 CmdArgs.push_back(Elt: "--enable-new-dtags");
58 }
59
60 CmdArgs.push_back(Elt: "-shared");
61
62 if (!Shared)
63 CmdArgs.push_back(Elt: "--no-undefined");
64
65 if (Triple.isRISCV64()) {
66 CmdArgs.push_back(Elt: "-X");
67 if (Args.hasArg(Ids: options::OPT_mno_relax))
68 CmdArgs.push_back(Elt: "--no-relax");
69 }
70
71 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
72 if (Output.isFilename()) {
73 CmdArgs.push_back(Elt: "-o");
74 CmdArgs.push_back(Elt: Output.getFilename());
75 }
76
77 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles,
78 Ids: options::OPT_r)) {
79 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crti.o")));
80 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crtbeginS.o")));
81 if (!Shared)
82 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "start_dyn.o")));
83 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "init_term_dyn.o")));
84 }
85
86 Args.addAllArgs(Output&: CmdArgs, Ids: {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, IsThinLTO: LTO == LTOK_Thin);
92
93 bool NeedsSanitizerDeps = addSanitizerRuntimes(TC: ToolChain, Args, CmdArgs);
94 addLinkerCompressDebugSectionsOption(TC: ToolChain, Args, CmdArgs);
95 AddLinkerInputs(TC: ToolChain, Inputs, Args, CmdArgs, JA);
96
97 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs,
98 Ids: options::OPT_r)) {
99 // Use the static OpenMP runtime with -static-openmp
100 bool StaticOpenMP = Args.hasArg(Ids: options::OPT_static_openmp) && !Static;
101 addOpenMPRuntime(C, CmdArgs, TC: ToolChain, Args, ForceStaticHostRuntime: 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(Id0: 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(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) {
115 ToolChain.addFortranRuntimeLibraryPath(Args, CmdArgs);
116 ToolChain.addFortranRuntimeLibs(Args, CmdArgs);
117 }
118
119 if (NeedsSanitizerDeps)
120 linkSanitizerRuntimeDeps(TC: ToolChain, Args, CmdArgs);
121
122 CmdArgs.push_back(Elt: "-lgcc");
123
124 CmdArgs.push_back(Elt: "--push-state");
125 CmdArgs.push_back(Elt: "--as-needed");
126 CmdArgs.push_back(Elt: "-lgcc_s");
127 CmdArgs.push_back(Elt: "--no-as-needed");
128 CmdArgs.push_back(Elt: "--pop-state");
129
130 CmdArgs.push_back(Elt: "-lroot");
131
132 CmdArgs.push_back(Elt: "-lgcc");
133
134 CmdArgs.push_back(Elt: "--push-state");
135 CmdArgs.push_back(Elt: "--as-needed");
136 CmdArgs.push_back(Elt: "-lgcc_s");
137 CmdArgs.push_back(Elt: "--no-as-needed");
138 CmdArgs.push_back(Elt: "--pop-state");
139 }
140
141 // No need to do anything for pthreads. Claim argument to avoid warning.
142 Args.claimAllArgs(Ids: options::OPT_pthread, Ids: options::OPT_pthreads);
143
144 if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nostartfiles,
145 Ids: options::OPT_r)) {
146 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crtendS.o")));
147 CmdArgs.push_back(Elt: Args.MakeArgString(Str: ToolChain.GetFilePath(Name: "crtn.o")));
148 }
149
150 ToolChain.addProfileRTLibs(Args, CmdArgs);
151
152 const char *Exec = Args.MakeArgString(Str: getToolChain().GetLinkerPath());
153 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this,
154 args: ResponseFileSupport::AtFileCurCP(),
155 args&: Exec, args&: CmdArgs, args: Inputs, args: 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(TargetTriple: Triple, Args);
164
165 getFilePaths().push_back(Elt: concat(Path: getDriver().SysRoot, A: "/boot/system/lib"));
166 getFilePaths().push_back(Elt: concat(Path: getDriver().SysRoot, A: "/boot/system/develop/lib"));
167
168 if (GCCInstallation.isValid())
169 getFilePaths().push_back(Elt: 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(Ids: options::OPT_nostdinc))
177 return;
178
179 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
180 SmallString<128> Dir(D.ResourceDir);
181 llvm::sys::path::append(path&: Dir, a: "include");
182 addSystemInclude(DriverArgs, CC1Args, Path: Dir.str());
183 }
184
185 if (DriverArgs.hasArg(Ids: 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()) {
191 SmallVector<StringRef, 5> dirs;
192 CIncludeDirs.split(A&: dirs, Separator: ":");
193 for (StringRef dir : dirs) {
194 StringRef Prefix =
195 llvm::sys::path::is_absolute(path: dir) ? StringRef(D.SysRoot) : "";
196 addExternCSystemInclude(DriverArgs, CC1Args, Path: Prefix + dir);
197 }
198 return;
199 }
200
201 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
202 A: "/boot/system/non-packaged/develop/headers"));
203 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
204 A: "/boot/system/develop/headers/os"));
205 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
206 A: "/boot/system/develop/headers/os/app"));
207 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
208 A: "/boot/system/develop/headers/os/device"));
209 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
210 A: "/boot/system/develop/headers/os/drivers"));
211 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
212 A: "/boot/system/develop/headers/os/game"));
213 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
214 A: "/boot/system/develop/headers/os/interface"));
215 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
216 A: "/boot/system/develop/headers/os/kernel"));
217 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
218 A: "/boot/system/develop/headers/os/locale"));
219 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
220 A: "/boot/system/develop/headers/os/mail"));
221 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
222 A: "/boot/system/develop/headers/os/media"));
223 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
224 A: "/boot/system/develop/headers/os/midi"));
225 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
226 A: "/boot/system/develop/headers/os/midi2"));
227 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
228 A: "/boot/system/develop/headers/os/net"));
229 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
230 A: "/boot/system/develop/headers/os/opengl"));
231 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
232 A: "/boot/system/develop/headers/os/storage"));
233 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
234 A: "/boot/system/develop/headers/os/support"));
235 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
236 A: "/boot/system/develop/headers/os/translation"));
237 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
238 A: "/boot/system/develop/headers/os/add-ons/graphics"));
239 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
240 A: "/boot/system/develop/headers/os/add-ons/input_server"));
241 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
242 A: "/boot/system/develop/headers/os/add-ons/mail_daemon"));
243 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
244 A: "/boot/system/develop/headers/os/add-ons/registrar"));
245 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
246 A: "/boot/system/develop/headers/os/add-ons/screen_saver"));
247 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
248 A: "/boot/system/develop/headers/os/add-ons/tracker"));
249 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
250 A: "/boot/system/develop/headers/os/be_apps/Deskbar"));
251 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
252 A: "/boot/system/develop/headers/os/be_apps/NetPositive"));
253 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
254 A: "/boot/system/develop/headers/os/be_apps/Tracker"));
255 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
256 A: "/boot/system/develop/headers/3rdparty"));
257 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
258 A: "/boot/system/develop/headers/bsd"));
259 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
260 A: "/boot/system/develop/headers/glibc"));
261 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
262 A: "/boot/system/develop/headers/gnu"));
263 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
264 A: "/boot/system/develop/headers/posix"));
265 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
266 A: "/boot/system/develop/headers/gcc/include"));
267 addSystemInclude(DriverArgs, CC1Args, Path: concat(Path: D.SysRoot,
268 A: "/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 Path: concat(Path: getDriver().SysRoot, A: "/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
281SanitizerMask
282Haiku::getSupportedSanitizers(BoundArch BA,
283 Action::OffloadKind DeviceOffloadKind) const {
284 SanitizerMask Res = ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind);
285
286 Res |= SanitizerKind::Address;
287
288 return Res;
289}
290