Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/ToolChains/Haiku.cpp
Line
Count
Source (jump to first uncovered line)
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 "CommonArgs.h"
11
#include "clang/Config/config.h"
12
#include "clang/Driver/Compilation.h"
13
#include "llvm/Support/Path.h"
14
15
using namespace clang::driver;
16
using namespace clang::driver::tools;
17
using namespace clang::driver::toolchains;
18
using namespace clang;
19
using namespace llvm::opt;
20
21
void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
22
                                   const InputInfo &Output,
23
                                   const InputInfoList &Inputs,
24
                                   const ArgList &Args,
25
0
                                   const char *LinkingOutput) const {
26
0
  const auto &ToolChain = static_cast<const Haiku &>(getToolChain());
27
0
  const Driver &D = ToolChain.getDriver();
28
0
  const llvm::Triple::ArchType Arch = ToolChain.getArch();
29
0
  const bool Static = Args.hasArg(options::OPT_static);
30
0
  const bool Shared = Args.hasArg(options::OPT_shared);
31
0
  ArgStringList CmdArgs;
32
33
  // Silence warning for "clang -g foo.o -o foo"
34
0
  Args.ClaimAllArgs(options::OPT_g_Group);
35
  // and "clang -emit-llvm foo.o -o foo"
36
0
  Args.ClaimAllArgs(options::OPT_emit_llvm);
37
  // and for "clang -w foo.o -o foo". Other warning options are already
38
  // handled somewhere else.
39
0
  Args.ClaimAllArgs(options::OPT_w);
40
41
  // Silence warning for "clang -pie foo.o -o foo"
42
0
  Args.ClaimAllArgs(options::OPT_pie);
43
44
  // -rdynamic is a no-op with Haiku. Claim argument to avoid warning.
45
0
  Args.ClaimAllArgs(options::OPT_rdynamic);
46
47
0
  if (!D.SysRoot.empty())
48
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
49
50
0
  CmdArgs.push_back("--eh-frame-hdr");
51
0
  if (Static) {
52
0
    CmdArgs.push_back("-Bstatic");
53
0
  } else {
54
0
    if (Shared)
55
0
      CmdArgs.push_back("-shared");
56
0
    CmdArgs.push_back("--enable-new-dtags");
57
0
  }
58
59
0
  CmdArgs.push_back("-shared");
60
61
0
  if (!Shared)
62
0
    CmdArgs.push_back("--no-undefined");
63
64
0
  if (Arch == llvm::Triple::riscv64)
65
0
    CmdArgs.push_back("-X");
66
67
0
  assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
68
0
  if (Output.isFilename()) {
69
0
    CmdArgs.push_back("-o");
70
0
    CmdArgs.push_back(Output.getFilename());
71
0
  }
72
73
0
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
74
0
                   options::OPT_r)) {
75
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
76
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtbeginS.o")));
77
0
    if (!Shared)
78
0
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("start_dyn.o")));
79
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("init_term_dyn.o")));
80
0
  }
81
82
0
  Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
83
0
                            options::OPT_s, options::OPT_t, options::OPT_r});
84
0
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
85
86
0
  if (D.isUsingLTO()) {
87
0
    assert(!Inputs.empty() && "Must have at least one input.");
88
    // Find the first filename InputInfo object.
89
0
    auto Input = llvm::find_if(
90
0
        Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
91
0
    if (Input == Inputs.end())
92
      // For a very rare case, all of the inputs to the linker are
93
      // InputArg. If that happens, just use the first InputInfo.
94
0
      Input = Inputs.begin();
95
96
0
    addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
97
0
                  D.getLTOMode() == LTOK_Thin);
98
0
  }
99
100
0
  addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
101
0
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
102
103
0
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
104
0
                   options::OPT_r)) {
105
    // Use the static OpenMP runtime with -static-openmp
106
0
    bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !Static;
107
0
    addOpenMPRuntime(CmdArgs, ToolChain, Args, StaticOpenMP);
108
109
0
    if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
110
0
      ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
111
112
    // Silence warnings when linking C code with a C++ '-stdlib' argument.
113
0
    Args.ClaimAllArgs(options::OPT_stdlib_EQ);
114
115
    // Additional linker set-up and flags for Fortran. This is required in order
116
    // to generate executables. As Fortran runtime depends on the C runtime,
117
    // these dependencies need to be listed before the C runtime below (i.e.
118
    // AddRunTimeLibs).
119
0
    if (D.IsFlangMode()) {
120
0
      addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
121
0
      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
122
0
    }
123
124
0
    CmdArgs.push_back("-lgcc");
125
126
0
    CmdArgs.push_back("--push-state");
127
0
    CmdArgs.push_back("--as-needed");
128
0
    CmdArgs.push_back("-lgcc_s");
129
0
    CmdArgs.push_back("--no-as-needed");
130
0
    CmdArgs.push_back("--pop-state");
131
132
0
    CmdArgs.push_back("-lroot");
133
134
0
    CmdArgs.push_back("-lgcc");
135
136
0
    CmdArgs.push_back("--push-state");
137
0
    CmdArgs.push_back("--as-needed");
138
0
    CmdArgs.push_back("-lgcc_s");
139
0
    CmdArgs.push_back("--no-as-needed");
140
0
    CmdArgs.push_back("--pop-state");
141
0
  }
142
143
  // No need to do anything for pthreads. Claim argument to avoid warning.
144
0
  Args.claimAllArgs(options::OPT_pthread, options::OPT_pthreads);
