16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Option/ArgList.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/VirtualFileSystem.h"
30 return llvm::StringSwitch<StringRef>(HvxVer)
39 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
40 StringRef Val = A->getValue();
41 if (!Val.equals_insensitive(
"64b") && !Val.equals_insensitive(
"128b"))
42 D.Diag(diag::err_drv_unsupported_option_argument)
43 << A->getSpelling() << Val;
49 std::vector<StringRef> &Features,
50 StringRef Cpu,
bool &HasHVX) {
54 auto makeFeature = [&Args](Twine
T,
bool Enable) -> StringRef {
55 const std::string &S =
T.str();
57 Opt.consume_back(
"=");
58 if (Opt.starts_with(
"mno-"))
59 Opt = Opt.drop_front(4);
60 else if (Opt.starts_with(
"m"))
61 Opt = Opt.drop_front(1);
62 return Args.MakeArgString(Twine(Enable ?
"+" :
"-") + Twine(Opt));
65 auto withMinus = [](StringRef S) -> std::string {
71 (Cpu.back() ==
'T' || Cpu.back() ==
't' ? Cpu.drop_back(1) : Cpu).str();
77 Args.getLastArg(options::OPT_mhexagon_hvx, options::OPT_mhexagon_hvx_EQ,
78 options::OPT_mno_hexagon_hvx);
80 if (HvxEnablingArg->getOption().matches(options::OPT_mno_hexagon_hvx))
81 HvxEnablingArg =
nullptr;
86 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx,
87 options::OPT_mhexagon_hvx_EQ)) {
90 if (A->getOption().matches(options::OPT_mhexagon_hvx_EQ))
91 HvxVer = StringRef(A->getValue()).lower();
94 Features.push_back(makeFeature(Twine(
"hvx") + HvxVer,
true));
95 }
else if (Arg *A = Args.getLastArg(options::OPT_mno_hexagon_hvx)) {
97 Features.push_back(makeFeature(A->getOption().getName(),
false));
103 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_length_EQ)) {
106 D.Diag(diag::err_drv_needs_hvx) << withMinus(A->getOption().getName());
107 else if (A->getOption().matches(options::OPT_mhexagon_hvx_length_EQ))
108 HvxLen = A->getValue();
112 StringRef L = makeFeature(Twine(
"hvx-length") + HvxLen.lower(),
true);
113 Features.push_back(L);
118 if (StringRef(HvxVer).drop_front(1).getAsInteger(10, HvxVerNum))
122 auto checkFlagHvxVersion =
123 [&](
auto FlagOn,
auto FlagOff,
124 unsigned MinVerNum) -> std::optional<StringRef> {
130 Arg *A = Args.getLastArg(FlagOn, FlagOff);
134 StringRef OptName = A->getOption().getName();
135 if (A->getOption().matches(FlagOff))
136 return makeFeature(OptName,
false);
139 D.Diag(diag::err_drv_needs_hvx) << withMinus(OptName);
142 if (HvxVerNum < MinVerNum) {
143 D.Diag(diag::err_drv_needs_hvx_version)
144 << withMinus(OptName) << (
"v" + std::to_string(HvxVerNum));
147 return makeFeature(OptName,
true);
150 if (
auto F = checkFlagHvxVersion(options::OPT_mhexagon_hvx_qfloat,
151 options::OPT_mno_hexagon_hvx_qfloat, 68)) {
152 Features.push_back(*F);
154 if (
auto F = checkFlagHvxVersion(options::OPT_mhexagon_hvx_ieee_fp,
155 options::OPT_mno_hexagon_hvx_ieee_fp, 68)) {
156 Features.push_back(*F);
162 const llvm::Triple &Triple,
164 std::vector<StringRef> &Features) {
166 options::OPT_m_hexagon_Features_Group);
168 bool UseLongCalls =
false;
169 if (Arg *A = Args.getLastArg(options::OPT_mlong_calls,
170 options::OPT_mno_long_calls)) {
171 if (A->getOption().matches(options::OPT_mlong_calls))
175 Features.push_back(UseLongCalls ?
"+long-calls" :
"-long-calls");
181 const bool TinyCore = Cpu.contains(
't');
184 Cpu = Cpu.take_front(Cpu.size() - 1);
189 D.Diag(diag::warn_drv_needs_hvx) <<
"auto-vectorization";
194 ArgStringList &CmdArgs)
const {
201 const char *LinkingOutput)
const {
205 const Driver &
D = HTC.getDriver();
206 ArgStringList CmdArgs;
208 CmdArgs.push_back(
"--arch=hexagon");
210 RenderExtraToolArgs(JA, CmdArgs);
212 const char *AsName =
"llvm-mc";
213 CmdArgs.push_back(
"-filetype=obj");
214 CmdArgs.push_back(Args.MakeArgString(
222 CmdArgs.push_back(
"-o");
225 CmdArgs.push_back(
"-fsyntax-only");
228 if (Arg *A = Args.getLastArg(options::OPT_mhexagon_hvx_ieee_fp,
229 options::OPT_mno_hexagon_hvx_ieee_fp)) {
230 if (A->getOption().matches(options::OPT_mhexagon_hvx_ieee_fp))
231 CmdArgs.push_back(
"-mhvx-ieee-fp");
235 CmdArgs.push_back(Args.MakeArgString(
"-gpsize=" + Twine(*G)));
238 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
248 for (
const auto &II : Inputs) {
251 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
252 << HTC.getTripleString();
253 else if (II.getType() == types::TY_AST)
254 D.Diag(clang::diag::err_drv_no_ast_support)
255 << HTC.getTripleString();
256 else if (II.getType() == types::TY_ModuleFile)
257 D.Diag(diag::err_drv_no_module_support)
258 << HTC.getTripleString();
261 CmdArgs.push_back(II.getFilename());
265 II.getInputArg().render(Args, CmdArgs);
268 auto *Exec = Args.MakeArgString(HTC.GetProgramPath(AsName));
269 C.addCommand(std::make_unique<Command>(JA, *
this,
271 Exec, CmdArgs, Inputs, Output));
275 ArgStringList &CmdArgs)
const {
282 const ArgList &Args, ArgStringList &CmdArgs,
283 const char *LinkingOutput) {
290 bool IsStatic = Args.hasArg(options::OPT_static);
291 bool IsShared = Args.hasArg(options::OPT_shared);
292 bool IsPIE = Args.hasArg(options::OPT_pie);
293 bool IncStdLib = !Args.hasArg(options::OPT_nostdlib);
294 bool IncStartFiles = !Args.hasArg(options::OPT_nostartfiles);
295 bool IncDefLibs = !Args.hasArg(options::OPT_nodefaultlibs);
298 bool UseLLD = (llvm::sys::path::filename(Exec).equals_insensitive(
"ld.lld") ||
299 llvm::sys::path::stem(Exec).equals_insensitive(
"ld.lld"));
300 bool UseShared = IsShared && !
IsStatic;
309 Args.ClaimAllArgs(options::OPT_g_Group);
310 Args.ClaimAllArgs(options::OPT_emit_llvm);
311 Args.ClaimAllArgs(options::OPT_w);
313 Args.ClaimAllArgs(options::OPT_static_libgcc);
318 if (Args.hasArg(options::OPT_s))
319 CmdArgs.push_back(
"-s");
321 if (Args.hasArg(options::OPT_r))
322 CmdArgs.push_back(
"-r");
325 CmdArgs.push_back(Opt.c_str());
328 CmdArgs.push_back(
"-march=hexagon");
329 CmdArgs.push_back(Args.MakeArgString(
"-mcpu=hexagon" + CpuVer));
333 CmdArgs.push_back(
"-shared");
335 CmdArgs.push_back(
"-call_shared");
339 CmdArgs.push_back(
"-static");
341 if (IsPIE && !IsShared)
342 CmdArgs.push_back(
"-pie");
345 CmdArgs.push_back(Args.MakeArgString(
"-G" + Twine(*G)));
349 CmdArgs.push_back(
"-o");
353 if (!Args.hasArg(options::OPT_shared, options::OPT_static))
354 CmdArgs.push_back(
"-dynamic-linker=/lib/ld-musl-hexagon.so.1");
356 if (!Args.hasArg(options::OPT_shared, options::OPT_nostartfiles,
357 options::OPT_nostdlib))
358 CmdArgs.push_back(Args.MakeArgString(
D.SysRoot +
"/usr/lib/crt1.o"));
359 else if (Args.hasArg(options::OPT_shared) &&
360 !Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib))
361 CmdArgs.push_back(Args.MakeArgString(
D.SysRoot +
"/usr/lib/crti.o"));
364 Args.MakeArgString(StringRef(
"-L") +
D.SysRoot +
"/usr/lib"));
365 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
366 options::OPT_t, options::OPT_u_Group});
371 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
372 if (NeedsSanitizerDeps) {
376 CmdArgs.push_back(
"-lunwind");
381 CmdArgs.push_back(
"-lclang_rt.builtins-hexagon");
382 if (!Args.hasArg(options::OPT_nolibc))
383 CmdArgs.push_back(
"-lc");
390 for (
const auto &LibPath : LibPaths)
391 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
392 Args.ClaimAllArgs(options::OPT_L);
399 std::vector<std::string> OsLibs;
400 bool HasStandalone =
false;
401 for (
const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
403 OsLibs.emplace_back(A->getValue());
404 HasStandalone = HasStandalone || (OsLibs.back() ==
"standalone");
406 if (OsLibs.empty()) {
407 OsLibs.push_back(
"standalone");
408 HasStandalone =
true;
414 const std::string MCpuSuffix =
"/" + CpuVer.str();
415 const std::string MCpuG0Suffix = MCpuSuffix +
"/G0";
416 const std::string RootDir =
418 const std::string StartSubDir =
419 "hexagon/lib" + (UseG0 ? MCpuG0Suffix : MCpuSuffix);
421 auto Find = [&HTC] (
const std::string &RootDir,
const std::string &SubDir,
422 const char *Name) -> std::string {
423 std::string RelName = SubDir + Name;
425 if (llvm::sys::fs::exists(
P))
427 return RootDir + RelName;
430 if (IncStdLib && IncStartFiles) {
433 std::string Crt0SA = Find(RootDir, StartSubDir,
"/crt0_standalone.o");
434 CmdArgs.push_back(Args.MakeArgString(Crt0SA));
436 std::string Crt0 = Find(RootDir, StartSubDir,
"/crt0.o");
437 CmdArgs.push_back(Args.MakeArgString(Crt0));
439 std::string
Init = UseShared
440 ? Find(RootDir, StartSubDir +
"/pic",
"/initS.o")
441 : Find(RootDir, StartSubDir,
"/init.o");
442 CmdArgs.push_back(Args.MakeArgString(
Init));
449 for (
const auto &LibPath : LibPaths)
450 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
451 Args.ClaimAllArgs(options::OPT_L);
456 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
457 options::OPT_t, options::OPT_u_Group});
464 if (IncStdLib && IncDefLibs) {
468 CmdArgs.push_back(
"-lm");
471 CmdArgs.push_back(
"--start-group");
474 for (StringRef Lib : OsLibs)
475 CmdArgs.push_back(Args.MakeArgString(
"-l" + Lib));
476 if (!Args.hasArg(options::OPT_nolibc))
477 CmdArgs.push_back(
"-lc");
479 CmdArgs.push_back(
"-lgcc");
481 CmdArgs.push_back(
"--end-group");
487 if (IncStdLib && IncStartFiles) {
488 std::string Fini = UseShared
489 ? Find(RootDir, StartSubDir +
"/pic",
"/finiS.o")
490 : Find(RootDir, StartSubDir,
"/fini.o");
491 CmdArgs.push_back(Args.MakeArgString(Fini));
499 const char *LinkingOutput)
const {
502 ArgStringList CmdArgs;
506 const char *Exec = Args.MakeArgString(HTC.GetLinkerPath());
507 C.addCommand(std::make_unique<Command>(JA, *
this,
509 Exec, CmdArgs, Inputs, Output));
516 const std::string &InstalledDir,
518 std::string InstallRelDir;
522 for (
auto &I : PrefixDirs)
523 if (
D.getVFS().exists(I))
526 if (getVFS().exists(InstallRelDir = InstalledDir +
"/../target"))
527 return InstallRelDir;
532std::optional<unsigned>
535 if (Arg *A = Args.getLastArg(options::OPT_G)) {
537 }
else if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
538 options::OPT_fPIC)) {
543 if (!Gn.getAsInteger(10, G))
551 llvm::sys::path::append(Dir,
"usr",
"lib");
552 if (!SelectedMultilibs.empty()) {
553 Dir += SelectedMultilibs.back().gccSuffix();
555 return std::string(Dir);
565 for (Arg *A : Args.filtered(options::OPT_L))
566 llvm::append_range(LibPaths, A->getValues());
571 std::vector<std::string> RootDirs;
572 std::copy(
D.PrefixDirs.begin(),
D.PrefixDirs.end(),
573 std::back_inserter(RootDirs));
575 std::string TargetDir = getHexagonTargetDir(
D.Dir,
D.PrefixDirs);
576 if (!llvm::is_contained(RootDirs, TargetDir))
577 RootDirs.push_back(TargetDir);
579 bool HasPIC = Args.hasArg(options::OPT_fpic, options::OPT_fPIC);
581 bool HasG0 = Args.hasArg(options::OPT_shared);
582 if (
auto G = getSmallDataThreshold(Args))
585 const std::string CpuVer = GetTargetCPUVersion(Args).str();
586 for (
auto &Dir : RootDirs) {
587 std::string LibDir = Dir +
"/hexagon/lib";
588 std::string LibDirCpu = LibDir +
'/' + CpuVer;
591 LibPaths.push_back(LibDirCpu +
"/G0/pic");
592 LibPaths.push_back(LibDirCpu +
"/G0");
594 LibPaths.push_back(LibDirCpu);
595 LibPaths.push_back(LibDir);
600 const llvm::opt::ArgList &Args)
601 :
Linux(
D, Triple, Args) {
606 const std::string BinDir(TargetDir +
"/bin");
607 if (
D.getVFS().exists(BinDir))
622 ArgStringList &CmdArgs)
const {
626 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
628 getDriver().
Diag(diag::err_drv_unsupported_unwind_for_platform)
629 << A->getValue() <<
getTriple().normalize();
636 CmdArgs.push_back(
"-lc++");
637 if (Args.hasArg(options::OPT_fexperimental_library))
638 CmdArgs.push_back(
"-lc++experimental");
639 CmdArgs.push_back(
"-lc++abi");
641 CmdArgs.push_back(
"-lunwind");
645 CmdArgs.push_back(
"-lstdc++");
659 const llvm::opt::ArgList &DriverArgs)
const {
661 Arg *A = DriverArgs.getLastArg(options::OPT_O_Group);
665 if (A->getOption().matches(options::OPT_O0))
667 if (A->getOption().matches(options::OPT_Ofast) ||
668 A->getOption().matches(options::OPT_O4))
670 assert(A->getNumValues() != 0);
671 StringRef S(A->getValue());
672 if (S ==
"s" || S ==
"z" || S.empty())
678 if (S.getAsInteger(10, OptLevel))
684 ArgStringList &CC1Args,
687 bool UseInitArrayDefault =
getTriple().isMusl();
689 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
690 options::OPT_fno_use_init_array,
691 UseInitArrayDefault))
692 CC1Args.push_back(
"-fno-use-init-array");
694 if (DriverArgs.hasArg(options::OPT_ffixed_r19)) {
695 CC1Args.push_back(
"-target-feature");
696 CC1Args.push_back(
"+reserved-r19");
699 CC1Args.push_back(
"-mllvm");
700 CC1Args.push_back(
"-hexagon-autohvx");
705 ArgStringList &CC1Args)
const {
706 if (DriverArgs.hasArg(options::OPT_nostdinc))
715 llvm::sys::path::append(ResourceDirInclude,
"include");
716 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
717 (!IsLinuxMusl || DriverArgs.hasArg(options::OPT_nostdlibinc)))
720 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
723 const bool HasSysRoot = !
D.SysRoot.empty();
727 llvm::sys::path::append(
P,
"usr/include");
729 llvm::sys::path::append(
P,
"include");
738 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && IsLinuxMusl)
748 const llvm::opt::ArgList &DriverArgs,
749 llvm::opt::ArgStringList &CC1Args)
const {
751 if (!
D.SysRoot.empty() &&
getTriple().isMusl())
753 DriverArgs, CC1Args);
760 DriverArgs, CC1Args);
764 const llvm::opt::ArgList &DriverArgs,
765 llvm::opt::ArgStringList &CC1Args)
const {
769 DriverArgs, CC1Args);
774 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
781 StringRef
Value = A->getValue();
782 if (
Value !=
"libstdc++" &&
Value !=
"libc++")
783 getDriver().
Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
785 if (
Value ==
"libstdc++")
787 else if (
Value ==
"libc++")
794 if (Arg *A = Args.getLastArg(options::OPT_fvectorize,
795 options::OPT_fno_vectorize))
796 return A->getOption().matches(options::OPT_fvectorize);
809 Arg *CpuArg =
nullptr;
810 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
813 StringRef CPU = CpuArg ? CpuArg->getValue() :
GetDefaultCPU();
814 CPU.consume_front(
"hexagon");
The base class of the type hierarchy.
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...
DiagnosticBuilder Diag(unsigned DiagID) const
bool isLLVMIR(ID Id)
Is this LLVM IR.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
static constexpr ResponseFileSupport AtFileCurCP()