| 1 | /* |
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "filecompiler.h" |
| 18 | #include "utils.h" |
| 19 | #include "../mainwindow.h" |
| 20 | #include "compilermanager.h" |
| 21 | |
| 22 | #include <QFile> |
| 23 | #include <QFileInfo> |
| 24 | #include <QMessageBox> |
| 25 | |
| 26 | |
| 27 | FileCompiler::FileCompiler(const QString &filename, const QByteArray &encoding,bool silent,bool onlyCheckSyntax): |
| 28 | Compiler(filename, silent,onlyCheckSyntax), |
| 29 | mEncoding(encoding) |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | bool FileCompiler::prepareForCompile() |
| 35 | { |
| 36 | log(tr("Compiling single file..." )); |
| 37 | log("------------------" ); |
| 38 | log(tr("- Filename: %1" ).arg(mFilename)); |
| 39 | log(tr("- Compiler Set Name: %1" ).arg(compilerSet()->name())); |
| 40 | log("" ); |
| 41 | FileType fileType = getFileType(mFilename); |
| 42 | mArguments= QString(" \"%1\"" ).arg(mFilename); |
| 43 | if (!mOnlyCheckSyntax) { |
| 44 | if (compilerSet()->getCompileOptionValue(CC_CMD_OPT_STOP_AFTER_PREPROCESSING)==COMPILER_OPTION_ON) |
| 45 | mOutputFile=changeFileExt(mFilename,"expanded.cpp" ); |
| 46 | else if (compilerSet()->getCompileOptionValue(CC_CMD_OPT_ONLY_GEN_ASM_CODE)==COMPILER_OPTION_ON) |
| 47 | mOutputFile=changeFileExt(mFilename,"s" ); |
| 48 | else |
| 49 | mOutputFile = getCompiledExecutableName(mFilename); |
| 50 | mArguments+=QString(" -o \"%1\"" ).arg(mOutputFile); |
| 51 | |
| 52 | //remove the old file if it exists |
| 53 | QFile outputFile(mOutputFile); |
| 54 | if (outputFile.exists()) { |
| 55 | if (!outputFile.remove()) { |
| 56 | error(tr("Can't delete the old executable file \"%1\".\n" ).arg(mOutputFile)); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | mArguments += getCharsetArgument(mEncoding, fileType, mOnlyCheckSyntax); |
| 63 | QString strFileType; |
| 64 | switch(fileType) { |
| 65 | case FileType::CSource: |
| 66 | mArguments += getCCompileArguments(mOnlyCheckSyntax); |
| 67 | mArguments += getCIncludeArguments(); |
| 68 | mArguments += getProjectIncludeArguments(); |
| 69 | strFileType = "C" ; |
| 70 | mCompiler = compilerSet()->CCompiler(); |
| 71 | break; |
| 72 | case FileType::CppSource: |
| 73 | mArguments += getCppCompileArguments(mOnlyCheckSyntax); |
| 74 | mArguments += getCppIncludeArguments(); |
| 75 | mArguments += getProjectIncludeArguments(); |
| 76 | strFileType = "C++" ; |
| 77 | mCompiler = compilerSet()->cppCompiler(); |
| 78 | break; |
| 79 | default: |
| 80 | throw CompileError(tr("Can't find the compiler for file %1" ).arg(mFilename)); |
| 81 | } |
| 82 | |
| 83 | if (!mOnlyCheckSyntax) |
| 84 | mArguments += getLibraryArguments(fileType); |
| 85 | |
| 86 | if (!fileExists(mCompiler)) { |
| 87 | throw CompileError(tr("The Compiler '%1' doesn't exists!" ).arg(mCompiler)); |
| 88 | } |
| 89 | |
| 90 | log(tr("Processing %1 source file:" ).arg(strFileType)); |
| 91 | log("------------------" ); |
| 92 | log(tr("%1 Compiler: %2" ).arg(strFileType).arg(mCompiler)); |
| 93 | log(tr("Command: %1 %2" ).arg(extractFileName(mCompiler)).arg(mArguments)); |
| 94 | mDirectory = extractFileDir(mFilename); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool FileCompiler::prepareForRebuild() |
| 99 | { |
| 100 | QString exeName = getCompiledExecutableName(mFilename); |
| 101 | QFile file(exeName); |
| 102 | |
| 103 | if (file.exists() && !file.remove()) { |
| 104 | QFileInfo info(exeName); |
| 105 | throw CompileError(tr("Can't delete the old executable file \"%1\".\n" ).arg(info.absoluteFilePath())); |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |