Coverage Report

Created: 2024-01-17 10:31

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