SDL: Dynamically generate __main__.py of Android aar package

From d6ad28a4cb87b8bb00893f049f7e6e72063fc3b6 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Thu, 29 Aug 2024 13:50:51 +0200
Subject: [PATCH] Dynamically generate __main__.py of Android aar package

---
 build-scripts/build-release.py                    | 15 +++++++++++----
 .../android/{__main__.py => __main__.py.in}       |  8 ++++----
 .../pkg-support/android/cmake/SDL3Config.cmake    | 10 +++++-----
 3 files changed, 20 insertions(+), 13 deletions(-)
 rename build-scripts/pkg-support/android/{__main__.py => __main__.py.in} (93%)

diff --git a/build-scripts/build-release.py b/build-scripts/build-release.py
index 8224fbdf01acd..fa33c604609af 100755
--- a/build-scripts/build-release.py
+++ b/build-scripts/build-release.py
@@ -609,21 +609,28 @@ def create_android_archives(self, android_api: int, android_home: Path, android_
         aar_path =  self.dist_path / f"{self.project}-{self.version}.aar"
         added_global_files = False
         with zipfile.ZipFile(aar_path, "w", compression=zipfile.ZIP_DEFLATED) as zip_object:
-            install_txt = (self.root / "build-scripts/pkg-support/android/INSTALL.md.in").read_text()
-            install_txt = install_txt.replace("@PROJECT_VERSION@", self.version)
-            install_txt = install_txt.replace("@PROJECT_NAME@", self.project)
+            def configure_file(path: Path) -> str:
+                text = path.read_text()
+                text = text.replace("@PROJECT_VERSION@", self.version)
+                text = text.replace("@PROJECT_NAME@", self.project)
+                return text
+
+            install_txt = configure_file(self.root / "build-scripts/pkg-support/android/INSTALL.md.in")
             zip_object.writestr("INSTALL.md", install_txt)
+
             project_description = {
                 "name": self.project,
                 "version": self.version,
                 "git-hash": self.commit,
             }
             zip_object.writestr("description.json", json.dumps(project_description, indent=0))
+            main_py = configure_file(self.root / "build-scripts/pkg-support/android/__main__.py.in")
+            zip_object.writestr("__main__.py", main_py)
+
             zip_object.writestr("AndroidManifest.xml", self.get_android_manifest_text())
             zip_object.write(self.root / "android-project/app/proguard-rules.pro", arcname="proguard.txt")
             zip_object.write(self.root / "LICENSE.txt", arcname="META-INF/LICENSE.txt")
             zip_object.write(self.root / "cmake/sdlcpu.cmake", arcname="cmake/sdlcpu.cmake")
-            zip_object.write(self.root / "build-scripts/pkg-support/android/__main__.py", arcname="__main__.py")
             zip_object.write(self.root / "build-scripts/pkg-support/android/cmake/SDL3Config.cmake", arcname="cmake/SDL3Config.cmake")
             zip_object.write(self.root / "build-scripts/pkg-support/android/cmake/SDL3ConfigVersion.cmake", arcname="cmake/SDL3ConfigVersion.cmake")
             zip_object.writestr("prefab/prefab.json", self.get_prefab_json_text())
diff --git a/build-scripts/pkg-support/android/__main__.py b/build-scripts/pkg-support/android/__main__.py.in
similarity index 93%
rename from build-scripts/pkg-support/android/__main__.py
rename to build-scripts/pkg-support/android/__main__.py.in
index c27a63316186b..c40ac01122bca 100755
--- a/build-scripts/pkg-support/android/__main__.py
+++ b/build-scripts/pkg-support/android/__main__.py.in
@@ -1,12 +1,12 @@
 #!/usr/bin/env python
 
 """
-Create a SDL SDK prefix from an Android archive
+Create a @PROJECT_NAME@ SDK prefix from an Android archive
 This file is meant to be placed in a the root of an android .aar archive
 
 Example usage:
 ```sh
-python SDL3-3.2.0.aar -o /usr/opt/android-sdks
+python @PROJECT_NAME@-@PROJECT_VERSION@.aar -o /usr/opt/android-sdks
 cmake -S my-project \
     -DCMAKE_PREFIX_PATH=/usr/opt/android-sdks \
     -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
@@ -31,13 +31,13 @@
 
 def main():
     parser = argparse.ArgumentParser(
-        description="Convert an Android .aar archive into a SDK",
+        description="Convert a @PROJECT_NAME@ Android .aar archive into a SDK",
         allow_abbrev=False,
     )
     parser.add_argument("-o", dest="output", type=pathlib.Path, required=True, help="Folder where to store the SDK")
     args = parser.parse_args()
 
-    print(f"Creating a SDK at {args.output}...")
+    print(f"Creating a @PROJECT_NAME@ SDK at {args.output}...")
 
     prefix = args.output
     incdir = prefix / "include"
diff --git a/build-scripts/pkg-support/android/cmake/SDL3Config.cmake b/build-scripts/pkg-support/android/cmake/SDL3Config.cmake
index fb914701ff940..a41275dc480c5 100644
--- a/build-scripts/pkg-support/android/cmake/SDL3Config.cmake
+++ b/build-scripts/pkg-support/android/cmake/SDL3Config.cmake
@@ -46,12 +46,12 @@ endif()
 get_filename_component(_sdl3_prefix "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
 get_filename_component(_sdl3_prefix "${_sdl3_prefix}/.." ABSOLUTE)
 get_filename_component(_sdl3_prefix "${_sdl3_prefix}/.." ABSOLUTE)
-set_and_check(_sdl3_prefix      "${_sdl3_prefix}")
-set(_sdl3_include_dirs          "${_sdl3_prefix}/include")
+set_and_check(_sdl3_prefix          "${_sdl3_prefix}")
+set_and_check(_sdl3_include_dirs    "${_sdl3_prefix}/include")
 
-set(_sdl3_lib         "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/libSDL3.so")
-set(_sdl3test_lib     "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/libSDL3_test.a")
-set(_sdl3_jar         "${_sdl3_prefix}/share/java/SDL3/SDL3-${SDL3_VERSION}.jar")
+set_and_check(_sdl3_lib             "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/libSDL3.so")
+set_and_check(_sdl3test_lib         "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/libSDL3_test.a")
+set_and_check(_sdl3_jar             "${_sdl3_prefix}/share/java/SDL3/SDL3-${SDL3_VERSION}.jar")
 
 unset(_sdl_arch_subdir)
 unset(_sdl3_prefix)