From d9d07770519c24d4b236e69a8c6efce9d7c835d8 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 28 Apr 2022 13:31:28 +0900 Subject: [PATCH] Move dlfcn.h availability check to platform_common.h (#1134) --- .../shared/platform/include/platform_common.h | 8 +++++++ .../shared/platform/nuttx/platform_internal.h | 6 ++++++ product-mini/platforms/posix/main.c | 21 ++++++++++--------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index 3f7d0c68..9890202d 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -65,6 +65,14 @@ BH_VPRINTF(const char *format, va_list ap); #define NULL (void *)0 #endif +#if !defined(BH_HAS_DLFCN) +#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#define BH_HAS_DLFCN 1 +#else +#define BH_HAS_DLFCN 0 +#endif +#endif + #ifndef __cplusplus #ifndef true diff --git a/core/shared/platform/nuttx/platform_internal.h b/core/shared/platform/nuttx/platform_internal.h index a4a2b697..7acfc730 100644 --- a/core/shared/platform/nuttx/platform_internal.h +++ b/core/shared/platform/nuttx/platform_internal.h @@ -41,6 +41,12 @@ typedef pthread_t korp_thread; #define os_printf printf #define os_vprintf vprintf +#if defined(CONFIG_LIBC_DLFCN) +#define BH_HAS_DLFCN 1 +#else +#define BH_HAS_DLFCN 0 +#endif + /* On NuttX, time_t is uint32_t */ #define BH_TIME_T_MAX 0xffffffff diff --git a/product-mini/platforms/posix/main.c b/product-mini/platforms/posix/main.c index 71cb278a..c78ef399 100644 --- a/product-mini/platforms/posix/main.c +++ b/product-mini/platforms/posix/main.c @@ -8,14 +8,15 @@ #endif #include #include -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) -#include -#endif #include "bh_platform.h" #include "bh_read_file.h" #include "wasm_export.h" +#if BH_HAS_DLFCN +#include +#endif + static int app_argc; static char **app_argv; @@ -47,7 +48,7 @@ print_help() printf(" for example:\n"); printf(" --addr-pool=1.2.3.4/15,2.3.4.5/16\n"); #endif -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN printf(" --native-lib= Register native libraries to the WASM module, which\n"); printf(" are shared object (.so) files, for example:\n"); printf(" --native-lib=test1.so --native-lib=test2.so\n"); @@ -182,7 +183,7 @@ validate_env_str(char *env) } #endif -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN typedef uint32 (*get_native_lib_func)(char **p_module_name, NativeSymbol **p_native_symbols); @@ -232,7 +233,7 @@ load_and_register_native_libs(const char **native_lib_list, return native_handle_count; } -#endif /* end of defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) */ +#endif /* BH_HAS_DLFCN */ #if WASM_ENABLE_MULTI_MODULE != 0 static char * @@ -309,7 +310,7 @@ main(int argc, char *argv[]) const char *addr_pool[8] = { NULL }; uint32 addr_pool_size = 0; #endif -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN const char *native_lib_list[8] = { NULL }; uint32 native_lib_count = 0; void *native_handle_list[8] = { NULL }; @@ -403,7 +404,7 @@ main(int argc, char *argv[]) } } #endif /* WASM_ENABLE_LIBC_WASI */ -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN else if (!strncmp(argv[0], "--native-lib=", 13)) { if (argv[0][13] == '\0') return print_help(); @@ -485,7 +486,7 @@ main(int argc, char *argv[]) bh_log_set_verbose_level(log_verbose_level); #endif -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN native_handle_count = load_and_register_native_libs( native_lib_list, native_lib_count, native_handle_list); #endif @@ -564,7 +565,7 @@ fail2: os_munmap(wasm_file_buf, wasm_file_size); fail1: -#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) +#if BH_HAS_DLFCN /* unload the native libraries */ for (native_handle_idx = 0; native_handle_idx < native_handle_count; native_handle_idx++)