| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2008 Jürgen Riegel <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This program is free software; you can redistribute it and/or modify * |
| 8 |
* it under the terms of the GNU Library General Public License (LGPL) * |
| 9 |
* as published by the Free Software Foundation; either version 2 of * |
| 10 |
* the License, or (at your option) any later version. * |
| 11 |
* for detail see the LICENCE text file. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with FreeCAD; if not, write to the Free Software * |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * |
| 21 |
* USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#include "../FCConfig.h" |
| 26 |
|
| 27 |
#if HAVE_CONFIG_H |
| 28 |
# include <config.h> |
| 29 |
#endif // HAVE_CONFIG_H |
| 30 |
|
| 31 |
#include <Build/Version.h> // For FCCopyrightYear |
| 32 |
|
| 33 |
#include <cstdio> |
| 34 |
#include <ostream> |
| 35 |
#include <QString> |
| 36 |
|
| 37 |
// FreeCAD Base header |
| 38 |
#include <Base/Console.h> |
| 39 |
#include <Base/Exception.h> |
| 40 |
#include <Base/Interpreter.h> |
| 41 |
|
| 42 |
// FreeCAD doc header |
| 43 |
#include <App/Application.h> |
| 44 |
|
| 45 |
|
| 46 |
using App::Application; |
| 47 |
using Base::Console; |
| 48 |
|
| 49 |
const auto sBanner = fmt::format( |
| 50 |
"(C) 2001-{} FreeCAD contributors\n" |
| 51 |
"FreeCAD is free and open-source software licensed under the terms of LGPL2+ license.\n\n", |
| 52 |
FCCopyrightYear |
| 53 |
); |
| 54 |
|
| 55 |
int main(int argc, char** argv) |
| 56 |
{ |
| 57 |
// Make sure that we use '.' as decimal point |
| 58 |
setlocale(LC_ALL, ""); |
| 59 |
setlocale(LC_NUMERIC, "C"); |
| 60 |
|
| 61 |
#if defined(__MINGW32__) |
| 62 |
const char* mingw_prefix = getenv("MINGW_PREFIX"); |
| 63 |
const char* py_home = getenv("PYTHONHOME"); |
| 64 |
if (!py_home && mingw_prefix) { |
| 65 |
_putenv_s("PYTHONHOME", mingw_prefix); |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
// Name and Version of the Application |
| 70 |
App::Application::Config()["ExeName"] = "FreeCAD"; |
| 71 |
App::Application::Config()["ExeVendor"] = "FreeCAD"; |
| 72 |
App::Application::Config()["AppDataSkipVendor"] = "true"; |
| 73 |
|
| 74 |
// set the banner (for logging and console) |
| 75 |
App::Application::Config()["CopyrightInfo"] = sBanner; |
| 76 |
|
| 77 |
try { |
| 78 |
// Init phase =========================================================== |
| 79 |
// sets the default run mode for FC, starts with command prompt if not overridden in |
| 80 |
// InitConfig... |
| 81 |
App::Application::Config()["RunMode"] = "Exit"; |
| 82 |
App::Application::Config()["LoggingConsole"] = "1"; |
| 83 |
|
| 84 |
// Inits the Application |
| 85 |
App::Application::init(argc, argv); |
| 86 |
} |
| 87 |
catch (const Base::UnknownProgramOption& e) { |
| 88 |
std::cerr << e.what(); |
| 89 |
exit(1); |
| 90 |
} |
| 91 |
catch (const Base::ProgramInformation& e) { |
| 92 |
if (std::strcmp(e.what(), App::Application::verboseVersionEmitMessage) == 0) { |
| 93 |
QString data; |
| 94 |
QTextStream str(&data); |
| 95 |
const std::map<std::string, std::string> config = App::Application::Config(); |
| 96 |
|
| 97 |
App::Application::getVerboseCommonInfo(str, config); |
| 98 |
App::Application::getVerboseAddOnsInfo(str, config); |
| 99 |
|
| 100 |
std::cout << data.toStdString(); |
| 101 |
exit(0); |
| 102 |
} |
| 103 |
std::cout << e.what(); |
| 104 |
exit(0); |
| 105 |
} |
| 106 |
catch (const Base::Exception& e) { |
| 107 |
std::string appName = App::Application::Config()["ExeName"]; |
| 108 |
std::cout << "While initializing " << appName << " the following exception occurred: '" |
| 109 |
<< e.what() << "'\n\n"; |
| 110 |
std::cout << "Python is searching for its runtime files in the following directories:\n" |
| 111 |
<< Base::Interpreter().getPythonPath() << "\n\n"; |
| 112 |
std::cout << "Python version information:\n" << Py_GetVersion() << "\n"; |
| 113 |
const char* pythonhome = getenv("PYTHONHOME"); |
| 114 |
if (pythonhome) { |
| 115 |
std::cout << "\nThe environment variable PYTHONHOME is set to '" << pythonhome << "'."; |
| 116 |
std::cout << "\nSetting this environment variable might cause Python to fail. " |
| 117 |
"Please contact your administrator to unset it on your system."; |
| 118 |
} |
| 119 |
else { |
| 120 |
std::cout << "\nPlease contact the application's support team for more information."; |
| 121 |
} |
| 122 |
std::cout << std::endl; |
| 123 |
exit(100); |
| 124 |
} |
| 125 |
catch (...) { |
| 126 |
std::string appName = App::Application::Config()["ExeName"]; |
| 127 |
std::cout << "Unknown runtime error occurred while initializing " << appName << ".\n\n"; |
| 128 |
std::cout << "Please contact the application's support team for more information."; |
| 129 |
std::cout << std::endl; |
| 130 |
exit(101); |
| 131 |
} |
| 132 |
|
| 133 |
// Run phase =========================================================== |
| 134 |
try { |
| 135 |
Application::runApplication(); |
| 136 |
} |
| 137 |
catch (const Base::SystemExitException& e) { |
| 138 |
exit(e.getExitCode()); |
| 139 |
} |
| 140 |
catch (const Base::Exception& e) { |
| 141 |
e.reportException(); |
| 142 |
exit(1); |
| 143 |
} |
| 144 |
catch (...) { |
| 145 |
Console().error("Application unexpectedly terminated\n"); |
| 146 |
exit(1); |
| 147 |
} |
| 148 |
|
| 149 |
// Destruction phase =========================================================== |
| 150 |
Console().log("FreeCAD terminating...\n"); |
| 151 |
|
| 152 |
try { |
| 153 |
// close open documents |
| 154 |
App::GetApplication().closeAllDocuments(); |
| 155 |
} |
| 156 |
catch (...) { |
| 157 |
} |
| 158 |
|
| 159 |
// cleans up |
| 160 |
Application::destruct(); |
| 161 |
|
| 162 |
Console().log("FreeCAD completely terminated\n"); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |