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