aboutsummaryrefslogtreecommitdiffstats
path: root/tools/cross_compile_android/android_utilities.py
blob: 039fa9431cf3a0ad6ebfe983c0e41b3d9f907a02 (plain)
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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

import logging
import shutil
import os
import stat
import sys
import subprocess

from urllib import request
from pathlib import Path
from typing import List
from packaging import version
from tqdm import tqdm

# the tag number does not matter much since we update the sdk later
DEFAULT_SDK_TAG = 6514223
ANDROID_NDK_VERSION = "26b"


def run_command(command: List[str], cwd: str = None, ignore_fail: bool = False,
                dry_run: bool = False, accept_prompts: bool = False, show_stdout: bool = False,
                capture_stdout: bool = False):

    if capture_stdout and not show_stdout:
        raise RuntimeError("capture_stdout should always be used together with show_stdout")

    if dry_run:
        print(" ".join(command))
        return

    input = None
    if accept_prompts:
        input = str.encode("y")

    if show_stdout:
        stdout = None
    else:
        stdout = subprocess.DEVNULL

    result = subprocess.run(command, cwd=cwd, input=input, stdout=stdout,
                            capture_output=capture_stdout)

    if result.returncode != 0 and not ignore_fail:
        sys.exit(result.returncode)

    if capture_stdout and not result.returncode:
        return result.stdout.decode("utf-8")

    return None


class DownloadProgressBar(tqdm):
    def update_to(self, b=1, bsize=1, tsize=None):
        if tsize is not None:
            self.total = tsize
        self.update(b * bsize - self.n)


class SdkManager:
    def __init__(self, android_sdk_dir: Path, dry_run: bool = False):
        self._sdk_manager = android_sdk_dir / "tools" / "bin" / "sdkmanager"

        if not self._sdk_manager.exists():
            raise RuntimeError(f"Unable to find SdkManager in {str(self._sdk_manager)}")

        if not os.access(self._sdk_manager, os.X_OK):
            current_permissions = stat.S_IMODE(os.lstat(self._sdk_manager).st_mode)
            os.chmod(self._sdk_manager, current_permissions | stat.S_IEXEC)

        self._android_sdk_dir = android_sdk_dir
        self._dry_run = dry_run

    def list_packages(self):
        command = [self._sdk_manager, f"--sdk_root={self._android_sdk_dir}", "--list"]
        return run_command(command=command, dry_run=self._dry_run, show_stdout=True,
                           capture_stdout=True)

    def install(self, *args, accept_license: bool = False, show_stdout=False):
        command = [str(self._sdk_manager), f"--sdk_root={self._android_sdk_dir}", *args]
        run_command(command=command, dry_run=self._dry_run,
                    accept_prompts=accept_license, show_stdout=show_stdout)


def _unpack(zip_file: Path, destination: Path):
    """
    Unpacks the zip_file into destination preserving all permissions

    TODO: Try to use zipfile module. Currently we cannot use zipfile module here because
    extractAll() does not preserve permissions.

    In case `unzip` is not available, the user is requested to install it manually
    """
    unzip = shutil.which("unzip")
    if not unzip:
        raise RuntimeError("Unable to find program unzip. Use `sudo apt-get install unzip`"
                           "to install it")

    command = [unzip, zip_file, "-d", destination]
    run_command(command=command, show_stdout=True)


