|
41 | 41 | #include <math.h> |
42 | 42 | #include <unistd.h> |
43 | 43 | #include <pthread.h> |
44 | | -#ifdef __linux__ |
| 44 | +#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) |
45 | 45 | #include <dlfcn.h> |
| 46 | +#define NEED_DLOPEN_GLOBAL 1 |
46 | 47 | #endif |
47 | 48 |
|
48 | 49 | /* ============================================================================ |
@@ -465,18 +466,24 @@ static ERL_NIF_TERM nif_py_init(ErlNifEnv *env, int argc, const ERL_NIF_TERM arg |
465 | 466 | return ATOM_OK; |
466 | 467 | } |
467 | 468 |
|
468 | | -#ifdef __linux__ |
469 | | - /* On Linux, we need to load libpython with RTLD_GLOBAL so that Python |
| 469 | +#ifdef NEED_DLOPEN_GLOBAL |
| 470 | + /* On Linux/FreeBSD/etc, we need to load libpython with RTLD_GLOBAL so that Python |
470 | 471 | * extension modules can find Python symbols when dynamically loaded. |
471 | 472 | * Without this, modules like _socket.so fail with "undefined symbol: PyByteArray_Type" */ |
472 | 473 | { |
473 | 474 | char libpython[256]; |
474 | | - snprintf(libpython, sizeof(libpython), "libpython%d.%d.so.1.0", |
475 | | - PY_MAJOR_VERSION, PY_MINOR_VERSION); |
476 | | - void *handle = dlopen(libpython, RTLD_NOW | RTLD_GLOBAL); |
477 | | - if (!handle) { |
478 | | - /* Try without .1.0 suffix */ |
479 | | - snprintf(libpython, sizeof(libpython), "libpython%d.%d.so", |
| 475 | + void *handle = NULL; |
| 476 | + |
| 477 | + /* Try various library name patterns */ |
| 478 | + const char *patterns[] = { |
| 479 | + "libpython%d.%d.so.1.0", /* Linux with full version */ |
| 480 | + "libpython%d.%d.so", /* Linux/FreeBSD */ |
| 481 | + "libpython%d.%d.so.1", /* Some systems */ |
| 482 | + NULL |
| 483 | + }; |
| 484 | + |
| 485 | + for (int i = 0; patterns[i] && !handle; i++) { |
| 486 | + snprintf(libpython, sizeof(libpython), patterns[i], |
480 | 487 | PY_MAJOR_VERSION, PY_MINOR_VERSION); |
481 | 488 | handle = dlopen(libpython, RTLD_NOW | RTLD_GLOBAL); |
482 | 489 | } |
|
0 commit comments