]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/CompilerDriver/Tool.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / lib / CompilerDriver / Tool.cpp
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
14 #include "llvm/CompilerDriver/BuiltinOptions.h"
15 #include "llvm/CompilerDriver/Tool.h"
16
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Path.h"
19
20 #include <algorithm>
21
22 using namespace llvm;
23 using namespace llvmc;
24
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
43     // they need to be placated with -f or something like that.
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) {
56     if (!OutputFilename.empty()) {
57       Out.set(OutputFilename);
58     }
59     else if (IsJoin()) {
60       Out.set("a");
61       Out.appendSuffix(OutputSuffix);
62     }
63     else {
64       Out.set(sys::path::stem(In.str()));
65       Out.appendSuffix(OutputSuffix);
66     }
67   }
68   else {
69     if (IsJoin())
70       Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
71     else
72       Out = MakeTempFile(TempDir, sys::path::stem(In.str()), OutputSuffix);
73   }
74   return Out;
75 }
76
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
84 StrVector Tool::SortArgs(ArgsVector& Args) const {
85   StrVector Out;
86
87   // HACK: this won't be needed when we'll migrate away from CommandLine.
88   std::stable_sort(Args.begin(), Args.end(),
89                    &CompareFirst<unsigned, std::string>);
90   for (ArgsVector::iterator B = Args.begin(), E = Args.end(); B != E; ++B) {
91     Out.push_back(B->second);
92   }
93
94   return Out;
95 }