blob: 9f4ab4956627afd5cfd3ba173105b917e7ec30a6 [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
Mikhail Glushenkov7defa2d2009-06-25 18:20:10 +000014#include "llvm/CompilerDriver/BuiltinOptions.h"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000015#include "llvm/CompilerDriver/Tool.h"
16
Mikhail Glushenkov95c1f5b2009-06-29 03:09:15 +000017#include "llvm/ADT/StringExtras.h"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000018#include "llvm/System/Path.h"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000019
20using namespace llvm;
21using namespace llvmc;
22
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000023namespace {
24 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
25 const std::string& Suffix) {
26 sys::Path Out;
27
28 // Make sure we don't end up with path names like '/file.o' if the
29 // TempDir is empty.
30 if (TempDir.empty()) {
31 Out.set(BaseName);
32 }
33 else {
34 Out = TempDir;
35 Out.appendComponent(BaseName);
36 }
37 Out.appendSuffix(Suffix);
38 // NOTE: makeUnique always *creates* a unique temporary file,
39 // which is good, since there will be no races. However, some
40 // tools do not like it when the output file already exists, so
Mikhail Glushenkov7defa2d2009-06-25 18:20:10 +000041 // they need to be placated with -f or something like that.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000042 Out.makeUnique(true, NULL);
43 return Out;
44 }
45}
46
47sys::Path Tool::OutFilename(const sys::Path& In,
48 const sys::Path& TempDir,
49 bool StopCompilation,
50 const char* OutputSuffix) const {
51 sys::Path Out;
52
53 if (StopCompilation) {
Mikhail Glushenkoveebf60c2009-07-04 14:23:32 +000054 if (!OutputFilename.empty()) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000055 Out.set(OutputFilename);
56 }
57 else if (IsJoin()) {
58 Out.set("a");
59 Out.appendSuffix(OutputSuffix);
60 }
61 else {
62 Out.set(In.getBasename());
63 Out.appendSuffix(OutputSuffix);
64 }
65 }
66 else {
67 if (IsJoin())
68 Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
69 else
70 Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix);
71 }
72 return Out;
73}