Fix compatibility across the board

Change-Id: I6376920775ddabb4d4af505fffd86e404403a64a
diff --git a/Android.mk b/Android.mk
index c28426a..1e894f1 100644
--- a/Android.mk
+++ b/Android.mk
@@ -119,7 +119,10 @@
 LOCAL_SHARED_LIBRARIES += libcrecovery
 
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_SHARED_LIBRARIES += libstlport libmincrypttwrp
+    LOCAL_SHARED_LIBRARIES += libstlport
+endif
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0)
+    LOCAL_SHARED_LIBRARIES += libmincrypttwrp
     LOCAL_C_INCLUDES += $(LOCAL_PATH)/libmincrypt/includes
     LOCAL_CFLAGS += -DUSE_OLD_VERIFIER
 else
@@ -563,12 +566,18 @@
 LOCAL_SRC_FILES := adb_install.cpp asn1_decoder.cpp bootloader.cpp legacy_property_service.cpp set_metadata.cpp tw_atomic.cpp
 LOCAL_SHARED_LIBRARIES += libc liblog libcutils libmtdutils libfusesideload libselinux
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_SHARED_LIBRARIES += libstdc++ libstlport libmincrypttwrp
-    LOCAL_C_INCLUDES := bionic external/stlport/stlport $(LOCAL_PATH)/libmincrypt/includes
+    LOCAL_SHARED_LIBRARIES += libstdc++ libstlport
+    LOCAL_C_INCLUDES := bionic external/stlport/stlport
+else
+    LOCAL_SHARED_LIBRARIES += libc++
+endif
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0)
+    LOCAL_SHARED_LIBRARIES += libmincrypttwrp
+    LOCAL_C_INCLUDES := $(LOCAL_PATH)/libmincrypt/includes
     LOCAL_SRC_FILES += oldverifier/verifier.cpp
     LOCAL_CFLAGS += -DUSE_OLD_VERIFIER
 else
-    LOCAL_SHARED_LIBRARIES += libc++ libcrypto
+    LOCAL_SHARED_LIBRARIES += libcrypto
     LOCAL_SRC_FILES += verifier.cpp
 endif
 
@@ -638,7 +647,7 @@
     $(commands_recovery_local_path)/adbbu/Android.mk \
     $(commands_recovery_local_path)/libpixelflinger/Android.mk
 
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 24; echo $$?),0)
     include $(commands_recovery_local_path)/libmincrypt/Android.mk
 endif
 
diff --git a/adbbu/Android.mk b/adbbu/Android.mk
index 2c2d50a..10faf6f 100644
--- a/adbbu/Android.mk
+++ b/adbbu/Android.mk
@@ -29,6 +29,9 @@
 LOCAL_C_INCLUDES += bionic external/zlib
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
     LOCAL_C_INCLUDES += external/stlport/stlport
+    LOCAL_SHARED_LIBRARIES += libstlport
+else
+    LOCAL_SHARED_LIBRARIES += libc++
 endif
 
 LOCAL_SRC_FILES = \
@@ -36,10 +39,4 @@
 
 LOCAL_SHARED_LIBRARIES += libz libc libstdc++
 
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_SHARED_LIBRARIES += libstlport
-else
-    LOCAL_SHARED_LIBRARIES += libc++
-endif
-
 include $(BUILD_SHARED_LIBRARY)
diff --git a/minadbd/Android.mk b/minadbd/Android.mk
index 2bfe119..ef59945 100644
--- a/minadbd/Android.mk
+++ b/minadbd/Android.mk
@@ -7,6 +7,7 @@
     -Wno-unused-parameter \
     -Wno-missing-field-initializers \
     -DADB_HOST=0 \
+    -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
 
 include $(CLEAR_VARS)
 
diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp
index 0694280..1db0d5f 100644
--- a/minadbd/adb_main.cpp
+++ b/minadbd/adb_main.cpp
@@ -36,7 +36,7 @@
     init_transport_registration();
     usb_init();
 
-    VLOG(ADB) << "Event loop starting";
+    //VLOG(ADB) << "Event loop starting";
     fdevent_loop();
 
     return 0;
diff --git a/minadbd/services.cpp b/minadbd/services.cpp
index 658a43f..753a14e 100644
--- a/minadbd/services.cpp
+++ b/minadbd/services.cpp
@@ -73,7 +73,12 @@
     sti->cookie = cookie;
     sti->fd = s[1];
 
+#if PLATFORM_SDK_VERSION == 23
+    adb_thread_t t;
+    if (adb_thread_create( &t, (adb_thread_func_t)service_bootstrap_func, sti)){
+#else
     if (!adb_thread_create(service_bootstrap_func, sti)) {
+#endif
         free(sti);
         adb_close(s[0]);
         adb_close(s[1]);
@@ -81,7 +86,7 @@
         return -1;
     }
 
-    VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
+    //VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
     return s[0];
 }
 
@@ -102,3 +107,10 @@
     }
     return ret;
 }
