Skip to content

Commit da0f275

Browse files
committed
Add Android (arm64) build script
1 parent a4e13c7 commit da0f275

File tree

7 files changed

+188
-0
lines changed

7 files changed

+188
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ test.exe
88
*.res
99
eepers.app
1010

11+
*.apk.idsig
12+
android/lib
13+
android/build
14+
eepers.apk
15+
1116
.idea
1217
.DS_Store

android/AndroidManifest.xml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
4+
package="io.github.tsoding.eepers"
5+
android:versionCode="12"
6+
android:versionName="1.2">
7+
8+
<application
9+
android:allowBackup="false"
10+
android:fullBackupContent="false"
11+
android:icon="@drawable/ic_launcher"
12+
android:label="@string/app_name"
13+
android:hasCode="false">
14+
<activity android:name="android.app.NativeActivity"
15+
android:label="@string/app_name"
16+
android:configChanges="orientation|keyboardHidden"
17+
android:exported="true">
18+
<meta-data android:name="android.app.lib_name"
19+
android:value="eepers" />
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
26+
27+
</manifest>

android/main.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <android/log.h>
4+
#include <android/asset_manager.h>
5+
#include <android_native_app_glue.h>
6+
7+
extern void eepersinit(void);
8+
extern void _ada_eepers(void);
9+
extern void eepersfinal(void);
10+
11+
#define LOG_TAG "Eepers"
12+
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
13+
14+
// Dummy implementations to workaround GNAT-LLVM bugs
15+
typedef void __sigtramphandler_t (int signo, void *siginfo, void *sigcontext);
16+
17+
void __gnat_sigtramp (int signo, void *siginfo, void *sigcontext, __sigtramphandler_t * handler) {
18+
ALOGD("%s: Signal %d, siginfo %p, sigcontext %p, handler %p", __func__, signo, siginfo, sigcontext, handler);
19+
exit(1);
20+
}
21+
unsigned char _r_debug[640*1024] = {};
22+
23+
24+
extern const char *GetApplicationDirectory(void);
25+
extern const char *GetWorkingDirectory(void);
26+
extern void SetLoadFileDataCallback(void *);
27+
28+
extern struct android_app *GetAndroidApp(void);
29+
static struct android_app *app;
30+
31+
const unsigned char *LoadFileDataCallback(const char *fileName, int *dataSize) {
32+
if (memcmp(fileName, "assets/", strlen("assets/")) != 0) {
33+
return 0;
34+
}
35+
fileName += strlen("assets/");
36+
37+
AAsset *asset = AAssetManager_open(app->activity->assetManager, fileName, AASSET_MODE_BUFFER);
38+
*dataSize = AAsset_getLength(asset);
39+
ALOGD("%s: Opening %s (size %d)", __func__, fileName, *dataSize);
40+
unsigned char *data = malloc(*dataSize);
41+
memcpy(data, AAsset_getBuffer(asset), *dataSize);
42+
return data;
43+
}
44+
45+
int main(void) {
46+
app = GetAndroidApp();
47+
SetLoadFileDataCallback(LoadFileDataCallback);
48+
49+
ALOGD("Initializing with eepersinit()");
50+
eepersinit();
51+
52+
53+
ALOGD("Entering Eepers main: _ada_eepers");
54+
_ada_eepers();
55+
ALOGD("Deinitializing with eepersfinal()");
56+
eepersfinal();
57+
ALOGD("%s: Done!", __func__);
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../assets/icon.png

android/res/values/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="app_name">Eepers</string>
4+
</resources>

build-android.sh

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/sh
2+
3+
set -xe
4+
5+
ABIS="arm64-v8a"
6+
API=33
7+
8+
BUILD_TOOLS=$ANDROID_HOME/Sdk/build-tools/34.0.0
9+
TOOLCHAIN=$CLANG_R475365B_CUSTOM
10+
ANDROID_NDK=$ANDROID_HOME/Sdk/ndk/26.1.10909125
11+
NATIVE_APP_GLUE=$ANDROID_NDK/sources/android/native_app_glue
12+
LLVM_GNAT_BIN=$LLVM_GNAT/llvm-interface/bin
13+
14+
SYSROOTS=$CLANG_R475365B_CUSTOM/../../../sysroots
15+
16+
FLAGS="-ffunction-sections -funwind-tables -fstack-protector-strong -fPIC -Wall \
17+
-Wformat -Werror=format-security -no-canonical-prefixes \
18+
-DANDROID -DPLATFORM_ANDROID -D__ANDROID_API__=$API"
19+
20+
mkdir -p android/res/drawable-hdpi
21+
mkdir -p android/lib/arm64-v8a
22+
mkdir -p android/build
23+
24+
SRC="eepers.adb raylib.adb raymath.ads"
25+
OBJ="raylib raymath eepers"
26+
27+
for ABI in $ABIS; do
28+
case "$ABI" in
29+
"armeabi-v7a")
30+
CCTYPE="armv7a-linux-androideabi"
31+
ARCH="arm"
32+
ARCH_SYSROOT="arm"
33+
LIBPATH="arm-linux-androideabi"
34+
ABI_FLAGS="-std=c99 -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
35+
;;
36+
37+
"arm64-v8a")
38+
CCTYPE="aarch64-linux-android"
39+
ARCH="aarch64"
40+
ARCH_SYSROOT="arm64"
41+
LIBPATH="aarch64-linux-android"
42+
ABI_FLAGS="-std=c99 -target aarch64"
43+
;;
44+
esac
45+
CC="$TOOLCHAIN/bin/clang"
46+
SYSROOT=$SYSROOTS/ndk/$ARCH_SYSROOT
47+
LLD=$CLANG_R475365B_CUSTOM/bin/ld.lld
48+
INCLUDES="-I$NATIVE_APP_GLUE -I$SYSROOT/usr/include/$CCTYPE -I$SYSROOT/usr/include"
49+
50+
# Compile native app glue
51+
#$CC $INCLUDES --target=$CCTYPE$API -c $NATIVE_APP_GLUE/android_native_app_glue.c -o android/build/native_app_glue.o $FLAGS $ABI_FLAGS
52+
$CC $INCLUDES --target=$CCTYPE$API -c android/main.c -o android/build/main.o $FLAGS $ABI_FLAGS
53+
54+
# Compile project
55+
56+
for file in $SRC; do
57+
$LLVM_GNAT_BIN/llvm-gcc -g --target=aarch64-linux-android$API -fPIC -gnat2022 $file -c
58+
done
59+
60+
$LLVM_GNAT/llvm-interface/bin/llvm-gnatbind -Leepers eepers.ali
61+
$LLVM_GNAT/llvm-interface/bin/llvm-gnatlink eepers.ali -o android/lib/$ABI/libeepers.so --LINK="$LLD" android/build/*.o -shared \
62+
--exclude-libs libatomic.a --build-id -z noexecstack -z relro -z now --warn-shared-textrel -u ANativeActivity_onCreate \
63+
--sysroot=$SYSROOT -L$CLANG_R475365B_CUSTOM/lib/clang/16.0.2/lib/linux -L$CLANG_R475365B_CUSTOM/lib/clang/16.0.2/lib/linux/$ARCH \
64+
-L$SYSROOT/usr/lib/$LIBPATH/$API -L$SYSROOT/usr/lib/$LIBPATH -Landroid/build -Lraylib/raylib-5.0_android_$ABI \
65+
-lraylib -llog -landroid -lEGL -lGLESv2 -lOpenSLES -lc -lm -ldl -l:libclang_rt.builtins-$ARCH-android.a -lunwind
66+
done
67+
68+
# ______________________________________________________________________________
69+
#
70+
# Build APK
71+
# ______________________________________________________________________________
72+
#
73+
$BUILD_TOOLS/aapt package -f -m \
74+
-S android/res -J android/build -M android/AndroidManifest.xml \
75+
-I $ANDROID_HOME/Sdk/platforms/android-$API/android.jar
76+
77+
# Add resources and assets to APK
78+
$BUILD_TOOLS/aapt package -f \
79+
-M android/AndroidManifest.xml -S android/res -A assets \
80+
-I $ANDROID_HOME/Sdk/platforms/android-$API/android.jar -F eepers.apk
81+
82+
# Add libraries to APK
83+
cd android
84+
for ABI in $ABIS; do
85+
$BUILD_TOOLS/aapt add ../eepers.apk lib/$ABI/libeepers.so
86+
done
87+
cd ..
88+
89+
$BUILD_TOOLS/zipalign -f 4 eepers.apk eepers.4.apk
90+
$BUILD_TOOLS/apksigner sign --ks ~/.android/debug.keystore --ks-pass pass:android eepers.4.apk
91+
mv -f eepers.4.apk eepers.apk

eepers.ads

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
procedure Eepers;
2+
pragma Export (C, Eepers, "_ada_eepers");

0 commit comments

Comments
 (0)