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