1//===--- XCore.cpp - XCore 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 "XCore.h"
10#include "CommonArgs.h"
11#include "clang/Driver/Compilation.h"
12#include "clang/Driver/Driver.h"
13#include "clang/Driver/Options.h"
14#include "llvm/Option/ArgList.h"
15#include <cstdlib> // ::getenv
16
17using namespace clang::driver;
18using namespace clang::driver::toolchains;
19using namespace clang;
20using namespace llvm::opt;
21
22/// XCore Tools
23// We pass assemble and link construction to the xcc tool.
24
25void tools::XCore::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
26 const InputInfo &Output,
27 const InputInfoList &Inputs,
28 const ArgList &Args,
29 const char *LinkingOutput) const {
30 claimNoWarnArgs(Args);
31 ArgStringList CmdArgs;
32
33 CmdArgs.push_back(Elt: "-o");
34 CmdArgs.push_back(Elt: Output.getFilename());
35
36 CmdArgs.push_back(Elt: "-c");
37
38 if (Args.hasArg(Ids: options::OPT_v))
39 CmdArgs.push_back(Elt: "-v");
40
41 if (Arg *A = Args.getLastArg(Ids: options::OPT_g_Group))
42 if (!A->getOption().matches(ID: options::OPT_g0))
43 CmdArgs.push_back(Elt: "-g");
44
45 if (Args.hasFlag(Pos: options::OPT_fverbose_asm, Neg: options::OPT_fno_verbose_asm,
46 Default: false))
47 CmdArgs.push_back(Elt: "-fverbose-asm");
48
49 Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wa_COMMA, Id1: options::OPT_Xassembler);
50
51 for (const auto &II : Inputs)
52 CmdArgs.push_back(Elt: II.getFilename());
53
54 const char *Exec = Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "xcc"));
55 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
56 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
57}
58
59void tools::XCore::Linker::ConstructJob(Compilation &C, const JobAction &JA,
60 const InputInfo &Output,
61 const InputInfoList &Inputs,
62 const ArgList &Args,
63 const char *LinkingOutput) const {
64 ArgStringList CmdArgs;
65
66 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
67 if (Output.isFilename()) {
68 CmdArgs.push_back(Elt: "-o");
69 CmdArgs.push_back(Elt: Output.getFilename());
70 }
71
72 if (Args.hasArg(Ids: options::OPT_v))
73 CmdArgs.push_back(Elt: "-v");
74
75 // Pass -fexceptions through to the linker if it was present.
76 if (Args.hasFlag(Pos: options::OPT_fexceptions, Neg: options::OPT_fno_exceptions,
77 Default: false))
78 CmdArgs.push_back(Elt: "-fexceptions");
79
80 AddLinkerInputs(TC: getToolChain(), Inputs, Args, CmdArgs, JA);
81
82 const char *Exec = Args.MakeArgString(Str: getToolChain().GetProgramPath(Name: "xcc"));
83 C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, args: ResponseFileSupport::None(),
84 args&: Exec, args&: CmdArgs, args: Inputs, args: Output));
85}
86
87/// XCore tool chain
88XCoreToolChain::XCoreToolChain(const Driver &D, const llvm::Triple &Triple,
89 const ArgList &Args)
90 : ToolChain(D, Triple, Args) {
91 // ProgramPaths are found via 'PATH' environment variable.
92}
93
94Tool *XCoreToolChain::buildAssembler() const {
95 return new tools::XCore::Assembler(*this);
96}
97
98Tool *XCoreToolChain::buildLinker() const {
99 return new tools::XCore::Linker(*this);
100}
101
102bool XCoreToolChain::isPICDefault() const { return false; }
103
104bool XCoreToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const {
105 return false;
106}
107
108bool XCoreToolChain::isPICDefaultForced() const { return false; }
109
110bool XCoreToolChain::SupportsProfiling() const { return false; }
111
112bool XCoreToolChain::hasBlocksRuntime() const { return false; }
113
114void XCoreToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
115 ArgStringList &CC1Args) const {
116 if (DriverArgs.hasArg(Ids: clang::driver::options::OPT_nostdinc) ||
117 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
118 return;
119 if (const char *cl_include_dir = getenv(name: "XCC_C_INCLUDE_PATH")) {
120 SmallVector<StringRef, 4> Dirs;
121 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
122 StringRef(cl_include_dir).split(A&: Dirs, Separator: StringRef(EnvPathSeparatorStr));
123 ArrayRef<StringRef> DirVec(Dirs);
124 addSystemIncludes(DriverArgs, CC1Args, Paths: DirVec);
125 }
126}
127
128void XCoreToolChain::addClangTargetOptions(const ArgList &DriverArgs,
129 ArgStringList &CC1Args,
130 Action::OffloadKind) const {
131 CC1Args.push_back(Elt: "-nostdsysteminc");
132 // Set `-fno-use-cxa-atexit` to default.
133 if (!DriverArgs.hasFlag(Pos: options::OPT_fuse_cxa_atexit,
134 Neg: options::OPT_fno_use_cxa_atexit, Default: false))
135 CC1Args.push_back(Elt: "-fno-use-cxa-atexit");
136}
137
138void XCoreToolChain::AddClangCXXStdlibIncludeArgs(
139 const ArgList &DriverArgs, ArgStringList &CC1Args) const {
140 if (DriverArgs.hasArg(Ids: clang::driver::options::OPT_nostdinc) ||
141 DriverArgs.hasArg(Ids: options::OPT_nostdlibinc) ||
142 DriverArgs.hasArg(Ids: options::OPT_nostdincxx))
143 return;
144 if (const char *cl_include_dir = getenv(name: "XCC_CPLUS_INCLUDE_PATH")) {
145 SmallVector<StringRef, 4> Dirs;
146 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
147 StringRef(cl_include_dir).split(A&: Dirs, Separator: StringRef(EnvPathSeparatorStr));
148 ArrayRef<StringRef> DirVec(Dirs);
149 addSystemIncludes(DriverArgs, CC1Args, Paths: DirVec);
150 }
151}
152
153void XCoreToolChain::AddCXXStdlibLibArgs(const ArgList &Args,
154 ArgStringList &CmdArgs) const {
155 // We don't output any lib args. This is handled by xcc.
156}
157