1 //===-- ToolRunner.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the interfaces described in the ToolRunner.h file.
11 //===----------------------------------------------------------------------===//
13 #include "ToolRunner.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/FileUtilities.h"
19 #include "llvm/Support/Program.h"
20 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "toolrunner"
29 cl::opt
<bool> SaveTemps("save-temps", cl::init(false),
30 cl::desc("Save temporary files"));
35 RemoteClient("remote-client",
36 cl::desc("Remote execution client (rsh/ssh)"));
38 cl::opt
<std::string
> RemoteHost("remote-host",
39 cl::desc("Remote execution (rsh/ssh) host"));
41 cl::opt
<std::string
> RemotePort("remote-port",
42 cl::desc("Remote execution (rsh/ssh) port"));
44 cl::opt
<std::string
> RemoteUser("remote-user",
45 cl::desc("Remote execution (rsh/ssh) user id"));
48 RemoteExtra("remote-extra-options",
49 cl::desc("Remote execution (rsh/ssh) extra options"));
52 /// RunProgramWithTimeout - This function provides an alternate interface
53 /// to the sys::Program::ExecuteAndWait interface.
54 /// @see sys::Program::ExecuteAndWait
55 static int RunProgramWithTimeout(StringRef ProgramPath
,
56 ArrayRef
<StringRef
> Args
, StringRef StdInFile
,
57 StringRef StdOutFile
, StringRef StdErrFile
,
58 unsigned NumSeconds
= 0,
59 unsigned MemoryLimit
= 0,
60 std::string
*ErrMsg
= nullptr) {
61 Optional
<StringRef
> Redirects
[3] = {StdInFile
, StdOutFile
, StdErrFile
};
62 return sys::ExecuteAndWait(ProgramPath
, Args
, None
, Redirects
, NumSeconds
,
66 /// RunProgramRemotelyWithTimeout - This function runs the given program
67 /// remotely using the given remote client and the sys::Program::ExecuteAndWait.
68 /// Returns the remote program exit code or reports a remote client error if it
69 /// fails. Remote client is required to return 255 if it failed or program exit
71 /// @see sys::Program::ExecuteAndWait
72 static int RunProgramRemotelyWithTimeout(
73 StringRef RemoteClientPath
, ArrayRef
<StringRef
> Args
, StringRef StdInFile
,
74 StringRef StdOutFile
, StringRef StdErrFile
, unsigned NumSeconds
= 0,
75 unsigned MemoryLimit
= 0) {
76 Optional
<StringRef
> Redirects
[3] = {StdInFile
, StdOutFile
, StdErrFile
};
78 // Run the program remotely with the remote client
79 int ReturnCode
= sys::ExecuteAndWait(RemoteClientPath
, Args
, None
, Redirects
,
80 NumSeconds
, MemoryLimit
);
82 // Has the remote client fail?
83 if (255 == ReturnCode
) {
84 std::ostringstream OS
;
85 OS
<< "\nError running remote client:\n ";
86 for (StringRef Arg
: Args
)
87 OS
<< " " << Arg
.str();
90 // The error message is in the output file, let's print it out from there.
91 std::string StdOutFileName
= StdOutFile
.str();
92 std::ifstream
ErrorFile(StdOutFileName
.c_str());
94 std::copy(std::istreambuf_iterator
<char>(ErrorFile
),
95 std::istreambuf_iterator
<char>(),
96 std::ostreambuf_iterator
<char>(OS
));
106 static Error
ProcessFailure(StringRef ProgPath
, ArrayRef
<StringRef
> Args
,
107 unsigned Timeout
= 0, unsigned MemoryLimit
= 0) {
108 std::ostringstream OS
;
109 OS
<< "\nError running tool:\n ";
110 for (StringRef Arg
: Args
)
111 OS
<< " " << Arg
.str();
114 // Rerun the compiler, capturing any error messages to print them.
115 SmallString
<128> ErrorFilename
;
116 std::error_code EC
= sys::fs::createTemporaryFile(
117 "bugpoint.program_error_messages", "", ErrorFilename
);
119 errs() << "Error making unique filename: " << EC
.message() << "\n";
123 RunProgramWithTimeout(ProgPath
, Args
, "", ErrorFilename
.str(),
124 ErrorFilename
.str(), Timeout
, MemoryLimit
);
125 // FIXME: check return code ?
127 // Print out the error messages generated by CC if possible...
128 std::ifstream
ErrorFile(ErrorFilename
.c_str());
130 std::copy(std::istreambuf_iterator
<char>(ErrorFile
),
131 std::istreambuf_iterator
<char>(),
132 std::ostreambuf_iterator
<char>(OS
));
136 sys::fs::remove(ErrorFilename
.c_str());
137 return make_error
<StringError
>(OS
.str(), inconvertibleErrorCode());
140 //===---------------------------------------------------------------------===//
141 // LLI Implementation of AbstractIntepreter interface
144 class LLI
: public AbstractInterpreter
{
145 std::string LLIPath
; // The path to the LLI executable
146 std::vector
<std::string
> ToolArgs
; // Args to pass to LLI
148 LLI(const std::string
&Path
, const std::vector
<std::string
> *Args
)
156 Expected
<int> ExecuteProgram(
157 const std::string
&Bitcode
, const std::vector
<std::string
> &Args
,
158 const std::string
&InputFile
, const std::string
&OutputFile
,
159 const std::vector
<std::string
> &CCArgs
,
160 const std::vector
<std::string
> &SharedLibs
= std::vector
<std::string
>(),
161 unsigned Timeout
= 0, unsigned MemoryLimit
= 0) override
;
165 Expected
<int> LLI::ExecuteProgram(const std::string
&Bitcode
,
166 const std::vector
<std::string
> &Args
,
167 const std::string
&InputFile
,
168 const std::string
&OutputFile
,
169 const std::vector
<std::string
> &CCArgs
,
170 const std::vector
<std::string
> &SharedLibs
,
171 unsigned Timeout
, unsigned MemoryLimit
) {
172 std::vector
<StringRef
> LLIArgs
;
173 LLIArgs
.push_back(LLIPath
);
174 LLIArgs
.push_back("-force-interpreter=true");
176 for (std::vector
<std::string
>::const_iterator i
= SharedLibs
.begin(),
177 e
= SharedLibs
.end();
179 LLIArgs
.push_back("-load");
180 LLIArgs
.push_back(*i
);
183 // Add any extra LLI args.
184 for (unsigned i
= 0, e
= ToolArgs
.size(); i
!= e
; ++i
)
185 LLIArgs
.push_back(ToolArgs
[i
]);
187 LLIArgs
.push_back(Bitcode
);
188 // Add optional parameters to the running program from Argv
189 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
190 LLIArgs
.push_back(Args
[i
]);
194 LLVM_DEBUG(errs() << "\nAbout to run:\t";
195 for (unsigned i
= 0, e
= LLIArgs
.size() - 1; i
!= e
; ++i
) errs()
196 << " " << LLIArgs
[i
];
198 return RunProgramWithTimeout(LLIPath
, LLIArgs
, InputFile
, OutputFile
,
199 OutputFile
, Timeout
, MemoryLimit
);
202 void AbstractInterpreter::anchor() {}
204 ErrorOr
<std::string
> llvm::FindProgramByName(const std::string
&ExeName
,
207 // Check the directory that the calling program is in. We can do
208 // this if ProgramPath contains at least one / character, indicating that it
209 // is a relative path to the executable itself.
210 std::string Main
= sys::fs::getMainExecutable(Argv0
, MainAddr
);
211 StringRef Result
= sys::path::parent_path(Main
);
212 if (ErrorOr
<std::string
> Path
= sys::findProgramByName(ExeName
, Result
))
215 // Check the user PATH.
216 return sys::findProgramByName(ExeName
);
219 // LLI create method - Try to find the LLI executable
220 AbstractInterpreter
*
221 AbstractInterpreter::createLLI(const char *Argv0
, std::string
&Message
,
222 const std::vector
<std::string
> *ToolArgs
) {
223 if (ErrorOr
<std::string
> LLIPath
=
224 FindProgramByName("lli", Argv0
, (void *)(intptr_t)&createLLI
)) {
225 Message
= "Found lli: " + *LLIPath
+ "\n";
226 return new LLI(*LLIPath
, ToolArgs
);
228 Message
= LLIPath
.getError().message() + "\n";
233 //===---------------------------------------------------------------------===//
234 // Custom compiler command implementation of AbstractIntepreter interface
236 // Allows using a custom command for compiling the bitcode, thus allows, for
237 // example, to compile a bitcode fragment without linking or executing, then
238 // using a custom wrapper script to check for compiler errors.
240 class CustomCompiler
: public AbstractInterpreter
{
241 std::string CompilerCommand
;
242 std::vector
<std::string
> CompilerArgs
;
245 CustomCompiler(const std::string
&CompilerCmd
,
246 std::vector
<std::string
> CompArgs
)
247 : CompilerCommand(CompilerCmd
), CompilerArgs(std::move(CompArgs
)) {}
249 Error
compileProgram(const std::string
&Bitcode
, unsigned Timeout
= 0,
250 unsigned MemoryLimit
= 0) override
;
252 Expected
<int> ExecuteProgram(
253 const std::string
&Bitcode
, const std::vector
<std::string
> &Args
,
254 const std::string
&InputFile
, const std::string
&OutputFile
,
255 const std::vector
<std::string
> &CCArgs
= std::vector
<std::string
>(),
256 const std::vector
<std::string
> &SharedLibs
= std::vector
<std::string
>(),
257 unsigned Timeout
= 0, unsigned MemoryLimit
= 0) override
{
258 return make_error
<StringError
>(
259 "Execution not supported with -compile-custom",
260 inconvertibleErrorCode());
265 Error
CustomCompiler::compileProgram(const std::string
&Bitcode
,
266 unsigned Timeout
, unsigned MemoryLimit
) {
268 std::vector
<StringRef
> ProgramArgs
;
269 ProgramArgs
.push_back(CompilerCommand
);
271 for (const auto &Arg
: CompilerArgs
)
272 ProgramArgs
.push_back(Arg
);
273 ProgramArgs
.push_back(Bitcode
);
275 // Add optional parameters to the running program from Argv
276 for (const auto &Arg
: CompilerArgs
)
277 ProgramArgs
.push_back(Arg
);
279 if (RunProgramWithTimeout(CompilerCommand
, ProgramArgs
, "", "", "", Timeout
,
281 return ProcessFailure(CompilerCommand
, ProgramArgs
, Timeout
, MemoryLimit
);
282 return Error::success();
285 //===---------------------------------------------------------------------===//
286 // Custom execution command implementation of AbstractIntepreter interface
288 // Allows using a custom command for executing the bitcode, thus allows,
289 // for example, to invoke a cross compiler for code generation followed by
290 // a simulator that executes the generated binary.
292 class CustomExecutor
: public AbstractInterpreter
{
293 std::string ExecutionCommand
;
294 std::vector
<std::string
> ExecutorArgs
;
297 CustomExecutor(const std::string
&ExecutionCmd
,
298 std::vector
<std::string
> ExecArgs
)
299 : ExecutionCommand(ExecutionCmd
), ExecutorArgs(std::move(ExecArgs
)) {}
301 Expected
<int> ExecuteProgram(
302 const std::string
&Bitcode
, const std::vector
<std::string
> &Args
,
303 const std::string
&InputFile
, const std::string
&OutputFile
,
304 const std::vector
<std::string
> &CCArgs
,
305 const std::vector
<std::string
> &SharedLibs
= std::vector
<std::string
>(),
306 unsigned Timeout
= 0, unsigned MemoryLimit
= 0) override
;
310 Expected
<int> CustomExecutor::ExecuteProgram(
311 const std::string
&Bitcode
, const std::vector
<std::string
> &Args
,
312 const std::string
&InputFile
, const std::string
&OutputFile
,
313 const std::vector
<std::string
> &CCArgs
,
314 const std::vector
<std::string
> &SharedLibs
, unsigned Timeout
,
315 unsigned MemoryLimit
) {
317 std::vector
<StringRef
> ProgramArgs
;
318 ProgramArgs
.push_back(ExecutionCommand
);
320 for (std::size_t i
= 0; i
< ExecutorArgs
.size(); ++i
)
321 ProgramArgs
.push_back(ExecutorArgs
[i
]);
322 ProgramArgs
.push_back(Bitcode
);
324 // Add optional parameters to the running program from Argv
325 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
326 ProgramArgs
.push_back(Args
[i
]);
328 return RunProgramWithTimeout(ExecutionCommand
, ProgramArgs
, InputFile
,
329 OutputFile
, OutputFile
, Timeout
, MemoryLimit
);
332 // Tokenize the CommandLine to the command and the args to allow
333 // defining a full command line as the command instead of just the
334 // executed program. We cannot just pass the whole string after the command
335 // as a single argument because then the program sees only a single
336 // command line argument (with spaces in it: "foo bar" instead
337 // of "foo" and "bar").
339 // Spaces are used as a delimiter; however repeated, leading, and trailing
340 // whitespace are ignored. Simple escaping is allowed via the '\'
341 // character, as seen below:
343 // Two consecutive '\' evaluate to a single '\'.
344 // A space after a '\' evaluates to a space that is not interpreted as a
346 // Any other instances of the '\' character are removed.
351 // 'exa\mple' -> 'example'
353 static void lexCommand(const char *Argv0
, std::string
&Message
,
354 const std::string
&CommandLine
, std::string
&CmdPath
,
355 std::vector
<std::string
> &Args
) {
359 bool FoundPath
= false;
361 // first argument is the PATH.
362 // Skip repeated whitespace, leading whitespace and trailing whitespace.
363 for (std::size_t Pos
= 0u; Pos
<= CommandLine
.size(); ++Pos
) {
364 if ('\\' == CommandLine
[Pos
]) {
365 if (Pos
+ 1 < CommandLine
.size())
366 Token
.push_back(CommandLine
[++Pos
]);
370 if (' ' == CommandLine
[Pos
] || CommandLine
.size() == Pos
) {
381 Args
.push_back(Token
);
385 Token
.push_back(CommandLine
[Pos
]);
388 auto Path
= FindProgramByName(Command
, Argv0
, (void *)(intptr_t)&lexCommand
);
390 Message
= std::string("Cannot find '") + Command
+
391 "' in PATH: " + Path
.getError().message() + "\n";
396 Message
= "Found command in: " + CmdPath
+ "\n";
399 // Custom execution environment create method, takes the execution command
401 AbstractInterpreter
*AbstractInterpreter::createCustomCompiler(
402 const char *Argv0
, std::string
&Message
,
403 const std::string
&CompileCommandLine
) {
406 std::vector
<std::string
> Args
;
407 lexCommand(Argv0
, Message
, CompileCommandLine
, CmdPath
, Args
);
411 return new CustomCompiler(CmdPath
, Args
);
414 // Custom execution environment create method, takes the execution command
416 AbstractInterpreter
*
417 AbstractInterpreter::createCustomExecutor(const char *Argv0
,
418 std::string
&Message
,
419 const std::string
&ExecCommandLine
) {
422 std::vector
<std::string
> Args
;
423 lexCommand(Argv0
, Message
, ExecCommandLine
, CmdPath
, Args
);
427 return new CustomExecutor(CmdPath
, Args
);
430 //===----------------------------------------------------------------------===//
431 // LLC Implementation of AbstractIntepreter interface
433 Expected
<CC::FileType
> LLC::OutputCode(const std::string
&Bitcode
,
434 std::string
&OutputAsmFile
,
435 unsigned Timeout
, unsigned MemoryLimit
) {
436 const char *Suffix
= (UseIntegratedAssembler
? ".llc.o" : ".llc.s");
438 SmallString
<128> UniqueFile
;
440 sys::fs::createUniqueFile(Bitcode
+ "-%%%%%%%" + Suffix
, UniqueFile
);
442 errs() << "Error making unique filename: " << EC
.message() << "\n";
445 OutputAsmFile
= UniqueFile
.str();
446 std::vector
<StringRef
> LLCArgs
;
447 LLCArgs
.push_back(LLCPath
);
449 // Add any extra LLC args.
450 for (unsigned i
= 0, e
= ToolArgs
.size(); i
!= e
; ++i
)
451 LLCArgs
.push_back(ToolArgs
[i
]);
453 LLCArgs
.push_back("-o");
454 LLCArgs
.push_back(OutputAsmFile
); // Output to the Asm file
455 LLCArgs
.push_back(Bitcode
); // This is the input bitcode
457 if (UseIntegratedAssembler
)
458 LLCArgs
.push_back("-filetype=obj");
460 outs() << (UseIntegratedAssembler
? "<llc-ia>" : "<llc>");
462 LLVM_DEBUG(errs() << "\nAbout to run:\t";
463 for (unsigned i
= 0, e
= LLCArgs
.size() - 1; i
!= e
; ++i
) errs()
464 << " " << LLCArgs
[i
];
466 if (RunProgramWithTimeout(LLCPath
, LLCArgs
, "", "", "", Timeout
, MemoryLimit
))
467 return ProcessFailure(LLCPath
, LLCArgs
, Timeout
, MemoryLimit
);
468 return UseIntegratedAssembler
? CC::ObjectFile
: CC::AsmFile
;
471 Error
LLC::compileProgram(const std::string
&Bitcode
, unsigned Timeout
,
472 unsigned MemoryLimit
) {
473 std::string OutputAsmFile
;
474 Expected
<CC::FileType
> Result
=
475 OutputCode(Bitcode
, OutputAsmFile
, Timeout
, MemoryLimit
);
476 sys::fs::remove(OutputAsmFile
);
477 if (Error E
= Result
.takeError())
479 return Error::success();
482 Expected
<int> LLC::ExecuteProgram(const std::string
&Bitcode
,
483 const std::vector
<std::string
> &Args
,
484 const std::string
&InputFile
,
485 const std::string
&OutputFile
,
486 const std::vector
<std::string
> &ArgsForCC
,
487 const std::vector
<std::string
> &SharedLibs
,
488 unsigned Timeout
, unsigned MemoryLimit
) {
490 std::string OutputAsmFile
;
491 Expected
<CC::FileType
> FileKind
=
492 OutputCode(Bitcode
, OutputAsmFile
, Timeout
, MemoryLimit
);
493 FileRemover
OutFileRemover(OutputAsmFile
, !SaveTemps
);
494 if (Error E
= FileKind
.takeError())
497 std::vector
<std::string
> CCArgs(ArgsForCC
);
498 CCArgs
.insert(CCArgs
.end(), SharedLibs
.begin(), SharedLibs
.end());
500 // Assuming LLC worked, compile the result with CC and run it.
501 return cc
->ExecuteProgram(OutputAsmFile
, Args
, *FileKind
, InputFile
,
502 OutputFile
, CCArgs
, Timeout
, MemoryLimit
);
505 /// createLLC - Try to find the LLC executable
507 LLC
*AbstractInterpreter::createLLC(const char *Argv0
, std::string
&Message
,
508 const std::string
&CCBinary
,
509 const std::vector
<std::string
> *Args
,
510 const std::vector
<std::string
> *CCArgs
,
511 bool UseIntegratedAssembler
) {
512 ErrorOr
<std::string
> LLCPath
=
513 FindProgramByName("llc", Argv0
, (void *)(intptr_t)&createLLC
);
515 Message
= LLCPath
.getError().message() + "\n";
519 CC
*cc
= CC::create(Argv0
, Message
, CCBinary
, CCArgs
);
521 errs() << Message
<< "\n";
524 Message
= "Found llc: " + *LLCPath
+ "\n";
525 return new LLC(*LLCPath
, cc
, Args
, UseIntegratedAssembler
);
528 //===---------------------------------------------------------------------===//
529 // JIT Implementation of AbstractIntepreter interface
532 class JIT
: public AbstractInterpreter
{
533 std::string LLIPath
; // The path to the LLI executable
534 std::vector
<std::string
> ToolArgs
; // Args to pass to LLI
536 JIT(const std::string
&Path
, const std::vector
<std::string
> *Args
)
544 Expected
<int> ExecuteProgram(
545 const std::string
&Bitcode
, const std::vector
<std::string
> &Args
,
546 const std::string
&InputFile
, const std::string
&OutputFile
,
547 const std::vector
<std::string
> &CCArgs
= std::vector
<std::string
>(),
548 const std::vector
<std::string
> &SharedLibs
= std::vector
<std::string
>(),
549 unsigned Timeout
= 0, unsigned MemoryLimit
= 0) override
;
553 Expected
<int> JIT::ExecuteProgram(const std::string
&Bitcode
,
554 const std::vector
<std::string
> &Args
,
555 const std::string
&InputFile
,
556 const std::string
&OutputFile
,
557 const std::vector
<std::string
> &CCArgs
,
558 const std::vector
<std::string
> &SharedLibs
,
559 unsigned Timeout
, unsigned MemoryLimit
) {
560 // Construct a vector of parameters, incorporating those from the command-line
561 std::vector
<StringRef
> JITArgs
;
562 JITArgs
.push_back(LLIPath
);
563 JITArgs
.push_back("-force-interpreter=false");
565 // Add any extra LLI args.
566 for (unsigned i
= 0, e
= ToolArgs
.size(); i
!= e
; ++i
)
567 JITArgs
.push_back(ToolArgs
[i
]);
569 for (unsigned i
= 0, e
= SharedLibs
.size(); i
!= e
; ++i
) {
570 JITArgs
.push_back("-load");
571 JITArgs
.push_back(SharedLibs
[i
]);
573 JITArgs
.push_back(Bitcode
);
574 // Add optional parameters to the running program from Argv
575 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
576 JITArgs
.push_back(Args
[i
]);
580 LLVM_DEBUG(errs() << "\nAbout to run:\t";
581 for (unsigned i
= 0, e
= JITArgs
.size() - 1; i
!= e
; ++i
) errs()
582 << " " << JITArgs
[i
];
584 LLVM_DEBUG(errs() << "\nSending output to " << OutputFile
<< "\n");
585 return RunProgramWithTimeout(LLIPath
, JITArgs
, InputFile
, OutputFile
,
586 OutputFile
, Timeout
, MemoryLimit
);
589 /// createJIT - Try to find the LLI executable
591 AbstractInterpreter
*
592 AbstractInterpreter::createJIT(const char *Argv0
, std::string
&Message
,
593 const std::vector
<std::string
> *Args
) {
594 if (ErrorOr
<std::string
> LLIPath
=
595 FindProgramByName("lli", Argv0
, (void *)(intptr_t)&createJIT
)) {
596 Message
= "Found lli: " + *LLIPath
+ "\n";
597 return new JIT(*LLIPath
, Args
);
599 Message
= LLIPath
.getError().message() + "\n";
604 //===---------------------------------------------------------------------===//
608 static bool IsARMArchitecture(std::vector
<StringRef
> Args
) {
609 for (size_t I
= 0; I
< Args
.size(); ++I
) {
610 if (!Args
[I
].equals_lower("-arch"))
613 if (I
== Args
.size())
615 if (Args
[I
].startswith_lower("arm"))
622 Expected
<int> CC::ExecuteProgram(const std::string
&ProgramFile
,
623 const std::vector
<std::string
> &Args
,
625 const std::string
&InputFile
,
626 const std::string
&OutputFile
,
627 const std::vector
<std::string
> &ArgsForCC
,
628 unsigned Timeout
, unsigned MemoryLimit
) {
629 std::vector
<StringRef
> CCArgs
;
631 CCArgs
.push_back(CCPath
);
633 if (TargetTriple
.getArch() == Triple::x86
)
634 CCArgs
.push_back("-m32");
636 for (std::vector
<std::string
>::const_iterator I
= ccArgs
.begin(),
639 CCArgs
.push_back(*I
);
641 // Specify -x explicitly in case the extension is wonky
642 if (fileType
!= ObjectFile
) {
643 CCArgs
.push_back("-x");
644 if (fileType
== CFile
) {
645 CCArgs
.push_back("c");
646 CCArgs
.push_back("-fno-strict-aliasing");
648 CCArgs
.push_back("assembler");
650 // For ARM architectures we don't want this flag. bugpoint isn't
651 // explicitly told what architecture it is working on, so we get
653 if (TargetTriple
.isOSDarwin() && !IsARMArchitecture(CCArgs
))
654 CCArgs
.push_back("-force_cpusubtype_ALL");
658 CCArgs
.push_back(ProgramFile
); // Specify the input filename.
660 CCArgs
.push_back("-x");
661 CCArgs
.push_back("none");
662 CCArgs
.push_back("-o");
664 SmallString
<128> OutputBinary
;
666 sys::fs::createUniqueFile(ProgramFile
+ "-%%%%%%%.cc.exe", OutputBinary
);
668 errs() << "Error making unique filename: " << EC
.message() << "\n";
671 CCArgs
.push_back(OutputBinary
); // Output to the right file...
673 // Add any arguments intended for CC. We locate them here because this is
674 // most likely -L and -l options that need to come before other libraries but
675 // after the source. Other options won't be sensitive to placement on the
676 // command line, so this should be safe.
677 for (unsigned i
= 0, e
= ArgsForCC
.size(); i
!= e
; ++i
)
678 CCArgs
.push_back(ArgsForCC
[i
]);
680 CCArgs
.push_back("-lm"); // Hard-code the math library...
681 CCArgs
.push_back("-O2"); // Optimize the program a bit...
682 if (TargetTriple
.getArch() == Triple::sparc
)
683 CCArgs
.push_back("-mcpu=v9");
687 LLVM_DEBUG(errs() << "\nAbout to run:\t";
688 for (unsigned i
= 0, e
= CCArgs
.size() - 1; i
!= e
; ++i
) errs()
691 if (RunProgramWithTimeout(CCPath
, CCArgs
, "", "", ""))
692 return ProcessFailure(CCPath
, CCArgs
);
694 std::vector
<StringRef
> ProgramArgs
;
696 // Declared here so that the destructor only runs after
697 // ProgramArgs is used.
700 if (RemoteClientPath
.empty())
701 ProgramArgs
.push_back(OutputBinary
);
703 ProgramArgs
.push_back(RemoteClientPath
);
704 ProgramArgs
.push_back(RemoteHost
);
705 if (!RemoteUser
.empty()) {
706 ProgramArgs
.push_back("-l");
707 ProgramArgs
.push_back(RemoteUser
);
709 if (!RemotePort
.empty()) {
710 ProgramArgs
.push_back("-p");
711 ProgramArgs
.push_back(RemotePort
);
713 if (!RemoteExtra
.empty()) {
714 ProgramArgs
.push_back(RemoteExtra
);
717 // Full path to the binary. We need to cd to the exec directory because
718 // there is a dylib there that the exec expects to find in the CWD
719 char *env_pwd
= getenv("PWD");
723 Exec
+= OutputBinary
.c_str();
724 ProgramArgs
.push_back(Exec
);
727 // Add optional parameters to the running program from Argv
728 for (unsigned i
= 0, e
= Args
.size(); i
!= e
; ++i
)
729 ProgramArgs
.push_back(Args
[i
]);
731 // Now that we have a binary, run it!
732 outs() << "<program>";
735 errs() << "\nAbout to run:\t";
736 for (unsigned i
= 0, e
= ProgramArgs
.size() - 1; i
!= e
; ++i
) errs()
737 << " " << ProgramArgs
[i
];
740 FileRemover
OutputBinaryRemover(OutputBinary
.str(), !SaveTemps
);
742 if (RemoteClientPath
.empty()) {
743 LLVM_DEBUG(errs() << "<run locally>");
745 int ExitCode
= RunProgramWithTimeout(OutputBinary
.str(), ProgramArgs
,
746 InputFile
, OutputFile
, OutputFile
,
747 Timeout
, MemoryLimit
, &Error
);
748 // Treat a signal (usually SIGSEGV) or timeout as part of the program output
749 // so that crash-causing miscompilation is handled seamlessly.
751 std::ofstream
outFile(OutputFile
.c_str(), std::ios_base::app
);
752 outFile
<< Error
<< '\n';
757 outs() << "<run remotely>";
759 return RunProgramRemotelyWithTimeout(RemoteClientPath
, ProgramArgs
,
760 InputFile
, OutputFile
, OutputFile
,
761 Timeout
, MemoryLimit
);
765 Error
CC::MakeSharedObject(const std::string
&InputFile
, FileType fileType
,
766 std::string
&OutputFile
,
767 const std::vector
<std::string
> &ArgsForCC
) {
768 SmallString
<128> UniqueFilename
;
769 std::error_code EC
= sys::fs::createUniqueFile(
770 InputFile
+ "-%%%%%%%" + LTDL_SHLIB_EXT
, UniqueFilename
);
772 errs() << "Error making unique filename: " << EC
.message() << "\n";
775 OutputFile
= UniqueFilename
.str();
777 std::vector
<StringRef
> CCArgs
;
779 CCArgs
.push_back(CCPath
);
781 if (TargetTriple
.getArch() == Triple::x86
)
782 CCArgs
.push_back("-m32");
784 for (std::vector
<std::string
>::const_iterator I
= ccArgs
.begin(),
787 CCArgs
.push_back(*I
);
789 // Compile the C/asm file into a shared object
790 if (fileType
!= ObjectFile
) {
791 CCArgs
.push_back("-x");
792 CCArgs
.push_back(fileType
== AsmFile
? "assembler" : "c");
794 CCArgs
.push_back("-fno-strict-aliasing");
795 CCArgs
.push_back(InputFile
); // Specify the input filename.
796 CCArgs
.push_back("-x");
797 CCArgs
.push_back("none");
798 if (TargetTriple
.getArch() == Triple::sparc
)
799 CCArgs
.push_back("-G"); // Compile a shared library, `-G' for Sparc
800 else if (TargetTriple
.isOSDarwin()) {
801 // link all source files into a single module in data segment, rather than
802 // generating blocks. dynamic_lookup requires that you set
803 // MACOSX_DEPLOYMENT_TARGET=10.3 in your env. FIXME: it would be better for
804 // bugpoint to just pass that in the environment of CC.
805 CCArgs
.push_back("-single_module");
806 CCArgs
.push_back("-dynamiclib"); // `-dynamiclib' for MacOS X/PowerPC
807 CCArgs
.push_back("-undefined");
808 CCArgs
.push_back("dynamic_lookup");
810 CCArgs
.push_back("-shared"); // `-shared' for Linux/X86, maybe others
812 if (TargetTriple
.getArch() == Triple::x86_64
)
813 CCArgs
.push_back("-fPIC"); // Requires shared objs to contain PIC
815 if (TargetTriple
.getArch() == Triple::sparc
)
816 CCArgs
.push_back("-mcpu=v9");
818 CCArgs
.push_back("-o");
819 CCArgs
.push_back(OutputFile
); // Output to the right filename.
820 CCArgs
.push_back("-O2"); // Optimize the program a bit.
822 // Add any arguments intended for CC. We locate them here because this is
823 // most likely -L and -l options that need to come before other libraries but
824 // after the source. Other options won't be sensitive to placement on the
825 // command line, so this should be safe.
826 for (unsigned i
= 0, e
= ArgsForCC
.size(); i
!= e
; ++i
)
827 CCArgs
.push_back(ArgsForCC
[i
]);
831 LLVM_DEBUG(errs() << "\nAbout to run:\t";
832 for (unsigned i
= 0, e
= CCArgs
.size() - 1; i
!= e
; ++i
) errs()
835 if (RunProgramWithTimeout(CCPath
, CCArgs
, "", "", ""))
836 return ProcessFailure(CCPath
, CCArgs
);
837 return Error::success();
840 /// create - Try to find the CC executable
842 CC
*CC::create(const char *Argv0
, std::string
&Message
,
843 const std::string
&CCBinary
,
844 const std::vector
<std::string
> *Args
) {
845 auto CCPath
= FindProgramByName(CCBinary
, Argv0
, (void *)(intptr_t)&create
);
847 Message
= "Cannot find `" + CCBinary
+ "' in PATH: " +
848 CCPath
.getError().message() + "\n";
852 std::string RemoteClientPath
;
853 if (!RemoteClient
.empty()) {
854 auto Path
= sys::findProgramByName(RemoteClient
);
856 Message
= "Cannot find `" + RemoteClient
+ "' in PATH: " +
857 Path
.getError().message() + "\n";
860 RemoteClientPath
= *Path
;
863 Message
= "Found CC: " + *CCPath
+ "\n";
864 return new CC(*CCPath
, RemoteClientPath
, Args
);