blob: 876759aa72b00420a368a3e5a7d8f21923bb9fcb [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"
Michael J. Spencer3cc52ea2010-11-29 18:47:54 +000018#include "llvm/Support/Path.h"
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000019
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +000020#include <algorithm>
21
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000022using namespace llvm;
23using namespace llvmc;
24
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000025namespace {
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 Glushenkov7defa2d2009-06-25 18:20:10 +000043 // they need to be placated with -f or something like that.
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000044 Out.makeUnique(true, NULL);
45 return Out;
46 }
47}
48
49sys::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 Glushenkoveebf60c2009-07-04 14:23:32 +000056 if (!OutputFilename.empty()) {
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000057 Out.set(OutputFilename);
58 }
59 else if (IsJoin()) {
60 Out.set("a");
61 Out.appendSuffix(OutputSuffix);
62 }
63 else {
Michael J. Spencerb9c767c2010-12-18 04:13:36 +000064 Out.set(sys::path::stem(In.str()));
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000065 Out.appendSuffix(OutputSuffix);
66 }
67 }
68 else {
69 if (IsJoin())
70 Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
71 else
Michael J. Spencerb9c767c2010-12-18 04:13:36 +000072 Out = MakeTempFile(TempDir, sys::path::stem(In.str()), OutputSuffix);
Mikhail Glushenkovf9152532008-12-07 16:41:11 +000073 }
74 return Out;
75}
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000076
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +000077namespace {
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 Glushenkova34f97a2010-02-23 09:04:44 +000084StrVector Tool::SortArgs(ArgsVector& Args) const {
85 StrVector Out;
86
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +000087 // HACK: this won't be needed when we'll migrate away from CommandLine.
Mikhail Glushenkova6139052010-07-01 01:00:27 +000088 std::stable_sort(Args.begin(), Args.end(),
89 &CompareFirst<unsigned, std::string>);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +000090 for (ArgsVector::iterator B = Args.begin(), E = Args.end(); B != E; ++B) {
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000091 Out.push_back(B->second);
Mikhail Glushenkov1afba8e2010-02-23 09:04:57 +000092 }
Mikhail Glushenkova34f97a2010-02-23 09:04:44 +000093
94 return Out;
95}