| #!/usr/bin/env python3 | |
| # Copyright 2024 - The Android Open Source Project | |
| # SPDX-License-Identifier: Apache-2.0 | |
| # | |
| import hashlib | |
| import json | |
| import logging | |
| import os | |
| from pathlib import Path | |
| import platform | |
| import shutil | |
| import stat | |
| from tasks.task import Task | |
| from utils import ( | |
| AOSP_ROOT, | |
| binary_extension, | |
| get_bazel_build_configs, | |
| get_bazel_path, | |
| get_bazel_startup_options, | |
| get_bazel_targets, | |
| get_netsim_binaries, | |
| platform_to_target_name, | |
| run, | |
| ) | |
| class CompileInstallTask(Task): | |
| def __init__(self, args, env): | |
| super().__init__("CompileInstall") | |
| self.args = args | |
| self.out = Path(args.out_dir) | |
| self.env = env | |
| self.binaries = get_netsim_binaries() | |
| def on_rm_error(self, func, path, exc_info): | |
| """Error handler for ``shutil.rmtree``. | |
| If the error is due to an access error (read only file) | |
| it attempts to add write permission and then retries. | |
| If the error is for another reason it re-raises the error. | |
| Usage : ``shutil.rmtree(path, onerror=on_rm_error)`` | |
| """ | |
| # Is the error an access error? | |
| if not os.access(path, os.W_OK): | |
| os.chmod(path, stat.S_IWRITE) | |
| func(path) | |
| else: | |
| raise | |
| def _generate_installed_files_json(self): | |
| """Generates installed-files.json for size tracking.""" | |
| search_dir = self.out / "distribution" / "emulator" | |
| if not search_dir.is_dir(): | |
| logging.warning( | |
| f"Artifact directory not found: {search_dir}. Skipping JSON" | |
| " generation." | |
| ) | |
| return | |
| installed_files = [] | |
| for bin_name in self.binaries: | |
| actual_name = binary_extension(bin_name) | |
| bin_path = search_dir / actual_name | |
| if bin_path.is_file(): | |
| size = bin_path.stat().st_size | |
| # Calculate SHA256 | |
| sha256_hash = hashlib.sha256() | |
| with open(bin_path, "rb") as f: | |
| while byte_block := f.read(4096): | |
| sha256_hash.update(byte_block) | |
| sha256 = sha256_hash.hexdigest() | |
| installed_files.append( | |
| {"Name": bin_name, "Size": size, "SHA256": sha256} | |
| ) | |
| if not installed_files: | |
| logging.warning("No binaries found to track for size report.") | |
| target_str = platform_to_target_name(self.args.target or platform.system()) | |
| dist_dir = Path(self.args.dist_dir).absolute() | |
| dist_dir.mkdir(exist_ok=True, parents=True) | |
| json_fname = dist_dir / f"installed-files-{target_str}.json" | |
| logging.info("Creating installed-files JSON: %s", json_fname) | |
| with open(json_fname, "w", encoding="utf-8") as f: | |
| json.dump(installed_files, f, indent=2) | |
| def do_run(self): | |
| res = self._run_bazel() | |
| if res: | |
| self._generate_installed_files_json() | |
| return res | |
| def _run_bazel(self): | |
| bazel = get_bazel_path() | |
| build_configs = get_bazel_build_configs(self.args, self.env) | |
| startup_options = get_bazel_startup_options() | |
| targets = get_bazel_targets(self.args) | |
| run( | |
| [bazel] + startup_options + ["build"] + targets + build_configs, | |
| self.env, | |
| "bazel build", | |
| AOSP_ROOT, | |
| ) | |
| # Bazel Install | |
| search_dir = AOSP_ROOT / "bazel-bin" / "external" / "netsim+" | |
| dest_dir = self.out / "distribution" / "emulator" | |
| logging.info(f"Installing artifacts from {search_dir} to {dest_dir}") | |
| try: | |
| if not search_dir.is_dir(): | |
| logging.error(f"Bazel output directory not found: {search_dir}") | |
| return False | |
| dest_dir.mkdir(exist_ok=True, parents=True) | |
| # Copy netsim binaries | |
| for binary, src in self.binaries.items(): | |
| binary_name = binary_extension(binary) | |
| src_name = binary_extension(src) | |
| src_file = search_dir / src_name | |
| logging.info(f"Copying {src_file} to {dest_dir}") | |
| dest_file = dest_dir / binary_name | |
| # Remove the file if it exists to avoid permission errors on overwrite. | |
| if dest_file.is_file(): | |
| try: | |
| dest_file.unlink() | |
| except PermissionError: | |
| self.on_rm_error(os.unlink, str(dest_file), None) | |
| shutil.copy(src_file, dest_file) | |
| except FileNotFoundError as e: | |
| logging.error( | |
| f"Artifact not found: {e}. A successful Bazel build is required." | |
| ) | |
| raise e | |
| except shutil.Error as e: | |
| logging.error(f"Error copying artifacts: {e}") | |
| raise e | |
| return True |