[docs] Fix build-docs.sh
[llvm-project.git] / clang / lib / Driver / ToolChains / PS4CPU.cpp
blob97688470008fa63c447c4f624abf3cf6a5e9099c
1 //===--- PS4CPU.cpp - PS4CPU 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 //===----------------------------------------------------------------------===//
9 #include "PS4CPU.h"
10 #include "CommonArgs.h"
11 #include "clang/Driver/Compilation.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/DriverDiagnostic.h"
14 #include "clang/Driver/Options.h"
15 #include "clang/Driver/SanitizerArgs.h"
16 #include "llvm/Option/ArgList.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 #include <cstdlib> // ::getenv
21 using namespace clang::driver;
22 using namespace clang;
23 using namespace llvm::opt;
25 // Helper to paste bits of an option together and return a saved string.
26 static const char *makeArgString(const ArgList &Args, const char *Prefix,
27 const char *Base, const char *Suffix) {
28 // Basically "Prefix + Base + Suffix" all converted to Twine then saved.
29 return Args.MakeArgString(Twine(StringRef(Prefix), Base) + Suffix);
32 void tools::PScpu::addProfileRTArgs(const ToolChain &TC, const ArgList &Args,
33 ArgStringList &CmdArgs) {
34 assert(TC.getTriple().isPS());
35 auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC);
37 if ((Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
38 false) ||
39 Args.hasFlag(options::OPT_fprofile_generate,
40 options::OPT_fno_profile_generate, false) ||
41 Args.hasFlag(options::OPT_fprofile_generate_EQ,
42 options::OPT_fno_profile_generate, false) ||
43 Args.hasFlag(options::OPT_fprofile_instr_generate,
44 options::OPT_fno_profile_instr_generate, false) ||
45 Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
46 options::OPT_fno_profile_instr_generate, false) ||
47 Args.hasFlag(options::OPT_fcs_profile_generate,
48 options::OPT_fno_profile_generate, false) ||
49 Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
50 options::OPT_fno_profile_generate, false) ||
51 Args.hasArg(options::OPT_fcreate_profile) ||
52 Args.hasArg(options::OPT_coverage)))
53 CmdArgs.push_back(makeArgString(
54 Args, "--dependent-lib=", PSTC.getProfileRTLibName(), ""));
57 void tools::PScpu::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
58 const InputInfo &Output,
59 const InputInfoList &Inputs,
60 const ArgList &Args,
61 const char *LinkingOutput) const {
62 auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain());
63 claimNoWarnArgs(Args);
64 ArgStringList CmdArgs;
66 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
68 CmdArgs.push_back("-o");
69 CmdArgs.push_back(Output.getFilename());
71 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
72 const InputInfo &Input = Inputs[0];
73 assert(Input.isFilename() && "Invalid input.");
74 CmdArgs.push_back(Input.getFilename());
76 std::string AsName = TC.qualifyPSCmdName("as");
77 const char *Exec = Args.MakeArgString(TC.GetProgramPath(AsName.c_str()));
78 C.addCommand(std::make_unique<Command>(JA, *this,
79 ResponseFileSupport::AtFileUTF8(),
80 Exec, CmdArgs, Inputs, Output));
83 void tools::PScpu::addSanitizerArgs(const ToolChain &TC, const ArgList &Args,
84 ArgStringList &CmdArgs) {
85 assert(TC.getTriple().isPS());
86 auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC);
87 PSTC.addSanitizerArgs(Args, CmdArgs, "--dependent-lib=lib", ".a");
90 void toolchains::PS4CPU::addSanitizerArgs(const ArgList &Args,
91 ArgStringList &CmdArgs,
92 const char *Prefix,
93 const char *Suffix) const {
94 auto arg = [&](const char *Name) -> const char * {
95 return makeArgString(Args, Prefix, Name, Suffix);
97 const SanitizerArgs &SanArgs = getSanitizerArgs(Args);
98 if (SanArgs.needsUbsanRt())
99 CmdArgs.push_back(arg("SceDbgUBSanitizer_stub_weak"));
100 if (SanArgs.needsAsanRt())
101 CmdArgs.push_back(arg("SceDbgAddressSanitizer_stub_weak"));
104 void toolchains::PS5CPU::addSanitizerArgs(const ArgList &Args,
105 ArgStringList &CmdArgs,
106 const char *Prefix,
107 const char *Suffix) const {
108 auto arg = [&](const char *Name) -> const char * {
109 return makeArgString(Args, Prefix, Name, Suffix);
111 const SanitizerArgs &SanArgs = getSanitizerArgs(Args);
112 if (SanArgs.needsUbsanRt())
113 CmdArgs.push_back(arg("SceUBSanitizer_nosubmission_stub_weak"));
114 if (SanArgs.needsAsanRt())
115 CmdArgs.push_back(arg("SceAddressSanitizer_nosubmission_stub_weak"));
116 if (SanArgs.needsTsanRt())
117 CmdArgs.push_back(arg("SceThreadSanitizer_nosubmission_stub_weak"));
120 void tools::PScpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
121 const InputInfo &Output,
122 const InputInfoList &Inputs,
123 const ArgList &Args,
124 const char *LinkingOutput) const {
125 auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain());
126 const Driver &D = TC.getDriver();
127 ArgStringList CmdArgs;
129 // Silence warning for "clang -g foo.o -o foo"
130 Args.ClaimAllArgs(options::OPT_g_Group);
131 // and "clang -emit-llvm foo.o -o foo"
132 Args.ClaimAllArgs(options::OPT_emit_llvm);
133 // and for "clang -w foo.o -o foo". Other warning options are already
134 // handled somewhere else.
135 Args.ClaimAllArgs(options::OPT_w);
137 if (!D.SysRoot.empty())
138 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
140 if (Args.hasArg(options::OPT_pie))
141 CmdArgs.push_back("-pie");
143 if (Args.hasArg(options::OPT_rdynamic))
144 CmdArgs.push_back("-export-dynamic");
145 if (Args.hasArg(options::OPT_shared))
146 CmdArgs.push_back("--shared");
148 if (Output.isFilename()) {
149 CmdArgs.push_back("-o");
150 CmdArgs.push_back(Output.getFilename());
151 } else {
152 assert(Output.isNothing() && "Invalid output.");
155 const bool UseLTO = D.isUsingLTO();
156 const bool UseJMC =
157 Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false);
158 const bool IsPS4 = TC.getTriple().isPS4();
159 const bool IsPS5 = TC.getTriple().isPS5();
160 assert(IsPS4 || IsPS5);
162 // This tells LTO to perform JustMyCode instrumentation.
163 if (UseLTO && UseJMC) {
164 if (IsPS4 && D.getLTOMode() == LTOK_Thin) {
165 CmdArgs.push_back("-lto-thin-debug-options=-enable-jmc-instrument");
166 } else if (IsPS4 && D.getLTOMode() == LTOK_Full) {
167 CmdArgs.push_back("-lto-debug-options=-enable-jmc-instrument");
168 } else if (IsPS5) {
169 CmdArgs.push_back("-mllvm");
170 CmdArgs.push_back("-enable-jmc-instrument");
171 } else
172 llvm_unreachable("new LTO mode?");
175 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
176 TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
178 Args.AddAllArgs(CmdArgs, options::OPT_L);
179 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
180 Args.AddAllArgs(CmdArgs, options::OPT_e);
181 Args.AddAllArgs(CmdArgs, options::OPT_s);
182 Args.AddAllArgs(CmdArgs, options::OPT_t);
183 Args.AddAllArgs(CmdArgs, options::OPT_r);
185 if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
186 CmdArgs.push_back("--no-demangle");
188 AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
190 if (Args.hasArg(options::OPT_pthread)) {
191 CmdArgs.push_back("-lpthread");
194 if (UseJMC) {
195 CmdArgs.push_back("--whole-archive");
196 if (IsPS4)
197 CmdArgs.push_back("-lSceDbgJmc");
198 else
199 CmdArgs.push_back("-lSceJmc_nosubmission");
200 CmdArgs.push_back("--no-whole-archive");
203 if (Args.hasArg(options::OPT_fuse_ld_EQ)) {
204 D.Diag(diag::err_drv_unsupported_opt_for_target)
205 << "-fuse-ld" << TC.getTriple().str();
208 std::string LdName = TC.qualifyPSCmdName(TC.getLinkerBaseName());
209 const char *Exec = Args.MakeArgString(TC.GetProgramPath(LdName.c_str()));
211 C.addCommand(std::make_unique<Command>(JA, *this,
212 ResponseFileSupport::AtFileUTF8(),
213 Exec, CmdArgs, Inputs, Output));
216 toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple,
217 const ArgList &Args, StringRef Platform,
218 const char *EnvVar)
219 : Generic_ELF(D, Triple, Args) {
220 if (Args.hasArg(clang::driver::options::OPT_static))
221 D.Diag(clang::diag::err_drv_unsupported_opt_for_target)
222 << "-static" << Platform;
224 // Determine where to find the PS4/PS5 libraries. We use the EnvVar
225 // if it exists; otherwise use the driver's installation path, which
226 // should be <SDK_DIR>/host_tools/bin.
228 SmallString<512> SDKDir;
229 if (const char *EnvValue = getenv(EnvVar)) {
230 if (!llvm::sys::fs::exists(EnvValue))
231 D.Diag(clang::diag::warn_drv_ps_sdk_dir) << EnvVar << EnvValue;
232 SDKDir = EnvValue;
233 } else {
234 SDKDir = D.Dir;
235 llvm::sys::path::append(SDKDir, "/../../");
238 // By default, the driver won't report a warning if it can't find the
239 // SDK include or lib directories. This behavior could be changed if
240 // -Weverything or -Winvalid-or-nonexistent-directory options are passed.
241 // If -isysroot was passed, use that as the SDK base path.
242 std::string PrefixDir;
243 if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
244 PrefixDir = A->getValue();
245 if (!llvm::sys::fs::exists(PrefixDir))
246 D.Diag(clang::diag::warn_missing_sysroot) << PrefixDir;
247 } else
248 PrefixDir = std::string(SDKDir.str());
250 SmallString<512> SDKIncludeDir(PrefixDir);
251 llvm::sys::path::append(SDKIncludeDir, "target/include");
252 if (!Args.hasArg(options::OPT_nostdinc) &&
253 !Args.hasArg(options::OPT_nostdlibinc) &&
254 !Args.hasArg(options::OPT_isysroot) &&
255 !Args.hasArg(options::OPT__sysroot_EQ) &&
256 !llvm::sys::fs::exists(SDKIncludeDir)) {
257 D.Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
258 << Twine(Platform, " system headers").str() << SDKIncludeDir;
261 SmallString<512> SDKLibDir(SDKDir);
262 llvm::sys::path::append(SDKLibDir, "target/lib");
263 if (!Args.hasArg(options::OPT_nostdlib) &&
264 !Args.hasArg(options::OPT_nodefaultlibs) &&
265 !Args.hasArg(options::OPT__sysroot_EQ) && !Args.hasArg(options::OPT_E) &&
266 !Args.hasArg(options::OPT_c) && !Args.hasArg(options::OPT_S) &&
267 !Args.hasArg(options::OPT_emit_ast) &&
268 !llvm::sys::fs::exists(SDKLibDir)) {
269 D.Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
270 << Twine(Platform, " system libraries").str() << SDKLibDir;
271 return;
273 getFilePaths().push_back(std::string(SDKLibDir.str()));
276 Tool *toolchains::PS4CPU::buildAssembler() const {
277 return new tools::PScpu::Assembler(*this);
280 Tool *toolchains::PS5CPU::buildAssembler() const {
281 // PS5 does not support an external assembler.
282 getDriver().Diag(clang::diag::err_no_external_assembler);
283 return nullptr;
286 Tool *toolchains::PS4PS5Base::buildLinker() const {
287 return new tools::PScpu::Linker(*this);
290 SanitizerMask toolchains::PS4PS5Base::getSupportedSanitizers() const {
291 SanitizerMask Res = ToolChain::getSupportedSanitizers();
292 Res |= SanitizerKind::Address;
293 Res |= SanitizerKind::PointerCompare;
294 Res |= SanitizerKind::PointerSubtract;
295 Res |= SanitizerKind::Vptr;
296 return Res;
299 SanitizerMask toolchains::PS5CPU::getSupportedSanitizers() const {
300 SanitizerMask Res = PS4PS5Base::getSupportedSanitizers();
301 Res |= SanitizerKind::Thread;
302 return Res;
305 void toolchains::PS4PS5Base::addClangTargetOptions(
306 const ArgList &DriverArgs, ArgStringList &CC1Args,
307 Action::OffloadKind DeviceOffloadingKind) const {
308 // PS4/PS5 do not use init arrays.
309 if (DriverArgs.hasArg(options::OPT_fuse_init_array)) {
310 Arg *A = DriverArgs.getLastArg(options::OPT_fuse_init_array);
311 getDriver().Diag(clang::diag::err_drv_unsupported_opt_for_target)
312 << A->getAsString(DriverArgs) << getTriple().str();
315 CC1Args.push_back("-fno-use-init-array");
317 const Arg *A =
318 DriverArgs.getLastArg(options::OPT_fvisibility_from_dllstorageclass,
319 options::OPT_fno_visibility_from_dllstorageclass);
320 if (!A ||
321 A->getOption().matches(options::OPT_fvisibility_from_dllstorageclass)) {
322 CC1Args.push_back("-fvisibility-from-dllstorageclass");
324 if (DriverArgs.hasArg(options::OPT_fvisibility_dllexport_EQ))
325 DriverArgs.AddLastArg(CC1Args, options::OPT_fvisibility_dllexport_EQ);
326 else
327 CC1Args.push_back("-fvisibility-dllexport=protected");
329 if (DriverArgs.hasArg(options::OPT_fvisibility_nodllstorageclass_EQ))
330 DriverArgs.AddLastArg(CC1Args,
331 options::OPT_fvisibility_nodllstorageclass_EQ);
332 else
333 CC1Args.push_back("-fvisibility-nodllstorageclass=hidden");
335 if (DriverArgs.hasArg(options::OPT_fvisibility_externs_dllimport_EQ))
336 DriverArgs.AddLastArg(CC1Args,
337 options::OPT_fvisibility_externs_dllimport_EQ);
338 else
339 CC1Args.push_back("-fvisibility-externs-dllimport=default");
341 if (DriverArgs.hasArg(
342 options::OPT_fvisibility_externs_nodllstorageclass_EQ))
343 DriverArgs.AddLastArg(
344 CC1Args, options::OPT_fvisibility_externs_nodllstorageclass_EQ);
345 else
346 CC1Args.push_back("-fvisibility-externs-nodllstorageclass=default");
350 // PS4 toolchain.
351 toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple,
352 const llvm::opt::ArgList &Args)
353 : PS4PS5Base(D, Triple, Args, "PS4", "SCE_ORBIS_SDK_DIR") {}
355 // PS5 toolchain.
356 toolchains::PS5CPU::PS5CPU(const Driver &D, const llvm::Triple &Triple,
357 const llvm::opt::ArgList &Args)
358 : PS4PS5Base(D, Triple, Args, "PS5", "SCE_PROSPERO_SDK_DIR") {}