2004-08-13 22:25:54 +02:00
|
|
|
//===- CompilerDriver.cpp - The LLVM Compiler Driver ------------*- C++ -*-===//
|
2004-08-13 22:22:43 +02:00
|
|
|
//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2004-08-13 22:22:43 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2005-04-22 02:00:37 +02:00
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
2004-08-13 22:22:43 +02:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2004-08-13 22:22:43 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-08-13 22:25:54 +02:00
|
|
|
// This file implements the bulk of the LLVM Compiler Driver (llvmc).
|
2004-08-13 22:22:43 +02:00
|
|
|
//
|
2006-05-29 20:52:05 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-08-13 22:22:43 +02:00
|
|
|
|
|
|
|
#include "CompilerDriver.h"
|
2004-08-15 10:19:46 +02:00
|
|
|
#include "ConfigLexer.h"
|
2004-08-24 15:55:17 +02:00
|
|
|
#include "llvm/Module.h"
|
2004-08-29 21:26:56 +02:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2004-09-14 03:58:45 +02:00
|
|
|
#include "llvm/Support/Timer.h"
|
2004-08-29 22:02:28 +02:00
|
|
|
#include "llvm/System/Signals.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-12-20 05:02:01 +01:00
|
|
|
#include "llvm/Config/alloca.h"
|
2005-04-20 06:51:29 +02:00
|
|
|
#include <iostream>
|
2004-08-13 22:22:43 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
void WriteAction(CompilerDriver::Action* action ) {
|
|
|
|
std::cerr << action->program.c_str();
|
2004-12-19 19:00:56 +01:00
|
|
|
std::vector<std::string>::const_iterator I = action->args.begin();
|
2004-10-28 06:05:06 +02:00
|
|
|
while (I != action->args.end()) {
|
2005-05-03 22:30:34 +02:00
|
|
|
std::cerr << ' ' << *I;
|
2004-10-28 06:05:06 +02:00
|
|
|
++I;
|
2004-08-14 11:37:15 +02:00
|
|
|
}
|
2005-05-03 22:30:34 +02:00
|
|
|
std::cerr << '\n';
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-14 11:37:15 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
void DumpAction(CompilerDriver::Action* action) {
|
|
|
|
std::cerr << "command = " << action->program.c_str();
|
2004-12-19 19:00:56 +01:00
|
|
|
std::vector<std::string>::const_iterator I = action->args.begin();
|
2004-10-28 06:05:06 +02:00
|
|
|
while (I != action->args.end()) {
|
2005-05-03 22:30:34 +02:00
|
|
|
std::cerr << ' ' << *I;
|
2004-10-28 06:05:06 +02:00
|
|
|
++I;
|
2004-08-15 10:19:46 +02:00
|
|
|
}
|
2005-05-03 22:30:34 +02:00
|
|
|
std::cerr << '\n';
|
|
|
|
std::cerr << "flags = " << action->flags << '\n';
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-15 10:19:46 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
void DumpConfigData(CompilerDriver::ConfigData* cd, const std::string& type ){
|
2005-04-22 02:00:37 +02:00
|
|
|
std::cerr << "Configuration Data For '" << cd->langName << "' (" << type
|
2004-10-28 06:05:06 +02:00
|
|
|
<< ")\n";
|
|
|
|
std::cerr << "PreProcessor: ";
|
|
|
|
DumpAction(&cd->PreProcessor);
|
|
|
|
std::cerr << "Translator: ";
|
|
|
|
DumpAction(&cd->Translator);
|
|
|
|
std::cerr << "Optimizer: ";
|
|
|
|
DumpAction(&cd->Optimizer);
|
|
|
|
std::cerr << "Assembler: ";
|
|
|
|
DumpAction(&cd->Assembler);
|
|
|
|
std::cerr << "Linker: ";
|
|
|
|
DumpAction(&cd->Linker);
|
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// This specifies the passes to run for OPT_FAST_COMPILE (-O1)
|
|
|
|
/// which should reduce the volume of code and make compilation
|
2005-04-22 02:00:37 +02:00
|
|
|
/// faster. This is also safe on any llvm module.
|
2004-10-28 06:05:06 +02:00
|
|
|
static const char* DefaultFastCompileOptimizations[] = {
|
|
|
|
"-simplifycfg", "-mem2reg", "-instcombine"
|
|
|
|
};
|
|
|
|
|
|
|
|
class CompilerDriverImpl : public CompilerDriver {
|
|
|
|
/// @name Constructors
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
CompilerDriverImpl(ConfigDataProvider& confDatProv )
|
|
|
|
: cdp(&confDatProv)
|
|
|
|
, finalPhase(LINKING)
|
2005-04-22 02:00:37 +02:00
|
|
|
, optLevel(OPT_FAST_COMPILE)
|
2004-10-28 06:05:06 +02:00
|
|
|
, Flags(0)
|
|
|
|
, machine()
|
|
|
|
, LibraryPaths()
|
|
|
|
, TempDir()
|
|
|
|
, AdditionalArgs()
|
|
|
|
{
|
|
|
|
AdditionalArgs.reserve(NUM_PHASES);
|
|
|
|
StringVector emptyVec;
|
|
|
|
for (unsigned i = 0; i < NUM_PHASES; ++i)
|
|
|
|
AdditionalArgs.push_back(emptyVec);
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual ~CompilerDriverImpl() {
|
|
|
|
cleanup();
|
|
|
|
cdp = 0;
|
|
|
|
LibraryPaths.clear();
|
|
|
|
IncludePaths.clear();
|
|
|
|
Defines.clear();
|
|
|
|
TempDir.clear();
|
|
|
|
AdditionalArgs.clear();
|
|
|
|
fOptions.clear();
|
|
|
|
MOptions.clear();
|
|
|
|
WOptions.clear();
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @}
|
|
|
|
/// @name Methods
|
|
|
|
/// @{
|
|
|
|
public:
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void setFinalPhase(Phases phase) {
|
2005-04-22 02:00:37 +02:00
|
|
|
finalPhase = phase;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void setOptimization(OptimizationLevels level) {
|
2005-04-22 02:00:37 +02:00
|
|
|
optLevel = level;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void setDriverFlags(unsigned flags) {
|
2005-04-22 02:00:37 +02:00
|
|
|
Flags = flags & DRIVER_FLAGS_MASK;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void setOutputMachine(const std::string& machineName) {
|
2004-10-28 06:05:06 +02:00
|
|
|
machine = machineName;
|
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual void setPhaseArgs(Phases phase, const StringVector& opts) {
|
|
|
|
assert(phase <= LINKING && phase >= PREPROCESSING);
|
|
|
|
AdditionalArgs[phase] = opts;
|
|
|
|
}
|
2004-08-15 10:19:46 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual void setIncludePaths(const StringVector& paths) {
|
|
|
|
StringVector::const_iterator I = paths.begin();
|
|
|
|
StringVector::const_iterator E = paths.end();
|
|
|
|
while (I != E) {
|
|
|
|
sys::Path tmp;
|
2005-07-08 01:21:43 +02:00
|
|
|
tmp.set(*I);
|
2004-10-28 06:05:06 +02:00
|
|
|
IncludePaths.push_back(tmp);
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
}
|
2004-08-30 08:29:06 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual void setSymbolDefines(const StringVector& defs) {
|
|
|
|
Defines = defs;
|
|
|
|
}
|
2004-08-30 08:29:06 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual void setLibraryPaths(const StringVector& paths) {
|
|
|
|
StringVector::const_iterator I = paths.begin();
|
|
|
|
StringVector::const_iterator E = paths.end();
|
|
|
|
while (I != E) {
|
|
|
|
sys::Path tmp;
|
2005-07-08 01:21:43 +02:00
|
|
|
tmp.set(*I);
|
2004-10-28 06:05:06 +02:00
|
|
|
LibraryPaths.push_back(tmp);
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
}
|
2004-08-15 10:19:46 +02:00
|
|
|
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void addLibraryPath(const sys::Path& libPath) {
|
2004-10-28 06:05:06 +02:00
|
|
|
LibraryPaths.push_back(libPath);
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2005-05-03 22:30:34 +02:00
|
|
|
virtual void addToolPath(const sys::Path& toolPath) {
|
2004-11-05 23:15:36 +01:00
|
|
|
ToolPaths.push_back(toolPath);
|
|
|
|
}
|
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
virtual void setfPassThrough(const StringVector& fOpts) {
|
|
|
|
fOptions = fOpts;
|
|
|
|
}
|
2004-09-14 03:58:45 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @brief Set the list of -M options to be passed through
|
|
|
|
virtual void setMPassThrough(const StringVector& MOpts) {
|
|
|
|
MOptions = MOpts;
|
|
|
|
}
|
2004-09-14 03:58:45 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @brief Set the list of -W options to be passed through
|
|
|
|
virtual void setWPassThrough(const StringVector& WOpts) {
|
|
|
|
WOptions = WOpts;
|
|
|
|
}
|
2004-11-24 00:45:49 +01:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @}
|
|
|
|
/// @name Functions
|
|
|
|
/// @{
|
|
|
|
private:
|
|
|
|
bool isSet(DriverFlags flag) {
|
|
|
|
return 0 != ((flag & DRIVER_FLAGS_MASK) & Flags);
|
|
|
|
}
|
2004-08-25 00:54:32 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
void cleanup() {
|
|
|
|
if (!isSet(KEEP_TEMPS_FLAG)) {
|
2006-08-01 20:12:29 +02:00
|
|
|
sys::FileStatus Status;
|
|
|
|
if (!TempDir.getFileStatus(Status) && Status.isDir)
|
2005-07-08 05:08:58 +02:00
|
|
|
TempDir.eraseFromDisk(/*remove_contents=*/true);
|
2004-10-28 06:05:06 +02:00
|
|
|
} else {
|
2004-12-13 09:53:36 +01:00
|
|
|
std::cout << "Temporary files are in " << TempDir << "\n";
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
2004-08-25 00:54:32 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
sys::Path MakeTempFile(const std::string& basename,
|
2006-08-22 21:01:30 +02:00
|
|
|
const std::string& suffix,
|
|
|
|
std::string* ErrMsg) {
|
|
|
|
if (TempDir.isEmpty()) {
|
|
|
|
TempDir = sys::Path::GetTemporaryDirectory(ErrMsg);
|
|
|
|
if (TempDir.isEmpty())
|
|
|
|
return sys::Path();
|
|
|
|
sys::RemoveDirectoryOnSignal(TempDir);
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
sys::Path result(TempDir);
|
2006-08-22 21:01:30 +02:00
|
|
|
if (!result.appendComponent(basename)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = basename + ": can't use this file name";
|
|
|
|
return sys::Path();
|
|
|
|
}
|
|
|
|
if (!result.appendSuffix(suffix)) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = suffix + ": can't use this file suffix";
|
|
|
|
return sys::Path();
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
return result;
|
|
|
|
}
|
2004-08-25 00:54:32 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
Action* GetAction(ConfigData* cd,
|
|
|
|
const sys::Path& input,
|
2004-10-28 06:05:06 +02:00
|
|
|
const sys::Path& output,
|
|
|
|
Phases phase)
|
|
|
|
{
|
|
|
|
Action* pat = 0; ///< The pattern/template for the action
|
|
|
|
Action* action = new Action; ///< The actual action to execute
|
|
|
|
|
|
|
|
// Get the action pattern
|
|
|
|
switch (phase) {
|
|
|
|
case PREPROCESSING: pat = &cd->PreProcessor; break;
|
|
|
|
case TRANSLATION: pat = &cd->Translator; break;
|
|
|
|
case OPTIMIZATION: pat = &cd->Optimizer; break;
|
|
|
|
case ASSEMBLY: pat = &cd->Assembler; break;
|
|
|
|
case LINKING: pat = &cd->Linker; break;
|
|
|
|
default:
|
|
|
|
assert(!"Invalid driver phase!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(pat != 0 && "Invalid command pattern");
|
|
|
|
|
|
|
|
// Copy over some pattern things that don't need to change
|
|
|
|
action->flags = pat->flags;
|
|
|
|
|
2006-08-16 22:31:44 +02:00
|
|
|
// See if program starts with wildcard...
|
|
|
|
std::string programName=pat->program.toString();
|
|
|
|
if (programName[0] == '%' && programName.length() >2) {
|
|
|
|
switch(programName[1]){
|
|
|
|
case 'b':
|
|
|
|
if (programName.substr(0,8) == "%bindir%") {
|
|
|
|
std::string tmp(LLVM_BINDIR);
|
|
|
|
tmp.append(programName.substr(8));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (programName.substr(0,12) == "%llvmgccdir%"){
|
|
|
|
std::string tmp(LLVMGCCDIR);
|
|
|
|
tmp.append(programName.substr(12));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,13) == "%llvmgccarch%"){
|
|
|
|
std::string tmp(LLVMGCCARCH);
|
|
|
|
tmp.append(programName.substr(13));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,9) == "%llvmgcc%"){
|
|
|
|
std::string tmp(LLVMGCC);
|
|
|
|
tmp.append(programName.substr(9));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,9) == "%llvmgxx%"){
|
|
|
|
std::string tmp(LLVMGXX);
|
|
|
|
tmp.append(programName.substr(9));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,9) == "%llvmcc1%"){
|
|
|
|
std::string tmp(LLVMCC1);
|
|
|
|
tmp.append(programName.substr(9));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,13) == "%llvmcc1plus%"){
|
|
|
|
std::string tmp(LLVMCC1PLUS);
|
|
|
|
tmp.append(programName.substr(13));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}else if (programName.substr(0,8) == "%libdir%") {
|
|
|
|
std::string tmp(LLVM_LIBDIR);
|
|
|
|
tmp.append(programName.substr(8));
|
|
|
|
pat->program.set(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action->program = pat->program;
|
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Do the substitutions from the pattern to the actual
|
|
|
|
StringVector::iterator PI = pat->args.begin();
|
|
|
|
StringVector::iterator PE = pat->args.end();
|
|
|
|
while (PI != PE) {
|
|
|
|
if ((*PI)[0] == '%' && PI->length() >2) {
|
|
|
|
bool found = true;
|
|
|
|
switch ((*PI)[1]) {
|
|
|
|
case 'a':
|
|
|
|
if (*PI == "%args%") {
|
|
|
|
if (AdditionalArgs.size() > unsigned(phase))
|
|
|
|
if (!AdditionalArgs[phase].empty()) {
|
|
|
|
// Get specific options for each kind of action type
|
|
|
|
StringVector& addargs = AdditionalArgs[phase];
|
|
|
|
// Add specific options for each kind of action type
|
2005-04-22 02:00:37 +02:00
|
|
|
action->args.insert(action->args.end(), addargs.begin(),
|
2004-10-28 06:05:06 +02:00
|
|
|
addargs.end());
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
2005-05-19 02:52:28 +02:00
|
|
|
case 'b':
|
|
|
|
if (*PI == "%bindir%") {
|
|
|
|
std::string tmp(*PI);
|
|
|
|
tmp.replace(0,8,LLVM_BINDIR);
|
|
|
|
action->args.push_back(tmp);
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
2004-10-28 06:05:06 +02:00
|
|
|
case 'd':
|
|
|
|
if (*PI == "%defs%") {
|
|
|
|
StringVector::iterator I = Defines.begin();
|
|
|
|
StringVector::iterator E = Defines.end();
|
|
|
|
while (I != E) {
|
|
|
|
action->args.push_back( std::string("-D") + *I);
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (*PI == "%fOpts%") {
|
2004-11-24 00:45:49 +01:00
|
|
|
if (!fOptions.empty())
|
2005-04-22 02:00:37 +02:00
|
|
|
action->args.insert(action->args.end(), fOptions.begin(),
|
2004-11-24 00:45:49 +01:00
|
|
|
fOptions.end());
|
2004-10-28 06:05:06 +02:00
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (*PI == "%in%") {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(input.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
} else if (*PI == "%incls%") {
|
|
|
|
PathVector::iterator I = IncludePaths.begin();
|
|
|
|
PathVector::iterator E = IncludePaths.end();
|
|
|
|
while (I != E) {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back( std::string("-I") + I->toString() );
|
2004-10-28 06:05:06 +02:00
|
|
|
++I;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'l':
|
2005-05-19 02:52:28 +02:00
|
|
|
if ((*PI)[1] == 'l') {
|
|
|
|
std::string tmp(*PI);
|
|
|
|
if (*PI == "%llvmgccdir%")
|
|
|
|
tmp.replace(0,12,LLVMGCCDIR);
|
|
|
|
else if (*PI == "%llvmgccarch%")
|
|
|
|
tmp.replace(0,13,LLVMGCCARCH);
|
|
|
|
else if (*PI == "%llvmgcc%")
|
|
|
|
tmp.replace(0,9,LLVMGCC);
|
|
|
|
else if (*PI == "%llvmgxx%")
|
|
|
|
tmp.replace(0,9,LLVMGXX);
|
|
|
|
else if (*PI == "%llvmcc1%")
|
|
|
|
tmp.replace(0,9,LLVMCC1);
|
2005-07-27 08:12:32 +02:00
|
|
|
else if (*PI == "%llvmcc1plus%")
|
2005-05-19 02:52:28 +02:00
|
|
|
tmp.replace(0,9,LLVMCC1);
|
|
|
|
else
|
|
|
|
found = false;
|
|
|
|
if (found)
|
|
|
|
action->args.push_back(tmp);
|
|
|
|
} else if (*PI == "%libs%") {
|
2004-10-28 06:05:06 +02:00
|
|
|
PathVector::iterator I = LibraryPaths.begin();
|
|
|
|
PathVector::iterator E = LibraryPaths.end();
|
|
|
|
while (I != E) {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back( std::string("-L") + I->toString() );
|
2004-10-28 06:05:06 +02:00
|
|
|
++I;
|
|
|
|
}
|
2005-05-19 02:52:28 +02:00
|
|
|
} else if (*PI == "%libdir%") {
|
|
|
|
std::string tmp(*PI);
|
|
|
|
tmp.replace(0,8,LLVM_LIBDIR);
|
|
|
|
action->args.push_back(tmp);
|
2004-10-28 06:05:06 +02:00
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
if (*PI == "%out%") {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(output.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
} else if (*PI == "%opt%") {
|
|
|
|
if (!isSet(EMIT_RAW_FLAG)) {
|
2005-04-22 02:00:37 +02:00
|
|
|
if (cd->opts.size() > static_cast<unsigned>(optLevel) &&
|
2004-10-28 06:05:06 +02:00
|
|
|
!cd->opts[optLevel].empty())
|
2005-04-22 02:00:37 +02:00
|
|
|
action->args.insert(action->args.end(),
|
2004-10-28 06:05:06 +02:00
|
|
|
cd->opts[optLevel].begin(),
|
|
|
|
cd->opts[optLevel].end());
|
|
|
|
else
|
2005-04-22 02:00:37 +02:00
|
|
|
throw std::string("Optimization options for level ") +
|
2004-10-28 06:05:06 +02:00
|
|
|
utostr(unsigned(optLevel)) + " were not specified";
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (*PI == "%stats%") {
|
|
|
|
if (isSet(SHOW_STATS_FLAG))
|
|
|
|
action->args.push_back("-stats");
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
if (*PI == "%target%") {
|
|
|
|
action->args.push_back(std::string("-march=") + machine);
|
|
|
|
} else if (*PI == "%time%") {
|
|
|
|
if (isSet(TIME_PASSES_FLAG))
|
|
|
|
action->args.push_back("-time-passes");
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
if (*PI == "%verbose%") {
|
|
|
|
if (isSet(VERBOSE_FLAG))
|
|
|
|
action->args.push_back("-v");
|
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'M':
|
2004-11-24 00:45:49 +01:00
|
|
|
if (*PI == "%Mopts%") {
|
|
|
|
if (!MOptions.empty())
|
2005-04-22 02:00:37 +02:00
|
|
|
action->args.insert(action->args.end(), MOptions.begin(),
|
2004-11-24 00:45:49 +01:00
|
|
|
MOptions.end());
|
2004-10-28 06:05:06 +02:00
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
case 'W':
|
2004-11-24 00:45:49 +01:00
|
|
|
if (*PI == "%Wopts%") {
|
|
|
|
for (StringVector::iterator I = WOptions.begin(),
|
|
|
|
E = WOptions.end(); I != E ; ++I ) {
|
2005-05-03 22:30:34 +02:00
|
|
|
action->args.push_back(std::string("-W") + *I);
|
2004-11-24 00:45:49 +01:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} else
|
|
|
|
found = false;
|
|
|
|
break;
|
2004-08-29 21:26:56 +02:00
|
|
|
default:
|
2004-10-28 06:05:06 +02:00
|
|
|
found = false;
|
2004-08-29 21:26:56 +02:00
|
|
|
break;
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
if (!found) {
|
|
|
|
// Did it even look like a substitution?
|
2005-04-22 02:00:37 +02:00
|
|
|
if (PI->length()>1 && (*PI)[0] == '%' &&
|
2004-10-28 06:05:06 +02:00
|
|
|
(*PI)[PI->length()-1] == '%') {
|
|
|
|
throw std::string("Invalid substitution token: '") + *PI +
|
2004-12-11 01:14:15 +01:00
|
|
|
"' for command '" + pat->program.toString() + "'";
|
2004-11-24 00:45:49 +01:00
|
|
|
} else if (!PI->empty()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
// It's not a legal substitution, just pass it through
|
2004-08-29 21:26:56 +02:00
|
|
|
action->args.push_back(*PI);
|
2004-08-25 00:54:32 +02:00
|
|
|
}
|
|
|
|
}
|
2004-11-24 00:45:49 +01:00
|
|
|
} else if (!PI->empty()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
// Its not a substitution, just put it in the action
|
|
|
|
action->args.push_back(*PI);
|
2004-08-13 22:22:43 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
PI++;
|
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Finally, we're done
|
|
|
|
return action;
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2006-08-21 08:04:45 +02:00
|
|
|
int DoAction(Action*action, std::string& ErrMsg) {
|
2004-10-28 06:05:06 +02:00
|
|
|
assert(action != 0 && "Invalid Action!");
|
|
|
|
if (isSet(VERBOSE_FLAG))
|
|
|
|
WriteAction(action);
|
|
|
|
if (!isSet(DRY_RUN_FLAG)) {
|
|
|
|
sys::Path progpath = sys::Program::FindProgramByName(
|
2004-12-11 01:14:15 +01:00
|
|
|
action->program.toString());
|
2004-11-05 23:15:36 +01:00
|
|
|
if (progpath.isEmpty())
|
2004-12-11 01:14:15 +01:00
|
|
|
throw std::string("Can't find program '" +
|
|
|
|
action->program.toString()+"'");
|
2005-07-07 20:21:42 +02:00
|
|
|
else if (progpath.canExecute())
|
2004-10-28 06:05:06 +02:00
|
|
|
action->program = progpath;
|
|
|
|
else
|
2004-12-11 01:14:15 +01:00
|
|
|
throw std::string("Program '"+action->program.toString()+
|
2004-11-24 00:45:49 +01:00
|
|
|
"' is not executable.");
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Invoke the program
|
2005-04-22 02:00:37 +02:00
|
|
|
const char** Args = (const char**)
|
2005-04-11 07:48:04 +02:00
|
|
|
alloca(sizeof(const char*)*(action->args.size()+2));
|
|
|
|
Args[0] = action->program.toString().c_str();
|
2006-08-16 22:31:44 +02:00
|
|
|
for (unsigned i = 1; i <= action->args.size(); ++i)
|
|
|
|
Args[i] = action->args[i-1].c_str();
|
|
|
|
Args[action->args.size()+1] = 0; // null terminate list.
|
2004-10-28 06:05:06 +02:00
|
|
|
if (isSet(TIME_ACTIONS_FLAG)) {
|
2004-12-11 01:14:15 +01:00
|
|
|
Timer timer(action->program.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
timer.startTimer();
|
2006-08-21 08:04:45 +02:00
|
|
|
int resultCode =
|
|
|
|
sys::Program::ExecuteAndWait(action->program, Args,0,0,0,&ErrMsg);
|
2004-10-28 06:05:06 +02:00
|
|
|
timer.stopTimer();
|
|
|
|
timer.print(timer,std::cerr);
|
2006-08-21 08:04:45 +02:00
|
|
|
return resultCode;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
else
|
2006-08-21 08:04:45 +02:00
|
|
|
return
|
|
|
|
sys::Program::ExecuteAndWait(action->program, Args, 0,0,0, &ErrMsg);
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2006-08-21 08:04:45 +02:00
|
|
|
return 0;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// This method tries various variants of a linkage item's file
|
|
|
|
/// name to see if it can find an appropriate file to link with
|
2004-11-24 00:45:49 +01:00
|
|
|
/// in the directories of the LibraryPaths.
|
2004-10-28 06:05:06 +02:00
|
|
|
llvm::sys::Path GetPathForLinkageItem(const std::string& link_item,
|
|
|
|
bool native = false) {
|
2004-11-24 00:45:49 +01:00
|
|
|
sys::Path fullpath;
|
2005-07-08 01:21:43 +02:00
|
|
|
fullpath.set(link_item);
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-11-24 00:45:49 +01:00
|
|
|
return fullpath;
|
2005-04-22 02:00:37 +02:00
|
|
|
for (PathVector::iterator PI = LibraryPaths.begin(),
|
2004-11-24 00:45:49 +01:00
|
|
|
PE = LibraryPaths.end(); PI != PE; ++PI) {
|
2005-07-08 01:21:43 +02:00
|
|
|
fullpath.set(PI->toString());
|
|
|
|
fullpath.appendComponent(link_item);
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-10-28 06:05:06 +02:00
|
|
|
return fullpath;
|
2004-11-24 00:45:49 +01:00
|
|
|
if (native) {
|
|
|
|
fullpath.appendSuffix("a");
|
|
|
|
} else {
|
|
|
|
fullpath.appendSuffix("bc");
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-11-24 00:45:49 +01:00
|
|
|
return fullpath;
|
2005-07-08 01:21:43 +02:00
|
|
|
fullpath.eraseSuffix();
|
2004-11-24 00:45:49 +01:00
|
|
|
fullpath.appendSuffix("o");
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-11-24 00:45:49 +01:00
|
|
|
return fullpath;
|
|
|
|
fullpath = *PI;
|
2005-07-08 01:21:43 +02:00
|
|
|
fullpath.appendComponent(std::string("lib") + link_item);
|
2004-11-24 00:45:49 +01:00
|
|
|
fullpath.appendSuffix("a");
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-11-24 00:45:49 +01:00
|
|
|
return fullpath;
|
2005-07-08 01:21:43 +02:00
|
|
|
fullpath.eraseSuffix();
|
2004-11-24 00:45:49 +01:00
|
|
|
fullpath.appendSuffix("so");
|
2005-07-07 20:21:42 +02:00
|
|
|
if (fullpath.canRead())
|
2004-11-24 00:45:49 +01:00
|
|
|
return fullpath;
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find one.
|
|
|
|
fullpath.clear();
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method processes a linkage item. The item could be a
|
|
|
|
/// Bytecode file needing translation to native code and that is
|
|
|
|
/// dependent on other bytecode libraries, or a native code
|
|
|
|
/// library that should just be linked into the program.
|
|
|
|
bool ProcessLinkageItem(const llvm::sys::Path& link_item,
|
|
|
|
SetVector<sys::Path>& set,
|
|
|
|
std::string& err) {
|
|
|
|
// First, see if the unadorned file name is not readable. If so,
|
|
|
|
// we must track down the file in the lib search path.
|
|
|
|
sys::Path fullpath;
|
2005-07-07 20:21:42 +02:00
|
|
|
if (!link_item.canRead()) {
|
2004-11-24 00:45:49 +01:00
|
|
|
// look for the library using the -L arguments specified
|
2004-10-28 06:05:06 +02:00
|
|
|
// on the command line.
|
2004-12-11 01:14:15 +01:00
|
|
|
fullpath = GetPathForLinkageItem(link_item.toString());
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// If we didn't find the file in any of the library search paths
|
2004-11-24 00:45:49 +01:00
|
|
|
// we have to bail. No where else to look.
|
2004-11-05 23:15:36 +01:00
|
|
|
if (fullpath.isEmpty()) {
|
2005-04-22 02:00:37 +02:00
|
|
|
err =
|
2004-12-11 01:14:15 +01:00
|
|
|
std::string("Can't find linkage item '") + link_item.toString() + "'";
|
2004-10-28 06:05:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fullpath = link_item;
|
|
|
|
}
|
2004-08-15 10:19:46 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// If we got here fullpath is the path to the file, and its readable.
|
|
|
|
set.insert(fullpath);
|
|
|
|
|
|
|
|
// If its an LLVM bytecode file ...
|
2004-11-05 23:15:36 +01:00
|
|
|
if (fullpath.isBytecodeFile()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
// Process the dependent libraries recursively
|
|
|
|
Module::LibraryListType modlibs;
|
2006-08-25 19:43:11 +02:00
|
|
|
if (GetBytecodeDependentLibraries(fullpath.toString(),modlibs,&err)) {
|
2004-10-28 06:05:06 +02:00
|
|
|
// Traverse the dependent libraries list
|
|
|
|
Module::lib_iterator LI = modlibs.begin();
|
|
|
|
Module::lib_iterator LE = modlibs.end();
|
|
|
|
while ( LI != LE ) {
|
|
|
|
if (!ProcessLinkageItem(sys::Path(*LI),set,err)) {
|
|
|
|
if (err.empty()) {
|
2005-04-22 02:00:37 +02:00
|
|
|
err = std::string("Library '") + *LI +
|
2004-10-28 06:05:06 +02:00
|
|
|
"' is not valid for linking but is required by file '" +
|
2004-12-11 01:14:15 +01:00
|
|
|
fullpath.toString() + "'";
|
2004-10-28 06:05:06 +02:00
|
|
|
} else {
|
2004-12-11 01:14:15 +01:00
|
|
|
err += " which is required by file '" + fullpath.toString() + "'";
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
++LI;
|
2004-08-19 06:49:47 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} else if (err.empty()) {
|
|
|
|
err = std::string(
|
2005-04-22 02:00:37 +02:00
|
|
|
"The dependent libraries could not be extracted from '") +
|
2004-12-11 01:14:15 +01:00
|
|
|
fullpath.toString();
|
2004-10-28 06:05:06 +02:00
|
|
|
return false;
|
2006-08-25 19:43:11 +02:00
|
|
|
} else
|
|
|
|
return false;
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @}
|
|
|
|
/// @name Methods
|
|
|
|
/// @{
|
|
|
|
public:
|
2006-08-21 08:04:45 +02:00
|
|
|
virtual int execute(const InputList& InpList, const sys::Path& Output, std::string& ErrMsg ) {
|
2004-10-28 06:05:06 +02:00
|
|
|
try {
|
|
|
|
// Echo the configuration of options if we're running verbose
|
|
|
|
if (isSet(DEBUG_FLAG)) {
|
|
|
|
std::cerr << "Compiler Driver Options:\n";
|
|
|
|
std::cerr << "DryRun = " << isSet(DRY_RUN_FLAG) << "\n";
|
|
|
|
std::cerr << "Verbose = " << isSet(VERBOSE_FLAG) << " \n";
|
|
|
|
std::cerr << "TimeActions = " << isSet(TIME_ACTIONS_FLAG) << "\n";
|
|
|
|
std::cerr << "TimePasses = " << isSet(TIME_PASSES_FLAG) << "\n";
|
|
|
|
std::cerr << "ShowStats = " << isSet(SHOW_STATS_FLAG) << "\n";
|
|
|
|
std::cerr << "EmitRawCode = " << isSet(EMIT_RAW_FLAG) << "\n";
|
|
|
|
std::cerr << "EmitNativeCode = " << isSet(EMIT_NATIVE_FLAG) << "\n";
|
|
|
|
std::cerr << "KeepTemps = " << isSet(KEEP_TEMPS_FLAG) << "\n";
|
|
|
|
std::cerr << "OutputMachine = " << machine << "\n";
|
|
|
|
InputList::const_iterator I = InpList.begin();
|
|
|
|
while ( I != InpList.end() ) {
|
2005-04-22 02:00:37 +02:00
|
|
|
std::cerr << "Input: " << I->first << "(" << I->second
|
2004-10-28 06:05:06 +02:00
|
|
|
<< ")\n";
|
|
|
|
++I;
|
2004-08-19 06:49:47 +02:00
|
|
|
}
|
2004-12-13 09:53:36 +01:00
|
|
|
std::cerr << "Output: " << Output << "\n";
|
2004-08-15 10:19:46 +02:00
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// If there's no input, we're done.
|
|
|
|
if (InpList.empty())
|
|
|
|
throw std::string("Nothing to compile.");
|
|
|
|
|
|
|
|
// If they are asking for linking and didn't provide an output
|
|
|
|
// file then its an error (no way for us to "make up" a meaningful
|
|
|
|
// file name based on the various linker input files).
|
2004-11-05 23:15:36 +01:00
|
|
|
if (finalPhase == LINKING && Output.isEmpty())
|
2004-10-28 06:05:06 +02:00
|
|
|
throw std::string(
|
|
|
|
"An output file name must be specified for linker output");
|
|
|
|
|
|
|
|
// If they are not asking for linking, provided an output file and
|
|
|
|
// there is more than one input file, its an error
|
2004-11-05 23:15:36 +01:00
|
|
|
if (finalPhase != LINKING && !Output.isEmpty() && InpList.size() > 1)
|
2004-10-28 06:05:06 +02:00
|
|
|
throw std::string("An output file name cannot be specified ") +
|
|
|
|
"with more than one input file name when not linking";
|
|
|
|
|
|
|
|
// This vector holds all the resulting actions of the following loop.
|
|
|
|
std::vector<Action*> actions;
|
|
|
|
|
|
|
|
/// PRE-PROCESSING / TRANSLATION / OPTIMIZATION / ASSEMBLY phases
|
|
|
|
// for each input item
|
|
|
|
SetVector<sys::Path> LinkageItems;
|
2004-12-19 19:00:56 +01:00
|
|
|
StringVector LibFiles;
|
2004-10-28 06:05:06 +02:00
|
|
|
InputList::const_iterator I = InpList.begin();
|
2004-11-20 21:39:33 +01:00
|
|
|
for (InputList::const_iterator I = InpList.begin(), E = InpList.end();
|
|
|
|
I != E; ++I ) {
|
2004-10-28 06:05:06 +02:00
|
|
|
// Get the suffix of the file name
|
|
|
|
const std::string& ftype = I->second;
|
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
// If its a library, bytecode file, or object file, save
|
|
|
|
// it for linking below and short circuit the
|
2004-10-28 06:05:06 +02:00
|
|
|
// pre-processing/translation/assembly phases
|
|
|
|
if (ftype.empty() || ftype == "o" || ftype == "bc" || ftype=="a") {
|
2005-04-22 02:00:37 +02:00
|
|
|
// We shouldn't get any of these types of files unless we're
|
2004-10-28 06:05:06 +02:00
|
|
|
// later going to link. Enforce this limit now.
|
|
|
|
if (finalPhase != LINKING) {
|
|
|
|
throw std::string(
|
|
|
|
"Pre-compiled objects found but linking not requested");
|
2004-08-24 15:55:17 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
if (ftype.empty())
|
2004-12-11 01:14:15 +01:00
|
|
|
LibFiles.push_back(I->first.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
else
|
|
|
|
LinkageItems.insert(I->first);
|
2004-11-20 21:39:33 +01:00
|
|
|
continue; // short circuit remainder of loop
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// At this point, we know its something we need to translate
|
|
|
|
// and/or optimize. See if we can get the configuration data
|
|
|
|
// for this kind of file.
|
|
|
|
ConfigData* cd = cdp->ProvideConfigData(I->second);
|
|
|
|
if (cd == 0)
|
2005-04-22 02:00:37 +02:00
|
|
|
throw std::string("Files of type '") + I->second +
|
|
|
|
"' are not recognized.";
|
2004-10-28 06:05:06 +02:00
|
|
|
if (isSet(DEBUG_FLAG))
|
|
|
|
DumpConfigData(cd,I->second);
|
2004-11-24 00:45:49 +01:00
|
|
|
|
|
|
|
// Add the config data's library paths to the end of the list
|
|
|
|
for (StringVector::iterator LPI = cd->libpaths.begin(),
|
|
|
|
LPE = cd->libpaths.end(); LPI != LPE; ++LPI){
|
|
|
|
LibraryPaths.push_back(sys::Path(*LPI));
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Initialize the input and output files
|
|
|
|
sys::Path InFile(I->first);
|
2004-11-05 23:15:36 +01:00
|
|
|
sys::Path OutFile(I->first.getBasename());
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// PRE-PROCESSING PHASE
|
|
|
|
Action& action = cd->PreProcessor;
|
|
|
|
|
|
|
|
// Get the preprocessing action, if needed, or error if appropriate
|
2004-11-05 23:15:36 +01:00
|
|
|
if (!action.program.isEmpty()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == PREPROCESSING) {
|
|
|
|
if (finalPhase == PREPROCESSING) {
|
2004-11-20 21:39:33 +01:00
|
|
|
if (Output.isEmpty()) {
|
|
|
|
OutFile.appendSuffix("E");
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,PREPROCESSING));
|
|
|
|
} else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,Output,PREPROCESSING));
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} else {
|
2006-08-22 21:01:30 +02:00
|
|
|
sys::Path TempFile(
|
|
|
|
MakeTempFile(I->first.getBasename(),"E",&ErrMsg));
|
|
|
|
if (TempFile.isEmpty())
|
|
|
|
return 1;
|
2004-10-28 06:05:06 +02:00
|
|
|
actions.push_back(GetAction(cd,InFile,TempFile,
|
|
|
|
PREPROCESSING));
|
|
|
|
InFile = TempFile;
|
2004-08-29 21:26:56 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
} else if (finalPhase == PREPROCESSING) {
|
|
|
|
throw cd->langName + " does not support pre-processing";
|
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
2005-04-22 02:00:37 +02:00
|
|
|
throw std::string("Don't know how to pre-process ") +
|
2004-10-28 06:05:06 +02:00
|
|
|
cd->langName + " files";
|
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
// Short-circuit remaining actions if all they want is
|
2004-10-28 06:05:06 +02:00
|
|
|
// pre-processing
|
2004-11-20 21:39:33 +01:00
|
|
|
if (finalPhase == PREPROCESSING) { continue; };
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
/// TRANSLATION PHASE
|
|
|
|
action = cd->Translator;
|
|
|
|
|
|
|
|
// Get the translation action, if needed, or error if appropriate
|
2004-11-05 23:15:36 +01:00
|
|
|
if (!action.program.isEmpty()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == TRANSLATION) {
|
|
|
|
if (finalPhase == TRANSLATION) {
|
2004-11-20 21:39:33 +01:00
|
|
|
if (Output.isEmpty()) {
|
|
|
|
OutFile.appendSuffix("o");
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,TRANSLATION));
|
|
|
|
} else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,Output,TRANSLATION));
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} else {
|
2006-08-22 21:01:30 +02:00
|
|
|
sys::Path TempFile(
|
|
|
|
MakeTempFile(I->first.getBasename(),"trans", &ErrMsg));
|
|
|
|
if (TempFile.isEmpty())
|
|
|
|
return 1;
|
2004-10-28 06:05:06 +02:00
|
|
|
actions.push_back(GetAction(cd,InFile,TempFile,TRANSLATION));
|
|
|
|
InFile = TempFile;
|
2004-08-29 21:26:56 +02:00
|
|
|
}
|
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// ll -> bc Helper
|
|
|
|
if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
|
|
|
|
/// The output of the translator is an LLVM Assembly program
|
|
|
|
/// We need to translate it to bytecode
|
|
|
|
Action* action = new Action();
|
2005-07-08 01:21:43 +02:00
|
|
|
action->program.set("llvm-as");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
action->args.push_back("-o");
|
2004-11-05 23:15:36 +01:00
|
|
|
InFile.appendSuffix("bc");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
actions.push_back(action);
|
2004-08-29 21:26:56 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
|
|
|
} else if (finalPhase == TRANSLATION) {
|
|
|
|
throw cd->langName + " does not support translation";
|
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
2005-04-22 02:00:37 +02:00
|
|
|
throw std::string("Don't know how to translate ") +
|
2004-10-28 06:05:06 +02:00
|
|
|
cd->langName + " files";
|
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Short-circuit remaining actions if all they want is translation
|
2004-11-20 21:39:33 +01:00
|
|
|
if (finalPhase == TRANSLATION) { continue; }
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// OPTIMIZATION PHASE
|
|
|
|
action = cd->Optimizer;
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Get the optimization action, if needed, or error if appropriate
|
|
|
|
if (!isSet(EMIT_RAW_FLAG)) {
|
2004-11-05 23:15:36 +01:00
|
|
|
if (!action.program.isEmpty()) {
|
2004-10-28 06:05:06 +02:00
|
|
|
if (action.isSet(REQUIRED_FLAG) || finalPhase == OPTIMIZATION) {
|
|
|
|
if (finalPhase == OPTIMIZATION) {
|
2004-11-20 21:39:33 +01:00
|
|
|
if (Output.isEmpty()) {
|
|
|
|
OutFile.appendSuffix("o");
|
|
|
|
actions.push_back(GetAction(cd,InFile,OutFile,OPTIMIZATION));
|
|
|
|
} else {
|
|
|
|
actions.push_back(GetAction(cd,InFile,Output,OPTIMIZATION));
|
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
} else {
|
2006-08-22 21:01:30 +02:00
|
|
|
sys::Path TempFile(
|
|
|
|
MakeTempFile(I->first.getBasename(),"opt", &ErrMsg));
|
|
|
|
if (TempFile.isEmpty())
|
|
|
|
return 1;
|
2004-10-28 06:05:06 +02:00
|
|
|
actions.push_back(GetAction(cd,InFile,TempFile,OPTIMIZATION));
|
|
|
|
InFile = TempFile;
|
|
|
|
}
|
|
|
|
// ll -> bc Helper
|
|
|
|
if (action.isSet(OUTPUT_IS_ASM_FLAG)) {
|
|
|
|
/// The output of the optimizer is an LLVM Assembly program
|
|
|
|
/// We need to translate it to bytecode with llvm-as
|
2004-08-29 21:26:56 +02:00
|
|
|
Action* action = new Action();
|
2005-07-08 01:21:43 +02:00
|
|
|
action->program.set("llvm-as");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-08-29 21:26:56 +02:00
|
|
|
action->args.push_back("-f");
|
|
|
|
action->args.push_back("-o");
|
2004-11-05 23:15:36 +01:00
|
|
|
InFile.appendSuffix("bc");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-08-29 21:26:56 +02:00
|
|
|
actions.push_back(action);
|
|
|
|
}
|
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} else if (finalPhase == OPTIMIZATION) {
|
|
|
|
throw cd->langName + " does not support optimization";
|
|
|
|
} else if (action.isSet(REQUIRED_FLAG)) {
|
2005-04-22 02:00:37 +02:00
|
|
|
throw std::string("Don't know how to optimize ") +
|
2004-10-28 06:05:06 +02:00
|
|
|
cd->langName + " files";
|
|
|
|
}
|
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Short-circuit remaining actions if all they want is optimization
|
2004-11-20 21:39:33 +01:00
|
|
|
if (finalPhase == OPTIMIZATION) { continue; }
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
/// ASSEMBLY PHASE
|
|
|
|
action = cd->Assembler;
|
|
|
|
|
|
|
|
if (finalPhase == ASSEMBLY) {
|
2004-11-20 21:39:33 +01:00
|
|
|
|
|
|
|
// Build either a native compilation action or a disassembly action
|
|
|
|
Action* action = new Action();
|
2004-10-28 06:05:06 +02:00
|
|
|
if (isSet(EMIT_NATIVE_FLAG)) {
|
|
|
|
// Use llc to get the native assembly file
|
2005-07-08 01:21:43 +02:00
|
|
|
action->program.set("llc");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
action->args.push_back("-f");
|
|
|
|
action->args.push_back("-o");
|
2004-11-20 21:39:33 +01:00
|
|
|
if (Output.isEmpty()) {
|
|
|
|
OutFile.appendSuffix("o");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(OutFile.toString());
|
2004-11-20 21:39:33 +01:00
|
|
|
} else {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(Output.toString());
|
2004-11-20 21:39:33 +01:00
|
|
|
}
|
|
|
|
actions.push_back(action);
|
2004-10-28 06:05:06 +02:00
|
|
|
} else {
|
|
|
|
// Just convert back to llvm assembly with llvm-dis
|
2005-07-08 01:21:43 +02:00
|
|
|
action->program.set("llvm-dis");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(InFile.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
action->args.push_back("-f");
|
|
|
|
action->args.push_back("-o");
|
2004-11-20 21:39:33 +01:00
|
|
|
if (Output.isEmpty()) {
|
|
|
|
OutFile.appendSuffix("ll");
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(OutFile.toString());
|
2004-11-20 21:39:33 +01:00
|
|
|
} else {
|
2004-12-11 01:14:15 +01:00
|
|
|
action->args.push_back(Output.toString());
|
2004-11-20 21:39:33 +01:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
}
|
2004-08-29 21:26:56 +02:00
|
|
|
|
2004-11-20 21:39:33 +01:00
|
|
|
// Put the action on the list
|
|
|
|
actions.push_back(action);
|
|
|
|
|
2005-04-22 02:00:37 +02:00
|
|
|
// Short circuit the rest of the loop, we don't want to link
|
2004-10-28 06:05:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
2004-08-19 06:49:47 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Register the result of the actions as a link candidate
|
|
|
|
LinkageItems.insert(InFile);
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
} // end while loop over each input file
|
2004-08-20 11:14:05 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// RUN THE COMPILATION ACTIONS
|
|
|
|
std::vector<Action*>::iterator AI = actions.begin();
|
|
|
|
std::vector<Action*>::iterator AE = actions.end();
|
|
|
|
while (AI != AE) {
|
2006-08-21 08:04:45 +02:00
|
|
|
int ActionResult = DoAction(*AI, ErrMsg);
|
|
|
|
if (ActionResult != 0)
|
|
|
|
return ActionResult;
|
2004-10-28 06:05:06 +02:00
|
|
|
AI++;
|
|
|
|
}
|
2004-09-14 03:58:45 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// LINKING PHASE
|
|
|
|
if (finalPhase == LINKING) {
|
2004-09-14 03:58:45 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
// Insert the platform-specific system libraries to the path list
|
2004-12-13 04:01:26 +01:00
|
|
|
std::vector<sys::Path> SysLibs;
|
|
|
|
sys::Path::GetSystemLibraryPaths(SysLibs);
|
|
|
|
LibraryPaths.insert(LibraryPaths.end(), SysLibs.begin(), SysLibs.end());
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Set up the linking action with llvm-ld
|
|
|
|
Action* link = new Action();
|
2005-07-08 01:21:43 +02:00
|
|
|
link->program.set("llvm-ld");
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Add in the optimization level requested
|
|
|
|
switch (optLevel) {
|
|
|
|
case OPT_FAST_COMPILE:
|
|
|
|
link->args.push_back("-O1");
|
|
|
|
break;
|
|
|
|
case OPT_SIMPLE:
|
|
|
|
link->args.push_back("-O2");
|
|
|
|
break;
|
|
|
|
case OPT_AGGRESSIVE:
|
|
|
|
link->args.push_back("-O3");
|
|
|
|
break;
|
|
|
|
case OPT_LINK_TIME:
|
|
|
|
link->args.push_back("-O4");
|
|
|
|
break;
|
|
|
|
case OPT_AGGRESSIVE_LINK_TIME:
|
|
|
|
link->args.push_back("-O5");
|
|
|
|
break;
|
|
|
|
case OPT_NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add in all the linkage items we generated. This includes the
|
|
|
|
// output from the translation/optimization phases as well as any
|
|
|
|
// -l arguments specified.
|
2005-04-22 02:00:37 +02:00
|
|
|
for (PathVector::const_iterator I=LinkageItems.begin(),
|
2004-10-28 06:05:06 +02:00
|
|
|
E=LinkageItems.end(); I != E; ++I )
|
2004-12-11 01:14:15 +01:00
|
|
|
link->args.push_back(I->toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Add in all the libraries we found.
|
2004-12-19 19:00:56 +01:00
|
|
|
for (StringVector::const_iterator I=LibFiles.begin(),
|
2004-10-28 06:05:06 +02:00
|
|
|
E=LibFiles.end(); I != E; ++I )
|
|
|
|
link->args.push_back(std::string("-l")+*I);
|
|
|
|
|
|
|
|
// Add in all the library paths to the command line
|
|
|
|
for (PathVector::const_iterator I=LibraryPaths.begin(),
|
|
|
|
E=LibraryPaths.end(); I != E; ++I)
|
2004-12-11 01:14:15 +01:00
|
|
|
link->args.push_back( std::string("-L") + I->toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Add in the additional linker arguments requested
|
|
|
|
for (StringVector::const_iterator I=AdditionalArgs[LINKING].begin(),
|
|
|
|
E=AdditionalArgs[LINKING].end(); I != E; ++I)
|
|
|
|
link->args.push_back( *I );
|
|
|
|
|
|
|
|
// Add in other optional flags
|
|
|
|
if (isSet(EMIT_NATIVE_FLAG))
|
|
|
|
link->args.push_back("-native");
|
|
|
|
if (isSet(VERBOSE_FLAG))
|
|
|
|
link->args.push_back("-v");
|
|
|
|
if (isSet(TIME_PASSES_FLAG))
|
|
|
|
link->args.push_back("-time-passes");
|
|
|
|
if (isSet(SHOW_STATS_FLAG))
|
|
|
|
link->args.push_back("-stats");
|
|
|
|
if (isSet(STRIP_OUTPUT_FLAG))
|
|
|
|
link->args.push_back("-s");
|
|
|
|
if (isSet(DEBUG_FLAG)) {
|
|
|
|
link->args.push_back("-debug");
|
|
|
|
link->args.push_back("-debug-pass=Details");
|
2004-08-20 11:14:05 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Add in mandatory flags
|
|
|
|
link->args.push_back("-o");
|
2004-12-11 01:14:15 +01:00
|
|
|
link->args.push_back(Output.toString());
|
2004-10-28 06:05:06 +02:00
|
|
|
|
|
|
|
// Execute the link
|
2006-08-21 08:04:45 +02:00
|
|
|
int ActionResult = DoAction(link, ErrMsg);
|
|
|
|
if (ActionResult != 0)
|
|
|
|
return ActionResult;
|
2004-08-20 11:14:05 +02:00
|
|
|
}
|
2004-10-28 06:05:06 +02:00
|
|
|
} catch (std::string& msg) {
|
|
|
|
cleanup();
|
|
|
|
throw;
|
|
|
|
} catch (...) {
|
|
|
|
cleanup();
|
|
|
|
throw std::string("Unspecified error");
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-25 00:54:32 +02:00
|
|
|
|
2004-10-28 06:05:06 +02:00
|
|
|
/// @}
|
|
|
|
/// @name Data
|
|
|
|
/// @{
|
|
|
|
private:
|
|
|
|
ConfigDataProvider* cdp; ///< Where we get configuration data from
|
|
|
|
Phases finalPhase; ///< The final phase of compilation
|
|
|
|
OptimizationLevels optLevel; ///< The optimization level to apply
|
|
|
|
unsigned Flags; ///< The driver flags
|
|
|
|
std::string machine; ///< Target machine name
|
|
|
|
PathVector LibraryPaths; ///< -L options
|
|
|
|
PathVector IncludePaths; ///< -I options
|
2004-11-05 23:15:36 +01:00
|
|
|
PathVector ToolPaths; ///< -B options
|
2004-10-28 06:05:06 +02:00
|
|
|
StringVector Defines; ///< -D options
|
|
|
|
sys::Path TempDir; ///< Name of the temporary directory.
|
|
|
|
StringTable AdditionalArgs; ///< The -Txyz options
|
|
|
|
StringVector fOptions; ///< -f options
|
|
|
|
StringVector MOptions; ///< -M options
|
|
|
|
StringVector WOptions; ///< -W options
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
};
|
2004-08-29 21:26:56 +02:00
|
|
|
}
|
2004-08-13 22:22:43 +02:00
|
|
|
|
2004-08-29 21:26:56 +02:00
|
|
|
CompilerDriver::~CompilerDriver() {
|
|
|
|
}
|
2004-08-16 09:06:38 +02:00
|
|
|
|
2005-05-19 02:52:28 +02:00
|
|
|
CompilerDriver::ConfigDataProvider::~ConfigDataProvider() {}
|
|
|
|
|
2004-08-29 21:26:56 +02:00
|
|
|
CompilerDriver*
|
|
|
|
CompilerDriver::Get(ConfigDataProvider& CDP) {
|
|
|
|
return new CompilerDriverImpl(CDP);
|
|
|
|
}
|
2004-08-15 10:19:46 +02:00
|
|
|
|
2004-08-29 21:26:56 +02:00
|
|
|
CompilerDriver::ConfigData::ConfigData()
|
|
|
|
: langName()
|
|
|
|
, PreProcessor()
|
|
|
|
, Translator()
|
|
|
|
, Optimizer()
|
|
|
|
, Assembler()
|
|
|
|
, Linker()
|
|
|
|
{
|
|
|
|
StringVector emptyVec;
|
|
|
|
for (unsigned i = 0; i < NUM_PHASES; ++i)
|
|
|
|
opts.push_back(emptyVec);
|
2004-08-13 22:22:43 +02:00
|
|
|
}
|