2008-12-08 00:41:11 +08:00
|
|
|
//===--- Tool.cpp - The LLVM Compiler Driver --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Tool base class - implementation details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-26 02:20:10 +08:00
|
|
|
#include "llvm/CompilerDriver/BuiltinOptions.h"
|
2008-12-08 00:41:11 +08:00
|
|
|
#include "llvm/CompilerDriver/Tool.h"
|
|
|
|
|
2009-06-29 11:09:15 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2010-11-30 02:47:54 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2008-12-08 00:41:11 +08:00
|
|
|
|
2010-02-23 17:04:57 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2008-12-08 00:41:11 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvmc;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
|
|
|
|
const std::string& Suffix) {
|
|
|
|
sys::Path Out;
|
|
|
|
|
|
|
|
// Make sure we don't end up with path names like '/file.o' if the
|
|
|
|
// TempDir is empty.
|
|
|
|
if (TempDir.empty()) {
|
|
|
|
Out.set(BaseName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Out = TempDir;
|
|
|
|
Out.appendComponent(BaseName);
|
|
|
|
}
|
|
|
|
Out.appendSuffix(Suffix);
|
|
|
|
// NOTE: makeUnique always *creates* a unique temporary file,
|
|
|
|
// which is good, since there will be no races. However, some
|
|
|
|
// tools do not like it when the output file already exists, so
|
2009-06-26 02:20:10 +08:00
|
|
|
// they need to be placated with -f or something like that.
|
2008-12-08 00:41:11 +08:00
|
|
|
Out.makeUnique(true, NULL);
|
|
|
|
return Out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sys::Path Tool::OutFilename(const sys::Path& In,
|
|
|
|
const sys::Path& TempDir,
|
|
|
|
bool StopCompilation,
|
|
|
|
const char* OutputSuffix) const {
|
|
|
|
sys::Path Out;
|
|
|
|
|
|
|
|
if (StopCompilation) {
|
2009-07-04 22:23:32 +08:00
|
|
|
if (!OutputFilename.empty()) {
|
2008-12-08 00:41:11 +08:00
|
|
|
Out.set(OutputFilename);
|
|
|
|
}
|
|
|
|
else if (IsJoin()) {
|
|
|
|
Out.set("a");
|
|
|
|
Out.appendSuffix(OutputSuffix);
|
|
|
|
}
|
|
|
|
else {
|
2010-12-18 15:37:18 +08:00
|
|
|
Out.set(In.getBasename());
|
2008-12-08 00:41:11 +08:00
|
|
|
Out.appendSuffix(OutputSuffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (IsJoin())
|
|
|
|
Out = MakeTempFile(TempDir, "tmp", OutputSuffix);
|
|
|
|
else
|
2010-12-18 15:37:18 +08:00
|
|
|
Out = MakeTempFile(TempDir, In.getBasename(), OutputSuffix);
|
2008-12-08 00:41:11 +08:00
|
|
|
}
|
|
|
|
return Out;
|
|
|
|
}
|
2010-02-23 17:04:44 +08:00
|
|
|
|
2010-02-23 17:04:57 +08:00
|
|
|
namespace {
|
|
|
|
template <class A, class B>
|
|
|
|
bool CompareFirst (std::pair<A,B> p1, std::pair<A,B> p2) {
|
|
|
|
return std::less<A>()(p1.first, p2.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 17:04:44 +08:00
|
|
|
StrVector Tool::SortArgs(ArgsVector& Args) const {
|
|
|
|
StrVector Out;
|
|
|
|
|
2010-02-23 17:04:57 +08:00
|
|
|
// HACK: this won't be needed when we'll migrate away from CommandLine.
|
2010-07-01 09:00:27 +08:00
|
|
|
std::stable_sort(Args.begin(), Args.end(),
|
|
|
|
&CompareFirst<unsigned, std::string>);
|
2010-02-23 17:04:57 +08:00
|
|
|
for (ArgsVector::iterator B = Args.begin(), E = Args.end(); B != E; ++B) {
|
2010-02-23 17:04:44 +08:00
|
|
|
Out.push_back(B->second);
|
2010-02-23 17:04:57 +08:00
|
|
|
}
|
2010-02-23 17:04:44 +08:00
|
|
|
|
|
|
|
return Out;
|
|
|
|
}
|