xref: /trunk/main/pyuno/source/module/pyuno_dlopenwrapper.c (revision 534d93521fb9d960038706348aeef53f37423a94)
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
10cdf0e10cSrcweir  *
11a06f7ccaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
20a06f7ccaSAndrew Rist  *************************************************************/
21a06f7ccaSAndrew Rist 
22a06f7ccaSAndrew Rist 
23cdf0e10cSrcweir 
24*29716487SDamjan Jovanovic #ifndef Py_PYTHON_H
25*29716487SDamjan Jovanovic #if defined _MSC_VER
26*29716487SDamjan Jovanovic #pragma warning(push, 1)
27*29716487SDamjan Jovanovic #endif
28*29716487SDamjan Jovanovic #ifdef _DEBUG
29*29716487SDamjan Jovanovic #undef _DEBUG
30*29716487SDamjan Jovanovic #include <Python.h>
31*29716487SDamjan Jovanovic #define _DEBUG
32*29716487SDamjan Jovanovic #else
33*29716487SDamjan Jovanovic #include <Python.h>
34*29716487SDamjan Jovanovic #endif // #ifdef _DEBUG
35*29716487SDamjan Jovanovic #if defined _MSC_VER
36*29716487SDamjan Jovanovic #pragma warning(pop)
37*29716487SDamjan Jovanovic #endif
38*29716487SDamjan Jovanovic #endif // #ifdef Py_PYTHON_H
39*29716487SDamjan 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*29716487SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
PyInit_pyuno(void)53*29716487SDamjan Jovanovic SAL_DLLPUBLIC_EXPORT void* PyInit_pyuno(void)
54*29716487SDamjan Jovanovic #else
551c25f252Sdamjan SAL_DLLPUBLIC_EXPORT void initpyuno ()
56*29716487SDamjan Jovanovic #endif
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     Dl_info dl_info;
59*29716487SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
60*29716487SDamjan Jovanovic     void* (*func)(void);
61*29716487SDamjan Jovanovic #else
62cdf0e10cSrcweir     void (*func)(void);
63*29716487SDamjan Jovanovic #endif
64cdf0e10cSrcweir 
65*29716487SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
66*29716487SDamjan Jovanovic     if (dladdr((void*)&PyInit_pyuno, &dl_info) != 0) {
67*29716487SDamjan Jovanovic #else
68cdf0e10cSrcweir     if (dladdr((void*)&initpyuno, &dl_info) != 0) {
69*29716487SDamjan 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*29716487SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
81*29716487SDamjan Jovanovic             func = (void* (*)(void))dlsym (h, "PyInit_pyuno");
82*29716487SDamjan Jovanovic #else
83*29716487SDamjan Jovanovic             func = (void (*)(void))dlsym (h, "initpyuno");
84*29716487SDamjan Jovanovic #endif
85*29716487SDamjan Jovanovic             return (func) ();
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88*29716487SDamjan Jovanovic #if PY_MAJOR_VERSION >= 3
89*29716487SDamjan Jovanovic     return NULL;
90*29716487SDamjan Jovanovic #endif
91cdf0e10cSrcweir }
92