sdl2-compat: add initial Android.mk and a build script for ndk-build.

From 135eeac471248d7c96df5bc53f656193f5b91fea Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Tue, 21 Nov 2023 23:10:14 +0300
Subject: [PATCH] add initial Android.mk and a build script for ndk-build.

---
 build-scripts/androidbuild.sh | 69 +++++++++++++++++++++++++++++++++++
 src/Android.mk                | 24 ++++++++++++
 src/sdl2_compat.c             |  8 ++++
 3 files changed, 101 insertions(+)
 create mode 100755 build-scripts/androidbuild.sh
 create mode 100644 src/Android.mk

diff --git a/build-scripts/androidbuild.sh b/build-scripts/androidbuild.sh
new file mode 100755
index 0000000..bc7f9e8
--- /dev/null
+++ b/build-scripts/androidbuild.sh
@@ -0,0 +1,69 @@
+#!/bin/bash
+# Usage: androidbuild.sh [arg for ndk-build ...]
+#
+# Useful NDK arguments:
+#
+#  NDK_DEBUG=1          - build debug version
+#  NDK_LIBS_OUT=<dest>  - specify alternate destination for installable
+#                         modules.
+
+srcdir=`dirname $0`/../src
+srcdir=`cd $srcdir && pwd`
+cd $srcdir
+
+#
+# Create the build directories
+#
+
+build=build
+buildandroid=$build/android
+platform=android-16
+abi="arm64-v8a" # "armeabi-v7a arm64-v8a x86 x86_64"
+obj=
+lib=
+ndk_args=
+
+# Allow an external caller to specify locations and platform.
+while [ $# -gt 0 ]; do
+    arg=$1
+    if [ "${arg:0:8}" == "NDK_OUT=" ]; then
+        obj=${arg#NDK_OUT=}
+    elif [ "${arg:0:13}" == "NDK_LIBS_OUT=" ]; then
+        lib=${arg#NDK_LIBS_OUT=}
+    elif [ "${arg:0:13}" == "APP_PLATFORM=" ]; then
+        platform=${arg#APP_PLATFORM=}
+    elif [ "${arg:0:8}" == "APP_ABI=" ]; then
+        abi=${arg#APP_ABI=}
+    else
+        ndk_args="$ndk_args $arg"
+    fi
+    shift
+done
+
+if [ -z $obj ]; then
+    obj=$buildandroid/obj
+fi
+if [ -z $lib ]; then
+    lib=$buildandroid/lib
+fi
+
+for dir in $build $buildandroid $obj $lib; do
+    if test -d $dir; then
+        :
+    else
+        mkdir $dir || exit 1
+    fi
+done
+
+# APP_* variables set in the environment here will not be seen by the
+# ndk-build makefile segments that use them, e.g., default-application.mk.
+# For consistency, pass all values on the command line.
+ndk-build \
+    NDK_PROJECT_PATH=null \
+    NDK_OUT=$obj \
+    NDK_LIBS_OUT=$lib \
+    APP_BUILD_SCRIPT=Android.mk \
+    APP_ABI="$abi" \
+    APP_PLATFORM="$platform" \
+    APP_MODULES="SDL2" \
+    $ndk_args
diff --git a/src/Android.mk b/src/Android.mk
new file mode 100644
index 0000000..87c4f3f
--- /dev/null
+++ b/src/Android.mk
@@ -0,0 +1,24 @@
+LOCAL_PATH := $(call my-dir)
+
+# SDL2 shared library
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := SDL2
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+
+LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
+
+LOCAL_SRC_FILES := dynapi/SDL_dynapi.c sdl2_compat.c
+
+LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DHAVE_ALLOCA -DHAVE_ALLOCA_H
+LOCAL_CFLAGS += -Wall -Wextra -Wfloat-conversion -Wno-unused-parameter
+LOCAL_LDLIBS := -ldl -llog
+LOCAL_LDFLAGS := -Wl,--no-undefined
+
+ifeq ($(NDK_DEBUG),1)
+    cmd-strip :=
+endif
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index d65f55a..14b3ad2 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -85,6 +85,9 @@ This breaks the build when creating SDL_ ## DisableScreenSaver
 #include <string.h>
 #define HAVE_STDIO_H 1
 #endif
+#ifdef __ANDROID__
+#include <android/log.h>
+#endif
 
 /* mingw headers may define these ... */
 #undef strtod
@@ -644,6 +647,11 @@ static void error_dialog(const char *errorMsg)
 }
 #elif defined(__APPLE__)
 extern void error_dialog(const char *errorMsg);
+#elif defined(__ANDROID__)
+static void error_dialog(const char *errorMsg)
+{
+    __android_log_print(ANDROID_LOG_FATAL, "SDL2COMPAT", "%s\n", errorMsg);
+}
 #else
 static void error_dialog(const char *errorMsg)
 {