Rename moc -> generator

This commit is contained in:
Martchus 2017-10-25 17:41:19 +02:00
parent 3889031247
commit b725f59e78
22 changed files with 108 additions and 108 deletions

View File

@ -33,4 +33,4 @@ add_subdirectory(lib)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/modules" "${CMAKE_MODULE_PATH}") set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/lib/cmake/modules" "${CMAKE_MODULE_PATH}")
# add code generator # add code generator
add_subdirectory(moc) add_subdirectory(generator)

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
# metadata # metadata
set(META_PROJECT_NAME reflective_rapidjson_moc) set(META_PROJECT_NAME reflective_rapidjson_generator)
set(META_PROJECT_TYPE application) set(META_PROJECT_TYPE application)
set(LINK_TESTS_AGAINST_APP_TARGET ON) set(LINK_TESTS_AGAINST_APP_TARGET ON)

View File

@ -1,105 +1,105 @@
#include "./codefactory.h" #include "./codefactory.h"
#include "resources/config.h" #include "resources/config.h"
#include <c++utilities/application/argumentparser.h> #include <c++utilities/application/argumentparser.h>
#include <c++utilities/application/commandlineutils.h> #include <c++utilities/application/commandlineutils.h>
#include <c++utilities/application/failure.h> #include <c++utilities/application/failure.h>
#include <c++utilities/io/ansiescapecodes.h> #include <c++utilities/io/ansiescapecodes.h>
#include <c++utilities/io/catchiofailure.h> #include <c++utilities/io/catchiofailure.h>
#include <c++utilities/io/misc.h> #include <c++utilities/io/misc.h>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
using namespace ApplicationUtilities; using namespace ApplicationUtilities;
using namespace EscapeCodes; using namespace EscapeCodes;
using namespace IoUtilities; using namespace IoUtilities;
using namespace ReflectiveRapidJSON; using namespace ReflectiveRapidJSON;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
SET_APPLICATION_INFO; SET_APPLICATION_INFO;
CMD_UTILS_CONVERT_ARGS_TO_UTF8; CMD_UTILS_CONVERT_ARGS_TO_UTF8;
// setup argument parser // setup argument parser
ArgumentParser parser; ArgumentParser parser;
ConfigValueArgument inputFileArg("input-file", 'i', "specifies the input file", { "path" }); ConfigValueArgument inputFileArg("input-file", 'i', "specifies the input file", { "path" });
inputFileArg.setRequired(true); inputFileArg.setRequired(true);
ConfigValueArgument outputFileArg("output-file", 'o', "specifies the output file", { "path" }); ConfigValueArgument outputFileArg("output-file", 'o', "specifies the output file", { "path" });
Argument generatorsArg("generators", 'g', "specifies the generators (by default all generators are enabled)"); Argument generatorsArg("generators", 'g', "specifies the generators (by default all generators are enabled)");
generatorsArg.setValueNames({ "json" }); generatorsArg.setValueNames({ "json" });
generatorsArg.setPreDefinedCompletionValues("json"); generatorsArg.setPreDefinedCompletionValues("json");
generatorsArg.setRequiredValueCount(Argument::varValueCount); generatorsArg.setRequiredValueCount(Argument::varValueCount);
generatorsArg.setCombinable(true); generatorsArg.setCombinable(true);
ConfigValueArgument clangOptionsArg("clang-opt", 'c', "specifies an argument to be passed to Clang", { "option" }); ConfigValueArgument clangOptionsArg("clang-opt", 'c', "specifies an argument to be passed to Clang", { "option" });
HelpArgument helpArg(parser); HelpArgument helpArg(parser);
NoColorArgument noColorArg; NoColorArgument noColorArg;
parser.setMainArguments({ &inputFileArg, &outputFileArg, &generatorsArg, &clangOptionsArg, &noColorArg, &helpArg }); parser.setMainArguments({ &inputFileArg, &outputFileArg, &generatorsArg, &clangOptionsArg, &noColorArg, &helpArg });
// parse arguments // parse arguments
parser.parseArgsOrExit(argc, argv); parser.parseArgsOrExit(argc, argv);
if (helpArg.isPresent()) { if (helpArg.isPresent()) {
return 0; return 0;
} }
// setup output stream // setup output stream
ostream *os = nullptr; ostream *os = nullptr;
try { try {
ofstream outputFile; ofstream outputFile;
if (outputFileArg.isPresent()) { if (outputFileArg.isPresent()) {
outputFile.exceptions(ios_base::badbit | ios_base::failbit); outputFile.exceptions(ios_base::badbit | ios_base::failbit);
outputFile.open(outputFileArg.values(0).front(), ios_base::out | ios_base::trunc | ios_base::binary); outputFile.open(outputFileArg.values(0).front(), ios_base::out | ios_base::trunc | ios_base::binary);
os = &outputFile; os = &outputFile;
} else { } else {
os = &cout; os = &cout;
} }
// configure code generator // configure code generator
vector<const char *> defaultClangOptions; vector<const char *> defaultClangOptions;
CodeFactory factory( CodeFactory factory(
parser.executable(), inputFileArg.values(0), clangOptionsArg.isPresent() ? clangOptionsArg.values(0) : defaultClangOptions, *os); parser.executable(), inputFileArg.values(0), clangOptionsArg.isPresent() ? clangOptionsArg.values(0) : defaultClangOptions, *os);
// add only specified generators if the --generator argument is present // add only specified generators if the --generator argument is present
if (generatorsArg.isPresent()) { if (generatorsArg.isPresent()) {
// find and construct generators by name // find and construct generators by name
for (const char *generatorName : generatorsArg.values(0)) { for (const char *generatorName : generatorsArg.values(0)) {
if (!strcmp(generatorName, "json")) { if (!strcmp(generatorName, "json")) {
factory.addGenerator<JSONSerializationCodeGenerator>(); factory.addGenerator<JSONSerializationCodeGenerator>();
} else { } else {
cerr << Phrases::Error << "The specified generator \"" << generatorName << "\" does not exist." << Phrases::EndFlush; cerr << Phrases::Error << "The specified generator \"" << generatorName << "\" does not exist." << Phrases::EndFlush;
return -5; return -5;
} }
} }
} else { } else {
// add default generators // add default generators
factory.addGenerator<JSONSerializationCodeGenerator>(); factory.addGenerator<JSONSerializationCodeGenerator>();
} }
// read AST elements from input files // read AST elements from input files
if (!factory.readAST()) { if (!factory.readAST()) {
cerr << Phrases::Error << "Errors occured when parsing the input file." << Phrases::EndFlush; cerr << Phrases::Error << "Errors occured when parsing the input file." << Phrases::EndFlush;
return -2; return -2;
} }
// run the code generator // run the code generator
if (!factory.generate()) { if (!factory.generate()) {
cerr << Phrases::Error << "Errors occured when during code generation." << Phrases::EndFlush; cerr << Phrases::Error << "Errors occured when during code generation." << Phrases::EndFlush;
return -3; return -3;
} }
} catch (...) { } catch (...) {
catchIoFailure(); catchIoFailure();
const char *errorMessage; const char *errorMessage;
if (os) { if (os) {
errorMessage = os->fail() || os->bad() ? "An IO error occured when writing to the output stream." : "An IO error occured."; errorMessage = os->fail() || os->bad() ? "An IO error occured when writing to the output stream." : "An IO error occured.";
} else { } else {
errorMessage = "An IO error when opening output stream."; errorMessage = "An IO error when opening output stream.";
} }
cerr << Phrases::Error << errorMessage << Phrases::EndFlush; cerr << Phrases::Error << errorMessage << Phrases::EndFlush;
return -4; return -4;
} }
return 0; return 0;
} }

View File

@ -7,7 +7,7 @@ endif()
set(REFLECTION_GENERATOR_MODULE_LOADED YES) set(REFLECTION_GENERATOR_MODULE_LOADED YES)
# find code generator # find code generator
set(REFLECTION_GENERATOR_EXECUTABLE reflective_rapidjson_moc) set(REFLECTION_GENERATOR_EXECUTABLE reflective_rapidjson_generator)
if(CMAKE_CROSSCOMPILING OR NOT TARGET "${REFLECTION_GENERATOR_EXECUTABLE}") if(CMAKE_CROSSCOMPILING OR NOT TARGET "${REFLECTION_GENERATOR_EXECUTABLE}")
# find "reflective_rapidjson_moc" from path # find "reflective_rapidjson_moc" from path
find_program(REFLECTION_GENERATOR_EXECUTABLE "${REFLECTION_GENERATOR_EXECUTABLE}") find_program(REFLECTION_GENERATOR_EXECUTABLE "${REFLECTION_GENERATOR_EXECUTABLE}")