Debian Codegraph @

src/Main/MainPy.cpp

raw
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 defined(FC_OS_WIN32)
28 # include <windows.h>
29 #endif
30
31 #ifdef FC_OS_MACOSX
32 # include <mach-o/dyld.h>
33 # include <string>
34 #endif
35
36 #if HAVE_CONFIG_H
37 # include <config.h>
38 #endif // HAVE_CONFIG_H
39
40 #include <cstdio>
41 #include <iostream>
42 #include <QByteArray>
43
44 // FreeCAD Base header
45 #include <Base/ConsoleObserver.h>
46 #include <Base/Exception.h>
47 #include <Base/PyObjectBase.h>
48 #include <Base/Sequencer.h>
49 #include <App/Application.h>
50
51 #if defined(FC_OS_WIN32)
52
53 /** DllMain is called when DLL is loaded
54 */
55 BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
56 {
57 switch (ul_reason_for_call) {
58 case DLL_PROCESS_ATTACH: {
59 // This name is preliminary, we pass it to Application::init() in initFreeCAD()
60 // which does the rest.
61 char szFileName[MAX_PATH];
62 GetModuleFileNameA((HMODULE)hModule, szFileName, MAX_PATH - 1);
63 App::Application::Config()["AppHomePath"] = szFileName;
64 } break;
65 default:
66 break;
67 }
68
69 return true;
70 }
71 #elif defined(FC_OS_LINUX) || defined(FC_OS_BSD)
72 # ifndef GNU_SOURCE
73 # define GNU_SOURCE
74 # endif
75 # include <dlfcn.h>
76 #elif defined(FC_OS_CYGWIN)
77 # include <windows.h>
78 #endif
79
80 PyMOD_INIT_FUNC(FreeCAD)
81 {
82 // Init phase ===========================================================
83 App::Application::Config()["ExeName"] = "FreeCAD";
84 App::Application::Config()["ExeVendor"] = "FreeCAD";
85 App::Application::Config()["AppDataSkipVendor"] = "true";
86
87 QByteArray path;
88
89 #if defined(FC_OS_WIN32)
90 path = App::Application::Config()["AppHomePath"].c_str();
91 #elif defined(FC_OS_CYGWIN)
92 HMODULE hModule = GetModuleHandle("FreeCAD.dll");
93 char szFileName[MAX_PATH];
94 GetModuleFileNameA(hModule, szFileName, MAX_PATH - 1);
95 path = szFileName;
96 #elif defined(FC_OS_LINUX) || defined(FC_OS_BSD)
97 putenv("LANG=C");
98 putenv("LC_ALL=C");
99 // get whole path of the library
100 Dl_info info;
101 int ret = dladdr((void*)PyInit_FreeCAD, &info);
102 if ((ret == 0) || (!info.dli_fname)) {
103 PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
104 return nullptr;
105 }
106
107 path = info.dli_fname;
108 // this is a workaround to avoid a crash in libuuid.so
109 #elif defined(FC_OS_MACOSX)
110
111 // The MacOS approach uses the Python sys.path list to find the path
112 // to FreeCAD.so - this should be OS-agnostic, except these two
113 // strings, and the call to access().
114 const static char libName[] = "/FreeCAD.so";
115 const static char upDir[] = "/../";
116
117 PyObject* pySysPath = PySys_GetObject("path");
118 if (PyList_Check(pySysPath)) {
119 int i;
120 // pySysPath should be a *PyList of strings - iterate through it
121 // backwards since the FreeCAD path was likely appended just before
122 // we were imported.
123 for (i = PyList_Size(pySysPath) - 1; i >= 0; --i) {
124 const char* basePath;
125 PyObject* pyPath = PyList_GetItem(pySysPath, i);
126 long sz = 0;
127
128 if (PyUnicode_Check(pyPath)) {
129 // Python 3 string
130 basePath = PyUnicode_AsUTF8AndSize(pyPath, &sz);
131 }
132 else {
133 continue;
134 }
135
136 if (sz + sizeof(libName) > PATH_MAX) {
137 continue;
138 }
139
140 path = basePath;
141
142 // append libName to the path
143 if (access(path + libName, R_OK | X_OK) == 0) {
144
145 // The FreeCAD "home" path is one level up from
146 // libName, so replace libName with upDir.
147 path += upDir;
148 break;
149 }
150 }
151 }
152
153 if (path.isEmpty()) {
154 PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
155 return nullptr;
156 }
157
158 #else
159 # error "Implement: Retrieve the path of the module for your platform."
160 #endif
161 int argc = 1;
162 std::vector<char*> argv;
163 argv.push_back(path.data());
164
165 try {
166 // Inits the Application
167 App::Application::init(argc, argv.data());
168 }
169 catch (const Base::Exception& e) {
170 std::string appName = App::Application::Config()["ExeName"];
171 std::cout << "While initializing " << appName << " the following exception occurred: '"
172 << e.what() << "'\n\n";
173 std::cout << "Please contact the application's support team for more information."
174 << std::endl;
175 }
176
177 Base::EmptySequencer* seq = new Base::EmptySequencer();
178 (void)seq;
179 static Base::RedirectStdOutput stdcout;
180 static Base::RedirectStdLog stdclog;
181 static Base::RedirectStdError stdcerr;
182 std::cout.rdbuf(&stdcout);
183 std::clog.rdbuf(&stdclog);
184 std::cerr.rdbuf(&stdcerr);
185
186 PyObject* modules = PyImport_GetModuleDict();
187 PyObject* module = PyDict_GetItemString(modules, "FreeCAD");
188 if (!module) {
189 PyErr_SetString(PyExc_ImportError, "Failed to load FreeCAD module!");
190 }
191 return module;
192 }