def _download(url: str, destination: Path):
    """
    Download url to destination
    """
    headers, download_path = None, None
    # https://github.com/tqdm/tqdm#hooks-and-callbacks
    with DownloadProgressBar(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t:
        download_path, headers = request.urlretrieve(url=url, filename=destination,
                                                     reporthook=t.update_to)
    assert headers["Content-Type"] == "application/zip"
    assert Path(download_path).resolve() == destination


def download_android_ndk(ndk_path: Path):
    """
    Downloads the given ndk_version into ndk_path
    """
    ndk_path = ndk_path / "android-ndk"
    ndk_zip_path = ndk_path / f"android-ndk-r{ANDROID_NDK_VERSION}-linux.zip"
    ndk_version_path = ndk_path / f"android-ndk-r{ANDROID_NDK_VERSION}"

    if ndk_version_path.exists():
        print(f"NDK path found in {str(ndk_version_path)}")
    else:
        ndk_path.mkdir(parents=True, exist_ok=True)
        url = (f"https://dl.google.com/android/repository"
               f"/android-ndk-r{ANDROID_NDK_VERSION}-linux.zip")

        print(f"Downloading Android Ndk version r{ANDROID_NDK_VERSION}")
        _download(url=url, destination=ndk_zip_path)

        print("Unpacking Android Ndk")
        _unpack(zip_file=(ndk_path / f"android-ndk-r{ANDROID_NDK_VERSION}-linux.zip"),
                destination=ndk_path)

    return ndk_version_path


def download_android_commandlinetools(android_sdk_dir: Path):
    """
    Downloads Android commandline tools into cltools_path.
    """
    android_sdk_dir = android_sdk_dir / "android-sdk"
    url = ("https://dl.google.com/android/repository/"
           f"commandlinetools-linux-{DEFAULT_SDK_TAG}_latest.zip")
    cltools_zip_path = android_sdk_dir / f"commandlinetools-linux-{DEFAULT_SDK_TAG}_latest.zip"
    cltools_path = android_sdk_dir / "tools"

    if cltools_path.exists():
        print(f"Command-line tools found in {str(cltools_path)}")
    else:
        android_sdk_dir.mkdir(parents=True, exist_ok=True)

        print("Download Android Command Line Tools: "
              f"commandlinetools-linux-{DEFAULT_SDK_TAG}_latest.zip")
        _download(url=url, destination=cltools_zip_path)

        print("Unpacking Android Command Line Tools")
        _unpack(zip_file=cltools_zip_path, destination=android_sdk_dir)

    return android_sdk_dir


def android_list_build_tools_versions(sdk_manager: SdkManager):
    """
    List all the build-tools versions available for download
    """
    available_packages = sdk_manager.list_packages()
    build_tools_versions = []
    lines = available_packages.split('\n')

    for line in lines:
        if not line.strip().startswith('build-tools;'):
            continue
        package_name = line.strip().split(' ')[0]
        if package_name.count(';') != 1:
            raise RuntimeError(f"Unable to parse build-tools version: {package_name}")
        ver = package_name.split(';')[1]

        build_tools_versions.append(version.Version(ver))

    return build_tools_versions


def find_installed_buildtools_version(build_tools_dir: Path):
    """
    It is possible that the user has multiple build-tools installed. The newer  version is generally
    used. This function find the newest among the installed build-tools
    """
    versions = [version.Version(bt_dir.name) for bt_dir in build_tools_dir.iterdir()
                if bt_dir.is_dir()]
    return max(versions)


def find_latest_buildtools_version(sdk_manager: SdkManager):
    """
    Uses sdk manager to find the latest build-tools version
    """
    available_build_tools_v = android_list_build_tools_versions(sdk_manager=sdk_manager)

    if not available_build_tools_v:
        raise RuntimeError('Unable to find any build tools available for download')

    return max(available_build_tools_v)


def install_android_packages(android_sdk_dir: Path, android_api: str, dry_run: bool = False,
                             accept_license: bool = False, skip_update: bool = False):
    """
    Use the sdk manager to install build-tools, platform-tools and platform API
    """
    tools_dir = android_sdk_dir / "tools"
    if not tools_dir.exists():
        raise RuntimeError("Unable to find Android command-line tools in "
                           f"{str(tools_dir)}")

    # incase of --verbose flag
    show_output = (logging.getLogger().getEffectiveLevel() == logging.INFO)

    sdk_manager = SdkManager(android_sdk_dir=android_sdk_dir, dry_run=dry_run)

    # install/upgrade platform-tools
    if not (android_sdk_dir / "platform-tools").exists():
        print("Installing/Updating Android platform-tools")
        sdk_manager.install("platform-tools", accept_license=accept_license,
                            show_stdout=show_output)
        # The --update command is only relevant for platform tools
        if not skip_update:
            sdk_manager.install("--update", show_stdout=show_output)

    # install/upgrade build-tools
    buildtools_dir = android_sdk_dir / "build-tools"

    if not buildtools_dir.exists():
        latest_build_tools_v = find_latest_buildtools_version(sdk_manager=sdk_manager)
        print(f"Installing Android build-tools version {latest_build_tools_v}")
        sdk_manager.install(f"build-tools;{latest_build_tools_v}", show_stdout=show_output)
    else:
        if not skip_update:
            latest_build_tools_v = find_latest_buildtools_version(sdk_manager=sdk_manager)
            installed_build_tools_v = find_installed_buildtools_version(buildtools_dir)
            if latest_build_tools_v > installed_build_tools_v:
                print(f"Updating Android build-tools version to {latest_build_tools_v}")
                sdk_manager.install(f"build-tools;{latest_build_tools_v}", show_stdout=show_output)
                installed_build_tools_v = latest_build_tools_v

    # install the platform API
    platform_api_dir = android_sdk_dir / "platforms" / f"android-{android_api}"
    if not platform_api_dir.exists():
        print(f"Installing Android platform API {android_api}")
        sdk_manager.install(f"platforms;android-{android_api}", show_stdout=show_output)

    print("Android packages installation done")