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" |
Michael J. Spencer | 3cc52ea | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 19 | |
Mikhail Glushenkov | 1afba8e | 2010-02-23 09:04:57 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace llvmc; |
| 24 | |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName, |
| 27 | const std::string& Suffix) { |
| 28 | sys::Path Out; |
| 29 | |
| 30 | // Make sure we don't end up with path names like '/file.o' if the |
| 31 | // TempDir is empty. |
| 32 | if (TempDir.empty()) { |
| 33 | Out.set(BaseName); |
| 34 | } |
| 35 | else { |
| 36 | Out = TempDir; |
| 37 | Out.appendComponent(BaseName); |
| 38 | } |
| 39 | Out.appendSuffix(Suffix); |
| 40 | // NOTE: makeUnique always *creates* a unique temporary file, |
| 41 | // which is good, since there will be no races. However, some |
| 42 | // tools do not like it when the output file already exists, so |
Mikhail Glushenkov | 7defa2d | 2009-06-25 18:20:10 +0000 | [diff] [blame] | 43 | // they need to be placated with -f or something like that. |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 44 | Out.makeUnique(true, NULL); |
| 45 | return Out; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | sys::Path Tool::OutFilename(const sys::Path& In, |
| 50 | const sys::Path& TempDir, |
| 51 | bool StopCompilation, |
| 52 | const char* OutputSuffix) const { |
| 53 | sys::Path Out; |
| 54 | |
| 55 | if (StopCompilation) { |
Mikhail Glushenkov | eebf60c | 2009-07-04 14:23:32 +0000 | [diff] [blame] | 56 | if (!OutputFilename.empty()) { |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 57 | Out.set(OutputFilename); |
| 58 | } |
| 59 | else if (IsJoin()) { |
| 60 | Out.set("a"); |
| 61 | Out.appendSuffix(OutputSuffix); |
| 62 | } |
| 63 | else { |
Michael J. Spencer | b9c767c | 2010-12-18 04:13:36 +0000 | [diff] [blame] | 64 | Out.set(sys::path::stem(In.str())); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 65 | Out.appendSuffix(OutputSuffix); |
| 66 | } |
| 67 | } |
| 68 | else { |
| 69 | if (IsJoin()) |
| 70 | Out = MakeTempFile(TempDir, "tmp", OutputSuffix); |
| 71 | else |
Michael J. Spencer | b9c767c | 2010-12-18 04:13:36 +0000 | [diff] [blame] | 72 | Out = MakeTempFile(TempDir, sys::path::stem(In.str()), OutputSuffix); |
Mikhail Glushenkov | f915253 | 2008-12-07 16:41:11 +0000 | [diff] [blame] | 73 | } |
| 74 | return Out; |
| 75 | } |
Mikhail Glushenkov | a34f97a | 2010-02-23 09:04:44 +0000 | [diff] [blame] | 76 | |
Mikhail Glushenkov | 1afba8e | 2010-02-23 09:04:57 +0000 | [diff] [blame] | 77 | namespace { |
| 78 | template <class A, class B> |
| 79 | bool CompareFirst (std::pair<A,B> p1, std::pair<A,B> p2) { |
| 80 | return std::less<A>()(p1.first, p2.first); |
| 81 | } |
| 82 | } |
| 83 | |
Mikhail Glushenkov | a34f97a | 2010-02-23 09:04:44 +0000 | [diff] [blame] | 84 | StrVector Tool::SortArgs(ArgsVector& Args) const { |
| 85 | StrVector Out; |
| 86 | |
Mikhail Glushenkov | 1afba8e | 2010-02-23 09:04:57 +0000 | [diff] [blame] | 87 | // HACK: this won't be needed when we'll migrate away from CommandLine. |
Mikhail Glushenkov | a613905 | 2010-07-01 01:00:27 +0000 | [diff] [blame] | 88 | std::stable_sort(Args.begin(), Args.end(), |
| 89 | &CompareFirst<unsigned, std::string>); |
Mikhail Glushenkov | 1afba8e | 2010-02-23 09:04:57 +0000 | [diff] [blame] | 90 | for (ArgsVector::iterator B = Args.begin(), E = Args.end(); B != E; ++B) { |
Mikhail Glushenkov | a34f97a | 2010-02-23 09:04:44 +0000 | [diff] [blame] | 91 | Out.push_back(B->second); |
Mikhail Glushenkov | 1afba8e | 2010-02-23 09:04:57 +0000 | [diff] [blame] | 92 | } |
Mikhail Glushenkov | a34f97a | 2010-02-23 09:04:44 +0000 | [diff] [blame] | 93 | |
| 94 | return Out; |
| 95 | } |