2023-08-13 18:58:04 +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/>.
|
|
|
|
*/
|
|
|
|
#include "sdccfilecompiler.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "compilermanager.h"
|
|
|
|
#include "../systemconsts.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
|
|
|
|
SDCCFileCompiler::SDCCFileCompiler(const QString &filename, const QByteArray &encoding,
|
2023-08-15 10:57:03 +08:00
|
|
|
CppCompileType compileType, bool onlyCheckSyntax):
|
|
|
|
Compiler(filename, onlyCheckSyntax),
|
2023-08-13 18:58:04 +08:00
|
|
|
mEncoding(encoding),
|
2023-08-15 00:29:48 +08:00
|
|
|
mCompileType(compileType),
|
|
|
|
mNoStartup(false)
|
2023-08-13 18:58:04 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SDCCFileCompiler::prepareForCompile()
|
|
|
|
{
|
2023-08-15 10:57:03 +08:00
|
|
|
//compilerSet()->setCompilationStage(Settings::CompilerSet::CompilationStage::GenerateExecutable);
|
|
|
|
|
|
|
|
if (mOnlyCheckSyntax) {
|
|
|
|
mCompiler = compilerSet()->CCompiler();
|
|
|
|
mArguments += getCCompileArguments(false);
|
|
|
|
mArguments += getCIncludeArguments();
|
|
|
|
mArguments += getProjectIncludeArguments();
|
|
|
|
mArguments = QString("--syntax-only \"%1\"").arg(mFilename);
|
|
|
|
mDirectory = extractFileDir(mFilename);
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-13 18:58:04 +08:00
|
|
|
log(tr("Compiling single file..."));
|
|
|
|
log("------------------");
|
|
|
|
log(tr("- Filename: %1").arg(mFilename));
|
|
|
|
log(tr("- Compiler Set Name: %1").arg(compilerSet()->name()));
|
|
|
|
log("");
|
|
|
|
|
2023-08-15 00:29:48 +08:00
|
|
|
QString strFileType = "C";
|
|
|
|
mCompiler = compilerSet()->CCompiler();
|
|
|
|
mArguments += getCCompileArguments(false);
|
|
|
|
mArguments += getCIncludeArguments();
|
|
|
|
mArguments += getProjectIncludeArguments();
|
2023-08-13 18:58:04 +08:00
|
|
|
|
2023-08-15 00:29:48 +08:00
|
|
|
if (!fileExists(mCompiler)) {
|
|
|
|
throw CompileError(
|
|
|
|
tr("The Compiler '%1' doesn't exists!").arg(mCompiler)
|
|
|
|
+"<br />"
|
|
|
|
+tr("Please check the \"program\" page of compiler settings."));
|
|
|
|
}
|
2023-08-13 18:58:04 +08:00
|
|
|
|
2023-08-15 00:29:48 +08:00
|
|
|
mOutputFile=changeFileExt(mFilename, compilerSet()->executableSuffix());
|
|
|
|
mIhxFilename = changeFileExt(mFilename,SDCC_IHX_SUFFIX);
|
2023-08-13 18:58:04 +08:00
|
|
|
|
2023-08-15 00:29:48 +08:00
|
|
|
QString val = compilerSet()->compileOptions().value(SDCC_OPT_NOSTARTUP);
|
|
|
|
mNoStartup = (val==COMPILER_OPTION_ON);
|
|
|
|
if (mNoStartup) {
|
|
|
|
mRelFilename = changeFileExt(mFilename,SDCC_REL_SUFFIX);
|
2023-08-21 11:48:23 +08:00
|
|
|
mArguments += QString(" -c \"%1\"").arg(mFilename);
|
2023-08-15 00:29:48 +08:00
|
|
|
mExtraCompilersList.append(mCompiler);
|
2023-08-21 11:48:23 +08:00
|
|
|
QString args = getLibraryArguments(FileType::CSource);
|
|
|
|
args += QString(" -o \"%1\" \"%2\" ").arg(mIhxFilename, mRelFilename);
|
2023-08-15 00:29:48 +08:00
|
|
|
mExtraArgumentsList.append(args);
|
|
|
|
mExtraOutputFilesList.append("");
|
|
|
|
} else {
|
2023-08-21 11:48:23 +08:00
|
|
|
mArguments += getLibraryArguments(FileType::CSource);
|
|
|
|
mArguments += QString(" \"%1\"").arg(mFilename);
|
2023-08-15 00:29:48 +08:00
|
|
|
mArguments+=QString(" -o \"%1\"").arg(mIhxFilename);
|
|
|
|
}
|
2023-08-13 18:58:04 +08:00
|
|
|
|
|
|
|
if (compilerSet()->executableSuffix() == SDCC_HEX_SUFFIX) {
|
|
|
|
QString packihx = compilerSet()->findProgramInBinDirs(PACKIHX_PROGRAM);
|
|
|
|
if (packihx.isEmpty()) {
|
|
|
|
error(tr("Can't find \"%1\".\n").arg(PACKIHX_PROGRAM));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mExtraCompilersList.append(packihx);
|
|
|
|
QString args;
|
2023-08-14 12:22:24 +08:00
|
|
|
args = QString(" \"%1\"").arg(mIhxFilename);
|
2023-08-13 18:58:04 +08:00
|
|
|
mExtraArgumentsList.append(args);
|
|
|
|
mExtraOutputFilesList.append(mOutputFile);
|
|
|
|
} else if (compilerSet()->executableSuffix() == SDCC_BIN_SUFFIX) {
|
|
|
|
QString makebin = compilerSet()->findProgramInBinDirs(MAKEBIN_PROGRAM);
|
|
|
|
if (makebin.isEmpty()) {
|
|
|
|
error(tr("Can't find \"%1\".\n").arg(PACKIHX_PROGRAM));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mExtraCompilersList.push_back(makebin);
|
|
|
|
QString args;
|
2023-08-14 12:22:24 +08:00
|
|
|
args = QString(" \"%1\"").arg(mIhxFilename);
|
2023-08-13 18:58:04 +08:00
|
|
|
args+=QString(" \"%1\"").arg(mOutputFile);
|
|
|
|
mExtraArgumentsList.push_back(args);
|
|
|
|
mExtraOutputFilesList.append("");
|
|
|
|
}
|
|
|
|
|
|
|
|
log(tr("Processing %1 source file:").arg(strFileType));
|
|
|
|
log("------------------");
|
|
|
|
log(tr("- %1 Compiler: %2").arg(strFileType).arg(mCompiler));
|
|
|
|
log(tr("- Command: %1 %2").arg(extractFileName(mCompiler)).arg(mArguments));
|
|
|
|
mDirectory = extractFileDir(mFilename);
|
2023-08-14 12:22:24 +08:00
|
|
|
mStartCompileTime = QDateTime::currentDateTime();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SDCCFileCompiler::beforeRunExtraCommand(int idx)
|
|
|
|
{
|
2023-08-15 00:29:48 +08:00
|
|
|
if (mNoStartup) {
|
|
|
|
if (idx==0) {
|
|
|
|
QFileInfo file(mRelFilename);
|
|
|
|
return file.exists() && (file.lastModified()>mStartCompileTime);
|
|
|
|
} else if (idx==1) {
|
|
|
|
QFileInfo file(mIhxFilename);
|
|
|
|
return file.exists() && (file.lastModified()>mStartCompileTime);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (idx==0) {
|
|
|
|
QFileInfo file(mIhxFilename);
|
|
|
|
return file.exists() && (file.lastModified()>mStartCompileTime);
|
|
|
|
}
|
2023-08-14 12:22:24 +08:00
|
|
|
}
|
2023-08-13 18:58:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SDCCFileCompiler::prepareForRebuild()
|
|
|
|
{
|
|
|
|
QString exeName=compilerSet()->getOutputFilename(mFilename);
|
|
|
|
|
|
|
|
QFile file(exeName);
|
|
|
|
|
|
|
|
if (file.exists() && !file.remove()) {
|
|
|
|
QFileInfo info(exeName);
|
|
|
|
throw CompileError(tr("Can't delete the old executable file \"%1\".\n").arg(info.absoluteFilePath()));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|