Android: add script for building prefab archive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
diff --git a/build-scripts/android-prefab.sh b/build-scripts/android-prefab.sh
new file mode 100755
index 0000000..aef588e
--- /dev/null
+++ b/build-scripts/android-prefab.sh
@@ -0,0 +1,351 @@
+#!/bin/bash
+
+set -e
+
+if ! [ "x$ANDROID_NDK_HOME" != "x" -a -d "$ANDROID_NDK_HOME" ]; then
+ echo "ANDROID_NDK_HOME environment variable is not set"
+ exit 1
+fi
+
+if ! [ "x$ANDROID_HOME" != "x" -a -d "$ANDROID_HOME" ]; then
+ echo "ANDROID_HOME environment variable is not set"
+ exit 1
+fi
+
+if [ "x$ANDROID_API" = "x" ]; then
+ ANDROID_API="$(ls "$ANDROID_HOME/platforms" | grep -E "^android-[0-9]+$" | sed 's/android-//' | sort -n -r | head -1)"
+ if [ "x$ANDROID_API" = "x" ]; then
+ echo "No Android platform found in $ANDROID_HOME/platforms"
+ exit 1
+ fi
+else
+ if ! [ -d "$ANDROID_HOME/platforms/android-$ANDROID_API" ]; then
+ echo "Android api version $ANDROID_API is not available ($ANDROID_HOME/platforms/android-$ANDROID_API does not exist)" >2
+ exit 1
+ fi
+fi
+
+android_platformdir="$ANDROID_HOME/platforms/android-$ANDROID_API"
+
+echo "Building for android api version $ANDROID_API"
+echo "android_platformdir=$android_platformdir"
+
+scriptdir=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)")
+sdl_root=$(cd -P -- "$(dirname -- "$0")/.." && printf '%s\n' "$(pwd -P)")
+
+build_root="${sdl_root}/build-android-prefab"
+
+android_abis="armeabi-v7a arm64-v8a x86 x86_64"
+android_api=19
+android_ndk=21
+android_stl="c++_shared"
+
+sdl_major=$(sed -ne 's/^#define SDL_MAJOR_VERSION *//p' "${sdl_root}/include/SDL_version.h")
+sdl_minor=$(sed -ne 's/^#define SDL_MINOR_VERSION *//p' "${sdl_root}/include/SDL_version.h")
+sdl_patch=$(sed -ne 's/^#define SDL_PATCHLEVEL *//p' "${sdl_root}/include/SDL_version.h")
+sdl_version="${sdl_major}.${sdl_minor}.${sdl_patch}"
+echo "Building Android prefab package for SDL version $sdl_version"
+
+prefabhome="${build_root}/prefab-${sdl_version}"
+rm -rf "$prefabhome"
+mkdir -p "${prefabhome}"
+
+build_cmake_projects() {
+ for android_abi in $android_abis; do
+ echo "Configuring CMake project for $android_abi"
+ cmake -S "$sdl_root" -B "${build_root}/build_${android_abi}" \
+ -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake" \
+ -DANDROID_PLATFORM=${android_platform} \
+ -DANDROID_ABI=${android_abi} \
+ -DSDL_SHARED=ON \
+ -DSDL_STATIC=ON \
+ -DSDL_STATIC_PIC=ON \
+ -DSDL_TEST=ON \
+ -DSDL2_DISABLE_SDL2MAIN=OFF \
+ -DSDL2_DISABLE_INSTALL=OFF \
+ -DCMAKE_INSTALL_PREFIX="${build_root}/build_${android_abi}/prefix" \
+ -DCMAKE_INSTALL_INCLUDEDIR=include \
+ -DCMAKE_INSTALL_LIBDIR=lib \
+ -DCMAKE_BUILD_TYPE=Release \
+ -GNinja
+
+ rm -rf "${build_root}/build_${android_abi}/prefix"
+
+ echo "Building CMake project for $android_abi"
+ cmake --build "${build_root}/build_${android_abi}"
+
+ echo "Installing CMake project for $android_abi"
+ cmake --install "${build_root}/build_${android_abi}"
+ done
+}
+
+classes_sources_jar_path="${prefabhome}/classes-sources.jar"
+classes_jar_path="${prefabhome}/classes.jar"
+compile_java() {
+ classes_sources_root="${prefabhome}/classes-sources"
+
+ rm -rf "${classes_sources_root}"
+ mkdir -p "${classes_sources_root}/META-INF"
+
+ echo "Copying LICENSE.txt to java build folder"
+ cp "$sdl_root/LICENSE.txt" "${classes_sources_root}/META-INF"
+
+ echo "Copy JAVA sources to java build folder"
+ cp -r "$sdl_root/android-project/app/src/main/java/org" "${classes_sources_root}"
+
+ java_sourceslist_path="${prefabhome}/java_sources.txt"
+ pushd "${classes_sources_root}"
+ echo "Collecting sources for classes-sources.jar"
+ find "." -name "*.java" >"${java_sourceslist_path}"
+ find "META-INF" -name "*" >>"${java_sourceslist_path}"
+
+ echo "Creating classes-sources.jar"
+ jar -cf "${classes_sources_jar_path}" "@${java_sourceslist_path}"
+ popd
+
+ classes_root="${prefabhome}/classes"
+ mkdir -p "${classes_root}/META-INF"
+ cp "$sdl_root/LICENSE.txt" "${classes_root}/META-INF"
+ java_sourceslist_path="${prefabhome}/java_sources.txt"
+
+ echo "Collecting sources for classes.jar"
+ find "$sdl_root/android-project/app/src/main/java" -name "*.java" >"${java_sourceslist_path}"
+
+ echo "Compiling classes"
+ javac -encoding utf-8 -classpath "$android_platformdir/android.jar" -d "${classes_root}" "@${java_sourceslist_path}"
+
+ java_classeslist_path="${prefabhome}/java_classes.txt"
+ pushd "${classes_root}"
+ find "." -name "*.class" >"${java_classeslist_path}"
+ find "META-INF" -name "*" >>"${java_classeslist_path}"
+ echo "Creating classes.jar"
+ jar -cf "${classes_jar_path}" "@${java_classeslist_path}"
+ popd
+}
+
+pom_filename="SDL${sdl_major}-${sdl_version}.pom"
+pom_filepath="${prefabhome}/${pom_filename}"
+create_pom_xml() {
+ echo "Creating ${pom_filename}"
+ cat >"${pom_filepath}" <<EOF
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.libsdl.android</groupId>
+ <artifactId>SDL${sdl_major}</artifactId>
+ <version>${sdl_version}</version>
+ <packaging>aar</packaging>
+ <name>SDL${sdl_major}</name>
+ <description>The AAR for SDL${sdl_major}</description>
+ <url>https://libsdl.org/</url>
+ <licenses>
+ <license>
+ <name>zlib License</name>
+ <url>https://github.com/libsdl-org/SDL/blob/main/LICENSE.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+ <scm>
+ <connection>scm:git:https://github.com/libsdl-org/SDL</connection>
+ <url>https://github.com/libsdl-org/SDL</url>
+ </scm>
+</project>
+EOF
+}
+
+create_aar_androidmanifest() {
+ echo "Creating AndroidManifest.xml"
+ cat >"${aar_root}/AndroidManifest.xml" <<EOF
+<manifest
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ package="org.libsdl.android" android:versionCode="1"
+ android:versionName="1.0">
+ <uses-sdk android:minSdkVersion="16"
+ android:targetSdkVersion="29"/>
+</manifest>
+EOF
+}
+
+echo "Creating AAR root directory"
+aar_root="${prefabhome}/SDL${sdl_major}-${sdl_version}"
+mkdir -p "${aar_root}"
+
+aar_metainfdir_path=${aar_root}/META-INF
+mkdir -p "${aar_metainfdir_path}"
+cp "${sdl_root}/LICENSE.txt" "${aar_metainfdir_path}"
+
+prefabworkdir="${aar_root}/prefab"
+mkdir -p "${prefabworkdir}"
+
+cat >"${prefabworkdir}/prefab.json" <<EOF
+{
+ "schema_version": 2,
+ "name": "SDL$sdl_major",
+ "version": "$sdl_version",
+ "dependencies": []
+}
+EOF
+
+modulesworkdir="${prefabworkdir}/modules"
+mkdir -p "${modulesworkdir}"
+
+create_shared_sdl_module() {
+ echo "Creating SDL${sdl_major} prefab module"
+ for android_abi in $android_abis; do
+ sdl_moduleworkdir="${modulesworkdir}/SDL${sdl_major}"
+ mkdir -p "${sdl_moduleworkdir}"
+
+ abi_build_prefix="${build_root}/build_${android_abi}/prefix"
+
+ cat >"${sdl_moduleworkdir}/module.json" <<EOF
+{
+ "export_libraries": [],
+ "library_name": "libSDL${sdl_major}"
+}
+EOF
+ mkdir -p "${sdl_moduleworkdir}/include"
+ cp -r "${abi_build_prefix}/include/SDL${sdl_major}/"* "${sdl_moduleworkdir}/include/"
+ rm "${sdl_moduleworkdir}/include/SDL_config.h"
+ cp "$sdl_root/include/SDL_config.h" "$sdl_root/include/SDL_config_android.h" "${sdl_moduleworkdir}/include/"
+
+ abi_sdllibdir="${sdl_moduleworkdir}/libs/android.${android_abi}"
+ mkdir -p "${abi_sdllibdir}"
+ cat >"${abi_sdllibdir}/abi.json" <<EOF
+{
+ "abi": "${android_abi}",
+ "api": ${android_api},
+ "ndk": ${android_ndk},
+ "stl": "${android_stl}",
+ "static": false
+}
+EOF
+ cp "${abi_build_prefix}/lib/libSDL${sdl_major}.so" "${abi_sdllibdir}"
+ done
+}
+
+create_static_sdl_module() {
+ echo "Creating SDL${sdl_major}-static prefab module"
+ for android_abi in $android_abis; do
+ sdl_moduleworkdir="${modulesworkdir}/SDL${sdl_major}-static"
+ mkdir -p "${sdl_moduleworkdir}"
+
+ abi_build_prefix="${build_root}/build_${android_abi}/prefix"
+
+ cat >"${sdl_moduleworkdir}/module.json" <<EOF
+{
+ "export_libraries": ["-ldl", "-lGLESv1_CM", "-lGLESv2", "-llog", "-landroid", "-lOpenSLES"]
+ "library_name": "libSDL${sdl_major}"
+}
+EOF
+ mkdir -p "${sdl_moduleworkdir}/include"
+ cp -r "${abi_build_prefix}/include/SDL${sdl_major}/"* "${sdl_moduleworkdir}/include"
+ rm "${sdl_moduleworkdir}/include/SDL_config.h"
+ cp "$sdl_root/include/SDL_config.h" "$sdl_root/include/SDL_config_android.h" "${sdl_moduleworkdir}/include/"
+
+ abi_sdllibdir="${sdl_moduleworkdir}/libs/android.${android_abi}"
+ mkdir -p "${abi_sdllibdir}"
+ cat >"${abi_sdllibdir}/abi.json" <<EOF
+{
+ "abi": "${android_abi}",
+ "api": ${android_api},
+ "ndk": ${android_ndk},
+ "stl": "${android_stl}",
+ "static": true
+}
+EOF
+ cp "${abi_build_prefix}/lib/libSDL${sdl_major}.a" "${abi_sdllibdir}"
+ done
+}
+
+create_sdlmain_module() {
+ echo "Creating SDL${sdl_major}main prefab module"
+ for android_abi in $android_abis; do
+ sdl_moduleworkdir="${modulesworkdir}/SDL${sdl_major}main"
+ mkdir -p "${sdl_moduleworkdir}"
+
+ abi_build_prefix="${build_root}/build_${android_abi}/prefix"
+
+ cat >"${sdl_moduleworkdir}/module.json" <<EOF
+{
+ "export_libraries": [],
+ "library_name": "libSDL${sdl_major}main"
+}
+EOF
+ abi_sdllibdir="${sdl_moduleworkdir}/libs/android.${android_abi}"
+ mkdir -p "${abi_sdllibdir}"
+ cat >"${abi_sdllibdir}/abi.json" <<EOF
+{
+ "abi": "${android_abi}",
+ "api": ${android_api},
+ "ndk": ${android_ndk},
+ "stl": "${android_stl}",
+ "static": true
+}
+EOF
+ cp "${abi_build_prefix}/lib/libSDL${sdl_major}main.a" "${abi_sdllibdir}"
+ done
+}
+
+create_sdltest_module() {
+ echo "Creating SDL${sdl_major}test prefab module"
+ for android_abi in $android_abis; do
+ sdl_moduleworkdir="${modulesworkdir}/SDL${sdl_major}test"
+ mkdir -p "${sdl_moduleworkdir}"
+
+ abi_build_prefix="${build_root}/build_${android_abi}/prefix"
+
+ cat >"${sdl_moduleworkdir}/module.json" <<EOF
+{
+ "export_libraries": [],
+ "library_name": "libSDL${sdl_major}_test"
+}
+EOF
+ abi_sdllibdir="${sdl_moduleworkdir}/libs/android.${android_abi}"
+ mkdir -p "${abi_sdllibdir}"
+ cat >"${abi_sdllibdir}/abi.json" <<EOF
+{
+ "abi": "${android_abi}",
+ "api": ${android_api},
+ "ndk": ${android_ndk},
+ "stl": "${android_stl}",
+ "static": true
+}
+EOF
+ cp "${abi_build_prefix}/lib/libSDL${sdl_major}_test.a" "${abi_sdllibdir}"
+ done
+}
+
+build_cmake_projects
+
+compile_java
+
+create_pom_xml
+
+create_aar_androidmanifest
+
+create_shared_sdl_module
+
+create_static_sdl_module
+
+create_sdlmain_module
+
+create_sdltest_module
+
+pushd "${aar_root}"
+ aar_filename="SDL${sdl_major}-${sdl_version}.aar"
+ cp "${classes_jar_path}" ./classes.jar
+ cp "${classes_sources_jar_path}" ./classes-sources.jar
+ zip -r "${aar_filename}" AndroidManifest.xml classes.jar classes-sources.jar prefab META-INF
+ zip -Tv "${aar_filename}" 2>/dev/null ;
+ mv "${aar_filename}" "${prefabhome}"
+popd
+
+maven_filename="SDL${sdl_major}-${sdl_version}.zip"
+
+pushd "${prefabhome}"
+ zip_filename="SDL${sdl_major}-${sdl_version}.zip"
+ zip "${maven_filename}" "${aar_filename}" "${pom_filename}" 2>/dev/null;
+ zip -Tv "${zip_filename}" 2>/dev/null;
+popd
+
+echo "Prefab zip is ready at ${prefabhome}/${aar_filename}"
+echo "Maven archive is ready at ${prefabhome}/${zip_filename}"