Skip to content

Commit

Permalink
WIP: add feature parsing to command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
sierdzio committed Jun 22, 2018
1 parent 14c61d2 commit 877e872
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ To define a feature, use this syntax:

Then you can select the feature next time you build your project, like this:

gibs main.cpp --tts-support
gibs main.cpp -- --tts-support

Or unselect it using:

gibs main.cpp --no-tts-support
gibs main.cpp -- --no-tts-support

# Recommendations

Expand Down
36 changes: 36 additions & 0 deletions gibs/src/gibs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "gibs.h"

#include <QDebug>

void Gibs::removeFile(const QString &path) {
if (QFile::exists(path)) {
qInfo() << "Removing:" << path;
Expand All @@ -17,3 +19,37 @@ QString Gibs::normalizeFeatureName(const QString &name)
result = result.replace('-', '_');
return result;
}

/*!
* Converts a \a command line flag into Feature and returns it.
*
* For example:
* $ gibs main.cpp -- --tts-support --no-opengl
*
* If you call this function for boths flags, it will enable TTS support, but
* keep OpenGL turned off.
*/
Gibs::Feature Gibs::commandLineToFeature(const QString &command)
{
Gibs::Feature result;
const QString negative("--no-");

if (command.length() < 2 or !command.startsWith("--")) {
qFatal("Invalid feature name: %s", qPrintable(command));
}

if (command.startsWith(negative)) {
result.name = command.mid(negative.length());
result.enabled = false;
} else {
// Cut '--' from beginning of the string:
result.name = command.mid(2);
result.enabled = true;
}

result.define = Gibs::normalizeFeatureName(result.name);
// result.defined is intentionally left out. Scope is responsible for setting
// it.

return result;
}
8 changes: 8 additions & 0 deletions gibs/src/gibs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include <QDebug>

namespace Gibs {
struct Feature {
QString name;
QString define;
bool defined = false;
bool enabled = false;
};

void removeFile(const QString &path);
QString normalizeFeatureName(const QString &name);
Feature commandLineToFeature(const QString &command);
}
13 changes: 13 additions & 0 deletions gibs/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,22 @@ int main(int argc, char *argv[]) {
qInfo() << "Maximum number of jobs:" << flags.jobs();
}

// Extract features:
if (args.length() > 1) {
const auto commands = args.mid(1);
qDebug() << "Features:" << commands;
QHash<QString, Gibs::Feature> features;
for (const auto &command: commands) {
const auto &feature = Gibs::commandLineToFeature(command);
features.insert(feature.name, feature);
qDebug() << "Feature:" << feature.name << feature.define << feature.defined << feature.enabled;
}
}

ProjectManager manager(flags);
manager.loadCache();
manager.loadCommands();
// TODO: pass features to manager
QObject::connect(&manager, &ProjectManager::finished, &app, &QCoreApplication::exit);

if (flags.clean()) {
Expand Down
18 changes: 17 additions & 1 deletion gibs/src/scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,23 @@ void Scope::onRunTool(const QString &tool, const QStringList &args)

void Scope::onFeature(const QString &name, const bool isOn)
{
if (isOn) {
Gibs::Feature result;
if (mFeatures.contains(name)) {
auto feat = mFeatures.value(name);
feat.defined = true;
mFeatures.insert(name, feat);
result = feat;
} else {
Gibs::Feature feat;
feat.enabled = isOn;
feat.defined = true;
feat.name = name;
feat.define = Gibs::normalizeFeatureName(name);
result = feat;
emit feature(name, isOn);
}

if (result.enabled) {
addDefines(QStringList {name});
}
}
Expand Down
3 changes: 3 additions & 0 deletions gibs/src/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "fileinfo.h"
#include "tags.h"
#include "metaprocess.h"
#include "gibs.h"

class Scope;

Expand Down Expand Up @@ -149,6 +150,8 @@ protected slots:
QStringList mCustomIncludes;
QStringList mCustomIncludeFlags;
QHash<QString, FileInfo> mParsedFiles;
// Name, Feature
QHash<QString, Gibs::Feature> mFeatures;
QVector<QByteArray> mScopeDependencyIds;
QVector<ScopePtr> mScopeDependencies;
// TODO: change into QStringList and use only file names here.
Expand Down

0 comments on commit 877e872

Please sign in to comment.