14#include "clang/Config/config.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Support/VirtualFileSystem.h"
32 const char *LinkingOutput)
const {
36 ArgStringList CmdArgs;
43 case llvm::Triple::x86:
44 CmdArgs.push_back(
"--32");
46 case llvm::Triple::arm:
47 case llvm::Triple::armeb:
48 case llvm::Triple::thumb:
49 case llvm::Triple::thumbeb: {
50 StringRef MArch, MCPU;
53 CmdArgs.push_back(Args.MakeArgString(
"-mcpu=" + Arch));
57 case llvm::Triple::mips:
58 case llvm::Triple::mipsel:
59 case llvm::Triple::mips64:
60 case llvm::Triple::mips64el: {
65 CmdArgs.push_back(
"-march");
66 CmdArgs.push_back(CPUName.data());
68 CmdArgs.push_back(
"-mabi");
71 if (Triple.isLittleEndian())
72 CmdArgs.push_back(
"-EL");
74 CmdArgs.push_back(
"-EB");
80 case llvm::Triple::sparc: {
81 CmdArgs.push_back(
"-32");
88 case llvm::Triple::sparcv9: {
89 CmdArgs.push_back(
"-64");
100 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
102 CmdArgs.push_back(
"-o");
105 for (
const auto &II : Inputs)
106 CmdArgs.push_back(II.getFilename());
109 C.addCommand(std::make_unique<Command>(JA, *
this,
111 Exec, CmdArgs, Inputs, Output));
118 const char *LinkingOutput)
const {
123 const bool Static = Args.hasArg(options::OPT_static);
124 const bool Shared = Args.hasArg(options::OPT_shared);
125 const bool Pie = Args.hasArg(options::OPT_pie);
126 ArgStringList CmdArgs;
128 if (!
D.SysRoot.empty())
129 CmdArgs.push_back(Args.MakeArgString(
"--sysroot=" +
D.SysRoot));
131 CmdArgs.push_back(
"--eh-frame-hdr");
133 CmdArgs.push_back(
"-Bstatic");
135 Args.AddAllArgs(CmdArgs, options::OPT_pie);
136 CmdArgs.push_back(
"--no-dynamic-linker");
139 if (Args.hasArg(options::OPT_rdynamic))
140 CmdArgs.push_back(
"-export-dynamic");
142 CmdArgs.push_back(
"-shared");
143 }
else if (!Args.hasArg(options::OPT_r)) {
144 Args.AddAllArgs(CmdArgs, options::OPT_pie);
145 CmdArgs.push_back(
"-dynamic-linker");
146 CmdArgs.push_back(
"/libexec/ld.elf_so");
153 case llvm::Triple::x86:
154 CmdArgs.push_back(
"-m");
155 CmdArgs.push_back(
"elf_i386");
157 case llvm::Triple::arm:
158 case llvm::Triple::thumb:
159 CmdArgs.push_back(
"-m");
160 switch (Triple.getEnvironment()) {
161 case llvm::Triple::EABI:
162 case llvm::Triple::GNUEABI:
163 CmdArgs.push_back(
"armelf_nbsd_eabi");
165 case llvm::Triple::EABIHF:
166 case llvm::Triple::GNUEABIHF:
167 CmdArgs.push_back(
"armelf_nbsd_eabihf");
170 CmdArgs.push_back(
"armelf_nbsd");
174 case llvm::Triple::armeb:
175 case llvm::Triple::thumbeb:
177 CmdArgs.push_back(
"-m");
178 switch (Triple.getEnvironment()) {
179 case llvm::Triple::EABI:
180 case llvm::Triple::GNUEABI:
181 CmdArgs.push_back(
"armelfb_nbsd_eabi");
183 case llvm::Triple::EABIHF:
184 case llvm::Triple::GNUEABIHF:
185 CmdArgs.push_back(
"armelfb_nbsd_eabihf");
188 CmdArgs.push_back(
"armelfb_nbsd");
192 case llvm::Triple::mips64:
193 case llvm::Triple::mips64el:
195 CmdArgs.push_back(
"-m");
196 if (Arch == llvm::Triple::mips64)
197 CmdArgs.push_back(
"elf32btsmip");
199 CmdArgs.push_back(
"elf32ltsmip");
201 CmdArgs.push_back(
"-m");
202 if (Arch == llvm::Triple::mips64)
203 CmdArgs.push_back(
"elf64btsmip");
205 CmdArgs.push_back(
"elf64ltsmip");
208 case llvm::Triple::ppc:
209 CmdArgs.push_back(
"-m");
210 CmdArgs.push_back(
"elf32ppc_nbsd");
213 case llvm::Triple::ppc64:
214 case llvm::Triple::ppc64le:
215 CmdArgs.push_back(
"-m");
216 CmdArgs.push_back(
"elf64ppc");
219 case llvm::Triple::riscv32:
220 CmdArgs.push_back(
"-m");
221 CmdArgs.push_back(
"elf32lriscv");
224 case llvm::Triple::riscv64:
225 CmdArgs.push_back(
"-m");
226 CmdArgs.push_back(
"elf64lriscv");
229 case llvm::Triple::sparc:
230 CmdArgs.push_back(
"-m");
231 CmdArgs.push_back(
"elf32_sparc");
234 case llvm::Triple::sparcv9:
235 CmdArgs.push_back(
"-m");
236 CmdArgs.push_back(
"elf64_sparc");
243 if (Triple.isRISCV()) {
244 CmdArgs.push_back(
"-X");
245 if (Args.hasArg(options::OPT_mno_relax))
246 CmdArgs.push_back(
"--no-relax");
251 CmdArgs.push_back(
"-o");
255 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
257 const char *crt0 =
nullptr;
258 const char *crtbegin =
nullptr;
263 crtbegin =
"crtbeginS.o";
265 crtbegin =
"crtbegin.o";
273 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
274 options::OPT_s, options::OPT_t});
283 CmdArgs.push_back(
"-rpath");
287 bool useLibgcc =
true;
289 case llvm::Triple::aarch64:
290 case llvm::Triple::aarch64_be:
291 case llvm::Triple::arm:
292 case llvm::Triple::armeb:
293 case llvm::Triple::thumb:
294 case llvm::Triple::thumbeb:
295 case llvm::Triple::ppc:
296 case llvm::Triple::ppc64:
297 case llvm::Triple::ppc64le:
298 case llvm::Triple::riscv32:
299 case llvm::Triple::riscv64:
300 case llvm::Triple::sparc:
301 case llvm::Triple::sparcv9:
302 case llvm::Triple::x86:
303 case llvm::Triple::x86_64:
310 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
313 bool StaticOpenMP = Args.hasArg(options::OPT_static_openmp) && !
Static;
319 CmdArgs.push_back(
"-lm");
323 Args.ClaimAllArgs(options::OPT_stdlib_EQ);
329 if (
D.IsFlangMode() &&
330 !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
333 CmdArgs.push_back(
"-lm");
336 if (NeedsSanitizerDeps)
340 if (Args.hasArg(options::OPT_pthread))
341 CmdArgs.push_back(
"-lpthread");
342 CmdArgs.push_back(
"-lc");
349 CmdArgs.push_back(
"-lgcc_eh");
350 CmdArgs.push_back(
"-lc");
351 CmdArgs.push_back(
"-lgcc");
353 CmdArgs.push_back(
"-lgcc");
354 CmdArgs.push_back(
"--as-needed");
355 CmdArgs.push_back(
"-lgcc_s");
356 CmdArgs.push_back(
"--no-as-needed");
361 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
363 const char *crtend =
nullptr;
365 crtend =
"crtendS.o";
376 C.addCommand(std::make_unique<Command>(JA, *
this,
378 Exec, CmdArgs, Inputs, Output));
385 if (!Args.hasArg(options::OPT_nostdlib)) {
391 switch (Triple.getArch()) {
392 case llvm::Triple::x86:
395 case llvm::Triple::arm:
396 case llvm::Triple::armeb:
397 case llvm::Triple::thumb:
398 case llvm::Triple::thumbeb:
399 switch (Triple.getEnvironment()) {
400 case llvm::Triple::EABI:
401 case llvm::Triple::GNUEABI:
404 case llvm::Triple::EABIHF:
405 case llvm::Triple::GNUEABIHF:
413 case llvm::Triple::mips64:
414 case llvm::Triple::mips64el:
420 case llvm::Triple::ppc:
423 case llvm::Triple::sparc:
442 case llvm::Triple::aarch64:
443 case llvm::Triple::aarch64_be:
444 case llvm::Triple::arm:
445 case llvm::Triple::armeb:
446 case llvm::Triple::thumb:
447 case llvm::Triple::thumbeb:
448 case llvm::Triple::ppc:
449 case llvm::Triple::ppc64:
450 case llvm::Triple::ppc64le:
451 case llvm::Triple::riscv32:
452 case llvm::Triple::riscv64:
453 case llvm::Triple::sparc:
454 case llvm::Triple::sparcv9:
455 case llvm::Triple::x86:
456 case llvm::Triple::x86_64:
465 const llvm::opt::ArgList &DriverArgs,
466 llvm::opt::ArgStringList &CC1Args)
const {
469 if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
472 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
474 llvm::sys::path::append(Dir,
"include");
478 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
482 StringRef CIncludeDirs(C_INCLUDE_DIRS);
483 if (CIncludeDirs !=
"") {
485 CIncludeDirs.split(dirs,
":");
486 for (StringRef dir : dirs) {
488 llvm::sys::path::is_absolute(dir) ? StringRef(
D.SysRoot) :
"";
495 concat(
D.SysRoot,
"/usr/include"));
499 llvm::opt::ArgStringList &CC1Args)
const {
500 const std::string Candidates[] = {
509 for (
const auto &IncludePath : Candidates) {
510 if (!
getVFS().exists(IncludePath +
"/__config"))
520 llvm::opt::ArgStringList &CC1Args)
const {
522 DriverArgs, CC1Args);
527 llvm::Triple::ArchType TArch =
getTriple().getArch();
528 if (TArch == llvm::Triple::arm || TArch == llvm::Triple::armeb ||
529 TArch == llvm::Triple::thumb || TArch == llvm::Triple::thumbeb)
530 return llvm::ExceptionHandling::DwarfCFI;
531 return llvm::ExceptionHandling::None;
535 const bool IsX86 =
getTriple().getArch() == llvm::Triple::x86;
536 const bool IsX86_64 =
getTriple().getArch() == llvm::Triple::x86_64;
538 if (IsX86 || IsX86_64) {
539 Res |= SanitizerKind::Address;
540 Res |= SanitizerKind::PointerCompare;
541 Res |= SanitizerKind::PointerSubtract;
542 Res |= SanitizerKind::Leak;
543 Res |= SanitizerKind::SafeStack;
544 Res |= SanitizerKind::Scudo;
545 Res |= SanitizerKind::Vptr;
548 Res |= SanitizerKind::DataFlow;
549 Res |= SanitizerKind::Fuzzer;
550 Res |= SanitizerKind::FuzzerNoLink;
551 Res |= SanitizerKind::HWAddress;
552 Res |= SanitizerKind::KernelAddress;
553 Res |= SanitizerKind::KernelHWAddress;
554 Res |= SanitizerKind::KernelMemory;
555 Res |= SanitizerKind::Memory;
556 Res |= SanitizerKind::Thread;
562 ArgStringList &CC1Args,
566 CC1Args.push_back(
"-D_REENTRANT");
568 VersionTuple OsVersion =
getTriple().getOSVersion();
569 bool UseInitArrayDefault =
570 OsVersion >= VersionTuple(9) || OsVersion.getMajor() == 0 ||
571 getTriple().getArch() == llvm::Triple::aarch64 ||
572 getTriple().getArch() == llvm::Triple::aarch64_be ||
573 getTriple().getArch() == llvm::Triple::arm ||
574 getTriple().getArch() == llvm::Triple::armeb ||
575 getTriple().getArch() == llvm::Triple::riscv32 ||
576 getTriple().getArch() == llvm::Triple::riscv64;
578 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
579 options::OPT_fno_use_init_array, UseInitArrayDefault))
580 CC1Args.push_back(
"-fno-use-init-array");
Compilation - A set of tasks to perform for a single driver invocation.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
bool hasAnySanitizer() const
bool needsSharedRt() const
The JSON file list parser is used to communicate input to InstallAPI.
static constexpr ResponseFileSupport AtFileCurCP()