1 //===- Compilation.cpp - Compilation Task Implementation ------------------===//
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 "clang/Driver/Compilation.h"
10 #include "clang/Basic/LLVM.h"
11 #include "clang/Driver/Action.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/DriverDiagnostic.h"
14 #include "clang/Driver/Job.h"
15 #include "clang/Driver/Options.h"
16 #include "clang/Driver/ToolChain.h"
17 #include "clang/Driver/Util.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Option/OptSpecifier.h"
24 #include "llvm/Option/Option.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <string>
29 #include <system_error>
30 #include <utility>
31
32 using namespace clang;
33 using namespace driver;
34 using namespace llvm::opt;
35
Compilation(const Driver & D,const ToolChain & _DefaultToolChain,InputArgList * _Args,DerivedArgList * _TranslatedArgs,bool ContainsError)36 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
37 InputArgList *_Args, DerivedArgList *_TranslatedArgs,
38 bool ContainsError)
39 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
40 TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
41 // The offloading host toolchain is the default toolchain.
42 OrderedOffloadingToolchains.insert(
43 std::make_pair(Action::OFK_Host, &DefaultToolChain));
44 }
45
~Compilation()46 Compilation::~Compilation() {
47 // Remove temporary files. This must be done before arguments are freed, as
48 // the file names might be derived from the input arguments.
49 if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
50 CleanupFileList(TempFiles);
51
52 delete TranslatedArgs;
53 delete Args;
54
55 // Free any derived arg lists.
56 for (auto Arg : TCArgs)
57 if (Arg.second != TranslatedArgs)
58 delete Arg.second;
59 }
60
61 const DerivedArgList &
getArgsForToolChain(const ToolChain * TC,StringRef BoundArch,Action::OffloadKind DeviceOffloadKind)62 Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
63 Action::OffloadKind DeviceOffloadKind) {
64 if (!TC)
65 TC = &DefaultToolChain;
66
67 DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];
68 if (!Entry) {
69 SmallVector<Arg *, 4> AllocatedArgs;
70 DerivedArgList *OpenMPArgs = nullptr;
71 // Translate OpenMP toolchain arguments provided via the -Xopenmp-target flags.
72 if (DeviceOffloadKind == Action::OFK_OpenMP) {
73 const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>();
74 bool SameTripleAsHost = (TC->getTriple() == HostTC->getTriple());
75 OpenMPArgs = TC->TranslateOpenMPTargetArgs(
76 *TranslatedArgs, SameTripleAsHost, AllocatedArgs);
77 }
78
79 DerivedArgList *NewDAL = nullptr;
80 if (!OpenMPArgs) {
81 NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
82 DeviceOffloadKind, &AllocatedArgs);
83 } else {
84 NewDAL = TC->TranslateXarchArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind,
85 &AllocatedArgs);
86 if (!NewDAL)
87 NewDAL = OpenMPArgs;
88 else
89 delete OpenMPArgs;
90 }
91
92 if (!NewDAL) {
93 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
94 if (!Entry)
95 Entry = TranslatedArgs;
96 } else {
97 Entry = TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind);
98 if (!Entry)
99 Entry = NewDAL;
100 else
101 delete NewDAL;
102 }
103
104 // Add allocated arguments to the final DAL.
105 for (auto ArgPtr : AllocatedArgs)
106 Entry->AddSynthesizedArg(ArgPtr);
107 }
108
109 return *Entry;
110 }
111
CleanupFile(const char * File,bool IssueErrors) const112 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
113 // FIXME: Why are we trying to remove files that we have not created? For
114 // example we should only try to remove a temporary assembly file if
115 // "clang -cc1" succeed in writing it. Was this a workaround for when
116 // clang was writing directly to a .s file and sometimes leaving it behind
117 // during a failure?
118
119 // FIXME: If this is necessary, we can still try to split
120 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
121 // duplicated stat from is_regular_file.
122
123 // Don't try to remove files which we don't have write access to (but may be
124 // able to remove), or non-regular files. Underlying tools may have
125 // intentionally not overwritten them.
126 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
127 return true;
128
129 if (std::error_code EC = llvm::sys::fs::remove(File)) {
130 // Failure is only failure if the file exists and is "regular". We checked
131 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
132 // so we don't need to check again.
133
134 if (IssueErrors)
135 getDriver().Diag(diag::err_drv_unable_to_remove_file)
136 << EC.message();
137 return false;
138 }
139 return true;
140 }
141
CleanupFileList(const llvm::opt::ArgStringList & Files,bool IssueErrors) const142 bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files,
143 bool IssueErrors) const {
144 bool Success = true;
145 for (const auto &File: Files)
146 Success &= CleanupFile(File, IssueErrors);
147 return Success;
148 }
149
CleanupFileMap(const ArgStringMap & Files,const JobAction * JA,bool IssueErrors) const150 bool Compilation::CleanupFileMap(const ArgStringMap &Files,
151 const JobAction *JA,
152 bool IssueErrors) const {
153 bool Success = true;
154 for (const auto &File : Files) {
155 // If specified, only delete the files associated with the JobAction.
156 // Otherwise, delete all files in the map.
157 if (JA && File.first != JA)
158 continue;
159 Success &= CleanupFile(File.second, IssueErrors);
160 }
161 return Success;
162 }
163
ExecuteCommand(const Command & C,const Command * & FailingCommand) const164 int Compilation::ExecuteCommand(const Command &C,
165 const Command *&FailingCommand) const {
166 if ((getDriver().CCPrintOptions ||
167 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
168 raw_ostream *OS = &llvm::errs();
169 std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
170
171 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
172 // output stream.
173 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
174 std::error_code EC;
175 OwnedStream.reset(new llvm::raw_fd_ostream(
176 getDriver().CCPrintOptionsFilename, EC,
177 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text));
178 if (EC) {
179 getDriver().Diag(diag::err_drv_cc_print_options_failure)
180 << EC.message();
181 FailingCommand = &C;
182 return 1;
183 }
184 OS = OwnedStream.get();
185 }
186
187 if (getDriver().CCPrintOptions)
188 *OS << "[Logging clang options]\n";
189
190 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
191 }
192
193 std::string Error;
194 bool ExecutionFailed;
195 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
196 if (!Error.empty()) {
197 assert(Res && "Error string set with 0 result code!");
198 getDriver().Diag(diag::err_drv_command_failure) << Error;
199 }
200
201 if (Res)
202 FailingCommand = &C;
203
204 return ExecutionFailed ? 1 : Res;
205 }
206
207 using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>;
208
ActionFailed(const Action * A,const FailingCommandList & FailingCommands)209 static bool ActionFailed(const Action *A,
210 const FailingCommandList &FailingCommands) {
211 if (FailingCommands.empty())
212 return false;
213
214 // CUDA/HIP can have the same input source code compiled multiple times so do
215 // not compiled again if there are already failures. It is OK to abort the
216 // CUDA pipeline on errors.
217 if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP))
218 return true;
219
220 for (const auto &CI : FailingCommands)
221 if (A == &(CI.second->getSource()))
222 return true;
223
224 for (const auto *AI : A->inputs())
225 if (ActionFailed(AI, FailingCommands))
226 return true;
227
228 return false;
229 }
230
InputsOk(const Command & C,const FailingCommandList & FailingCommands)231 static bool InputsOk(const Command &C,
232 const FailingCommandList &FailingCommands) {
233 return !ActionFailed(&C.getSource(), FailingCommands);
234 }
235
ExecuteJobs(const JobList & Jobs,FailingCommandList & FailingCommands) const236 void Compilation::ExecuteJobs(const JobList &Jobs,
237 FailingCommandList &FailingCommands) const {
238 // According to UNIX standard, driver need to continue compiling all the
239 // inputs on the command line even one of them failed.
240 // In all but CLMode, execute all the jobs unless the necessary inputs for the
241 // job is missing due to previous failures.
242 for (const auto &Job : Jobs) {
243 if (!InputsOk(Job, FailingCommands))
244 continue;
245 const Command *FailingCommand = nullptr;
246 if (int Res = ExecuteCommand(Job, FailingCommand)) {
247 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
248 // Bail as soon as one command fails in cl driver mode.
249 if (TheDriver.IsCLMode())
250 return;
251 }
252 }
253 }
254
initCompilationForDiagnostics()255 void Compilation::initCompilationForDiagnostics() {
256 ForDiagnostics = true;
257
258 // Free actions and jobs.
259 Actions.clear();
260 AllActions.clear();
261 Jobs.clear();
262
263 // Remove temporary files.
264 if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
265 CleanupFileList(TempFiles);
266
267 // Clear temporary/results file lists.
268 TempFiles.clear();
269 ResultFiles.clear();
270 FailureResultFiles.clear();
271
272 // Remove any user specified output. Claim any unclaimed arguments, so as
273 // to avoid emitting warnings about unused args.
274 OptSpecifier OutputOpts[] = {
275 options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M,
276 options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ,
277 options::OPT_MQ, options::OPT_MT, options::OPT_MV};
278 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
279 if (TranslatedArgs->hasArg(OutputOpts[i]))
280 TranslatedArgs->eraseArg(OutputOpts[i]);
281 }
282 TranslatedArgs->ClaimAllArgs();
283
284 // Force re-creation of the toolchain Args, otherwise our modifications just
285 // above will have no effect.
286 for (auto Arg : TCArgs)
287 if (Arg.second != TranslatedArgs)
288 delete Arg.second;
289 TCArgs.clear();
290
291 // Redirect stdout/stderr to /dev/null.
292 Redirects = {None, {""}, {""}};
293
294 // Temporary files added by diagnostics should be kept.
295 ForceKeepTempFiles = true;
296 }
297
getSysRoot() const298 StringRef Compilation::getSysRoot() const {
299 return getDriver().SysRoot;
300 }
301
Redirect(ArrayRef<Optional<StringRef>> Redirects)302 void Compilation::Redirect(ArrayRef<Optional<StringRef>> Redirects) {
303 this->Redirects = Redirects;
304 }
305