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