xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/PS4CPU.cpp (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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 "FreeBSD.h"
11 #include "CommonArgs.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 void tools::PS4cpu::addProfileRTArgs(const ToolChain &TC, const ArgList &Args,
27                                      ArgStringList &CmdArgs) {
28   if ((Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
29                     false) ||
30        Args.hasFlag(options::OPT_fprofile_generate,
31                     options::OPT_fno_profile_generate, false) ||
32        Args.hasFlag(options::OPT_fprofile_generate_EQ,
33                     options::OPT_fno_profile_generate, false) ||
34        Args.hasFlag(options::OPT_fprofile_instr_generate,
35                     options::OPT_fno_profile_instr_generate, false) ||
36        Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
37                     options::OPT_fno_profile_instr_generate, false) ||
38        Args.hasFlag(options::OPT_fcs_profile_generate,
39                     options::OPT_fno_profile_generate, false) ||
40        Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
41                     options::OPT_fno_profile_generate, false) ||
42        Args.hasArg(options::OPT_fcreate_profile) ||
43        Args.hasArg(options::OPT_coverage)))
44     CmdArgs.push_back("--dependent-lib=libclang_rt.profile-x86_64.a");
45 }
46 
47 void tools::PS4cpu::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
48                                            const InputInfo &Output,
49                                            const InputInfoList &Inputs,
50                                            const ArgList &Args,
51                                            const char *LinkingOutput) const {
52   claimNoWarnArgs(Args);
53   ArgStringList CmdArgs;
54 
55   Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
56 
57   CmdArgs.push_back("-o");
58   CmdArgs.push_back(Output.getFilename());
59 
60   assert(Inputs.size() == 1 && "Unexpected number of inputs.");
61   const InputInfo &Input = Inputs[0];
62   assert(Input.isFilename() && "Invalid input.");
63   CmdArgs.push_back(Input.getFilename());
64 
65   const char *Exec =
66       Args.MakeArgString(getToolChain().GetProgramPath("orbis-as"));
67   C.addCommand(std::make_unique<Command>(JA, *this,
68                                          ResponseFileSupport::AtFileUTF8(),
69                                          Exec, CmdArgs, Inputs, Output));
70 }
71 
72 static void AddPS4SanitizerArgs(const ToolChain &TC, const ArgList &Args,
73                                 ArgStringList &CmdArgs) {
74   const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args);
75   if (SanArgs.needsUbsanRt()) {
76     CmdArgs.push_back("-lSceDbgUBSanitizer_stub_weak");
77   }
78   if (SanArgs.needsAsanRt()) {
79     CmdArgs.push_back("-lSceDbgAddressSanitizer_stub_weak");
80   }
81 }
82 
83 void tools::PS4cpu::addSanitizerArgs(const ToolChain &TC, const ArgList &Args,
84                                      ArgStringList &CmdArgs) {
85   const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args);
86   if (SanArgs.needsUbsanRt())
87     CmdArgs.push_back("--dependent-lib=libSceDbgUBSanitizer_stub_weak.a");
88   if (SanArgs.needsAsanRt())
89     CmdArgs.push_back("--dependent-lib=libSceDbgAddressSanitizer_stub_weak.a");
90 }
91 
92 void tools::PS4cpu::Link::ConstructJob(Compilation &C, const JobAction &JA,
93                                        const InputInfo &Output,
94                                        const InputInfoList &Inputs,
95                                        const ArgList &Args,
96                                        const char *LinkingOutput) const {
97   const toolchains::FreeBSD &ToolChain =
98       static_cast<const toolchains::FreeBSD &>(getToolChain());
99   const Driver &D = ToolChain.getDriver();
100   ArgStringList CmdArgs;
101 
102   // Silence warning for "clang -g foo.o -o foo"
103   Args.ClaimAllArgs(options::OPT_g_Group);
104   // and "clang -emit-llvm foo.o -o foo"
105   Args.ClaimAllArgs(options::OPT_emit_llvm);
106   // and for "clang -w foo.o -o foo". Other warning options are already
107   // handled somewhere else.
108   Args.ClaimAllArgs(options::OPT_w);
109 
110   if (!D.SysRoot.empty())
111     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
112 
113   if (Args.hasArg(options::OPT_pie))
114     CmdArgs.push_back("-pie");
115 
116   if (Args.hasArg(options::OPT_rdynamic))
117     CmdArgs.push_back("-export-dynamic");
118   if (Args.hasArg(options::OPT_shared))
119     CmdArgs.push_back("--oformat=so");
120 
121   if (Output.isFilename()) {
122     CmdArgs.push_back("-o");
123     CmdArgs.push_back(Output.getFilename());
124   } else {
125     assert(Output.isNothing() && "Invalid output.");
126   }
127 
128   if(!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
129     AddPS4SanitizerArgs(ToolChain, Args, CmdArgs);
130 
131   Args.AddAllArgs(CmdArgs, options::OPT_L);
132   Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
133   Args.AddAllArgs(CmdArgs, options::OPT_e);
134   Args.AddAllArgs(CmdArgs, options::OPT_s);
135   Args.AddAllArgs(CmdArgs, options::OPT_t);
136   Args.AddAllArgs(CmdArgs, options::OPT_r);
137 
138   if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
139     CmdArgs.push_back("--no-demangle");
140 
141   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
142 
143   if (Args.hasArg(options::OPT_pthread)) {
144     CmdArgs.push_back("-lpthread");
145   }
146 
147   if (Args.hasArg(options::OPT_fuse_ld_EQ)) {
148     D.Diag(diag::err_drv_unsupported_opt_for_target)
149         << "-fuse-ld" << getToolChain().getTriple().str();
150   }
151 
152   const char *Exec =
153       Args.MakeArgString(ToolChain.GetProgramPath("orbis-ld"));
154 
155   C.addCommand(std::make_unique<Command>(JA, *this,
156                                          ResponseFileSupport::AtFileUTF8(),
157                                          Exec, CmdArgs, Inputs, Output));
158 }
159 
160 toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple,
161                            const ArgList &Args)
162     : Generic_ELF(D, Triple, Args) {
163   if (Args.hasArg(clang::driver::options::OPT_static))
164     D.Diag(clang::diag::err_drv_unsupported_opt_for_target) << "-static"
165                                                             << "PS4";
166 
167   // Determine where to find the PS4 libraries. We use SCE_ORBIS_SDK_DIR
168   // if it exists; otherwise use the driver's installation path, which
169   // should be <SDK_DIR>/host_tools/bin.
170 
171   SmallString<512> PS4SDKDir;
172   if (const char *EnvValue = getenv("SCE_ORBIS_SDK_DIR")) {
173     if (!llvm::sys::fs::exists(EnvValue))
174       getDriver().Diag(clang::diag::warn_drv_ps4_sdk_dir) << EnvValue;
175     PS4SDKDir = EnvValue;
176   } else {
177     PS4SDKDir = getDriver().Dir;
178     llvm::sys::path::append(PS4SDKDir, "/../../");
179   }
180 
181   // By default, the driver won't report a warning if it can't find
182   // PS4's include or lib directories. This behavior could be changed if
183   // -Weverything or -Winvalid-or-nonexistent-directory options are passed.
184   // If -isysroot was passed, use that as the SDK base path.
185   std::string PrefixDir;
186   if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
187     PrefixDir = A->getValue();
188     if (!llvm::sys::fs::exists(PrefixDir))
189       getDriver().Diag(clang::diag::warn_missing_sysroot) << PrefixDir;
190   } else
191     PrefixDir = std::string(PS4SDKDir.str());
192 
193   SmallString<512> PS4SDKIncludeDir(PrefixDir);
194   llvm::sys::path::append(PS4SDKIncludeDir, "target/include");
195   if (!Args.hasArg(options::OPT_nostdinc) &&
196       !Args.hasArg(options::OPT_nostdlibinc) &&
197       !Args.hasArg(options::OPT_isysroot) &&
198       !Args.hasArg(options::OPT__sysroot_EQ) &&
199       !llvm::sys::fs::exists(PS4SDKIncludeDir)) {
200     getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
201         << "PS4 system headers" << PS4SDKIncludeDir;
202   }
203 
204   SmallString<512> PS4SDKLibDir(PS4SDKDir);
205   llvm::sys::path::append(PS4SDKLibDir, "target/lib");
206   if (!Args.hasArg(options::OPT_nostdlib) &&
207       !Args.hasArg(options::OPT_nodefaultlibs) &&
208       !Args.hasArg(options::OPT__sysroot_EQ) && !Args.hasArg(options::OPT_E) &&
209       !Args.hasArg(options::OPT_c) && !Args.hasArg(options::OPT_S) &&
210       !Args.hasArg(options::OPT_emit_ast) &&
211       !llvm::sys::fs::exists(PS4SDKLibDir)) {
212     getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
213         << "PS4 system libraries" << PS4SDKLibDir;
214     return;
215   }
216   getFilePaths().push_back(std::string(PS4SDKLibDir.str()));
217 }
218 
219 Tool *toolchains::PS4CPU::buildAssembler() const {
220   return new tools::PS4cpu::Assemble(*this);
221 }
222 
223 Tool *toolchains::PS4CPU::buildLinker() const {
224   return new tools::PS4cpu::Link(*this);
225 }
226 
227 bool toolchains::PS4CPU::isPICDefault() const { return true; }
228 
229 bool toolchains::PS4CPU::HasNativeLLVMSupport() const { return true; }
230 
231 SanitizerMask toolchains::PS4CPU::getSupportedSanitizers() const {
232   SanitizerMask Res = ToolChain::getSupportedSanitizers();
233   Res |= SanitizerKind::Address;
234   Res |= SanitizerKind::PointerCompare;
235   Res |= SanitizerKind::PointerSubtract;
236   Res |= SanitizerKind::Vptr;
237   return Res;
238 }
239 
240 void toolchains::PS4CPU::addClangTargetOptions(
241     const ArgList &DriverArgs, ArgStringList &CC1Args,
242     Action::OffloadKind DeviceOffloadingKind) const {
243   // PS4 does not use init arrays.
244   if (DriverArgs.hasArg(options::OPT_fuse_init_array)) {
245     Arg *A = DriverArgs.getLastArg(options::OPT_fuse_init_array);
246     getDriver().Diag(clang::diag::err_drv_unsupported_opt_for_target)
247         << A->getAsString(DriverArgs) << getTriple().str();
248   }
249 
250   CC1Args.push_back("-fno-use-init-array");
251 
252   const Arg *A =
253       DriverArgs.getLastArg(options::OPT_fvisibility_from_dllstorageclass,
254                             options::OPT_fno_visibility_from_dllstorageclass);
255   if (!A ||
256       A->getOption().matches(options::OPT_fvisibility_from_dllstorageclass)) {
257     CC1Args.push_back("-fvisibility-from-dllstorageclass");
258 
259     if (DriverArgs.hasArg(options::OPT_fvisibility_dllexport_EQ))
260       DriverArgs.AddLastArg(CC1Args, options::OPT_fvisibility_dllexport_EQ);
261     else
262       CC1Args.push_back("-fvisibility-dllexport=protected");
263 
264     if (DriverArgs.hasArg(options::OPT_fvisibility_nodllstorageclass_EQ))
265       DriverArgs.AddLastArg(CC1Args,
266                             options::OPT_fvisibility_nodllstorageclass_EQ);
267     else
268       CC1Args.push_back("-fvisibility-nodllstorageclass=hidden");
269 
270     if (DriverArgs.hasArg(options::OPT_fvisibility_externs_dllimport_EQ))
271       DriverArgs.AddLastArg(CC1Args,
272                             options::OPT_fvisibility_externs_dllimport_EQ);
273     else
274       CC1Args.push_back("-fvisibility-externs-dllimport=default");
275 
276     if (DriverArgs.hasArg(
277             options::OPT_fvisibility_externs_nodllstorageclass_EQ))
278       DriverArgs.AddLastArg(
279           CC1Args, options::OPT_fvisibility_externs_nodllstorageclass_EQ);
280     else
281       CC1Args.push_back("-fvisibility-externs-nodllstorageclass=default");
282   }
283 }
284