2021-04-20 22:24:33 +08:00
|
|
|
#include "filecompiler.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "../mainwindow.h"
|
2021-06-21 11:21:26 +08:00
|
|
|
#include "compilermanager.h"
|
2021-04-20 22:24:33 +08:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
|
|
|
|
FileCompiler::FileCompiler(const QString &filename, const QByteArray &encoding,bool silent,bool onlyCheckSyntax):
|
2021-06-24 20:43:09 +08:00
|
|
|
Compiler(filename, silent,onlyCheckSyntax),
|
2021-04-20 22:24:33 +08:00
|
|
|
mEncoding(encoding)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileCompiler::prepareForCompile()
|
|
|
|
{
|
|
|
|
log(tr("Compiling single file..."));
|
|
|
|
log("------------------");
|
2021-06-24 20:43:09 +08:00
|
|
|
log(tr("- Filename: %1").arg(mFilename));
|
2021-04-20 22:24:33 +08:00
|
|
|
log(tr("- Compiler Set Name: %1").arg(compilerSet()->name()));
|
|
|
|
log("");
|
2021-06-24 20:43:09 +08:00
|
|
|
FileType fileType = getFileType(mFilename);
|
|
|
|
mArguments= QString(" \"%1\"").arg(mFilename);
|
2021-04-20 22:24:33 +08:00
|
|
|
if (!mOnlyCheckSyntax) {
|
2021-06-24 20:43:09 +08:00
|
|
|
mOutputFile = getCompiledExecutableName(mFilename);
|
2021-04-21 23:06:55 +08:00
|
|
|
mArguments+=QString(" -o \"%1\"").arg(mOutputFile);
|
2021-04-20 22:24:33 +08:00
|
|
|
|
2021-09-04 21:21:45 +08:00
|
|
|
//remove the old file if it exists
|
2021-04-21 23:06:55 +08:00
|
|
|
QFile outputFile(mOutputFile);
|
2021-04-20 22:24:33 +08:00
|
|
|
if (outputFile.exists()) {
|
|
|
|
if (!outputFile.remove()) {
|
2021-04-21 23:06:55 +08:00
|
|
|
error(tr("Can't delete the old executable file \"%1\".\n").arg(mOutputFile));
|
2021-04-20 22:24:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mArguments += getCharsetArgument(mEncoding);
|
|
|
|
QString strFileType;
|
|
|
|
switch(fileType) {
|
|
|
|
case FileType::CSource:
|
|
|
|
mArguments += getCCompileArguments(mOnlyCheckSyntax);
|
|
|
|
mArguments += getCIncludeArguments();
|
2021-09-12 22:45:00 +08:00
|
|
|
mArguments += getProjectIncludeArguments();
|
2021-04-20 22:24:33 +08:00
|
|
|
strFileType = "C";
|
|
|
|
mCompiler = compilerSet()->CCompiler();
|
|
|
|
break;
|
|
|
|
case FileType::CppSource:
|
2021-09-12 22:45:00 +08:00
|
|
|
mArguments += getCppCompileArguments(mOnlyCheckSyntax);
|
|
|
|
mArguments += getCppIncludeArguments();
|
|
|
|
mArguments += getProjectIncludeArguments();
|
2021-04-20 22:24:33 +08:00
|
|
|
strFileType = "C++";
|
|
|
|
mCompiler = compilerSet()->cppCompiler();
|
|
|
|
break;
|
|
|
|
default:
|
2021-06-24 20:43:09 +08:00
|
|
|
throw CompileError(tr("Can't find the compiler for file %1").arg(mFilename));
|
2021-04-20 22:24:33 +08:00
|
|
|
}
|
2021-09-12 22:45:00 +08:00
|
|
|
|
2021-09-04 14:06:48 +08:00
|
|
|
mArguments += getLibraryArguments(fileType);
|
2021-04-20 22:24:33 +08:00
|
|
|
|
2021-09-07 10:28:40 +08:00
|
|
|
if (!fileExists(mCompiler)) {
|
2021-06-21 11:21:26 +08:00
|
|
|
throw CompileError(tr("The Compiler '%1' doesn't exists!").arg(mCompiler));
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:24:33 +08:00
|
|
|
log(tr("Processing %1 source file:").arg(strFileType));
|
|
|
|
log("------------------");
|
|
|
|
log(tr("%1 Compiler: %2").arg(strFileType).arg(mCompiler));
|
2021-09-14 12:10:43 +08:00
|
|
|
log(tr("Command: %1 %2").arg(extractFileName(mCompiler)).arg(mArguments));
|
2021-09-13 22:45:50 +08:00
|
|
|
mDirectory = extractFileDir(mFilename);
|
2021-04-20 22:24:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-24 20:43:09 +08:00
|
|
|
|
|
|
|
QString FileCompiler::pipedText()
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
2021-06-25 12:40:11 +08:00
|
|
|
|
|
|
|
bool FileCompiler::prepareForRebuild()
|
|
|
|
{
|
|
|
|
QString exeName = getCompiledExecutableName(mFilename);
|
|
|
|
QFile file(exeName);
|
|
|
|
|
2021-09-04 21:33:10 +08:00
|
|
|
if (file.exists() && !file.remove()) {
|
2021-06-25 12:40:11 +08:00
|
|
|
QFileInfo info(exeName);
|
|
|
|
throw CompileError(tr("Can't delete the old executable file \"%1\".\n").arg(info.absoluteFilePath()));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|