| 1 | // Copyright David Abrahams 2002. |
|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 5 | #ifdef _AIX |
|---|
| 6 | #include <cstdio> |
|---|
| 7 | #include <cstdlib> |
|---|
| 8 | |
|---|
| 9 | extern "C" |
|---|
| 10 | { |
|---|
| 11 | #include <sys/stat.h> |
|---|
| 12 | #include <unistd.h> |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | # include <string> |
|---|
| 16 | # include <boost/python/detail/wrap_python.hpp> |
|---|
| 17 | # include <boost/python/errors.hpp> |
|---|
| 18 | # include <boost/python/detail/aix_init_module.hpp> |
|---|
| 19 | # include <boost/python/module.hpp> |
|---|
| 20 | |
|---|
| 21 | namespace boost { namespace python { namespace detail { |
|---|
| 22 | |
|---|
| 23 | namespace |
|---|
| 24 | { |
|---|
| 25 | static PyMethodDef initial_methods[] = { { 0, 0, 0, 0 } }; |
|---|
| 26 | extern "C" void initlibboost_python() |
|---|
| 27 | { |
|---|
| 28 | Py_InitModule("libboost_python", initial_methods); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | struct find_and_open_file |
|---|
| 32 | { |
|---|
| 33 | FILE* fp; |
|---|
| 34 | std::string libpath; // -- search path |
|---|
| 35 | std::string filename; // -- filename to look for |
|---|
| 36 | std::string fullpath; // -- full path to file |
|---|
| 37 | |
|---|
| 38 | find_and_open_file( |
|---|
| 39 | const std::string& libpath_env |
|---|
| 40 | , const std::string& file); |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | find_and_open_file::find_and_open_file( |
|---|
| 44 | const std::string& libpath_env |
|---|
| 45 | , const std::string& file) |
|---|
| 46 | : fp(0) |
|---|
| 47 | { |
|---|
| 48 | char* value = std::getenv(libpath_env.c_str()); |
|---|
| 49 | |
|---|
| 50 | if(value == 0) |
|---|
| 51 | return; |
|---|
| 52 | |
|---|
| 53 | libpath = value; |
|---|
| 54 | |
|---|
| 55 | if (libpath == "") |
|---|
| 56 | return; |
|---|
| 57 | |
|---|
| 58 | std::string::size_type pos = 0, prev_pos = 0; |
|---|
| 59 | |
|---|
| 60 | // -- loop through all search paths looking for file |
|---|
| 61 | while((pos = libpath.find_first_of(":",pos)) != std::string::npos) |
|---|
| 62 | { |
|---|
| 63 | fullpath = libpath.substr(prev_pos,pos - prev_pos) + "/" + file; |
|---|
| 64 | if (::access(fullpath.c_str(), R_OK) == 0) |
|---|
| 65 | { |
|---|
| 66 | struct stat filestat; |
|---|
| 67 | ::stat(fullpath.c_str(), &filestat); |
|---|
| 68 | if (!S_ISDIR(filestat.st_mode)) |
|---|
| 69 | { |
|---|
| 70 | fp = std::fopen(fullpath.c_str(), "r"); |
|---|
| 71 | if (fp) |
|---|
| 72 | { |
|---|
| 73 | filename = file; |
|---|
| 74 | } |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | prev_pos = ++pos; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // -- mop up odd path |
|---|
| 82 | if (libpath.find_first_of(":", prev_pos) == std::string::npos) |
|---|
| 83 | { |
|---|
| 84 | fullpath = libpath.substr(prev_pos, libpath.size() - prev_pos) + "/" + file; |
|---|
| 85 | if (::access(fullpath.c_str(), R_OK) == 0) |
|---|
| 86 | { |
|---|
| 87 | struct stat filestat; |
|---|
| 88 | ::stat(fullpath.c_str(),&filestat); |
|---|
| 89 | if (!S_ISDIR(filestat.st_mode)) |
|---|
| 90 | { |
|---|
| 91 | fp = std::fopen(fullpath.c_str(), "r"); |
|---|
| 92 | filename = file; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void aix_init_module( |
|---|
| 100 | so_load_function load_dynamic_module |
|---|
| 101 | , char const* module_name |
|---|
| 102 | , void (*init_module)()) |
|---|
| 103 | { |
|---|
| 104 | static bool initialized; |
|---|
| 105 | if (!initialized) |
|---|
| 106 | { |
|---|
| 107 | char const* const name = "libboost_python.so"; |
|---|
| 108 | find_and_open_file dynlib("LIBPATH", name); |
|---|
| 109 | if (dynlib.fp == 0) |
|---|
| 110 | { |
|---|
| 111 | fprintf(stderr, " Error: could not find %s\n", name); |
|---|
| 112 | return; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | std::string::size_type pos = pos = dynlib.filename.rfind(".so"); |
|---|
| 116 | if (pos != dynlib.filename.size() - 3) |
|---|
| 117 | { |
|---|
| 118 | fprintf(stderr, "dynamic library %s must end with .so\n", dynlib.filename.c_str()); |
|---|
| 119 | return; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | PyObject* m = |
|---|
| 123 | load_dynamic_module( |
|---|
| 124 | const_cast<char*>(dynlib.filename.substr(0,pos).c_str()), |
|---|
| 125 | const_cast<char*>(dynlib.fullpath.c_str()), |
|---|
| 126 | dynlib.fp); |
|---|
| 127 | |
|---|
| 128 | if (m == 0) |
|---|
| 129 | { |
|---|
| 130 | fprintf(stderr, "failed to load library %s\n", name); |
|---|
| 131 | return; |
|---|
| 132 | } |
|---|
| 133 | Py_DECREF(m); |
|---|
| 134 | |
|---|
| 135 | initialized = true; |
|---|
| 136 | } |
|---|
| 137 | python::detail::init_module(module_name, init_module); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | }}} // namespace boost::python |
|---|
| 141 | #endif |
|---|