Repository navigation
Cloudflared recipe added #3415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SNEHASISHROY-125
wants to merge
6
commits into
kivy:develop
Choose a base branch
from
SNEHASISHROY-125:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+311
−0
Open
Cloudflared recipe added #3415
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3adc0b6
cloudflared , libcloudflared recipe++
SNEHASISHROY-125 1ca8771
Merge branch 'kivy:develop' into develop
SNEHASISHROY-125 b456845
trimmed down whitespaces , new lines
SNEHASISHROY-125 d8116fc
complying with strict checks
SNEHASISHROY-125 b478490
changes to comply with++
SNEHASISHROY-125 52ddd56
Merge branch 'kivy:develop' into develop
SNEHASISHROY-125 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from pythonforandroid.recipe import Recipe | ||
| from pythonforandroid.logger import info | ||
|
|
||
|
|
||
| class CloudflaredRecipe(Recipe): | ||
|
|
||
| version = "1.4.0" | ||
| url = None | ||
| depends = ["pyjnius", "cloudflared-kivy", "libcloudflared"] | ||
| site_packages_name = "cloudflared" | ||
|
|
||
| def build_arch(self, arch): | ||
| super().build_arch(arch) | ||
| info(f"done building cloudflared-{self.version}") | ||
|
|
||
|
|
||
| recipe = CloudflaredRecipe() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,294 @@ | ||
| import os | ||
| import sh | ||
| 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 GoCompiledComponentsRecipe(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.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 | ||
| ) | ||
|
|
||
| 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") | ||
| 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( | ||
| 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 = GoCompiledComponentsRecipe() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a GOLANG recipe, let's try to generalize this class same as
RustCompiledComponentsRecipeto maybe something likeGoCompiledComponentsRecipe. And use that as a subclass for this recipe.