xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/PS4CPU.cpp (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 //===----------------------------------------------------------------------===//
8 
9 #include "PS4CPU.h"
10 #include "CommonArgs.h"
11 #include "clang/Config/config.h"
12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/DriverDiagnostic.h"
15 #include "clang/Driver/Options.h"
16 #include "clang/Driver/SanitizerArgs.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include <cstdlib> // ::getenv
21 
22 using namespace clang::driver;
23 using namespace clang;
24 using namespace llvm::opt;
25 
26 // Helper to paste bits of an option together and return a saved string.
27 static const char *makeArgString(const ArgList &Args, const char *Prefix,
28                                  const char *Base, const char *Suffix) {
29   // Basically "Prefix + Base + Suffix" all converted to Twine then saved.
30   return Args.MakeArgString(Twine(StringRef(Prefix), Base) + Suffix);
31 }
32 
33 void tools::PScpu::addProfileRTArgs(const ToolChain &TC, const ArgList &Args,
34                                     ArgStringList &CmdArgs) {
35   assert(TC.getTriple().isPS());
36   auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC);
37 
38   if ((Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
39                     false) ||
40        Args.hasFlag(options::OPT_fprofile_generate,
41                     options::OPT_fno_profile_generate, false) ||
42        Args.hasFlag(options::OPT_fprofile_generate_EQ,
43                     options::OPT_fno_profile_generate, false) ||
44        Args.hasFlag(options::OPT_fprofile_instr_generate,
45                     options::OPT_fno_profile_instr_generate, false) ||
46        Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
47                     options::OPT_fno_profile_instr_generate, false) ||
48        Args.hasFlag(options::OPT_fcs_profile_generate,
49                     options::OPT_fno_profile_generate, false) ||
50        Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
51                     options::OPT_fno_profile_generate, false) ||
52        Args.hasArg(options::OPT_fcreate_profile) ||
53        Args.hasArg(options::OPT_coverage)))
54     CmdArgs.push_back(makeArgString(
55         Args, "--dependent-lib=", PSTC.getProfileRTLibName(), ""));
56 }
57 
58 void tools::PScpu::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
59                                            const InputInfo &Output,
60                                            const InputInfoList &Inputs,
61                                            const ArgList &Args,
62                                            const char *LinkingOutput) const {
63   auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain());
64   claimNoWarnArgs(Args);
65   ArgStringList CmdArgs;
66 
67   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
68 
69   CmdArgs.push_back("-o");
70   CmdArgs.push_back(Output.getFilename());
71 
72   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
73   const InputInfo &Input = Inputs[0];
74   assert(Input.isFilename() && "Invalid input.");
75   CmdArgs.push_back(Input.getFilename());
76 
77   std::string AsName = TC.qualifyPSCmdName("as");
78   const char *Exec = Args.MakeArgString(TC.GetProgramPath(AsName.c_str()));
79   C.addCommand(std::make_unique<Command>(JA, *this,
80                                          ResponseFileSupport::AtFileUTF8(),
81                                          Exec, CmdArgs, Inputs, Output));
82 }
83 
84 void tools::PScpu::addSanitizerArgs(const ToolChain &TC, const ArgList &Args,
85                                     ArgStringList &CmdArgs) {
86   assert(TC.getTriple().isPS());
87   auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC);
88   PSTC.addSanitizerArgs(Args, CmdArgs, "--dependent-lib=lib", ".a");
89 }
90 
91 void toolchains::PS4CPU::addSanitizerArgs(const ArgList &Args,
92                                           ArgStringList &CmdArgs,
93                                           const char *Prefix,
94                                           const char *Suffix) const {
95   auto arg = [&](const char *Name) -> const char * {
96     return makeArgString(Args, Prefix, Name, Suffix);
97   };
98   const SanitizerArgs &SanArgs = getSanitizerArgs(Args);
99   if (SanArgs.needsUbsanRt())
100     CmdArgs.push_back(arg("SceDbgUBSanitizer_stub_weak"));
101   if (SanArgs.needsAsanRt())
102     CmdArgs.push_back(arg("SceDbgAddressSanitizer_stub_weak"));
103 }
104 
105 void toolchains::PS5CPU::addSanitizerArgs(const ArgList &Args,
106                                           ArgStringList &CmdArgs,
107                                           const char *Prefix,
108                                           const char *Suffix) const {
109   auto arg = [&](const char *Name) -> const char * {
110     return makeArgString(Args, Prefix, Name, Suffix);
111   };
112   const SanitizerArgs &SanArgs = getSanitizerArgs(Args);
113   if (SanArgs.needsUbsanRt())
114     CmdArgs.push_back(arg("SceUBSanitizer_nosubmission_stub_weak"));
115   if (SanArgs.needsAsanRt())
116     CmdArgs.push_back(arg("SceAddressSanitizer_nosubmission_stub_weak"));
117   if (SanArgs.needsTsanRt())
118     CmdArgs.push_back(arg("SceThreadSanitizer_nosubmission_stub_weak"));
119 }
120 
121 void tools::PScpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
122                                         const InputInfo &Output,
123                                         const InputInfoList &Inputs,
124                                         const ArgList &Args,
125                                         const char *LinkingOutput) const {
126   auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain());
127   const Driver &D = TC.getDriver();
128   ArgStringList CmdArgs;
129 
130   // Silence warning for "clang -g foo.o -o foo"
131   Args.ClaimAllArgs(options::OPT_g_Group);
132   // and "clang -emit-llvm foo.o -o foo"
133   Args.ClaimAllArgs(options::OPT_emit_llvm);
134   // and for "clang -w foo.o -o foo". Other warning options are already
135   // handled somewhere else.
136   Args.ClaimAllArgs(options::OPT_w);
137 
138   if (!D.SysRoot.empty())
139     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
140 
141   if (Args.hasArg(options::OPT_pie))
142     CmdArgs.push_back("-pie");
143 
144   if (Args.hasArg(options::OPT_rdynamic))
145     CmdArgs.push_back("-export-dynamic");
146   if (Args.hasArg(options::OPT_shared))
147     CmdArgs.push_back("--shared");
148 
149   if (Output.isFilename()) {
150     CmdArgs.push_back("-o");
151     CmdArgs.push_back(Output.getFilename());
152   } else {
153     assert(Output.isNothing() && "Invalid output.");
154   }
155 
156   const bool UseLTO = D.isUsingLTO();
157   const bool UseJMC =
158       Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false);
159   const bool IsPS4 = TC.getTriple().isPS4();
160   const bool IsPS5 = TC.getTriple().isPS5();
161   assert(IsPS4 || IsPS5);
162 
163   const char *PS4LTOArgs = "";
164   auto AddCodeGenFlag = [&](Twine Flag) {
165     if (IsPS4)
166       PS4LTOArgs = Args.MakeArgString(Twine(PS4LTOArgs) + " " + Flag);
167     else if (IsPS5)
168       CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=") + Flag));
169   };
170 
171   if (UseLTO) {
172     // We default to creating the arange section, but LTO does not. Enable it
173     // here.
174     AddCodeGenFlag("-generate-arange-section");
175 
176     // This tells LTO to perform JustMyCode instrumentation.
177     if (UseJMC)
178       AddCodeGenFlag("-enable-jmc-instrument");
179 
180     if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir))
181       AddCodeGenFlag(Twine("-crash-diagnostics-dir=") + A->getValue());
182 
183     StringRef Parallelism = getLTOParallelism(Args, D);
184     if (!Parallelism.empty()) {
185       if (IsPS4)
186         AddCodeGenFlag(Twine("-threads=") + Parallelism);
187       else
188         CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=jobs=") + Parallelism));
189     }
190 
191     if (IsPS4) {
192       const char *Prefix = nullptr;
193       if (D.getLTOMode() == LTOK_Thin)
194         Prefix = "-lto-thin-debug-options=";
195       else if (D.getLTOMode() == LTOK_Full)
196         Prefix = "-lto-debug-options=";
197       else
198         llvm_unreachable("new LTO mode?");
199 
200       CmdArgs.push_back(Args.MakeArgString(Twine(Prefix) + PS4LTOArgs));
201     }
202   }
203 
204   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
205     TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
206 
207   if (D.isUsingLTO() && Args.hasArg(options::OPT_funified_lto)) {
208     if (D.getLTOMode() == LTOK_Thin)
209       CmdArgs.push_back("--lto=thin");
210     else if (D.getLTOMode() == LTOK_Full)
211       CmdArgs.push_back("--lto=full");
212   }
213 
214   Args.AddAllArgs(CmdArgs, options::OPT_L);
215   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
216   Args.AddAllArgs(CmdArgs, options::OPT_s);
217   Args.AddAllArgs(CmdArgs, options::OPT_t);
218   Args.AddAllArgs(CmdArgs, options::OPT_r);
219 
220   if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
221     CmdArgs.push_back("--no-demangle");
222 
223   AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
224 
225   if (Args.hasArg(options::OPT_pthread)) {
226     CmdArgs.push_back("-lpthread");
227   }
228 
229   if (UseJMC) {
230     CmdArgs.push_back("--whole-archive");
231     if (IsPS4)
232       CmdArgs.push_back("-lSceDbgJmc");
233     else
234       CmdArgs.push_back("-lSceJmc_nosubmission");
235     CmdArgs.push_back("--no-whole-archive");
236   }
237 
238   if (Args.hasArg(options::OPT_fuse_ld_EQ)) {
239     D.Diag(diag::err_drv_unsupported_opt_for_target)
240         << "-fuse-ld" << TC.getTriple().str();
241   }
242 
243   std::string LdName = TC.qualifyPSCmdName(TC.getLinkerBaseName());
244   const char *Exec = Args.MakeArgString(TC.GetProgramPath(LdName.c_str()));
245 
246   C.addCommand(std::make_unique<Command>(JA, *this,
247                                          ResponseFileSupport::AtFileUTF8(),
248                                          Exec, CmdArgs, Inputs, Output));
249 }
250 
251 toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple,
252                                    const ArgList &Args, StringRef Platform,
253                                    const char *EnvVar)
254     : Generic_ELF(D, Triple, Args) {
255   if (Args.hasArg(clang::driver::options::OPT_static))
256     D.Diag(clang::diag::err_drv_unsupported_opt_for_target)
257         << "-static" << Platform;
258 
259   // Determine where to find the PS4/PS5 libraries.
260   // If -isysroot was passed, use that as the SDK base path.
261   // If not, we use the EnvVar if it exists; otherwise use the driver's
262   // installation path, which should be <SDK_DIR>/host_tools/bin.
263   SmallString<80> Whence;
264   if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
265     SDKRootDir = A->getValue();
266     if (!llvm::sys::fs::exists(SDKRootDir))
267       D.Diag(clang::diag::warn_missing_sysroot) << SDKRootDir;
268     Whence = A->getSpelling();
269   } else if (const char *EnvValue = getenv(EnvVar)) {
270     SDKRootDir = EnvValue;
271     Whence = { "environment variable '", EnvVar, "'" };
272   } else {
273     SDKRootDir = D.Dir + "/../../";
274     Whence = "compiler's location";
275   }
276 
277   SmallString<512> SDKIncludeDir(SDKRootDir);
278   llvm::sys::path::append(SDKIncludeDir, "target/include");
279   if (!Args.hasArg(options::OPT_nostdinc) &&
280       !Args.hasArg(options::OPT_nostdlibinc) &&
281       !Args.hasArg(options::OPT_isysroot) &&
282       !Args.hasArg(options::OPT__sysroot_EQ) &&
283       !llvm::sys::fs::exists(SDKIncludeDir)) {
284     D.Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
285         << Twine(Platform, " system headers").str() << SDKIncludeDir << Whence;
286   }
287 
288   SmallString<512> SDKLibDir(SDKRootDir);
289   llvm::sys::path::append(SDKLibDir, "target/lib");
290   if (!Args.hasArg(options::OPT_nostdlib) &&
291       !Args.hasArg(options::OPT_nodefaultlibs) &&
292       !Args.hasArg(options::OPT__sysroot_EQ) && !Args.hasArg(options::OPT_E) &&
293       !Args.hasArg(options::OPT_c) && !Args.hasArg(options::OPT_S) &&
294       !Args.hasArg(options::OPT_emit_ast) &&
295       !llvm::sys::fs::exists(SDKLibDir)) {
296     D.Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
297         << Twine(Platform, " system libraries").str() << SDKLibDir << Whence;
298     return;
299   }
300   getFilePaths().push_back(std::string(SDKLibDir.str()));
301 }
302 
303 void toolchains::PS4PS5Base::AddClangSystemIncludeArgs(
304     const ArgList &DriverArgs,
305     ArgStringList &CC1Args) const {
306   const Driver &D = getDriver();
307 
308   if (DriverArgs.hasArg(options::OPT_nostdinc))
309     return;
310 
311   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
312     SmallString<128> Dir(D.ResourceDir);
313     llvm::sys::path::append(Dir, "include");
314     addSystemInclude(DriverArgs, CC1Args, Dir.str());
315   }
316 
317   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
318     return;
319 
320   addExternCSystemInclude(DriverArgs, CC1Args,
321                           SDKRootDir + "/target/include");
322   addExternCSystemInclude(DriverArgs, CC1Args,
323                           SDKRootDir + "/target/include_common");
324 }
325 
326 Tool *toolchains::PS4CPU::buildAssembler() const {
327   return new tools::PScpu::Assembler(*this);
328 }
329 
330 Tool *toolchains::PS5CPU::buildAssembler() const {
331   // PS5 does not support an external assembler.
332   getDriver().Diag(clang::diag::err_no_external_assembler);
333   return nullptr;
334 }
335 
336 Tool *toolchains::PS4PS5Base::buildLinker() const {
337   return new tools::PScpu::Linker(*this);
338 }
339 
340 SanitizerMask toolchains::PS4PS5Base::getSupportedSanitizers() const {
341   SanitizerMask Res = ToolChain::getSupportedSanitizers();
342   Res |= SanitizerKind::Address;
343   Res |= SanitizerKind::PointerCompare;
344   Res |= SanitizerKind::PointerSubtract;
345   Res |= SanitizerKind::Vptr;
346   return Res;
347 }
348 
349 SanitizerMask toolchains::PS5CPU::getSupportedSanitizers() const {
350   SanitizerMask Res = PS4PS5Base::getSupportedSanitizers();
351   Res |= SanitizerKind::Thread;
352   return Res;
353 }
354 
355 void toolchains::PS4PS5Base::addClangTargetOptions(
356     const ArgList &DriverArgs, ArgStringList &CC1Args,
357     Action::OffloadKind DeviceOffloadingKind) const {
358   // PS4/PS5 do not use init arrays.
359   if (DriverArgs.hasArg(options::OPT_fuse_init_array)) {
360     Arg *A = DriverArgs.getLastArg(options::OPT_fuse_init_array);
361     getDriver().Diag(clang::diag::err_drv_unsupported_opt_for_target)
362         << A->getAsString(DriverArgs) << getTriple().str();
363   }
364 
365   CC1Args.push_back("-fno-use-init-array");
366 
367   const Arg *A =
368       DriverArgs.getLastArg(options::OPT_fvisibility_from_dllstorageclass,
369                             options::OPT_fno_visibility_from_dllstorageclass);
370   if (!A ||
371       A->getOption().matches(options::OPT_fvisibility_from_dllstorageclass)) {
372     CC1Args.push_back("-fvisibility-from-dllstorageclass");
373 
374     if (DriverArgs.hasArg(options::OPT_fvisibility_dllexport_EQ))
375       DriverArgs.AddLastArg(CC1Args, options::OPT_fvisibility_dllexport_EQ);
376     else
377       CC1Args.push_back("-fvisibility-dllexport=protected");
378 
379     if (DriverArgs.hasArg(options::OPT_fvisibility_nodllstorageclass_EQ))
380       DriverArgs.AddLastArg(CC1Args,
381                             options::OPT_fvisibility_nodllstorageclass_EQ);
382     else
383       CC1Args.push_back("-fvisibility-nodllstorageclass=hidden");
384 
385     if (DriverArgs.hasArg(options::OPT_fvisibility_externs_dllimport_EQ))
386       DriverArgs.AddLastArg(CC1Args,
387                             options::OPT_fvisibility_externs_dllimport_EQ);
388     else
389       CC1Args.push_back("-fvisibility-externs-dllimport=default");
390 
391     if (DriverArgs.hasArg(
392             options::OPT_fvisibility_externs_nodllstorageclass_EQ))
393       DriverArgs.AddLastArg(
394           CC1Args, options::OPT_fvisibility_externs_nodllstorageclass_EQ);
395     else
396       CC1Args.push_back("-fvisibility-externs-nodllstorageclass=default");
397   }
398 }
399 
400 // PS4 toolchain.
401 toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple,
402                            const llvm::opt::ArgList &Args)
403     : PS4PS5Base(D, Triple, Args, "PS4", "SCE_ORBIS_SDK_DIR") {}
404 
405 // PS5 toolchain.
406 toolchains::PS5CPU::PS5CPU(const Driver &D, const llvm::Triple &Triple,
407                            const llvm::opt::ArgList &Args)
408     : PS4PS5Base(D, Triple, Args, "PS5", "SCE_PROSPERO_SDK_DIR") {}
409