145
146
0
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
147
0
                   options::OPT_r)) {
148
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
149
0
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
150
0
  }
151
152
0
  ToolChain.addProfileRTLibs(Args, CmdArgs);
153
154
0
  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
155
0
  C.addCommand(std::make_unique<Command>(JA, *this,
156
0
                                         ResponseFileSupport::AtFileCurCP(),
157
0
                                         Exec, CmdArgs, Inputs, Output));
158
0
}
159
160
/// Haiku - Haiku tool chain which can call as(1) and ld(1) directly.
161
162
Haiku::Haiku(const Driver &D, const llvm::Triple& Triple, const ArgList &Args)
163
0
  : Generic_ELF(D, Triple, Args) {
164
165
0
  GCCInstallation.init(Triple, Args);
166
167
0
  getFilePaths().push_back(concat(getDriver().SysRoot, "/boot/system/lib"));
168
0
  getFilePaths().push_back(concat(getDriver().SysRoot, "/boot/system/develop/lib"));
169
170
0
  if (GCCInstallation.isValid())
171
0
    getFilePaths().push_back(GCCInstallation.getInstallPath().str());
172
0
}
173
174
void Haiku::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
175
0
                                      llvm::opt::ArgStringList &CC1Args) const {
176
0
  const Driver &D = getDriver();
177
178
0
  if (DriverArgs.hasArg(options::OPT_nostdinc))
179
0
    return;
180
181
0
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
182
0
    SmallString<128> Dir(D.ResourceDir);
183
0
    llvm::sys::path::append(Dir, "include");
184
0
    addSystemInclude(DriverArgs, CC1Args, Dir.str());
185
0
  }
186
187
0
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
188
0
    return;
189
190
  // Add dirs specified via 'configure --with-c-include-dirs'.
191
0
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
192
0
  if (!CIncludeDirs.empty()) {
193
0
    SmallVector<StringRef, 5> dirs;
194
0
    CIncludeDirs.split(dirs, ":");
195
0
    for (StringRef dir : dirs) {
196
0
      StringRef Prefix =
197
0
        llvm::sys::path::is_absolute(dir) ? StringRef(D.SysRoot) : "";
198
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
199
0
    }
200
0
    return;
201
0
  }
202
203
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
204
0
                   "/boot/system/non-packaged/develop/headers"));
205
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
206
0
                   "/boot/system/develop/headers/os"));
207
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
208
0
                   "/boot/system/develop/headers/os/app"));
209
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
210
0
                   "/boot/system/develop/headers/os/device"));
211
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
212
0
                   "/boot/system/develop/headers/os/drivers"));
213
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
214
0
                   "/boot/system/develop/headers/os/game"));
215
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
216
0
                   "/boot/system/develop/headers/os/interface"));
217
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
218
0
                   "/boot/system/develop/headers/os/kernel"));
219
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
220
0
                   "/boot/system/develop/headers/os/locale"));
221
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
222
0
                   "/boot/system/develop/headers/os/mail"));
223
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
224
0
                   "/boot/system/develop/headers/os/media"));
225
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
226
0
                   "/boot/system/develop/headers/os/midi"));
227
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
228
0
                   "/boot/system/develop/headers/os/midi2"));
229
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
230
0
                   "/boot/system/develop/headers/os/net"));
231
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
232
0
                   "/boot/system/develop/headers/os/opengl"));
233
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
234
0
                   "/boot/system/develop/headers/os/storage"));
235
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
236
0
                   "/boot/system/develop/headers/os/support"));
237
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
238
0
                   "/boot/system/develop/headers/os/translation"));
239
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
240
0
                   "/boot/system/develop/headers/os/add-ons/graphics"));
241
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
242
0
                   "/boot/system/develop/headers/os/add-ons/input_server"));
243
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
244
0
                   "/boot/system/develop/headers/os/add-ons/mail_daemon"));
245
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
246
0
                   "/boot/system/develop/headers/os/add-ons/registrar"));
247
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
248
0
                   "/boot/system/develop/headers/os/add-ons/screen_saver"));
249
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
250
0
                   "/boot/system/develop/headers/os/add-ons/tracker"));
251
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
252
0
                   "/boot/system/develop/headers/os/be_apps/Deskbar"));
253
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
254
0
                   "/boot/system/develop/headers/os/be_apps/NetPositive"));
255
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
256
0
                   "/boot/system/develop/headers/os/be_apps/Tracker"));
257
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
258
0
                   "/boot/system/develop/headers/3rdparty"));
259
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
260
0
                   "/boot/system/develop/headers/bsd"));
261
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
262
0
                   "/boot/system/develop/headers/glibc"));
263
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
264
0
                   "/boot/system/develop/headers/gnu"));
265
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
266
0
                   "/boot/system/develop/headers/posix"));
267
0
  addSystemInclude(DriverArgs, CC1Args, concat(D.SysRoot,
268
0
                   "/boot/system/develop/headers"));
269
0
}
270
271
void Haiku::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
272
0
                                  llvm::opt::ArgStringList &CC1Args) const {
273
0
  addSystemInclude(DriverArgs, CC1Args,
274
0
                   concat(getDriver().SysRoot, "/boot/system/develop/headers/c++/v1"));
275
0
}
276
277
0
Tool *Haiku::buildLinker() const { return new tools::haiku::Linker(*this); }
278
279
0
bool Haiku::HasNativeLLVMSupport() const { return true; }