1a06f7ccaSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a06f7ccaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a06f7ccaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a06f7ccaSAndrew Rist  * distributed with this work for additional information
6a06f7ccaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a06f7ccaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a06f7ccaSAndrew Rist  * "License"); you may not use this file except in compliance
9a06f7ccaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a06f7ccaSAndrew Rist  *
11a06f7ccaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a06f7ccaSAndrew Rist  *
13a06f7ccaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a06f7ccaSAndrew Rist  * software distributed under the License is distributed on an
15a06f7ccaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a06f7ccaSAndrew Rist  * KIND, either express or implied.  See the License for the
17a06f7ccaSAndrew Rist  * specific language governing permissions and limitations
18a06f7ccaSAndrew Rist  * under the License.
19a06f7ccaSAndrew Rist  *
20a06f7ccaSAndrew Rist  *************************************************************/
21a06f7ccaSAndrew Rist 
22a06f7ccaSAndrew Rist 
23cdf0e10cSrcweir 
24*bee4b995SDamjan Jovanovic #ifndef Py_PYTHON_H
25*bee4b995SDamjan Jovanovic #if defined _MSC_VER
26*bee4b995SDamjan Jovanovic #pragma warning(push, 1)
27*bee4b995SDamjan Jovanovic #endif
28*bee4b995SDamjan Jovanovic #ifdef _DEBUG
29*bee4b995SDamjan Jovanovic #undef _DEBUG
30*bee4b995SDamjan Jovanovic #include <Python.h>
31*bee4b995SDamjan Jovanovic #define _DEBUG
32*bee4b995SDamjan Jovanovic #else
33*bee4b995SDamjan Jovanovic #include <Python.h>
34*bee4b995SDamjan Jovanovic #endif // #ifdef _DEBUG
35*bee4b995SDamjan Jovanovic #if defined _MSC_VER
36*bee4b995SDamjan Jovanovic #pragma warning(pop)
37*bee4b995SDamjan Jovanovic #endif
38*bee4b995SDamjan Jovanovic #endif // #ifdef Py_PYTHON_H
39*bee4b995SDamjan Jovanovic 
40cdf0e10cSrcweir #include <rtl/string.h>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <stdlib.h>
43cdf0e10cSrcweir #include <string.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifdef LINUX
46cdf0e10cSrcweir #  ifndef __USE_GNU
47cdf0e10cSrcweir #  define __USE_GNU
48cdf0e10cSrcweir #  endif
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #include <dlfcn.h>
51cdf0e10cSrcweir 
52*bee4b995SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
PyInit_pyuno(void)53*bee4b995SDamjan Jovanovic SAL_DLLPUBLIC_EXPORT void* PyInit_pyuno(void)
54*bee4b995SDamjan Jovanovic #else
5558c70741SJim Jagielski SAL_DLLPUBLIC_EXPORT void initpyuno ()
56*bee4b995SDamjan Jovanovic #endif
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     Dl_info dl_info;
59*bee4b995SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
60*bee4b995SDamjan Jovanovic     void* (*func)(void);
61*bee4b995SDamjan Jovanovic #else
62cdf0e10cSrcweir     void (*func)(void);
63*bee4b995SDamjan Jovanovic #endif
64cdf0e10cSrcweir 
65*bee4b995SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
66*bee4b995SDamjan Jovanovic     if (dladdr((void*)&PyInit_pyuno, &dl_info) != 0) {
67*bee4b995SDamjan Jovanovic #else
68cdf0e10cSrcweir     if (dladdr((void*)&initpyuno, &dl_info) != 0) {
69*bee4b995SDamjan Jovanovic #endif
70cdf0e10cSrcweir         void* h = 0;
71cdf0e10cSrcweir 	size_t len = strrchr(dl_info.dli_fname, '/') - dl_info.dli_fname + 1;
72cdf0e10cSrcweir 	char* libname = malloc(len + RTL_CONSTASCII_LENGTH( SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION ) + 1);
73cdf0e10cSrcweir         strncpy(libname, dl_info.dli_fname, len);
74cdf0e10cSrcweir         strcpy(libname + (len), SAL_DLLPREFIX "pyuno" SAL_DLLEXTENSION);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         h = dlopen (libname, RTLD_NOW | RTLD_GLOBAL);
77cdf0e10cSrcweir 	free(libname);
78cdf0e10cSrcweir         if( h )
79cdf0e10cSrcweir         {
80*bee4b995SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
81*bee4b995SDamjan Jovanovic             func = (void* (*)(void))dlsym (h, "PyInit_pyuno");
82*bee4b995SDamjan Jovanovic #else
83*bee4b995SDamjan Jovanovic             func = (void (*)(void))dlsym (h, "initpyuno");
84*bee4b995SDamjan Jovanovic #endif
85*bee4b995SDamjan Jovanovic             return (func) ();
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88*bee4b995SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
89*bee4b995SDamjan Jovanovic     return NULL;
90*bee4b995SDamjan Jovanovic #endif
91cdf0e10cSrcweir }
92