From e883b6ede9320bfc08d4ceb71a303df80d24f816 Mon Sep 17 00:00:00 2001 From: Anh Bui Date: Wed, 20 Jan 2021 17:11:23 -0800 Subject: [PATCH] make jni libs Android-agnostic to be able to write robolectric tests Summary: - in the next diff D25963204, unit test with Robolectric is added that utilize these jni deps - make these jni deps Android-aware to be able to run Robolectric tests (basically shimming the necessary deps when `#ifndef __ANDROID__`) Differential Revision: D25921303 fbshipit-source-id: 16feca072155543c72a5362f746e81d7b8109dd4 --- deps/build/build.cpp | 10 +++++++++ deps/cjni/log.h | 36 ++++++++++++++++++++++++++++++++ deps/linker/elfSharedLibData.cpp | 1 - deps/linker/sharedlibs.cpp | 4 +++- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/deps/build/build.cpp b/deps/build/build.cpp index 4b07cd0ef..0886472d2 100644 --- a/deps/build/build.cpp +++ b/deps/build/build.cpp @@ -3,12 +3,15 @@ #include #include +#ifdef __ANDROID__ #include +#endif namespace facebook { namespace build { int getAndroidSdk() { +#ifdef __ANDROID__ static auto android_sdk = ([] { char sdk_version_str[PROP_VALUE_MAX]; __system_property_get("ro.build.version.sdk", sdk_version_str); @@ -16,9 +19,13 @@ int getAndroidSdk() { })(); return android_sdk; +#else + return 0; +#endif } bool isArt() { +#ifdef __ANDROID__ int sdk = getAndroidSdk(); if (sdk >= 21) { // Lollipop (5.0) return true; @@ -33,6 +40,9 @@ bool isArt() { return running_art; } +#else + return false; +#endif } bool isDalvik() { diff --git a/deps/cjni/log.h b/deps/cjni/log.h index 0fc46fc2b..eead1d3dd 100644 --- a/deps/cjni/log.h +++ b/deps/cjni/log.h @@ -15,7 +15,43 @@ */ #pragma once +#ifdef ANDROID #include +#else +// These declarations are needed for our internal use even on non-Android +// builds. +// (they are borrowed from ) + +/* + * Android log priority values, in ascending priority order. + */ +typedef enum android_LogPriority { + ANDROID_LOG_UNKNOWN = 0, + ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */ + ANDROID_LOG_VERBOSE, + ANDROID_LOG_DEBUG, + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR, + ANDROID_LOG_FATAL, + ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */ +} android_LogPriority; + +/* + * Send a simple string to the log. + */ +int __android_log_write(int prio, const char *tag, const char *text); + +/* + * Send a formatted string to the log, used like printf(fmt,...) + */ +int __android_log_print(int prio, const char *tag, const char *fmt, ...) +#if defined(__GNUC__) + __attribute__((format(printf, 3, 4))) +#endif + ; + +#endif #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) diff --git a/deps/linker/elfSharedLibData.cpp b/deps/linker/elfSharedLibData.cpp index 979892b1e..ea92e95c8 100644 --- a/deps/linker/elfSharedLibData.cpp +++ b/deps/linker/elfSharedLibData.cpp @@ -20,7 +20,6 @@ #include -#include #include #include #include diff --git a/deps/linker/sharedlibs.cpp b/deps/linker/sharedlibs.cpp index c5d785637..9201055a2 100644 --- a/deps/linker/sharedlibs.cpp +++ b/deps/linker/sharedlibs.cpp @@ -19,11 +19,13 @@ #include -#include #include #include #include #include +#ifndef __ANDROID__ +char * basename(char const* path); +#endif #define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */