Skip to content

Commit e352b8d

Browse files
committed
Add Android (arm64) build script
1 parent 0252eb2 commit e352b8d

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Dummy implementations to workaround GNAT-LLVM bugs
12+
void __gnat_sigtramp(void) {}
13+
unsigned char _r_debug[640*1024] = {};
14+
15+
16+
#define LOG_TAG "Eepers"
17+
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
18+
19+
extern const char *GetApplicationDirectory(void);
20+
extern const char *GetWorkingDirectory(void);
21+
extern void SetLoadFileDataCallback(void *);
22+
23+
extern struct android_app *GetAndroidApp(void);
24+
static struct android_app *app;
25+
26+
const unsigned char *LoadFileDataCallback(const char *fileName, int *dataSize) {
27+
if (memcmp(fileName, "assets/", strlen("assets/")) != 0) {
28+
return 0;
29+
}
30+
fileName += strlen("assets/");
31+
32+
AAsset *asset = AAssetManager_open(app->activity->assetManager, fileName, AASSET_MODE_BUFFER);
33+
*dataSize = AAsset_getLength(asset);
34+
ALOGD("%s: Opening %s (size %d)", __func__, fileName, *dataSize);
35+
unsigned char *data = malloc(*dataSize);
36+
memcpy(data, AAsset_getBuffer(asset), *dataSize);
37+
return data;
38+
}
39+
40+
int main(void) {
41+
app = GetAndroidApp();
42+
SetLoadFileDataCallback(LoadFileDataCallback);
43+
ALOGD("Asset manager: %p, path: %s", app->activity->assetManager, app->activity->internalDataPath);
44+
45+
AAssetDir* dir = AAssetManager_openDir(app->activity->assetManager, "");
46+
ALOGD("Asset dir: %p", dir);
47+
const char *fileName = NULL;
48+
49+
while ((fileName = AAssetDir_getNextFileName(dir)) != NULL) {
50+
ALOGD("Assets file: %s", fileName);
51+
}
52+
AAssetDir_close(dir);
53+
54+
ALOGD("Initializing with eepersinit()");
55+
eepersinit();
56+
57+
58+
ALOGD("Entering Eepers main: _ada_eepers");
59+
_ada_eepers();
60+
ALOGD("Deinitializing with eepersfinal()");
61+
eepersfinal();
62+
ALOGD("%s: Done!", __func__);
63+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../assets/icon.png

android/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
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

Lines changed: 91 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)