clang 20.0.0git
ZOS.cpp
Go to the documentation of this file.
1//===--- ZOS.cpp - z/OS 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 "ZOS.h"
10#include "CommonArgs.h"
13#include "llvm/Option/ArgList.h"
14#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/VirtualFileSystem.h"
16#include "llvm/Support/WithColor.h"
17
18using namespace clang;
19using namespace clang::driver;
20using namespace clang::driver::tools;
21using namespace clang::driver::toolchains;
22using namespace llvm;
23using namespace llvm::opt;
24using namespace llvm::sys;
25
26ZOS::ZOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
27 : ToolChain(D, Triple, Args) {}
28
30
31void ZOS::addClangTargetOptions(const ArgList &DriverArgs,
32 ArgStringList &CC1Args,
33 Action::OffloadKind DeviceOffloadKind) const {
34 // Pass "-faligned-alloc-unavailable" only when the user hasn't manually
35 // enabled or disabled aligned allocations.
36 if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
37 options::OPT_fno_aligned_allocation))
38 CC1Args.push_back("-faligned-alloc-unavailable");
39
40 if (DriverArgs.hasFlag(options::OPT_fxl_pragma_pack,
41 options::OPT_fno_xl_pragma_pack, true))
42 CC1Args.push_back("-fxl-pragma-pack");
43
44 // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
45 // or disabled sized deallocations.
46 if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
47 options::OPT_fno_sized_deallocation))
48 CC1Args.push_back("-fno-sized-deallocation");
49}
50
52 const InputInfo &Output,
53 const InputInfoList &Inputs,
54 const ArgList &Args,
55 const char *LinkingOutput) const {
56 ArgStringList CmdArgs;
57
58 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
59
60 // Specify assembler output file.
61 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
62 if (Output.isFilename()) {
63 CmdArgs.push_back("-o");
64 CmdArgs.push_back(Output.getFilename());
65 }
66
67 // Specify assembler input file.
68 // The system assembler on z/OS takes exactly one input file. The driver is
69 // expected to invoke as(1) separately for each assembler source input file.
70 if (Inputs.size() != 1)
71 llvm_unreachable("Invalid number of input files.");
72 const InputInfo &II = Inputs[0];
73 assert((II.isFilename() || II.isNothing()) && "Invalid input.");
74 if (II.isFilename())
75 CmdArgs.push_back(II.getFilename());
76
77 const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
78 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
79 Exec, CmdArgs, Inputs));
80}
81
82static std::string getLEHLQ(const ArgList &Args) {
83 if (Args.hasArg(options::OPT_mzos_hlq_le_EQ)) {
84 Arg *LEHLQArg = Args.getLastArg(options::OPT_mzos_hlq_le_EQ);
85 StringRef HLQ = LEHLQArg->getValue();
86 if (!HLQ.empty())
87 return HLQ.str();
88 }
89 return "CEE";
90}
91
92static std::string getClangHLQ(const ArgList &Args) {
93 if (Args.hasArg(options::OPT_mzos_hlq_clang_EQ)) {
94 Arg *ClangHLQArg = Args.getLastArg(options::OPT_mzos_hlq_clang_EQ);
95 StringRef HLQ = ClangHLQArg->getValue();
96 if (!HLQ.empty())
97 return HLQ.str();
98 }
99 return getLEHLQ(Args);
100}
101
102static std::string getCSSHLQ(const ArgList &Args) {
103 if (Args.hasArg(options::OPT_mzos_hlq_csslib_EQ)) {
104 Arg *CsslibHLQArg = Args.getLastArg(options::OPT_mzos_hlq_csslib_EQ);
105 StringRef HLQ = CsslibHLQArg->getValue();
106 if (!HLQ.empty())
107 return HLQ.str();
108 }
109 return "SYS1";
110}
111
113 const InputInfo &Output,
114 const InputInfoList &Inputs, const ArgList &Args,
115 const char *LinkingOutput) const {
116 const ZOS &ToolChain = static_cast<const ZOS &>(getToolChain());
117 ArgStringList CmdArgs;
118
119 const bool IsSharedLib =
120 Args.hasFlag(options::OPT_shared, options::OPT_static, false);
121
122 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
123 if (Output.isFilename()) {
124 CmdArgs.push_back("-o");
125 CmdArgs.push_back(Output.getFilename());
126 }
127
128 SmallString<128> LinkerOptions;
129 LinkerOptions = "AMODE=";
130 LinkerOptions += "64";
131 LinkerOptions += ",LIST";
132 LinkerOptions += ",DYNAM=DLL";
133 LinkerOptions += ",MSGLEVEL=4";
134 LinkerOptions += ",CASE=MIXED";
135 LinkerOptions += ",REUS=RENT";
136
137 CmdArgs.push_back("-b");
138 CmdArgs.push_back(Args.MakeArgString(LinkerOptions));
139
140 if (!IsSharedLib) {
141 CmdArgs.push_back("-e");
142 CmdArgs.push_back("CELQSTRT");
143
144 CmdArgs.push_back("-O");
145 CmdArgs.push_back("CELQSTRT");
146
147 CmdArgs.push_back("-u");
148 CmdArgs.push_back("CELQMAIN");
149 }
150
151 // Generate side file if -shared option is present.
152 if (IsSharedLib) {
153 StringRef OutputName = Output.getFilename();
154 // Strip away the last file suffix in presence from output name and add
155 // a new .x suffix.
156 size_t Suffix = OutputName.find_last_of('.');
157 const char *SideDeckName =
158 Args.MakeArgString(OutputName.substr(0, Suffix) + ".x");
159 CmdArgs.push_back("-x");
160 CmdArgs.push_back(SideDeckName);
161 } else {
162 // We need to direct side file to /dev/null to suppress linker warning when
163 // the object file contains exported symbols, and -shared or
164 // -Wl,-x<sidedeck>.x is not specified.
165 CmdArgs.push_back("-x");
166 CmdArgs.push_back("/dev/null");
167 }
168
169 // Add archive library search paths.
170 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
171
172 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
173
174 // Specify linker input file(s)
175 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
176
177 // z/OS tool chain depends on LE data sets and the CSSLIB data set.
178 // These data sets can have different high level qualifiers (HLQs)
179 // as each installation can define them differently.
180
181 std::string LEHLQ = getLEHLQ(Args);
182 std::string CsslibHLQ = getCSSHLQ(Args);
183
184 StringRef ld_env_var = StringRef(getenv("_LD_SYSLIB")).trim();
185 if (ld_env_var.empty()) {
186 CmdArgs.push_back("-S");
187 CmdArgs.push_back(Args.MakeArgString("//'" + LEHLQ + ".SCEEBND2'"));
188 CmdArgs.push_back("-S");
189 CmdArgs.push_back(Args.MakeArgString("//'" + CsslibHLQ + ".CSSLIB'"));
190 }
191
192 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
193 ld_env_var = StringRef(getenv("_LD_SIDE_DECKS")).trim();
194 if (ld_env_var.empty()) {
195 CmdArgs.push_back(
196 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS001)'"));
197 CmdArgs.push_back(
198 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS003)'"));
199 } else {
200 SmallVector<StringRef> ld_side_deck;
201 ld_env_var.split(ld_side_deck, ":");
202 for (StringRef ld_loc : ld_side_deck) {
203 CmdArgs.push_back((ld_loc.str()).c_str());
204 }
205 }
206 }
207 // Link libc++ library
208 if (ToolChain.ShouldLinkCXXStdlib(Args)) {
209 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
210 }
211
212 // Specify compiler-rt library path for linker
213 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
214 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
215
216 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
217 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
218 Exec, CmdArgs, Inputs));
219}
220
223}
224
227}
228
229void ZOS::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
230 llvm::opt::ArgStringList &CmdArgs) const {
231 switch (GetCXXStdlibType(Args)) {
233 llvm::report_fatal_error("linking libstdc++ is unimplemented on z/OS");
234 break;
236 std::string ClangHLQ = getClangHLQ(Args);
237 CmdArgs.push_back(
238 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXE)'"));
239 CmdArgs.push_back(
240 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXS)'"));
241 CmdArgs.push_back(
242 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXP)'"));
243 CmdArgs.push_back(
244 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXA)'"));
245 CmdArgs.push_back(
246 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQXLA)'"));
247 CmdArgs.push_back(
248 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQUNW)'"));
249 } break;
250 }
251}
252
253auto ZOS::buildAssembler() const -> Tool * { return new zos::Assembler(*this); }
254
255auto ZOS::buildLinker() const -> Tool * { return new zos::Linker(*this); }
256
257void ZOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
258 ArgStringList &CC1Args) const {
259 if (DriverArgs.hasArg(options::OPT_nostdinc))
260 return;
261
262 const Driver &D = getDriver();
263
264 // resolve ResourceDir
265 std::string ResourceDir(D.ResourceDir);
266
267 // zos_wrappers must take highest precedence
268
269 // - <clang>/lib/clang/<ver>/include/zos_wrappers
270 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
271 SmallString<128> P(ResourceDir);
272 path::append(P, "include", "zos_wrappers");
273 addSystemInclude(DriverArgs, CC1Args, P.str());
274
275 // - <clang>/lib/clang/<ver>/include
276 SmallString<128> P2(ResourceDir);
277 path::append(P2, "include");
278 addSystemInclude(DriverArgs, CC1Args, P2.str());
279 }
280
281 // - /usr/include
282 if (Arg *SysIncludeArg =
283 DriverArgs.getLastArg(options::OPT_mzos_sys_include_EQ)) {
284 StringRef SysInclude = SysIncludeArg->getValue();
285
286 // fall back to the default include path
287 if (!SysInclude.empty()) {
288
289 // -mzos-sys-include opton can have colon separated
290 // list of paths, so we need to parse the value.
291 StringRef PathLE(SysInclude);
292 size_t Colon = PathLE.find(':');
293 if (Colon == StringRef::npos) {
294 addSystemInclude(DriverArgs, CC1Args, PathLE.str());
295 return;
296 }
297
298 while (Colon != StringRef::npos) {
299 SmallString<128> P = PathLE.substr(0, Colon);
300 addSystemInclude(DriverArgs, CC1Args, P.str());
301 PathLE = PathLE.substr(Colon + 1);
302 Colon = PathLE.find(':');
303 }
304 if (PathLE.size())
305 addSystemInclude(DriverArgs, CC1Args, PathLE.str());
306
307 return;
308 }
309 }
310
311 addSystemInclude(DriverArgs, CC1Args, "/usr/include");
312}
313
315 const llvm::opt::ArgList &DriverArgs,
316 llvm::opt::ArgStringList &CC1Args) const {
317 if (!getVFS().exists(Path)) {
318 if (DriverArgs.hasArg(options::OPT_v))
319 WithColor::warning(errs(), "Clang")
320 << "ignoring nonexistent directory \"" << Path << "\"\n";
321 if (!DriverArgs.hasArg(options::OPT__HASH_HASH_HASH))
322 return;
323 }
324 addSystemInclude(DriverArgs, CC1Args, Path);
325}
326
328 const llvm::opt::ArgList &DriverArgs,
329 llvm::opt::ArgStringList &CC1Args) const {
330 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
331 DriverArgs.hasArg(options::OPT_nostdincxx) ||
332 DriverArgs.hasArg(options::OPT_nostdlibinc))
333 return;
334
335 switch (GetCXXStdlibType(DriverArgs)) {
337 // <install>/bin/../include/c++/v1
338 llvm::SmallString<128> InstallBin(getDriver().Dir);
339 llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
340 TryAddIncludeFromPath(InstallBin, DriverArgs, CC1Args);
341 break;
342 }
344 llvm::report_fatal_error(
345 "picking up libstdc++ headers is unimplemented on z/OS");
346 break;
347 }
348}
StringRef P
const Decl * D
IndirectLocalPath & Path
static std::string getClangHLQ(const ArgList &Args)
Definition: ZOS.cpp:92
static std::string getLEHLQ(const ArgList &Args)
Definition: ZOS.cpp:82
static std::string getCSSHLQ(const ArgList &Args)
Definition: ZOS.cpp:102
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
const char * getFilename() const
Definition: InputInfo.h:83
bool isNothing() const
Definition: InputInfo.h:74
bool isFilename() const
Definition: InputInfo.h:75
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
static void addSystemInclude(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, const Twine &Path)
Utility function to add a system include directory to CC1 arguments.
Definition: ToolChain.cpp:1265
bool ShouldLinkCXXStdlib(const llvm::opt::ArgList &Args) const
Returns if the C++ standard library should be linked in.
Definition: ToolChain.cpp:1367
const Driver & getDriver() const
Definition: ToolChain.h:252
llvm::vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:153
virtual void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ToolChain.cpp:1373
std::string GetLinkerPath(bool *LinkerIsLLD=nullptr) const
Returns the linker path, respecting the -fuse-ld= argument to determine the linker suffix or name.
Definition: ToolChain.cpp:957
void AddFilePathLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
AddFilePathLibArgs - Add each thing in getFilePaths() as a "-L" option.
Definition: ToolChain.cpp:1392
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:1239
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadingKind) const override
Add options that need to be passed to cc1 for this target.
Definition: ZOS.cpp:31
Tool * buildLinker() const override
Definition: ZOS.cpp:255
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: ZOS.cpp:225
void AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
AddClangCXXStdlibIncludeArgs - Add the clang -cc1 level arguments to set the include paths to use for...
Definition: ZOS.cpp:327
ZOS(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: ZOS.cpp:26
RuntimeLibType GetDefaultRuntimeLibType() const override
GetDefaultRuntimeLibType - Get the default runtime library variant to use.
Definition: ZOS.cpp:221
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: ZOS.cpp:229
void AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add the clang cc1 arguments for system include paths.
Definition: ZOS.cpp:257
void TryAddIncludeFromPath(llvm::SmallString< 128 > Path, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: ZOS.cpp:314
Tool * buildAssembler() const override
Definition: ZOS.cpp:253
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: ZOS.cpp:51
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition: ZOS.cpp:112
void AddRunTimeLibs(const ToolChain &TC, const Driver &D, llvm::opt::ArgStringList &CmdArgs, const llvm::opt::ArgList &Args)
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
static constexpr ResponseFileSupport None()
Returns a ResponseFileSupport indicating that response files are not supported.
Definition: Job.h:78