Fixed some bugs.
[llvm/zpu.git] / lib / CompilerDriver / Tool.cpp
blobc8488b226e28747bd62c11b84b205cc138a967ce
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CompilerDriver/BuiltinOptions.h"
15 #include "llvm/CompilerDriver/Tool.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/System/Path.h"
20 #include <algorithm>
22 using namespace llvm;
23 using namespace llvmc;
25 namespace {
26 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
27 const std::string& Suffix) {
28 sys::Path Out;
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);
35 else {
36 Out = TempDir;
37 Out.appendComponent(BaseName);
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;
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;
55 if (StopCompilation) {
56 if (!OutputFilename.empty()) {
57 Out.set(OutputFilename);
59 else if (IsJoin()) {
60 Out.set("a");
61 Out.appendSuffix(OutputSuffix);
63 else {
64 Out.set(In.getBasename());
65 Out.appendSuffix(OutputSuffix);
68 else {
69 if (IsJoin())
70 Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
71 else
72 Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix);
74 return Out;
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);
84 StrVector Tool::SortArgs(ArgsVector& Args) const {
85 StrVector Out;
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);
94 return Out;