1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
// Copyright (C) 2017-2020 Ford Motor Company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <qcommandlineoption.h>
#include <qcommandlineparser.h>
#include <qcoreapplication.h>
#include <qfileinfo.h>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qjsonarray.h>
#include <qhashfunctions.h>
#include "cppcodegenerator.h"
#include "repcodegenerator.h"
#include "repparser.h"
#include "utils.h"
#include <cstdio>
#define PROGRAM_NAME "repc"
#define REPC_VERSION "1.0.0"
enum Mode {
InRep = 1,
InJson = 2,
OutRep = 4,
OutSource = 8,
OutReplica = 16,
OutMerged = OutSource | OutReplica
};
static const QLatin1String REP("rep");
QT_USE_NAMESPACE
int main(int argc, char **argv)
{
QHashSeed::setDeterministicGlobalSeed();
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationVersion(QString::fromLatin1(REPC_VERSION));
QString outputFile;
QString inputFile;
int mode = 0;
QCommandLineParser parser;
parser.setApplicationDescription(QStringLiteral("repc tool v%1 (Qt %2).\n")
.arg(QStringLiteral(REPC_VERSION), QString::fromLatin1(QT_VERSION_STR)));
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption inputTypeOption(QStringLiteral("i"));
inputTypeOption.setDescription(QLatin1String("Input file type:\n"
"rep: replicant template files.\n"
"json: JSON output from moc of a Qt header file."));
inputTypeOption.setValueName(QStringLiteral("rep|json"));
parser.addOption(inputTypeOption);
QCommandLineOption outputTypeOption(QStringLiteral("o"));
outputTypeOption.setDescription(QLatin1String("Output file type:\n"
"source: generates source header. Is incompatible with \"-i src\" option.\n"
"replica: generates replica header.\n"
"merged: generates combined replica/source header.\n"
"rep: generates replicant template file from C++ QOject classes. Is not compatible with \"-i rep\" option."));
outputTypeOption.setValueName(QStringLiteral("source|replica|merged|rep"));
parser.addOption(outputTypeOption);
QCommandLineOption includePathOption(QStringLiteral("I"));
includePathOption.setDescription(QStringLiteral("Add dir to the include path for header files. This parameter is needed only if the input file type is src (.h file)."));
includePathOption.setValueName(QStringLiteral("dir"));
parser.addOption(includePathOption);
QCommandLineOption alwaysClassOption(QStringLiteral("c"));
alwaysClassOption.setDescription(QStringLiteral("Always output `class` type for .rep files and never `POD`."));
parser.addOption(alwaysClassOption);
QCommandLineOption debugOption(QStringLiteral("d"));
debugOption.setDescription(QStringLiteral("Print out parsing debug information (for troubleshooting)."));
parser.addOption(debugOption);
parser.addPositionalArgument(QStringLiteral("[json-file/rep-file]"),
QStringLiteral("Input json/rep file to read from, otherwise stdin."));
parser.addPositionalArgument(QStringLiteral("[rep-file/header-file]"),
QStringLiteral("Output header/rep file to write to, otherwise stdout."));
parser.process(app.arguments());
const QStringList files = parser.positionalArguments();
if (files.size() > 2) {
fprintf(stderr, "%s", qPrintable(QLatin1String(PROGRAM_NAME ": Too many input, output files specified: '") + files.join(QStringLiteral("' '")) + QStringLiteral("\'.\n")));
parser.showHelp(1);
}
if (parser.isSet(inputTypeOption)) {
const QString &inputType = parser.value(inputTypeOption);
if (inputType == REP)
mode = InRep;
else if (inputType == u"json")
mode = InJson;
else {
fprintf(stderr, PROGRAM_NAME ": Unknown input type\"%s\".\n", qPrintable(inputType));
parser.showHelp(1);
}
}
if (parser.isSet(outputTypeOption)) {
const QString &outputType = parser.value(outputTypeOption);
if (outputType == REP)
mode |= OutRep;
else if (outputType == u"replica")
mode |= OutReplica;
else if (outputType == u"source")
mode |= OutSource;
else if (outputType == u"merged")
mode |= OutMerged;
else {
fprintf(stderr, PROGRAM_NAME ": Unknown output type\"%s\".\n", qPrintable(outputType));
parser.showHelp(1);
}
}
switch (files.size()) {
case 2:
outputFile = files.last();
if (!(mode & (OutRep | OutSource | OutReplica))) {
// try to figure out the Out mode from file extension
if (outputFile.endsWith(QLatin1String(".rep")))
mode |= OutRep;
}
Q_FALLTHROUGH();
case 1:
inputFile = files.first();
if (!(mode & (InRep | InJson))) {
// try to figure out the In mode from file extension
if (inputFile.endsWith(QLatin1String(".rep")))
mode |= InRep;
else
mode |= InJson;
}
break;
}
// check mode sanity
if (!(mode & (InRep | InJson))) {
fprintf(stderr, PROGRAM_NAME ": Unknown input type, please use -i option to specify one.\n");
parser.showHelp(1);
}
if (!(mode & (OutRep | OutSource | OutReplica))) {
fprintf(stderr, PROGRAM_NAME ": Unknown output type, please use -o option to specify one.\n");
parser.showHelp(1);
}
if (mode & InRep && mode & OutRep) {
fprintf(stderr, PROGRAM_NAME ": Invalid input/output type combination, both are rep files.\n");
parser.showHelp(1);
}
if (mode & InJson && mode & OutSource) {
fprintf(stderr, PROGRAM_NAME ": Invalid input/output type combination, both are source header files.\n");
parser.showHelp(1);
}
QFile input;
if (inputFile.isEmpty()) {
inputFile = QStringLiteral("<stdin>");
input.open(stdin, QIODevice::ReadOnly);
} else {
input.setFileName(inputFile);
if (!input.open(QIODevice::ReadOnly)) {
fprintf(stderr, PROGRAM_NAME ": %s: No such file.\n", qPrintable(inputFile));
return 1;
}
}
QFile output;
if (outputFile.isEmpty()) {
output.open(stdout, QIODevice::WriteOnly);
} else {
output.setFileName(outputFile);
if (!output.open(QIODevice::WriteOnly)) {
fprintf(stderr, PROGRAM_NAME ": could not open output file '%s': %s.\n",
qPrintable(outputFile), qPrintable(output.errorString()));
return 1;
}
}
if (mode & InJson) {
QJsonDocument doc(QJsonDocument::fromJson(input.readAll()));
input.close();
if (!doc.isObject()) {
fprintf(stderr, PROGRAM_NAME ": Unable to read json input.\n");
return 0;
}
QJsonObject json = doc.object();
if (!json.contains(QLatin1String("classes")) || !json[QLatin1String("classes")].isArray()) {
fprintf(stderr, PROGRAM_NAME ": No QObject classes found.\n");
return 0;
}
QJsonArray classes = json[QLatin1String("classes")].toArray();
if (mode & OutRep) {
CppCodeGenerator generator(&output);
generator.generate(classes, parser.isSet(alwaysClassOption));
} else {
Q_ASSERT(mode & OutReplica);
RepCodeGenerator generator(&output, classList2AST(classes));
generator.generate(RepCodeGenerator::REPLICA, outputFile);
}
} else {
Q_ASSERT(!(mode & OutRep));
RepParser repparser(input);
if (parser.isSet(debugOption))
repparser.setDebug();
if (!repparser.parse()) {
fprintf(stderr, PROGRAM_NAME ": %s:%d: error: %s\n", qPrintable(inputFile), repparser.lineNumber(), qPrintable(repparser.errorString()));
// if everything is okay and only the input was malformed => remove the output file
// let's not create an empty file -- make sure the build system tries to run repc again
// this is the same behavior other code generators exhibit (e.g. flex)
output.remove();
return 1;
}
input.close();
RepCodeGenerator generator(&output, repparser.ast());
if ((mode & OutMerged) == OutMerged)
generator.generate(RepCodeGenerator::MERGED, outputFile);
else if (mode & OutReplica)
generator.generate(RepCodeGenerator::REPLICA, outputFile);
else if (mode & OutSource)
generator.generate(RepCodeGenerator::SOURCE, outputFile);
else {
fprintf(stderr, PROGRAM_NAME ": Unknown mode.\n");
return 1;
}
}
output.close();
return 0;
}
|