2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
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"
|
2023-02-12 15:13:04 +08:00
|
|
|
#include "qsynedit/syntaxer/asm.h"
|
2021-04-20 22:24:33 +08:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
|
2022-12-01 22:10:44 +08:00
|
|
|
FileCompiler::FileCompiler(const QString &filename, const QByteArray &encoding,
|
|
|
|
CppCompileType compileType,bool silent,bool onlyCheckSyntax):
|
2021-06-24 20:43:09 +08:00
|
|
|
Compiler(filename, silent,onlyCheckSyntax),
|
2022-12-01 22:10:44 +08:00
|
|
|
mEncoding(encoding),
|
|
|
|
mCompileType(compileType)
|
2021-04-20 22:24:33 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileCompiler::prepareForCompile()
|
|
|
|
{
|
2022-12-01 22:10:44 +08:00
|
|
|
Settings::CompilerSet::CompilationStage oldStage = compilerSet()->compilationStage();
|
2022-12-17 17:20:33 +08:00
|
|
|
QString oldDebugOptionValue = compilerSet()->getCompileOptionValue(CC_CMD_OPT_DEBUG_INFO);
|
|
|
|
auto action = finally([this,oldStage,oldDebugOptionValue]{
|
2022-12-01 22:10:44 +08:00
|
|
|
compilerSet()->setCompilationStage(oldStage);
|
2022-12-17 17:20:33 +08:00
|
|
|
compilerSet()->setCompileOption(CC_CMD_OPT_DEBUG_INFO,oldDebugOptionValue);
|
2022-12-01 22:10:44 +08:00
|
|
|
});
|
|
|
|
Settings::CompilerSet::CompilationStage stage = oldStage;
|
|
|
|
switch(mCompileType) {
|
|
|
|
case CppCompileType::PreprocessOnly:
|
|
|
|
stage = Settings::CompilerSet::CompilationStage::PreprocessingOnly;
|
|
|
|
break;
|
|
|
|
case CppCompileType::GenerateAssemblyOnly:
|
|
|
|
stage = Settings::CompilerSet::CompilationStage::CompilationProperOnly;
|
2023-02-28 10:49:50 +08:00
|
|
|
if (pSettings->languages().noDebugDirectivesWhenGenerateASM())
|
|
|
|
compilerSet()->setCompileOption(CC_CMD_OPT_DEBUG_INFO,COMPILER_OPTION_OFF);
|
2022-12-01 22:10:44 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stage = oldStage;
|
|
|
|
}
|
|
|
|
compilerSet()->setCompilationStage(stage);
|
2022-12-25 12:00:09 +08:00
|
|
|
if (mOnlyCheckSyntax) {
|
|
|
|
log(tr("Checking single file..."));
|
|
|
|
} else {
|
|
|
|
log(tr("Compiling single file..."));
|
|
|
|
}
|
2021-04-20 22:24:33 +08:00
|
|
|
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);
|
2023-02-28 10:49:50 +08:00
|
|
|
mArguments = QString(" \"%1\"").arg(mFilename);
|
2021-04-20 22:24:33 +08:00
|
|
|
if (!mOnlyCheckSyntax) {
|
2022-10-12 19:48:35 +08:00
|
|
|
switch(compilerSet()->compilationStage()) {
|
|
|
|
case Settings::CompilerSet::CompilationStage::PreprocessingOnly:
|
|
|
|
mOutputFile=changeFileExt(mFilename,compilerSet()->preprocessingSuffix());
|
|
|
|
mArguments+=" -E";
|
|
|
|
break;
|
|
|
|
case Settings::CompilerSet::CompilationStage::CompilationProperOnly:
|
2023-03-13 22:00:55 +08:00
|
|
|
mOutputFile=changeFileExt(mFilename,compilerSet()->compilationProperSuffix());
|
2022-10-12 19:48:35 +08:00
|
|
|
mArguments+=" -S -fverbose-asm";
|
|
|
|
break;
|
|
|
|
case Settings::CompilerSet::CompilationStage::AssemblingOnly:
|
|
|
|
mOutputFile=changeFileExt(mFilename,compilerSet()->assemblingSuffix());
|
|
|
|
mArguments+=" -c";
|
|
|
|
break;
|
|
|
|
case Settings::CompilerSet::CompilationStage::GenerateExecutable:
|
|
|
|
mOutputFile = changeFileExt(mFilename,compilerSet()->executableSuffix());
|
|
|
|
}
|
|
|
|
|
2021-04-21 23:06:55 +08:00
|
|
|
mArguments+=QString(" -o \"%1\"").arg(mOutputFile);
|
2021-04-20 22:24:33 +08:00
|
|
|
|
2023-02-28 10:49:50 +08:00
|
|
|
#if defined(ARCH_X86_64) || defined(ARCH_X86)
|
|
|
|
if (mCompileType == CppCompileType::GenerateAssemblyOnly) {
|
|
|
|
if (pSettings->languages().noSEHDirectivesWhenGenerateASM())
|
|
|
|
mArguments+=" -fno-asynchronous-unwind-tables";
|
|
|
|
if (pSettings->languages().x86DialectOfASMGenerated()==Settings::Languages::X86ASMDialect::Intel)
|
|
|
|
mArguments+=" -masm=intel";
|
|
|
|
}
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 19:07:48 +08:00
|
|
|
mArguments += getCharsetArgument(mEncoding, fileType, mOnlyCheckSyntax);
|
2021-04-20 22:24:33 +08:00
|
|
|
QString strFileType;
|
|
|
|
switch(fileType) {
|
2023-02-12 12:53:14 +08:00
|
|
|
case FileType::GAS:
|
|
|
|
mArguments += getCCompileArguments(mOnlyCheckSyntax);
|
|
|
|
mArguments += getCIncludeArguments();
|
|
|
|
mArguments += getProjectIncludeArguments();
|
|
|
|
strFileType = "GNU Assembler";
|
|
|
|
mCompiler = compilerSet()->CCompiler();
|
|
|
|
break;
|
2021-04-20 22:24:33 +08:00
|
|
|
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-10-25 09:31:58 +08:00
|
|
|
if (!mOnlyCheckSyntax)
|
|
|
|
mArguments += getLibraryArguments(fileType);
|
2021-04-20 22:24:33 +08:00
|
|
|
|
2023-02-12 15:13:04 +08:00
|
|
|
if (fileType==FileType::GAS) {
|
|
|
|
bool hasStart=false;
|
|
|
|
QStringList lines=readFileToLines(mFilename);
|
|
|
|
QSynedit::ASMSyntaxer syntaxer;
|
|
|
|
syntaxer.resetState();
|
|
|
|
QString lastToken;
|
|
|
|
QString token;
|
|
|
|
QSynedit::PTokenAttribute attr;
|
|
|
|
for (int i=0;i<lines.count();i++) {
|
|
|
|
QString line=lines[i];
|
|
|
|
syntaxer.setLine(line,i+1);
|
|
|
|
lastToken="";
|
|
|
|
while(!syntaxer.eol()) {
|
|
|
|
token=syntaxer.getToken();
|
|
|
|
if (token==":" && lastToken=="_start") {
|
|
|
|
hasStart=true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
attr = syntaxer.getTokenAttribute();
|
|
|
|
if (attr->tokenType() != QSynedit::TokenType::Space
|
|
|
|
&& attr->tokenType()!=QSynedit::TokenType::String
|
|
|
|
&& attr->tokenType()!=QSynedit::TokenType::Character)
|
|
|
|
lastToken=token;
|
|
|
|
syntaxer.next();
|
|
|
|
}
|
|
|
|
if (hasStart)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (hasStart) {
|
2023-02-12 18:15:41 +08:00
|
|
|
mArguments+=" -nostartfiles";
|
2023-02-12 15:13:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:28:40 +08:00
|
|
|
if (!fileExists(mCompiler)) {
|
2022-12-16 11:10:46 +08:00
|
|
|
throw CompileError(
|
|
|
|
tr("The Compiler '%1' doesn't exists!").arg(mCompiler)
|
|
|
|
+"<br />"
|
|
|
|
+tr("Please check the \"program\" page of compiler settings."));
|
2021-06-21 11:21:26 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-06-25 12:40:11 +08:00
|
|
|
bool FileCompiler::prepareForRebuild()
|
|
|
|
{
|
2023-01-19 11:08:32 +08:00
|
|
|
QString exeName=compilerSet()->getOutputFilename(mFilename);
|
2022-10-12 19:48:35 +08:00
|
|
|
|
2021-06-25 12:40:11 +08:00
|
|
|
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;
|
|
|
|
}
|