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-06-24 20:43:09 +08:00
|
|
|
#include "stdincompiler.h"
|
|
|
|
#include "compilermanager.h"
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2022-03-31 09:56:49 +08:00
|
|
|
#include <QTextCodec>
|
2021-09-03 21:06:53 +08:00
|
|
|
#include "../platform.h"
|
2021-06-24 20:43:09 +08:00
|
|
|
|
2022-03-27 11:44:52 +08:00
|
|
|
StdinCompiler::StdinCompiler(const QString &filename,const QByteArray& encoding, const QString& content,bool silent, bool onlyCheckSyntax):
|
2022-03-31 09:56:49 +08:00
|
|
|
Compiler(filename,silent, onlyCheckSyntax),
|
2021-09-03 21:06:53 +08:00
|
|
|
mContent(content),
|
2022-03-27 11:44:52 +08:00
|
|
|
mEncoding(encoding)
|
2021-06-24 20:43:09 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StdinCompiler::prepareForCompile()
|
|
|
|
{
|
|
|
|
log(tr("Checking file syntax..."));
|
|
|
|
log("------------------");
|
|
|
|
log(tr("- Filename: %1").arg(mFilename));
|
|
|
|
log(tr("- Compiler Set Name: %1").arg(compilerSet()->name()));
|
|
|
|
log("");
|
|
|
|
FileType fileType = getFileType(mFilename);
|
|
|
|
if (fileType == FileType::Other)
|
|
|
|
fileType = FileType::CppSource;
|
|
|
|
QString strFileType;
|
2022-03-31 09:56:49 +08:00
|
|
|
if (mEncoding!=ENCODING_ASCII) {
|
2022-06-23 19:07:48 +08:00
|
|
|
mArguments += getCharsetArgument(mEncoding,fileType, mOnlyCheckSyntax);
|
2022-03-31 09:56:49 +08:00
|
|
|
}
|
2021-06-24 20:43:09 +08:00
|
|
|
switch(fileType) {
|
|
|
|
case FileType::CSource:
|
|
|
|
mArguments += " -x c - ";
|
|
|
|
mArguments += getCCompileArguments(mOnlyCheckSyntax);
|
|
|
|
mArguments += getCIncludeArguments();
|
2021-09-12 22:45:00 +08:00
|
|
|
mArguments += getProjectIncludeArguments();
|
2021-06-24 20:43:09 +08:00
|
|
|
strFileType = "C";
|
|
|
|
mCompiler = compilerSet()->CCompiler();
|
|
|
|
break;
|
|
|
|
case FileType::CppSource:
|
2021-08-30 13:30:42 +08:00
|
|
|
case FileType::CppHeader:
|
2021-10-09 18:04:31 +08:00
|
|
|
case FileType::CHeader:
|
2021-06-24 20:43:09 +08:00
|
|
|
mArguments += " -x c++ - ";
|
2021-09-12 22:45:00 +08:00
|
|
|
mArguments += getCppCompileArguments(mOnlyCheckSyntax);
|
|
|
|
mArguments += getCppIncludeArguments();
|
|
|
|
mArguments += getProjectIncludeArguments();
|
2021-06-24 20:43:09 +08:00
|
|
|
strFileType = "C++";
|
|
|
|
mCompiler = compilerSet()->cppCompiler();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw CompileError(tr("Can't find the compiler for file %1").arg(mFilename));
|
|
|
|
}
|
2021-10-25 09:31:58 +08:00
|
|
|
if (!mOnlyCheckSyntax)
|
|
|
|
mArguments += getLibraryArguments(fileType);
|
2021-06-24 20:43:09 +08:00
|
|
|
|
2021-09-07 10:28:40 +08:00
|
|
|
if (!fileExists(mCompiler)) {
|
2021-06-24 20:43:09 +08:00
|
|
|
throw CompileError(tr("The Compiler '%1' doesn't exists!").arg(mCompiler));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-24 20:43:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:56:49 +08:00
|
|
|
QByteArray StdinCompiler::pipedText()
|
2021-06-24 20:43:09 +08:00
|
|
|
{
|
2022-03-31 09:56:49 +08:00
|
|
|
if (mEncoding == ENCODING_ASCII)
|
|
|
|
return mContent.toLatin1();
|
|
|
|
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(mEncoding);
|
|
|
|
if (codec) {
|
|
|
|
return codec->fromUnicode(mContent);
|
|
|
|
} else {
|
|
|
|
return mContent.toLocal8Bit();
|
|
|
|
}
|
2021-06-24 20:43:09 +08:00
|
|
|
}
|
2021-06-25 12:40:11 +08:00
|
|
|
|
|
|
|
bool StdinCompiler::prepareForRebuild()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|