Skip to content
This repository has been archived by the owner on Jan 23, 2019. It is now read-only.

Commit

Permalink
support multiple hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Skycoder42 committed Oct 13, 2017
1 parent c4198bb commit daf2670
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
20 changes: 19 additions & 1 deletion qpmx/compilecommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "topsort.h"

#include <QCryptographicHash>
#include <QDirIterator>
#include <QProcess>
#include <QQueue>
#include <QStandardPaths>
Expand Down Expand Up @@ -399,7 +400,7 @@ void CompileCommand::priGen()
<< "\telse:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/lib/" << libName << "d.lib\n"
<< "\telse:unix: PRE_TARGETDEPS += $$PWD/lib/lib" << libName << ".a\n\n";
//add startup hook (if needed)
auto hooks = readVar(_compileDir->filePath(QStringLiteral(".qpmx_startup_hooks")));
auto hooks = readMultiVar(_compileDir->filePath(QStringLiteral(".qpmx_startup_hooks")));
if(!hooks.isEmpty())
stream << "\tQPMX_STARTUP_HOOKS += \"" << hooks.join(QStringLiteral("\" \"")) << "\"\n";

Expand Down Expand Up @@ -484,6 +485,23 @@ QString CompileCommand::findMake()
return make;
}

QStringList CompileCommand::readMultiVar(const QString &dirName, bool recursive)
{
QDir dir(dirName);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
QDirIterator iter(dir,
recursive ?
(QDirIterator::Subdirectories | QDirIterator::FollowSymlinks) :
QDirIterator::NoIteratorFlags);

QStringList resList;
while(iter.hasNext()) {
iter.next();
resList.append(readVar(iter.filePath()));
}
return resList;
}

QStringList CompileCommand::readVar(const QString &fileName)
{
QFile file(fileName);
Expand Down
1 change: 1 addition & 0 deletions qpmx/compilecommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private slots:
QString stage();
void depCollect();
QString findMake();
QStringList readMultiVar(const QString &dirName, bool recursive = false);
QStringList readVar(const QString &fileName);
void initProcess();
#ifndef QPMX_NO_MAKEBUG
Expand Down
27 changes: 10 additions & 17 deletions qpmx/hookcommand.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hookcommand.h"

#include <QCryptographicHash>
#include <QLockFile>
using namespace qpmx;

HookCommand::HookCommand(QObject *parent) :
Expand Down Expand Up @@ -124,8 +123,14 @@ void HookCommand::createHookCompile(const QString &inFile, QIODevice *out)
stream << "#define Q_CONSTRUCTOR_FUNCTION(x)\n" //define to nothing to prevent code generation
<< "#include \"" << inFile << "\"\n";

QDir hookDir(QStringLiteral(".qpmx_startup_hooks"));
if(!hookDir.mkpath(QStringLiteral(".")))
throw tr("Failed to create hook directory");
QFile hookFile(hookDir.absoluteFilePath(QFileInfo(inFile).fileName()));
if(!functions.isEmpty()) {
QByteArrayList hooks;
if(!hookFile.open(QIODevice::WriteOnly | QIODevice::Text))
throw tr("Failed to create qpmx hook cache with error: %1").arg(hookFile.errorString());
QTextStream hookStream(&hookFile);

stream << "\nnamespace __qpmx_startup_hooks {";
foreach(auto fn, functions) {
Expand All @@ -134,26 +139,14 @@ void HookCommand::createHookCompile(const QString &inFile, QIODevice *out)
stream << "\n\tvoid hook_" << fnId << "() {\n"
<< "\t\t" << fn << "_ctor_function();\n"
<< "\t}\n";
hooks.append(fnId);
hookStream << fnId << "\n";
}
stream << "}\n";

//lock and update the hooks file
QLockFile hookLock(QStringLiteral(".qpmx_startup_hooks.lock"));
if(!hookLock.lock())
throw tr("Failed to aquire lock for hook-file");

QFile hookFile(QStringLiteral(".qpmx_startup_hooks"));
if(!hookFile.open(QIODevice::Append | QIODevice::Text))
throw tr("Failed to create qpmx hook cache with error: %1").arg(hookFile.errorString());
QTextStream hookStream(&hookFile);
foreach(auto hook, hooks)
hookStream << hook << "\n";
hookStream.flush();
hookFile.close();

hookLock.unlock();
}
} else
hookFile.remove();

stream.flush();
}

0 comments on commit daf2670

Please sign in to comment.