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,
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 if (!IsSharedLib) {
144 CmdArgs.push_back("-e");
145 CmdArgs.push_back("CELQSTRT");
146
147 CmdArgs.push_back("-O");
148 CmdArgs.push_back("CELQSTRT");
149
150 CmdArgs.push_back("-u");
151 CmdArgs.push_back("CELQMAIN");
152 }
153
154 // Generate side file if -shared option is present.
155 if (IsSharedLib) {
156 StringRef OutputName = Output.getFilename();
157 // Strip away the last file suffix in presence from output name and add
158 // a new .x suffix.
159 SmallString<128> SideDeckName = OutputName;
160 llvm::sys::path::replace_extension(SideDeckName, "x");
161 CmdArgs.push_back("-x");
162 CmdArgs.push_back(Args.MakeArgString(SideDeckName));
163 } else {
164 // We need to direct side file to /dev/null to suppress linker warning when
165 // the object file contains exported symbols, and -shared or
166 // -Wl,-x<sidedeck>.x is not specified.
167 CmdArgs.push_back("-x");
168 CmdArgs.push_back("/dev/null");
169 }
170
171 // Add archive library search paths.
172 Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_u});
173
174 ToolChain.AddFilePathLibArgs(Args, CmdArgs);
175
176 // Specify linker input file(s)
177 AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
178
179 // z/OS tool chain depends on LE data sets and the CSSLIB data set.
180 // These data sets can have different high level qualifiers (HLQs)
181 // as each installation can define them differently.
182
183 std::string LEHLQ = getLEHLQ(Args);
184 std::string CsslibHLQ = getCSSHLQ(Args);
185
186 StringRef ld_env_var = StringRef(getenv("_LD_SYSLIB")).trim();
187 if (ld_env_var.empty()) {
188 CmdArgs.push_back("-S");
189 CmdArgs.push_back(Args.MakeArgString("//'" + LEHLQ + ".SCEEBND2'"));
190 CmdArgs.push_back("-S");
191 CmdArgs.push_back(Args.MakeArgString("//'" + CsslibHLQ + ".CSSLIB'"));
192 }
193
194 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
195 ld_env_var = StringRef(getenv("_LD_SIDE_DECKS")).trim();
196 if (ld_env_var.empty()) {
197 CmdArgs.push_back(
198 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS001)'"));
199 CmdArgs.push_back(
200 Args.MakeArgString("//'" + LEHLQ + ".SCEELIB(CELQS003)'"));
201 } else {
202 SmallVector<StringRef> ld_side_deck;
203 ld_env_var.split(ld_side_deck, ":");
204 for (StringRef ld_loc : ld_side_deck) {
205 CmdArgs.push_back((ld_loc.str()).c_str());
206 }
207 }
208 }
209 // Link libc++ library
210 if (ToolChain.ShouldLinkCXXStdlib(Args)) {
211 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
212 }
213
214 // Specify compiler-rt library path for linker
215 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
216 AddRunTimeLibs(ToolChain, ToolChain.getDriver(), CmdArgs, Args);
217
218 const char *Exec = Args.MakeArgString(ToolChain.GetLinkerPath());
219 C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
220 Exec, CmdArgs, Inputs, Output));
221}
222
226
230
231void ZOS::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
232 llvm::opt::ArgStringList &CmdArgs) const {
233 switch (GetCXXStdlibType(Args)) {
235 llvm::report_fatal_error("linking libstdc++ is unimplemented on z/OS");
236 break;
238 std::string ClangHLQ = getClangHLQ(Args);
239 CmdArgs.push_back(
240 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXE)'"));
241 CmdArgs.push_back(
242 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXS)'"));
243 CmdArgs.push_back(
244 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXP)'"));
245 CmdArgs.push_back(
246 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQCXA)'"));
247 CmdArgs.push_back(
248 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQXLA)'"));
249 CmdArgs.push_back(
250 Args.MakeArgString("//'" + ClangHLQ + ".SCEELIB(CRTDQUNW)'"));
251 } break;
252 }
253}
254
255auto ZOS::buildAssembler() const -> Tool * { return new zos::Assembler(*this); }
256
257auto ZOS::buildLinker() const -> Tool * { return new zos::Linker(*this); }
258
259void ZOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
260 ArgStringList &CC1Args) const {
261 if (DriverArgs.hasArg(options::OPT_nostdinc))
262 return;
263
264 const Driver &D = getDriver();
265
266 // resolve ResourceDir
267 std::string ResourceDir(D.ResourceDir);
268
269 // zos_wrappers must take highest precedence
270
271 // - <clang>/lib/clang/<ver>/include/zos_wrappers
272 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
273 SmallString<128> P(ResourceDir);
274 path::append(P, "include", "zos_wrappers");
275 addSystemInclude(DriverArgs, CC1Args, P.str());
276
277 // - <clang>/lib/clang/<ver>/include
278 SmallString<128> P2(ResourceDir);
279 path::append(P2, "include");
280 addSystemInclude(DriverArgs, CC1Args, P2.str());
281 }
282
283 // - /usr/include
284 if (Arg *SysIncludeArg =
285 DriverArgs.getLastArg(options::OPT_mzos_sys_include_EQ)) {
286 StringRef SysInclude = SysIncludeArg->getValue();
287
288 // fall back to the default include path
289 if (!SysInclude.empty()) {
290
291 // -mzos-sys-include opton can have colon separated
292 // list of paths, so we need to parse the value.
293 StringRef PathLE(SysInclude);
294 size_t Colon = PathLE.find(':');
295 if (Colon == StringRef::npos) {
296 addSystemInclude(DriverArgs, CC1Args, PathLE.str());
297 return;
298 }
299
300 while (Colon != StringRef::npos) {
301 SmallString<128> P = PathLE.substr(0, Colon);
302 addSystemInclude(DriverArgs, CC1Args, P.str());
303 PathLE = PathLE.substr(Colon + 1);
304 Colon = PathLE.find(':');
305 }
306 if (PathLE.size())
307 addSystemInclude(DriverArgs, CC1Args, PathLE.str());
308
309 return;
310 }
311 }
312
313 addSystemInclude(DriverArgs, CC1Args, "/usr/include");
314}
315
317 const llvm::opt::ArgList &DriverArgs,
318 llvm::opt::ArgStringList &CC1Args) const {
319 if (!getVFS().exists(Path)) {
320 if (DriverArgs.hasArg(options::OPT_v))
321 WithColor::warning(errs(), "Clang")
322 << "ignoring nonexistent directory \"" << Path << "\"\n";
323 if (!DriverArgs.hasArg(options::OPT__HASH_HASH_HASH))
324 return;
325 }
326 addSystemInclude(DriverArgs, CC1Args, Path);
327}
328
330 const llvm::opt::ArgList &DriverArgs,
331 llvm::opt::ArgStringList &CC1Args) const {
332 if (DriverArgs.hasArg(options::OPT_nostdinc) ||
333 DriverArgs.hasArg(options::OPT_nostdincxx) ||
334 DriverArgs.hasArg(options::OPT_nostdlibinc))
335 return;
336
337 switch (GetCXXStdlibType(DriverArgs)) {
339 // <install>/bin/../include/c++/v1
340 llvm::SmallString<128> InstallBin(getDriver().Dir);
341 llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
342 TryAddIncludeFromPath(InstallBin, DriverArgs, CC1Args);
343 break;
344 }
346 llvm::report_fatal_error(
347 "picking up libstdc++ headers is unimplemented on z/OS");
348 break;
349 }
350}
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:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:99
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.
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:253
llvm::vfs::FileSystem & getVFS() const
ToolChain(const Driver &D, const llvm::Triple &T, const llvm::opt::ArgList &Args)
Definition ToolChain.cpp:89
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.
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
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:30
Tool * buildLinker() const override
Definition ZOS.cpp:257
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition ZOS.cpp:227
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:329
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:223
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:231
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:259
void TryAddIncludeFromPath(llvm::SmallString< 128 > Path, const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition ZOS.cpp:316
Tool * buildAssembler() const override
Definition ZOS.cpp:255
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:50
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