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 const char *Exec = Args.MakeArgString(HTC.
GetLinkerPath(&UseLLD));
299 UseLLD = UseLLD || llvm::sys::path::filename(Exec).ends_with(
"ld.lld") ||
300 llvm::sys::path::stem(Exec).ends_with(
"ld.lld");
301 bool UseShared = IsShared && !
IsStatic;
310 Args.ClaimAllArgs(options::OPT_g_Group);
311 Args.ClaimAllArgs(options::OPT_emit_llvm);
312 Args.ClaimAllArgs(options::OPT_w);
314 Args.ClaimAllArgs(options::OPT_static_libgcc);
319 if (Args.hasArg(options::OPT_s))
320 CmdArgs.push_back(
"-s");
322 if (Args.hasArg(options::OPT_r))
323 CmdArgs.push_back(
"-r");
326 CmdArgs.push_back(Opt.c_str());
329 CmdArgs.push_back(
"-march=hexagon");
330 CmdArgs.push_back(Args.MakeArgString(
"-mcpu=hexagon" + CpuVer));
334 CmdArgs.push_back(
"-shared");
336 CmdArgs.push_back(
"-call_shared");
340 CmdArgs.push_back(
"-static");
342 if (IsPIE && !IsShared)
343 CmdArgs.push_back(
"-pie");
346 CmdArgs.push_back(Args.MakeArgString(
"-G" + Twine(*G)));
350 CmdArgs.push_back(
"-o");
354 if (!Args.hasArg(options::OPT_shared, options::OPT_static))
355 CmdArgs.push_back(
"-dynamic-linker=/lib/ld-musl-hexagon.so.1");
357 if (!Args.hasArg(options::OPT_shared, options::OPT_nostartfiles,
358 options::OPT_nostdlib))
359 CmdArgs.push_back(Args.MakeArgString(
D.SysRoot +
"/usr/lib/crt1.o"));
360 else if (Args.hasArg(options::OPT_shared) &&
361 !Args.hasArg(options::OPT_nostartfiles, options::OPT_nostdlib))
362 CmdArgs.push_back(Args.MakeArgString(
D.SysRoot +
"/usr/lib/crti.o"));
365 Args.MakeArgString(StringRef(
"-L") +
D.SysRoot +
"/usr/lib"));
366 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
367 options::OPT_t, options::OPT_u_Group});
372 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
373 if (NeedsSanitizerDeps) {
377 CmdArgs.push_back(
"-lunwind");
382 if (!Args.hasArg(options::OPT_nolibc))
383 CmdArgs.push_back(
"-lc");
384 CmdArgs.push_back(
"-lclang_rt.builtins-hexagon");
391 for (
const auto &LibPath : LibPaths)
392 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
393 Args.ClaimAllArgs(options::OPT_L);
400 std::vector<std::string> OsLibs;
401 bool HasStandalone =
false;
402 for (
const Arg *A : Args.filtered(options::OPT_moslib_EQ)) {
404 OsLibs.emplace_back(A->getValue());
405 HasStandalone = HasStandalone || (OsLibs.back() ==
"standalone");
407 if (OsLibs.empty()) {
408 OsLibs.push_back(
"standalone");
409 HasStandalone =
true;
415 const std::string MCpuSuffix =
"/" + CpuVer.str();
416 const std::string MCpuG0Suffix = MCpuSuffix +
"/G0";
417 const std::string RootDir =
419 const std::string StartSubDir =
420 "hexagon/lib" + (UseG0 ? MCpuG0Suffix : MCpuSuffix);
422 auto Find = [&HTC] (
const std::string &RootDir,
const std::string &SubDir,
423 const char *Name) -> std::string {
424 std::string RelName = SubDir + Name;
426 if (llvm::sys::fs::exists(
P))
428 return RootDir + RelName;
431 if (IncStdLib && IncStartFiles) {
434 std::string Crt0SA = Find(RootDir, StartSubDir,
"/crt0_standalone.o");
435 CmdArgs.push_back(Args.MakeArgString(Crt0SA));
437 std::string Crt0 = Find(RootDir, StartSubDir,
"/crt0.o");
438 CmdArgs.push_back(Args.MakeArgString(Crt0));
440 std::string
Init = UseShared
441 ? Find(RootDir, StartSubDir +
"/pic",
"/initS.o")
442 : Find(RootDir, StartSubDir,
"/init.o");
443 CmdArgs.push_back(Args.MakeArgString(
Init));
450 for (
const auto &LibPath : LibPaths)
451 CmdArgs.push_back(Args.MakeArgString(StringRef(
"-L") + LibPath));
452 Args.ClaimAllArgs(options::OPT_L);
457 Args.addAllArgs(CmdArgs, {options::OPT_T_Group, options::OPT_s,
458 options::OPT_t, options::OPT_u_Group});
465 if (IncStdLib && IncDefLibs) {
469 CmdArgs.push_back(
"-lm");
472 CmdArgs.push_back(
"--start-group");
475 for (StringRef Lib : OsLibs)
476 CmdArgs.push_back(Args.MakeArgString(
"-l" + Lib));
477 if (!Args.hasArg(options::OPT_nolibc))
478 CmdArgs.push_back(
"-lc");
480 CmdArgs.push_back(
"-lgcc");
482 CmdArgs.push_back(
"--end-group");
488 if (IncStdLib && IncStartFiles) {
489 std::string Fini = UseShared
490 ? Find(RootDir, StartSubDir +
"/pic",
"/finiS.o")
491 : Find(RootDir, StartSubDir,
"/fini.o");
492 CmdArgs.push_back(Args.MakeArgString(Fini));
500 const char *LinkingOutput)
const {
503 ArgStringList CmdArgs;
507 const char *Exec = Args.MakeArgString(HTC.GetLinkerPath());
508 C.addCommand(std::make_unique<Command>(JA, *
this,
510 Exec, CmdArgs, Inputs, Output));
517 const std::string &InstalledDir,
519 std::string InstallRelDir;
523 for (
auto &I : PrefixDirs)
524 if (
D.getVFS().exists(I))
527 if (getVFS().exists(InstallRelDir = InstalledDir +
"/../target"))
528 return InstallRelDir;
533std::optional<unsigned>
536 if (Arg *A = Args.getLastArg(options::OPT_G)) {
538 }
else if (Args.getLastArg(options::OPT_shared, options::OPT_fpic,
539 options::OPT_fPIC)) {
544 if (!Gn.getAsInteger(10, G))
552 llvm::sys::path::append(Dir,
"usr",
"lib");
553 if (!SelectedMultilibs.empty()) {
554 Dir += SelectedMultilibs.back().gccSuffix();
556 return std::string(Dir);
566 for (Arg *A : Args.filtered(options::OPT_L))
567 llvm::append_range(LibPaths, A->getValues());
572 std::vector<std::string> RootDirs;
573 std::copy(
D.PrefixDirs.begin(),
D.PrefixDirs.end(),
574 std::back_inserter(RootDirs));
576 std::string TargetDir = getHexagonTargetDir(
D.Dir,
D.PrefixDirs);
577 if (!llvm::is_contained(RootDirs, TargetDir))
578 RootDirs.push_back(TargetDir);
580 bool HasPIC = Args.hasArg(options::OPT_fpic, options::OPT_fPIC);
582 bool HasG0 = Args.hasArg(options::OPT_shared);
583 if (
auto G = getSmallDataThreshold(Args))
586 const std::string CpuVer = GetTargetCPUVersion(Args).str();
587 for (
auto &Dir : RootDirs) {
588 std::string LibDir = Dir +
"/hexagon/lib";
589 std::string LibDirCpu = LibDir +
'/' + CpuVer;
592 LibPaths.push_back(LibDirCpu +
"/G0/pic");
593 LibPaths.push_back(LibDirCpu +
"/G0");
595 LibPaths.push_back(LibDirCpu);
596 LibPaths.push_back(LibDir);
601 const llvm::opt::ArgList &Args)
602 :
Linux(
D, Triple, Args) {
607 const std::string BinDir(TargetDir +
"/bin");
608 if (
D.getVFS().exists(BinDir))
623 ArgStringList &CmdArgs)
const {
627 const Arg *A = Args.getLastArg(options::OPT_unwindlib_EQ);
629 getDriver().
Diag(diag::err_drv_unsupported_unwind_for_platform)
630 << A->getValue() <<
getTriple().normalize();
637 CmdArgs.push_back(
"-lc++");
638 if (Args.hasArg(options::OPT_fexperimental_library))
639 CmdArgs.push_back(
"-lc++experimental");
640 CmdArgs.push_back(
"-lc++abi");
642 CmdArgs.push_back(
"-lunwind");
646 CmdArgs.push_back(
"-lstdc++");
660 const llvm::opt::ArgList &DriverArgs)
const {
662 Arg *A = DriverArgs.getLastArg(options::OPT_O_Group);
666 if (A->getOption().matches(options::OPT_O0))
668 if (A->getOption().matches(options::OPT_Ofast) ||
669 A->getOption().matches(options::OPT_O4))
671 assert(A->getNumValues() != 0);
672 StringRef S(A->getValue());
673 if (S ==
"s" || S ==
"z" || S.empty())
679 if (S.getAsInteger(10, OptLevel))
685 ArgStringList &CC1Args,
688 bool UseInitArrayDefault =
getTriple().isMusl();
690 if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
691 options::OPT_fno_use_init_array,
692 UseInitArrayDefault))
693 CC1Args.push_back(
"-fno-use-init-array");
695 if (DriverArgs.hasArg(options::OPT_ffixed_r19)) {
696 CC1Args.push_back(
"-target-feature");
697 CC1Args.push_back(
"+reserved-r19");
700 CC1Args.push_back(
"-mllvm");
701 CC1Args.push_back(
"-hexagon-autohvx");
706 ArgStringList &CC1Args)
const {
707 if (DriverArgs.hasArg(options::OPT_nostdinc))
716 llvm::sys::path::append(ResourceDirInclude,
"include");
717 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
718 (!IsLinuxMusl || DriverArgs.hasArg(options::OPT_nostdlibinc)))
721 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
724 const bool HasSysRoot = !
D.SysRoot.empty();
728 llvm::sys::path::append(
P,
"usr/include");
730 llvm::sys::path::append(
P,
"include");
739 if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && IsLinuxMusl)
749 const llvm::opt::ArgList &DriverArgs,
750 llvm::opt::ArgStringList &CC1Args)
const {
752 if (!
D.SysRoot.empty() &&
getTriple().isMusl())
754 DriverArgs, CC1Args);
761 DriverArgs, CC1Args);
765 const llvm::opt::ArgList &DriverArgs,
766 llvm::opt::ArgStringList &CC1Args)
const {
770 DriverArgs, CC1Args);
775 Arg *A = Args.getLastArg(options::OPT_stdlib_EQ);
782 StringRef
Value = A->getValue();
783 if (
Value !=
"libstdc++" &&
Value !=
"libc++")
784 getDriver().
Diag(diag::err_drv_invalid_stdlib_name) << A->getAsString(Args);
786 if (
Value ==
"libstdc++")
788 else if (
Value ==
"libc++")
795 if (Arg *A = Args.getLastArg(options::OPT_fvectorize,
796 options::OPT_fno_vectorize))
797 return A->getOption().matches(options::OPT_fvectorize);
810 Arg *CpuArg =
nullptr;
811 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
814 StringRef CPU = CpuArg ? CpuArg->getValue() :
GetDefaultCPU();
815 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()