Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 1 | //===--- 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 Glushenkov | 7defa2d | 2009-06-25 18:20:10 +0000 | [diff] [blame] | 14 | #include "llvm/CompilerDriver/BuiltinOptions.h" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 15 | #include "llvm/CompilerDriver/Tool.h" |
| 16 | |
Mikhail Glushenkov | 95c1f5b | 2009-06-29 03:09:15 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 18 | #include "llvm/System/Path.h" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvmc; |
| 22 | |
Mikhail Glushenkov | 95c1f5b | 2009-06-29 03:09:15 +0000 | [diff] [blame] | 23 | // SplitString is used by derived Tool classes. |
| 24 | typedef void (*SplitStringFunPtr)(const std::string&, |
| 25 | std::vector<std::string>&, const char*); |
| 26 | SplitStringFunPtr ForceLinkageSplitString = &llvm::SplitString; |
| 27 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName, |
| 30 | const std::string& Suffix) { |
| 31 | sys::Path Out; |
| 32 | |
| 33 | // Make sure we don't end up with path names like '/file.o' if the |
| 34 | // TempDir is empty. |
| 35 | if (TempDir.empty()) { |
| 36 | Out.set(BaseName); |
| 37 | } |
| 38 | else { |
| 39 | Out = TempDir; |
| 40 | Out.appendComponent(BaseName); |
| 41 | } |
| 42 | Out.appendSuffix(Suffix); |
| 43 | // NOTE: makeUnique always *creates* a unique temporary file, |
| 44 | // which is good, since there will be no races. However, some |
| 45 | // tools do not like it when the output file already exists, so |
Mikhail Glushenkov | 7defa2d | 2009-06-25 18:20:10 +0000 | [diff] [blame] | 46 | // they need to be placated with -f or something like that. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 47 | Out.makeUnique(true, NULL); |
| 48 | return Out; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | sys::Path Tool::OutFilename(const sys::Path& In, |
| 53 | const sys::Path& TempDir, |
| 54 | bool StopCompilation, |
| 55 | const char* OutputSuffix) const { |
| 56 | sys::Path Out; |
| 57 | |
| 58 | if (StopCompilation) { |
Mikhail Glushenkov | eebf60c | 2009-07-04 14:23:32 +0000 | [diff] [blame] | 59 | if (!OutputFilename.empty()) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 60 | Out.set(OutputFilename); |
| 61 | } |
| 62 | else if (IsJoin()) { |
| 63 | Out.set("a"); |
| 64 | Out.appendSuffix(OutputSuffix); |
| 65 | } |
| 66 | else { |
| 67 | Out.set(In.getBasename()); |
| 68 | Out.appendSuffix(OutputSuffix); |
| 69 | } |
| 70 | } |
| 71 | else { |
| 72 | if (IsJoin()) |
| 73 | Out = MakeTempFile(TempDir, "tmp", OutputSuffix); |
| 74 | else |
| 75 | Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix); |
| 76 | } |
| 77 | return Out; |
| 78 | } |