From 3adc0b693e84d60dcee2e56cc7b8fa3ea553f088 Mon Sep 17 00:00:00 2001 From: snehasish Date: Sat, 21 Nov 2026 22:29:24 +0530 Subject: [PATCH 1/4] cloudflared , libcloudflared recipe++ --- .../recipes/cloudflared/__init__.py | 18 ++ .../recipes/libcloudflared/__init__.py | 298 ++++++++++++++++++ .../recipes/libcloudflared/build.sh | 28 ++ 3 files changed, 344 insertions(+) create mode 100644 pythonforandroid/recipes/cloudflared/__init__.py create mode 100644 pythonforandroid/recipes/libcloudflared/__init__.py create mode 100644 pythonforandroid/recipes/libcloudflared/build.sh diff --git a/pythonforandroid/recipes/cloudflared/__init__.py b/pythonforandroid/recipes/cloudflared/__init__.py new file mode 100644 index 0000000000..ea2f8bf214 --- /dev/null +++ b/pythonforandroid/recipes/cloudflared/__init__.py @@ -0,0 +1,18 @@ +from pythonforandroid.recipe import PythonRecipe +from pythonforandroid.logger import info +# from subprocess import Popen + + +class CloudflaredRecipe(PythonRecipe): + + version = "1.4.0" + url = "https://github.com/SNEHASISHROY-125/cloudflared4kivy/releases/download/latest/cloudflared-{version}.tar.gz" + depends = ["pyjnius", "libcloudflared"] + site_packages_name = "cloudflared" + + def build_arch(self, arch): + super().build_arch(arch) + info(f"done building cloudflared-{self.version}") + + +recipe = CloudflaredRecipe() \ No newline at end of file diff --git a/pythonforandroid/recipes/libcloudflared/__init__.py b/pythonforandroid/recipes/libcloudflared/__init__.py new file mode 100644 index 0000000000..b8dcfca1c5 --- /dev/null +++ b/pythonforandroid/recipes/libcloudflared/__init__.py @@ -0,0 +1,298 @@ + +import os +import re +import shutil +import subprocess + +import hashlib +import platform +import tarfile +import tempfile +import urllib.request +from pathlib import Path + +from pythonforandroid.recipe import Recipe +from pythonforandroid.logger import info + + +class LibCloudflaredRecipe(Recipe): + + version = "1.4.0" + url = None + depends = ["pyjnius"] + + GO_VERSION = "1.27.4" + + GO_RELEASES = { + ("linux", "x86_64"): { + "archive": "go1.27.4.linux-amd64.tar.gz", + "sha256": "7226736c2339719cda28fbf3a54d71aa63bcefe376e98cef00034268dab24840", + }, + ("linux", "aarch64"): { + "archive": "go1.27.4.linux-arm64.tar.gz", + "sha256": "3392582988a273a87ab5043ee1b2c9c17a2c3c00580bf9326c47a2e0aaaeac92", + }, + } + + def ensure_go(self): + + # Persistent cache shared by Buildozer builds. + buildozer_home = Path( + os.environ.get( + "BUILDOZER_HOME", + str(Path.home() / ".buildozer") + ) + ) + + go_cache = buildozer_home / "tools" / "go" + download_cache = go_cache / "downloads" + + host = ( + platform.system().lower(), + platform.machine().lower() + ) + + release = self.GO_RELEASES.get(host) + + if not release: + raise RuntimeError( + f"Unsupported Go host platform: {host}" + ) + + archive_name = release["archive"] + expected_sha = release["sha256"] + + install_dir = go_cache / f"go{self.GO_VERSION}" + go_bin = install_dir / "bin" / "go" + + # Reuse existing installation. + if go_bin.is_file(): + + result = subprocess.run( + [str(go_bin), "version"], + capture_output=True, + text=True + ) + + if ( + result.returncode == 0 + and f"go{self.GO_VERSION}" in result.stdout + ): + info(f"Using cached Go: {result.stdout.strip()}") + return str(go_bin) + + download_cache.mkdir(parents=True, exist_ok=True) + + archive_path = download_cache / archive_name + + download_url = ( + f"https://go.dev/dl/{archive_name}" + ) + + # Download only when archive is missing or corrupted. + valid_archive = False + + if archive_path.is_file(): + digest = hashlib.sha256( + archive_path.read_bytes() + ).hexdigest() + + valid_archive = digest == expected_sha + + if not valid_archive: + + info(f"Downloading Go {self.GO_VERSION}") + + temporary_archive = archive_path.with_suffix( + ".tmp" + ) + + urllib.request.urlretrieve( + download_url, + temporary_archive + ) + + digest = hashlib.sha256( + temporary_archive.read_bytes() + ).hexdigest() + + if digest != expected_sha: + temporary_archive.unlink(missing_ok=True) + + raise RuntimeError( + "Go archive SHA256 verification failed" + ) + + os.replace(temporary_archive, archive_path) + + # Extract into a temporary directory first. + go_cache.mkdir(parents=True, exist_ok=True) + + with tempfile.TemporaryDirectory( + dir=go_cache + ) as temp_dir: + + with tarfile.open( + archive_path, "r:gz" + ) as tar: + + # Reject unsafe archive paths. + for member in tar.getmembers(): + target = Path(temp_dir) / member.name + + if not target.resolve().is_relative_to( + Path(temp_dir).resolve() + ): + raise RuntimeError( + f"Unsafe Go archive path: {member.name}" + ) + + tar.extractall(temp_dir) + + extracted_go = Path(temp_dir) / "go" + + if not (extracted_go / "bin" / "go").is_file(): + raise RuntimeError( + "Invalid Go archive: missing bin/go" + ) + + if install_dir.exists(): + shutil.rmtree(install_dir) + + shutil.move( + str(extracted_go), + str(install_dir) + ) + + # Final verification. + result = subprocess.run( + [str(go_bin), "version"], + capture_output=True, + text=True, + check=True + ) + + info(f"Go installed: {result.stdout.strip()}") + + return str(go_bin) + + + def build_arch(self, arch): + super().build_arch(arch) + + # go = self.check_go() + go = self.ensure_go() + go_root = str(Path(go).parent.parent) + + if arch.arch != "arm64-v8a": + raise RuntimeError( + f"Cloudflared currently supports only arm64-v8a, " + f"got {arch.arch}" + ) + + ndk = self.ctx.ndk_dir + api = str(self.ctx.ndk_api) + + host_tag = platform.system().lower() + "-" + platform.machine().lower() + if not host_tag: + raise RuntimeError("Unable to determine NDK host tag") + + toolchain = os.path.join( + ndk, "toolchains", "llvm", + "prebuilt", host_tag, "bin" + ) + + cc = os.path.join( + toolchain, + f"aarch64-linux-android{api}-clang" + ) + + cxx = os.path.join( + toolchain, + f"aarch64-linux-android{api}-clang++" + ) + + if not os.path.isfile(cc): + raise RuntimeError(f"NDK compiler not found: {cc}") + + if not os.path.isfile(cxx): + raise RuntimeError(f"NDK C++ compiler not found: {cxx}") + + build_dir = self.get_build_dir(arch.arch) + os.makedirs(build_dir, exist_ok=True) + + source_dir = os.path.join(build_dir, "cloudflared") + + if not os.path.isdir(source_dir): + info("Cloning Cloudflare cloudflared") + + subprocess.run( + [ + "git", "clone", + "--depth", "1", + "https://github.com/cloudflare/cloudflared.git", + source_dir + ], + check=True + ) + + recipe_dir = self.get_recipe_dir() + build_script = os.path.join(recipe_dir, "build.sh") + + if not os.path.isfile(build_script): + raise RuntimeError( + f"Missing build script: {build_script}" + ) + + env = os.environ.copy() + + env.update({ + "GO": go, + "GOROOT": go_root, + "PATH": str(Path(go).parent) + os.pathsep + env.get("PATH", ""), + + "GOTOOLCHAIN": "local", + + "CC": cc, + "CXX": cxx, + "GOOS": "android", + "GOARCH": "arm64", + "CGO_ENABLED": "1", + "ANDROID_NDK": ndk, + "ANDROID_API": api, + }) + + info("Building cloudflared for Android arm64-v8a") + + subprocess.run( + ["sh", build_script], + cwd=source_dir, + env=env, + check=True + ) + + output = os.path.join( + source_dir, "libs", "aarch64", + "cloudflared-arm64" + ) + + if not os.path.isfile(output): + raise RuntimeError( + f"Cloudflared output not found: {output}" + ) + + # p4a automatically packages libraries from this directory. + destination_dir = self.ctx.get_libs_dir(arch.arch) + os.makedirs(destination_dir, exist_ok=True) + + destination = os.path.join( + destination_dir, "libcloudflared.so" + ) + + shutil.copy2(output, destination) + os.chmod(destination, 0o755) + + info(f"Installed cloudflared: {destination}") + + +recipe = LibCloudflaredRecipe() \ No newline at end of file diff --git a/pythonforandroid/recipes/libcloudflared/build.sh b/pythonforandroid/recipes/libcloudflared/build.sh new file mode 100644 index 0000000000..5e8bf3f04a --- /dev/null +++ b/pythonforandroid/recipes/libcloudflared/build.sh @@ -0,0 +1,28 @@ + +#!/bin/sh +set -eu + +: "${GO:?GO executable not supplied}" +: "${CC:?Android CC not supplied}" +: "${CXX:?Android CXX not supplied}" + +mkdir -p libs/aarch64 + +echo "Go: $("$GO" version)" +echo "CC: $CC" +echo "CXX: $CXX" + +export CGO_ENABLED=1 +export GOOS=android +export GOARCH=arm64 +export CC +export CXX + +"$GO" build \ + -ldflags="-linkmode=external -extldflags=-Wl,-z,max-page-size=16384" \ + -o libs/aarch64/cloudflared-arm64 \ + ./cmd/cloudflared/ + +chmod 755 libs/aarch64/cloudflared-arm64 + +echo "Cloudflared Android ARM64 build completed" From b4568452ce36da9f28606f222883721537c4afd5 Mon Sep 17 00:00:00 2001 From: snehasish Date: Sun, 22 Nov 2026 22:26:14 +0530 Subject: [PATCH 2/4] trimmed down whitespaces , new lines --- pythonforandroid/recipes/cloudflared/__init__.py | 3 --- pythonforandroid/recipes/libcloudflared/__init__.py | 9 --------- 2 files changed, 12 deletions(-) diff --git a/pythonforandroid/recipes/cloudflared/__init__.py b/pythonforandroid/recipes/cloudflared/__init__.py index ea2f8bf214..f6f3973d16 100644 --- a/pythonforandroid/recipes/cloudflared/__init__.py +++ b/pythonforandroid/recipes/cloudflared/__init__.py @@ -1,7 +1,5 @@ from pythonforandroid.recipe import PythonRecipe from pythonforandroid.logger import info -# from subprocess import Popen - class CloudflaredRecipe(PythonRecipe): @@ -14,5 +12,4 @@ def build_arch(self, arch): super().build_arch(arch) info(f"done building cloudflared-{self.version}") - recipe = CloudflaredRecipe() \ No newline at end of file diff --git a/pythonforandroid/recipes/libcloudflared/__init__.py b/pythonforandroid/recipes/libcloudflared/__init__.py index b8dcfca1c5..c570f06349 100644 --- a/pythonforandroid/recipes/libcloudflared/__init__.py +++ b/pythonforandroid/recipes/libcloudflared/__init__.py @@ -1,20 +1,15 @@ - import os -import re import shutil import subprocess - import hashlib import platform import tarfile import tempfile import urllib.request from pathlib import Path - from pythonforandroid.recipe import Recipe from pythonforandroid.logger import info - class LibCloudflaredRecipe(Recipe): version = "1.4.0" @@ -35,7 +30,6 @@ class LibCloudflaredRecipe(Recipe): } def ensure_go(self): - # Persistent cache shared by Buildozer builds. buildozer_home = Path( os.environ.get( @@ -176,11 +170,9 @@ def ensure_go(self): return str(go_bin) - def build_arch(self, arch): super().build_arch(arch) - # go = self.check_go() go = self.ensure_go() go_root = str(Path(go).parent.parent) @@ -294,5 +286,4 @@ def build_arch(self, arch): info(f"Installed cloudflared: {destination}") - recipe = LibCloudflaredRecipe() \ No newline at end of file From d8116fc970f6a23a6ede2442203643b3c2041e37 Mon Sep 17 00:00:00 2001 From: snehasish Date: Mon, 23 Nov 2026 00:46:17 +0530 Subject: [PATCH 3/4] complying with strict checks --- pythonforandroid/recipes/cloudflared/__init__.py | 4 +++- pythonforandroid/recipes/libcloudflared/__init__.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pythonforandroid/recipes/cloudflared/__init__.py b/pythonforandroid/recipes/cloudflared/__init__.py index f6f3973d16..3b29e8e7ab 100644 --- a/pythonforandroid/recipes/cloudflared/__init__.py +++ b/pythonforandroid/recipes/cloudflared/__init__.py @@ -1,6 +1,7 @@ from pythonforandroid.recipe import PythonRecipe from pythonforandroid.logger import info + class CloudflaredRecipe(PythonRecipe): version = "1.4.0" @@ -12,4 +13,5 @@ def build_arch(self, arch): super().build_arch(arch) info(f"done building cloudflared-{self.version}") -recipe = CloudflaredRecipe() \ No newline at end of file + +recipe = CloudflaredRecipe() diff --git a/pythonforandroid/recipes/libcloudflared/__init__.py b/pythonforandroid/recipes/libcloudflared/__init__.py index c570f06349..abeae7a49d 100644 --- a/pythonforandroid/recipes/libcloudflared/__init__.py +++ b/pythonforandroid/recipes/libcloudflared/__init__.py @@ -10,6 +10,7 @@ from pythonforandroid.recipe import Recipe from pythonforandroid.logger import info + class LibCloudflaredRecipe(Recipe): version = "1.4.0" @@ -286,4 +287,5 @@ def build_arch(self, arch): info(f"Installed cloudflared: {destination}") -recipe = LibCloudflaredRecipe() \ No newline at end of file + +recipe = LibCloudflaredRecipe() From b4784904260f07e430dda3217fd9bb44bf399ab1 Mon Sep 17 00:00:00 2001 From: snehasish Date: Sat, 5 Dec 2026 02:03:50 +0530 Subject: [PATCH 4/4] changes to comply with++ --- .../recipes/cloudflared/__init__.py | 8 ++--- .../recipes/libcloudflared/__init__.py | 35 ++++++++++--------- .../recipes/libcloudflared/build.sh | 28 --------------- 3 files changed, 23 insertions(+), 48 deletions(-) delete mode 100644 pythonforandroid/recipes/libcloudflared/build.sh diff --git a/pythonforandroid/recipes/cloudflared/__init__.py b/pythonforandroid/recipes/cloudflared/__init__.py index 3b29e8e7ab..3749f8541b 100644 --- a/pythonforandroid/recipes/cloudflared/__init__.py +++ b/pythonforandroid/recipes/cloudflared/__init__.py @@ -1,12 +1,12 @@ -from pythonforandroid.recipe import PythonRecipe +from pythonforandroid.recipe import Recipe from pythonforandroid.logger import info -class CloudflaredRecipe(PythonRecipe): +class CloudflaredRecipe(Recipe): version = "1.4.0" - url = "https://github.com/SNEHASISHROY-125/cloudflared4kivy/releases/download/latest/cloudflared-{version}.tar.gz" - depends = ["pyjnius", "libcloudflared"] + url = None + depends = ["pyjnius", "cloudflared-kivy", "libcloudflared"] site_packages_name = "cloudflared" def build_arch(self, arch): diff --git a/pythonforandroid/recipes/libcloudflared/__init__.py b/pythonforandroid/recipes/libcloudflared/__init__.py index abeae7a49d..197e98eb04 100644 --- a/pythonforandroid/recipes/libcloudflared/__init__.py +++ b/pythonforandroid/recipes/libcloudflared/__init__.py @@ -1,4 +1,5 @@ import os +import sh import shutil import subprocess import hashlib @@ -11,7 +12,7 @@ from pythonforandroid.logger import info -class LibCloudflaredRecipe(Recipe): +class GoCompiledComponentsRecipe(Recipe): version = "1.4.0" url = None @@ -229,14 +230,6 @@ def build_arch(self, arch): check=True ) - recipe_dir = self.get_recipe_dir() - build_script = os.path.join(recipe_dir, "build.sh") - - if not os.path.isfile(build_script): - raise RuntimeError( - f"Missing build script: {build_script}" - ) - env = os.environ.copy() env.update({ @@ -256,18 +249,28 @@ def build_arch(self, arch): }) info("Building cloudflared for Android arm64-v8a") - - subprocess.run( - ["sh", build_script], - cwd=source_dir, - env=env, - check=True + cmd = sh.Command(go) + cmd( + 'build', + '-ldflags=-linkmode=external -extldflags=-Wl,-z,max-page-size=16384', + '-o', + 'libs/aarch64/cloudflared-arm64', + './cmd/cloudflared/', + _env=env, + _cwd=source_dir ) output = os.path.join( source_dir, "libs", "aarch64", "cloudflared-arm64" ) + + os.chmod( + output, + 0o755, + ) + + print("Cloudflared Android ARM64 build completed") if not os.path.isfile(output): raise RuntimeError( @@ -288,4 +291,4 @@ def build_arch(self, arch): info(f"Installed cloudflared: {destination}") -recipe = LibCloudflaredRecipe() +recipe = GoCompiledComponentsRecipe() diff --git a/pythonforandroid/recipes/libcloudflared/build.sh b/pythonforandroid/recipes/libcloudflared/build.sh deleted file mode 100644 index 5e8bf3f04a..0000000000 --- a/pythonforandroid/recipes/libcloudflared/build.sh +++ /dev/null @@ -1,28 +0,0 @@ - -#!/bin/sh -set -eu - -: "${GO:?GO executable not supplied}" -: "${CC:?Android CC not supplied}" -: "${CXX:?Android CXX not supplied}" - -mkdir -p libs/aarch64 - -echo "Go: $("$GO" version)" -echo "CC: $CC" -echo "CXX: $CXX" - -export CGO_ENABLED=1 -export GOOS=android -export GOARCH=arm64 -export CC -export CXX - -"$GO" build \ - -ldflags="-linkmode=external -extldflags=-Wl,-z,max-page-size=16384" \ - -o libs/aarch64/cloudflared-arm64 \ - ./cmd/cloudflared/ - -chmod 755 libs/aarch64/cloudflared-arm64 - -echo "Cloudflared Android ARM64 build completed"