2011-04-01 03:55:00 +04:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
|
|
|
|
class CompileCommandParser {
|
|
|
|
public:
|
2011-05-16 20:05:49 +04:00
|
|
|
class CommandType: public std::map<cmStdString, cmStdString>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmStdString const& at(cmStdString const& k) const
|
|
|
|
{
|
|
|
|
const_iterator i = this->find(k);
|
|
|
|
if(i != this->end()) { return i->second; }
|
2011-05-17 17:25:44 +04:00
|
|
|
static cmStdString emptyString;
|
|
|
|
return emptyString;
|
2011-05-16 20:05:49 +04:00
|
|
|
}
|
|
|
|
};
|
2011-04-01 03:55:00 +04:00
|
|
|
typedef std::vector<CommandType> TranslationUnitsType;
|
|
|
|
|
|
|
|
CompileCommandParser(std::ifstream *input)
|
|
|
|
{
|
|
|
|
this->Input = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parse()
|
|
|
|
{
|
|
|
|
NextNonWhitespace();
|
|
|
|
ParseTranslationUnits();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TranslationUnitsType& GetTranslationUnits()
|
|
|
|
{
|
|
|
|
return this->TranslationUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void ParseTranslationUnits()
|
|
|
|
{
|
|
|
|
this->TranslationUnits = TranslationUnitsType();
|
2012-05-10 00:55:51 +04:00
|
|
|
ExpectOrDie('[', "at start of compile command file\n");
|
2011-04-01 03:55:00 +04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ParseTranslationUnit();
|
|
|
|
this->TranslationUnits.push_back(this->Command);
|
|
|
|
} while(Expect(','));
|
|
|
|
ExpectOrDie(']', "at end of array");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseTranslationUnit()
|
|
|
|
{
|
|
|
|
this->Command = CommandType();
|
|
|
|
if(!Expect('{')) return;
|
|
|
|
if(Expect('}')) return;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ParseString();
|
|
|
|
std::string name = this->String;
|
|
|
|
ExpectOrDie(':', "between name and value");
|
|
|
|
ParseString();
|
|
|
|
std::string value = this->String;
|
|
|
|
this->Command[name] = value;
|
|
|
|
} while(Expect(','));
|
|
|
|
ExpectOrDie('}', "at end of object");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseString()
|
|
|
|
{
|
|
|
|
this->String.clear();
|
|
|
|
if(!Expect('"')) return;
|
|
|
|
while (!Expect('"'))
|
|
|
|
{
|
|
|
|
Expect('\\');
|
|
|
|
this->String.push_back(C);
|
|
|
|
Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expect(char c)
|
|
|
|
{
|
|
|
|
if(this->C == c)
|
|
|
|
{
|
|
|
|
NextNonWhitespace();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectOrDie(char c, const std::string & message)
|
|
|
|
{
|
|
|
|
if (!Expect(c))
|
|
|
|
ErrorExit(std::string("'") + c + "' expected " + message + ".");
|
|
|
|
}
|
|
|
|
|
|
|
|
void NextNonWhitespace()
|
|
|
|
{
|
|
|
|
do { Next(); } while (IsWhitespace());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Next()
|
|
|
|
{
|
2011-05-19 16:02:21 +04:00
|
|
|
this->C = char(Input->get());
|
2011-04-01 03:55:00 +04:00
|
|
|
if (this->Input->bad()) ErrorExit("Unexpected end of file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ErrorExit(const std::string &message) {
|
|
|
|
std::cout << "ERROR: " << message;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWhitespace()
|
|
|
|
{
|
|
|
|
return (this->C == ' ' || this->C == '\t' ||
|
|
|
|
this->C == '\n' || this->C == '\r');
|
|
|
|
}
|
|
|
|
|
|
|
|
char C;
|
|
|
|
TranslationUnitsType TranslationUnits;
|
|
|
|
CommandType Command;
|
|
|
|
std::string String;
|
|
|
|
std::ifstream *Input;
|
|
|
|
};
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
std::ifstream file("compile_commands.json");
|
|
|
|
CompileCommandParser parser(&file);
|
|
|
|
parser.Parse();
|
|
|
|
for(CompileCommandParser::TranslationUnitsType::const_iterator
|
|
|
|
it = parser.GetTranslationUnits().begin(),
|
|
|
|
end = parser.GetTranslationUnits().end(); it != end; ++it)
|
|
|
|
{
|
2011-05-18 17:44:28 +04:00
|
|
|
std::vector<cmStdString> command;
|
|
|
|
cmSystemTools::ParseUnixCommandLine(it->at("command").c_str(), command);
|
2011-04-01 03:55:00 +04:00
|
|
|
if (!cmSystemTools::RunSingleCommand(
|
|
|
|
command, 0, 0, it->at("directory").c_str()))
|
|
|
|
{
|
|
|
|
std::cout << "ERROR: Failed to run command \""
|
|
|
|
<< command[0] << "\"" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|