1 //===--- Tool.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
23 using namespace llvmc
;
26 sys::Path
MakeTempFile(const sys::Path
& TempDir
, const std::string
& BaseName
,
27 const std::string
& Suffix
) {
30 // Make sure we don't end up with path names like '/file.o' if the
32 if (TempDir
.empty()) {
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
);
49 sys::Path
Tool::OutFilename(const sys::Path
& In
,
50 const sys::Path
& TempDir
,
52 const char* OutputSuffix
) const {
55 if (StopCompilation
) {
56 if (!OutputFilename
.empty()) {
57 Out
.set(OutputFilename
);
61 Out
.appendSuffix(OutputSuffix
);
64 Out
.set(In
.getBasename());
65 Out
.appendSuffix(OutputSuffix
);
70 Out
= MakeTempFile(TempDir
, "tmp", OutputSuffix
);
72 Out
= MakeTempFile(TempDir
, In
.getBasename(), OutputSuffix
);
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 {
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
);