1
/***************************************************************************
2
* Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU Library General Public License (LGPL) *
8
* as published by the Free Software Foundation; either version 2 of *
9
* the License, or (at your option) any later version. *
10
* for detail see the LICENCE text file. *
12
* FreeCAD is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU Library General Public License for more details. *
17
* You should have received a copy of the GNU Library General Public *
18
* License along with FreeCAD; if not, write to the Free Software *
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22
***************************************************************************/
30
#if defined(FC_OS_WIN32)
34
#if defined(FC_OS_LINUX) || defined(FC_OS_BSD)
39
# include <mach-o/dyld.h>
45
#endif // HAVE_CONFIG_H
53
#include <Base/ConsoleObserver.h>
54
#include <Base/Exception.h>
55
#include <Base/PyObjectBase.h>
56
#include <Base/Sequencer.h>
57
#include <App/Application.h>
59
#if defined(FC_OS_WIN32)
61
/** DllMain is called when DLL is loaded
63
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
65
switch (ul_reason_for_call) {
66
case DLL_PROCESS_ATTACH: {
67
// This name is preliminary, we pass it to Application::init() in initFreeCAD()
68
// which does the rest.
69
char szFileName [MAX_PATH];
70
GetModuleFileNameA((HMODULE)hModule, szFileName, MAX_PATH-1);
71
App::Application::Config()["AppHomePath"] = szFileName;
80
#elif defined(FC_OS_LINUX) || defined(FC_OS_BSD)
85
#elif defined(FC_OS_CYGWIN)
89
PyMOD_INIT_FUNC(FreeCAD)
91
// Init phase ===========================================================
92
App::Application::Config()["ExeName"] = "FreeCAD";
93
App::Application::Config()["ExeVendor"] = "FreeCAD";
94
App::Application::Config()["AppDataSkipVendor"] = "true";
98
#if defined(FC_OS_WIN32)
99
path = App::Application::Config()["AppHomePath"].c_str();
100
#elif defined(FC_OS_CYGWIN)
101
HMODULE hModule = GetModuleHandle("FreeCAD.dll");
102
char szFileName [MAX_PATH];
103
GetModuleFileNameA(hModule, szFileName, MAX_PATH-1);
105
#elif defined(FC_OS_LINUX) || defined(FC_OS_BSD)
108
// get whole path of the library
110
int ret = dladdr((void*)PyInit_FreeCAD, &info);
111
if ((ret == 0) || (!info.dli_fname)) {
112
PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
116
path = info.dli_fname;
117
// this is a workaround to avoid a crash in libuuid.so
118
#elif defined(FC_OS_MACOSX)
120
// The MacOS approach uses the Python sys.path list to find the path
121
// to FreeCAD.so - this should be OS-agnostic, except these two
122
// strings, and the call to access().
123
const static char libName[] = "/FreeCAD.so";
124
const static char upDir[] = "/../";
126
PyObject *pySysPath = PySys_GetObject("path");
127
if ( PyList_Check(pySysPath) ) {
129
// pySysPath should be a *PyList of strings - iterate through it
130
// backwards since the FreeCAD path was likely appended just before
132
for (i = PyList_Size(pySysPath) - 1; i >= 0 ; --i) {
133
const char *basePath;
134
PyObject *pyPath = PyList_GetItem(pySysPath, i);
137
if ( PyUnicode_Check(pyPath) ) {
139
basePath = PyUnicode_AsUTF8AndSize(pyPath, &sz);
145
if (sz + sizeof(libName) > PATH_MAX) {
151
// append libName to the path
152
if (access(path + libName, R_OK | X_OK) == 0) {
154
// The FreeCAD "home" path is one level up from
155
// libName, so replace libName with upDir.
159
} // end for (i = PyList_Size(pySysPath) - 1; i >= 0 ; --i) {
160
} // end if ( PyList_Check(pySysPath) ) {
162
if (path.isEmpty()) {
163
PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!");
168
# error "Implement: Retrieve the path of the module for your platform."
171
std::vector<char*> argv;
172
argv.push_back(path.data());
175
// Inits the Application
176
App::Application::init(argc, argv.data());
178
catch (const Base::Exception& e) {
179
std::string appName = App::Application::Config()["ExeName"];
180
std::stringstream msg;
181
msg << "While initializing " << appName << " the following exception occurred: '"
182
<< e.what() << "'\n\n";
183
msg << "\nPlease contact the application's support team for more information.\n\n";
184
printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str());
187
Base::EmptySequencer* seq = new Base::EmptySequencer();
189
static Base::RedirectStdOutput stdcout;
190
static Base::RedirectStdLog stdclog;
191
static Base::RedirectStdError stdcerr;
192
std::cout.rdbuf(&stdcout);
193
std::clog.rdbuf(&stdclog);
194
std::cerr.rdbuf(&stdcerr);
196
//PyObject* module = _PyImport_FindBuiltin("FreeCAD");
197
PyObject* modules = PyImport_GetModuleDict();
198
PyObject* module = PyDict_GetItemString(modules, "FreeCAD");
200
PyErr_SetString(PyExc_ImportError, "Failed to load FreeCAD module!");