android: add script in Android archive to create a SDK

Also,
- Move mingw's pkg-support into build-scripts
- Add type annotations to python scripts for mypy
- ci: use v4 tag of actions/cache
- cmake: use PYTHON3_EXECUTABLE for running xxd.py
  (Python3::Interpreter is not always recognized.)
This commit is contained in:
Anonymous Maarten 2024-07-31 00:05:54 +02:00
parent 091960441b
commit a1c6f99877
13 changed files with 414 additions and 62 deletions

View file

@ -10,7 +10,7 @@ import textwrap
SDL_ROOT = Path(__file__).resolve().parents[1]
def extract_sdl_version():
def extract_sdl_version() -> str:
"""
Extract SDL version from SDL3/SDL_version.h
"""
@ -23,8 +23,8 @@ def extract_sdl_version():
micro = int(next(re.finditer(r"#define\s+SDL_MICRO_VERSION\s+([0-9]+)", data)).group(1))
return f"{major}.{minor}.{micro}"
def replace_in_file(path, regex_what, replace_with):
with open(path, "r") as f:
def replace_in_file(path: Path, regex_what: str, replace_with: str) -> None:
with path.open("r") as f:
data = f.read()
new_data, count = re.subn(regex_what, replace_with, data)
@ -35,12 +35,12 @@ def replace_in_file(path, regex_what, replace_with):
f.write(new_data)
def android_mk_use_prefab(path):
def android_mk_use_prefab(path: Path) -> None:
"""
Replace relative SDL inclusion with dependency on prefab package
"""
with open(path) as f:
with path.open() as f:
data = "".join(line for line in f.readlines() if "# SDL" not in line)
data, _ = re.subn("[\n]{3,}", "\n\n", data)
@ -55,18 +55,19 @@ def android_mk_use_prefab(path):
$(call import-module,prefab/SDL3)
""")
with open(path, "w") as f:
with path.open("w") as f:
f.write(newdata)
def cmake_mk_no_sdl(path):
def cmake_mk_no_sdl(path: Path) -> None:
"""
Don't add the source directories of SDL/SDL_image/SDL_mixer/...
"""
with open(path) as f:
with path.open() as f:
lines = f.readlines()
newlines = []
newlines: list[str] = []
for line in lines:
if "add_subdirectory(SDL" in line:
while newlines[-1].startswith("#"):
@ -76,11 +77,12 @@ def cmake_mk_no_sdl(path):
newdata, _ = re.subn("[\n]{3,}", "\n\n", "".join(newlines))
with open(path, "w") as f:
with path.open("w") as f:
f.write(newdata)
def gradle_add_prefab_and_aar(path, aar):
with open(path) as f:
def gradle_add_prefab_and_aar(path: Path, aar: str) -> None:
with path.open() as f:
data = f.read()
data, count = re.subn("android {", textwrap.dedent("""
@ -95,25 +97,26 @@ def gradle_add_prefab_and_aar(path, aar):
implementation files('libs/{aar}')"""), data)
assert count == 1
with open(path, "w") as f:
with path.open("w") as f:
f.write(data)
def gradle_add_package_name(path, package_name):
with open(path) as f:
def gradle_add_package_name(path: Path, package_name: str) -> None:
with path.open() as f:
data = f.read()
data, count = re.subn("org.libsdl.app", package_name, data)
assert count >= 1
with open(path, "w") as f:
with path.open("w") as f:
f.write(data)
def main():
def main() -> int:
description = "Create a simple Android gradle project from input sources."
epilog = "You need to manually copy a prebuilt SDL3 Android archive into the project tree when using the aar variant."
parser = ArgumentParser(description=description, allow_abbrev=False)
parser.add_argument("package_name", metavar="PACKAGENAME", help="Android package name e.g. com.yourcompany.yourapp")
parser = ArgumentParser(description=description, epilog=epilog, allow_abbrev=False)
parser.add_argument("package_name", metavar="PACKAGENAME", help="Android package name (e.g. com.yourcompany.yourapp)")
parser.add_argument("sources", metavar="SOURCE", nargs="*", help="Source code of your application. The files are copied to the output directory.")
parser.add_argument("--variant", choices=["copy", "symlink", "aar"], default="copy", help="Choose variant of SDL project (copy: copy SDL sources, symlink: symlink SDL sources, aar: use Android aar archive)")
parser.add_argument("--output", "-o", default=SDL_ROOT / "build", type=Path, help="Location where to store the Android project")
@ -225,6 +228,7 @@ def main():
print("To build and install to a device for testing, run the following:")
print(f"cd {build_path}")
print("./gradlew installDebug")
return 0
if __name__ == "__main__":
raise SystemExit(main())