Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
make jni libs Android-agnostic to be able to write robolectric tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Anh Bui authored and facebook-github-bot committed Jan 21, 2021
1 parent 14f0d34 commit e883b6e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 10 additions & 0 deletions deps/build/build.cpp
Expand Up @@ -3,22 +3,29 @@
#include <stdlib.h>
#include <string.h>

#ifdef __ANDROID__
#include <sys/system_properties.h>
#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);
return atoi(sdk_version_str);
})();

return android_sdk;
#else
return 0;
#endif
}

bool isArt() {
#ifdef __ANDROID__
int sdk = getAndroidSdk();
if (sdk >= 21) { // Lollipop (5.0)
return true;
Expand All @@ -33,6 +40,9 @@ bool isArt() {

return running_art;
}
#else
return false;
#endif
}

bool isDalvik() {
Expand Down
36 changes: 36 additions & 0 deletions deps/cjni/log.h
Expand Up @@ -15,7 +15,43 @@
*/

#pragma once
#ifdef ANDROID
#include <android/log.h>
#else
// These declarations are needed for our internal use even on non-Android
// builds.
// (they are borrowed from <android/log.h>)

/*
* 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__)
Expand Down
1 change: 0 additions & 1 deletion deps/linker/elfSharedLibData.cpp
Expand Up @@ -20,7 +20,6 @@

#include <build/build.h>

#include <sys/system_properties.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdexcept>
Expand Down
4 changes: 3 additions & 1 deletion deps/linker/sharedlibs.cpp
Expand Up @@ -19,11 +19,13 @@

#include <build/build.h>

#include <sys/system_properties.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unordered_map>
#include <libgen.h>
#ifndef __ANDROID__
char * basename(char const* path);
#endif

#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */

Expand Down

0 comments on commit e883b6e

Please sign in to comment.