From 5c971ce281d1eb7ca032ff23369a52e7783a29f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Wed, 3 Jul 2024 09:56:32 +0200 Subject: [PATCH] Add `BARE_ANDROID_USE_LOGCAT` build option (#34) --- CMakeLists.txt | 26 +++++++++++++++++++------- cmake/options.cmake | 1 + src/runtime.c | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 cmake/options.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 92494782..1094621d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ include(bare) bare_target(target) +include(cmake/options.cmake) + install_node_modules(LOCKFILE) mirror_drive( @@ -15,11 +17,9 @@ mirror_drive( CHECKOUT 141 ) -if(MSVC) +if(target MATCHES "win32") add_compile_options(/MT$<$:d>) -endif() -if(WIN32) add_definitions(-DWIN32_LEAN_AND_MEAN) endif() @@ -89,12 +89,10 @@ if(NOT TARGET v8) m ) elseif(target MATCHES "android") - find_library(log log) - target_link_libraries( v8 INTERFACE - ${log} + log ) elseif(target MATCHES "win32") target_link_libraries( @@ -206,7 +204,7 @@ target_link_libraries( hex ) -if(MSVC) +if(target MATCHES "win32") target_compile_options( bare PRIVATE @@ -214,6 +212,20 @@ if(MSVC) ) endif() +if(target MATCHES "android") + target_compile_definitions( + bare + PRIVATE + BARE_ANDROID_USE_LOGCAT=$ + ) + + target_link_libraries( + bare + PUBLIC + log + ) +endif() + add_library(bare_shared SHARED) set_target_properties( diff --git a/cmake/options.cmake b/cmake/options.cmake new file mode 100644 index 00000000..123511e6 --- /dev/null +++ b/cmake/options.cmake @@ -0,0 +1 @@ +option(BARE_ANDROID_USE_LOGCAT "Use Logcat instead of standard I/O for logging on Android" OFF) diff --git a/src/runtime.c b/src/runtime.c index 6b55667a..dc01a0b0 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -10,6 +10,10 @@ #include "../include/bare.h" +#if BARE_ANDROID_USE_LOGCAT +#include +#endif + #include "addon.h" #include "bare.js.h" #include "runtime.h" @@ -72,8 +76,13 @@ err: { err = js_close_handle_scope(env, scope); assert(err == 0); +#if BARE_ANDROID_USE_LOGCAT + err = __android_log_print(ANDROID_LOG_FATAL, "bare", "Uncaught %s", str); + assert(err == 1); +#else err = fprintf(stderr, "Uncaught %s\n", str); assert(err >= 0); +#endif abort(); } @@ -130,8 +139,13 @@ err: { err = js_close_handle_scope(env, scope); assert(err == 0); +#if BARE_ANDROID_USE_LOGCAT + err = __android_log_print(ANDROID_LOG_FATAL, "bare", "Uncaught (in promise) %s", str); + assert(err == 1); +#else err = fprintf(stderr, "Uncaught (in promise) %s\n", str); assert(err >= 0); +#endif abort(); } @@ -454,8 +468,13 @@ bare_runtime_print_info (js_env_t *env, js_callback_info_t *info) { err = js_close_handle_scope(env, scope); assert(err == 0); +#if BARE_ANDROID_USE_LOGCAT + err = __android_log_print(ANDROID_LOG_INFO, "bare", "%s", data); + assert(err == 1); +#else err = fprintf(stdout, "%s", data); assert(err >= 0); +#endif err = fflush(stdout); assert(err == 0); @@ -494,8 +513,13 @@ bare_runtime_print_error (js_env_t *env, js_callback_info_t *info) { err = js_close_handle_scope(env, scope); assert(err == 0); +#if BARE_ANDROID_USE_LOGCAT + err = __android_log_print(ANDROID_LOG_ERROR, "bare", "%s", data); + assert(err == 1); +#else err = fprintf(stderr, "%s", data); assert(err >= 0); +#endif err = fflush(stderr); assert(err == 0);