blob: 886b26b5d713f0d5764e296c93b42bc0fde243de [file] [log] [blame]
Mikhail Glushenkovf9152532008-12-07 16:41:11 +00001//===--- Tool.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Tool base class - implementation details.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CompilerDriver/Tool.h"
15
16#include "llvm/System/Path.h"
17#include "llvm/Support/CommandLine.h"
18
19using namespace llvm;
20using namespace llvmc;
21
22extern cl::opt<std::string> OutputFilename;
23
24namespace {
25 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
26 const std::string& Suffix) {
27 sys::Path Out;
28
29 // Make sure we don't end up with path names like '/file.o' if the
30 // TempDir is empty.
31 if (TempDir.empty()) {
32 Out.set(BaseName);
33 }
34 else {
35 Out = TempDir;
36 Out.appendComponent(BaseName);
37 }
38 Out.appendSuffix(Suffix);
39 // NOTE: makeUnique always *creates* a unique temporary file,
40 // which is good, since there will be no races. However, some
41 // tools do not like it when the output file already exists, so
42 // they have to be placated with -f or something like that.
43 Out.makeUnique(true, NULL);
44 return Out;
45 }
46}
47
48sys::Path Tool::OutFilename(const sys::Path& In,
49 const sys::Path& TempDir,
50 bool StopCompilation,
51 const char* OutputSuffix) const {
52 sys::Path Out;
53
54 if (StopCompilation) {
55 if (!OutputFilename.empty()) {
56 Out.set(OutputFilename);
57 }
58 else if (IsJoin()) {
59 Out.set("a");
60 Out.appendSuffix(OutputSuffix);
61 }
62 else {
63 Out.set(In.getBasename());
64 Out.appendSuffix(OutputSuffix);
65 }
66 }
67 else {
68 if (IsJoin())
69 Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
70 else
71 Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix);
72 }
73 return Out;
74}