[android] Enable compile with clang

Glue with libBlocksRuntime and compile with
minimum API 23 which works with current HF
ptrace backend.

Use 'ANDROID_CLANG=true' flag to enable
clang builds (default off)

Signed-off-by: Anestis Bechtsoudis <anestis@census-labs.com>

# third_party/android/scripts/compile-libBlocksRuntime.sh third_party/android/libBlocksRuntime arm
# make android ANDROID_CLANG=true
diff --git a/Makefile b/Makefile
index 9ac4155..4af53dd 100644
--- a/Makefile
+++ b/Makefile
@@ -145,14 +145,32 @@
 
 # Control Android builds
 ANDROID_DEBUG_ENABLED ?= false
+ANDROID_CLANG         ?= false
 ANDROID_APP_ABI       ?= armeabi-v7a
-ANDROID_API           ?= android-21
-ANDROID_NDK_TOOLCHAIN ?=
 NDK_BUILD_ARGS :=
 ifeq ($(ANDROID_DEBUG_ENABLED),true)
     NDK_BUILD_ARGS += V=1 NDK_DEBUG=1 APP_OPTIM=debug
 endif
 
+ifeq ($(ANDROID_CLANG),true)
+  # clang works only for APIs >= 23, so default to it if not set
+  ANDROID_API ?= android-23
+  ifeq ($(ANDROID_APP_ABI),armeabi-v7a)
+    ANDROID_NDK_TOOLCHAIN ?= arm-linux-androideabi-clang3.6
+  else ifeq ($(ANDROID_APP_ABI),x86)
+    ANDROID_NDK_TOOLCHAIN ?= x86-clang3.6
+  else ifeq ($(ANDROID_APP_ABI),arm64-v8a)
+    ANDROID_NDK_TOOLCHAIN ?= aarch64-linux-android-clang3.6
+  else ifeq ($(ANDROID_APP_ABI),x86_64)
+    ANDROID_NDK_TOOLCHAIN ?= x86_64-clang3.6
+  else
+    $(error Unsuported / Unknown APP_API '$(ANDROID_APP_ABI)')
+  endif
+else
+  ANDROID_API           ?= android-21
+  ANDROID_NDK_TOOLCHAIN ?=
+endif
+
 SUBDIR_ROOTS := linux mac posix libraries
 DIRS := . $(shell find $(SUBDIR_ROOTS) -type d)
 CLEAN_PATTERNS := *.o *~ core *.a *.dSYM *.la *.so *.dylib
diff --git a/android/Android.mk b/android/Android.mk
index aab3f1e..a8003b2 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -15,15 +15,9 @@
 
 LOCAL_PATH := $(abspath $(call my-dir)/..)
 
-# Enable Linux ptrace() instead of POSIX signal interface by default 
+# Enable Linux ptrace() instead of POSIX signal interface by default
 ANDROID_WITH_PTRACE ?= true
 
-# Make sure compiler toolchain is compatible / supported
-ifneq (,$(findstring clang,$(NDK_TOOLCHAIN)))
-  $(error Clang toolchains are not supported yet. Clang uses __aeabi_read_tp to \
-  implement thread_local, which isn't supported by bionic [$(NDK_TOOLCHAIN)])
-endif
-
 ifeq ($(ANDROID_WITH_PTRACE),true)
   ifeq ($(APP_ABI),$(filter $(APP_ABI),armeabi armeabi-v7a))
     ARCH_ABI := arm
@@ -91,6 +85,18 @@
   include $(PREBUILT_STATIC_LIBRARY)
 endif
 
+ifneq (,$(findstring clang,$(NDK_TOOLCHAIN)))
+  LIBBRT_A := third_party/android/libBlocksRuntime/$(ARCH_ABI)/libblocksruntime.a
+  ifeq ("$(wildcard $(LIBBRT_A))","")
+    $(error libBlocksRuntime is missing. Please execute \
+            'third_party/android/scripts/compile-libBlocksRuntime.sh third_party/android/libBlocksRuntime $(ARCH_ABI)')
+  endif
+  include $(CLEAR_VARS)
+  LOCAL_MODULE := libblocksruntime
+  LOCAL_SRC_FILES := $(LIBBRT_A)
+  include $(PREBUILT_STATIC_LIBRARY)
+endif
+
 # Main honggfuzz module
 include $(CLEAR_VARS)
 
@@ -126,4 +132,9 @@
 LOCAL_SRC_FILES += $(ARCH_SRCS)
 LOCAL_CFLAGS += -D_HF_ARCH_${ARCH}
 
+ifneq (,$(findstring clang,$(NDK_TOOLCHAIN)))
+  LOCAL_CFLAGS += -fblocks
+  LOCAL_STATIC_LIBRARIES += libblocksruntime
+endif
+
 include $(BUILD_EXECUTABLE)