Extract CCManager compile functionality to separate functions
MODIFY
src/ccmanager.cpp
src/ccmanager.cpp
@@ -69,25 +69,51 @@ bool CCManager::trySetCC(const QString& CC) {
return success;
}
-bool CCManager::verifyCC(const QString& CCPath) {
- // Write test program to temporary file with a .c extension
+CCManager::CCRes CCManager::compileRaw(const QString& rawsource, QString outname) {
+ // Write program to temporary file with a .c extension
+ QTemporaryFile tmpSrcFile(QDir::tempPath() + QDir::separator() + QCoreApplication::applicationName() + ".XXXXXX.c");
+ if (tmpSrcFile.open()) {
+ QTextStream stream(&tmpSrcFile);
+ stream << rawsource;
+ }
+ Q_ASSERT(!tmpSrcFile.fileName().isEmpty());
+ return compile(tmpSrcFile.fileName(), outname);
+}
+
+CCManager::CCRes CCManager::compile(const QTextDocument& source, QString outname) {
+ return compileRaw(source.toRawText(), outname);
+}
+
+CCManager::CCRes CCManager::compile(const QString& filename, QString outname) {
+ CCRes res;
+ if (outname.isEmpty()) {
+ outname = QDir::tempPath() + QDir::separator() + QCoreApplication::applicationName() + ".XXXXXX.out";
+ }
+
+ res.inFile = filename;
+ res.outFile = outname;
+
+ const QString ccc = createCompileCommand(filename, outname);
- QTemporaryFile testSrcFile(QDir::tempPath() + QDir::separator() + QCoreApplication::applicationName() +
- ".XXXXXX.c");
- if (testSrcFile.open()) {
- QTextStream stream(&testSrcFile);
- stream << s_testprogram;
+ // Run compiler
+ m_process.close();
+ m_process.start(ccc);
+ m_process.waitForFinished();
+
+ const bool success = QFile::exists(outname);
+ res.success = success;
+
+ return res;
}
- Q_ASSERT(!testSrcFile.fileName().isEmpty());
- const QString& testSrcFileOut = testSrcFile.fileName() + ".out";
+QString CCManager::createCompileCommand(const QString& filename, const QString& outname) const {
const auto& currentISA = ProcessorHandler::get()->currentISA();
// Generate compile command
QString s_cc = s_baseCC;
// Substitute compiler path
- s_cc = s_cc.arg(CCPath);
+ s_cc = s_cc.arg(m_currentCC);
// Substitute machine architecture
s_cc = s_cc.arg(currentISA->CCmarch());
@@ -99,29 +125,29 @@ bool CCManager::verifyCC(const QString& CCPath) {
s_cc = s_cc.arg(RipesSettings::value(RIPES_SETTING_CCARGS).toString());
// Substitute in and out files
- s_cc = s_cc.arg(testSrcFile.fileName()).arg(testSrcFileOut);
+ s_cc = s_cc.arg(filename).arg(outname);
- // Run compiler
- QProcess process;
- process.start(s_cc);
- process.waitForFinished();
+ return s_cc;
+}
- const bool success = QFile::exists(testSrcFileOut);
+bool CCManager::verifyCC(const QString& CCPath) {
+ // Try to set CCPath as current compiler, and compile test program
+ m_currentCC = CCPath;
+ auto res = compileRaw(s_testprogram);
#ifdef QT_DEBUG
- if (!success) {
+ if (!res.success) {
qDebug() << "Failed to compile test program";
qDebug() << "CC output: ";
- qDebug() << "Standard output: " << process.readAllStandardOutput();
- qDebug() << "Standard error: " << process.readAllStandardError();
+ qDebug() << "Standard output: " << m_process.readAllStandardOutput();
+ qDebug() << "Standard error: " << m_process.readAllStandardError();
}
#endif
// Cleanup
- QFile::remove(testSrcFile.fileName());
- QFile::remove(testSrcFileOut);
+ res.clean();
- return success;
+ return res.success;
}
} // namespace Ripes

MODIFY
src/ccmanager.h
src/ccmanager.h
#pragma once
+#include <QFile>
#include <QObject>
+#include <QProcess>
#include <QString>
+#include <QTextDocument>
namespace Ripes {
@@ -13,6 +16,17 @@ namespace Ripes {
class CCManager : public QObject {
Q_OBJECT
public:
+ struct CCRes {
+ QString inFile;
+ QString outFile;
+ bool success;
+
+ void clean() {
+ QFile::remove(inFile);
+ QFile::remove(outFile);
+ }
+ };
+
static CCManager& get() {
static CCManager manager;
return manager;
@@ -21,6 +35,16 @@ public:
bool hasValidCC() const;
const QString& currentCC() const { return m_currentCC; }
+ /**
+ * @brief compile
+ * Runs the current compiler, with the current set of arguments, on file @p filename. @returns information about the
+ * compiled file, such as source file, output file and compilation status. If no @p outname has been provided, the
+ * output file will be placed in a temporary directory.
+ */
+ CCRes compile(const QString& filename, QString outname = QString());
+ CCRes compile(const QTextDocument& source, QString outname = QString());
+ CCRes compileRaw(const QString& rawsource, QString outname = QString());
+
signals:
/**
* @brief ccChanged
@@ -37,6 +61,8 @@ public slots:
bool trySetCC(const QString& CC);
private:
+ QString createCompileCommand(const QString& filename, const QString& outname) const;
+
/**
* @brief tryAutodetectCC
* Will attempt to scan the current PATH to locate a valid compiler
@@ -51,8 +77,8 @@ private:
bool verifyCC(const QString& CC);
CCManager();
-
QString m_currentCC;
+ QProcess m_process;
};
} // namespace Ripes

Extract CCManager compile functionality to separate functions