+
+#if PLATFORM_SDK_VERSION == 23
+int service_to_fd(const char* name) {
+    atransport transport;
+    return service_to_fd(name, &transport);
+}
+#endif
diff --git a/minui/minui.h b/minui/minui.h
index 5e49100..dee4641 100644
--- a/minui/minui.h
+++ b/minui/minui.h
@@ -108,6 +108,8 @@
 // by row.
 int res_create_multi_display_surface(const char* name, int* frames,
                                      int* fps, GRSurface*** pSurface);
+int res_create_multi_display_surface(const char* name, int* frames,
+                                     GRSurface*** pSurface);
 
 // Load a single alpha surface from a grayscale PNG image.
 int res_create_alpha_surface(const char* name, GRSurface** pSurface);
diff --git a/minui/resources.cpp b/minui/resources.cpp
index 40d3c2c..34c9c82 100644
--- a/minui/resources.cpp
+++ b/minui/resources.cpp
@@ -323,6 +323,12 @@
     return result;
 }
 
+int res_create_multi_display_surface(const char* name, int* frames,
+        GRSurface*** pSurface) {
+    int fps = 0;
+    return res_create_multi_display_surface(name, frames, &fps, pSurface);
+}
+
 int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
     GRSurface* surface = NULL;
     int result = 0;
diff --git a/mtp/Android.mk b/mtp/Android.mk
index 7128bde..c6a7fb7 100644
--- a/mtp/Android.mk
+++ b/mtp/Android.mk
@@ -9,6 +9,9 @@
 LOCAL_C_INCLUDES += $(LOCAL_PATH) bionic frameworks/base/include system/core/include bionic/libc/private/
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
     LOCAL_C_INCLUDES += external/stlport/stlport
+    LOCAL_SHARED_LIBRARIES += libstlport
+else
+    LOCAL_SHARED_LIBRARIES += libc++
 endif
 
 LOCAL_SRC_FILES = \
@@ -34,12 +37,6 @@
     node.cpp
 LOCAL_SHARED_LIBRARIES += libz libc libusbhost libstdc++ libdl libcutils libutils libaosprecovery libselinux
 
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_SHARED_LIBRARIES += libstlport
-else
-    LOCAL_SHARED_LIBRARIES += libc++
-endif
-
 ifneq ($(TW_MTP_DEVICE),)
 	LOCAL_CFLAGS += -DUSB_MTP_DEVICE=$(TW_MTP_DEVICE)
 endif
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 538be71..f7f8c91 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -91,7 +91,7 @@
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstlport.so
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libgccdemangle.so
 endif
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 23; echo $$?),0)
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 23; echo $$?),0)
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libcrypto.so
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libpackagelistparser.so
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/liblzma.so
@@ -233,6 +233,9 @@
         RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libsysutils.so
         RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libnl.so
     endif
+    ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 24; echo $$?),0)
+        RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libpcrecpp.so
+    endif
 endif
 TWRP_AUTOGEN := $(intermediates)/teamwin
 GEN := $(intermediates)/teamwin
diff --git a/twrp.cpp b/twrp.cpp
index 3de35af..859a7fc 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -59,6 +59,8 @@
 struct selabel_handle *selinux_handle;
 #endif
 
+extern int adb_server_main(int is_daemon, int server_port, int /* reply_fd */);
+
 TWPartitionManager PartitionManager;
 int Log_Offset;
 bool datamedia;
diff --git a/twrpTarMain/Android.mk b/twrpTarMain/Android.mk
index c88351b..71b9bcd 100644
--- a/twrpTarMain/Android.mk
+++ b/twrpTarMain/Android.mk
@@ -14,12 +14,10 @@
 LOCAL_CFLAGS:= -g -c -W -DBUILD_TWRPTAR_MAIN
 
 LOCAL_C_INCLUDES += bionic
-ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
-    LOCAL_C_INCLUDES += external/stlport/stlport bionic/libstdc++/include
-endif
 
 LOCAL_STATIC_LIBRARIES := libc libtar_static
 ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+    LOCAL_C_INCLUDES += external/stlport/stlport bionic/libstdc++/include
     LOCAL_STATIC_LIBRARIES += libstlport_static
 endif
 LOCAL_STATIC_LIBRARIES += libstdc++
@@ -59,8 +57,13 @@
 	../gui/twmsg.cpp
 LOCAL_CFLAGS:= -g -c -W -DBUILD_TWRPTAR_MAIN
 
-LOCAL_C_INCLUDES += bionic external/stlport/stlport
-LOCAL_SHARED_LIBRARIES := libc libtar libstlport libstdc++
+LOCAL_C_INCLUDES += bionic
+LOCAL_SHARED_LIBRARIES := libc libtar
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
+    LOCAL_C_INCLUDES += external/stlport/stlport bionic/libstdc++/include
+    LOCAL_SHARED_LIBRARIES += libstlport_static
+endif
+LOCAL_SHARED_LIBRARIES += libstdc++
 
 ifeq ($(TWHAVE_SELINUX), true)
     LOCAL_C_INCLUDES += external/libselinux/include