Files

422 lines
14 KiB
Bash

#!/usr/bin/env bash
# SPDX-License-Identifier: (GPL-2.0-only OR MIT)
# Copyright (C) 2026 Amlogic, Inc. All rights reserved
################################################################################
# Amlbian Logcat Compilation Module
#
# Purpose: Compile and package logcat tool for Android debugging
# Provides: logcat binary for system logging
################################################################################
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "$(dirname "$(dirname "${SCRIPT_DIR}")")")"
BASE_DIR="$(dirname "$(dirname "${AMLBIAN_DIR}")")"
# Source common functions
if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then
source "${AMLBIAN_DIR}/lib/function-common.sh"
fi
# ==============================================================================
# Shared Helper Functions
# ==============================================================================
# ------------------------------------------------------------------------------
# Step 1: Version and Package Information Definition
# ------------------------------------------------------------------------------
# Initialize common package metadata for logcat
function _logcat_init_package_info() {
PKG_MAINTAINER="Armbian Build <build@armbian.com>"
PKG_DESCRIPTION="Android logcat tool for system logging"
PKG_NAME="android-tools-logcat"
PKG_VERSION="1.0"
PKG_ARCH="all"
# Source directory for logcat
LOGCAT_SRC_DIR="${BASE_DIR}/aml-comp/vendor/amlogic/aml_commonlib/logcat"
FILES_DIR="${SCRIPT_DIR}/files"
}
# ------------------------------------------------------------------------------
# Step 2: Build Directory Structure Setup
# ------------------------------------------------------------------------------
# Create layered build directory structure
# Parameters:
# $1 - base name (e.g. "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}")
# Sets: TMP_DIR, BASE_BUILD_DIR, SOURCE_DIR, BUILD_DIR, INSTALL_DIR, PATCH_DIR,
# DEB_PACKAGE_DIR, DEBIAN_DIR
function _logcat_create_build_dirs() {
local base_name="$1"
TMP_DIR="${SRC}/.tmp"
mkdir -p "${TMP_DIR}"
# 基础构建目录
BASE_BUILD_DIR="${TMP_DIR}/${base_name}"
rm -rf ${BASE_BUILD_DIR}
# 分层目录
SOURCE_DIR="${BASE_BUILD_DIR}/source" # 源码目录
BUILD_DIR="${BASE_BUILD_DIR}/build" # 编译目录
INSTALL_DIR="${BASE_BUILD_DIR}/install" # 安装目录
PATCH_DIR="${BASE_BUILD_DIR}/patch" # 补丁目录
# 创建所有目录
mkdir -p "${SOURCE_DIR}"
mkdir -p "${BUILD_DIR}"
mkdir -p "${INSTALL_DIR}"
mkdir -p "${PATCH_DIR}"
# 在BUILD_DIR下创建deb/包名目录
DEB_PACKAGE_DIR="${BUILD_DIR}/deb/${base_name}"
mkdir -p "${DEB_PACKAGE_DIR}"
# 安装目录的子目录
DEBIAN_DIR="${INSTALL_DIR}/DEBIAN"
mkdir -p "${DEBIAN_DIR}"
}
# Create standard install subdirectories for logcat
function _logcat_create_install_dirs() {
mkdir -p "${INSTALL_DIR}/usr/bin"
}
# ------------------------------------------------------------------------------
# Step 3: Debian Packaging Scripts Creation
# ------------------------------------------------------------------------------
# Create Debian control file
# Parameters:
# $1 - package name
# $2 - package version
# $3 - package architecture
# $4 - maintainer
# $5 - description
# $6 - section (optional, default "utils")
function _logcat_create_control_file() {
local pkg_name="$1"
local pkg_version="$2"
local pkg_arch="$3"
local maintainer="$4"
local description="$5"
local section="${6:-utils}"
display_alert "COMPILE-LOGCAT Creating control file DEBIAN/control" "info"
cat > "${DEBIAN_DIR}/control" <<EOF
Package: ${pkg_name}
Version: ${pkg_version}
Architecture: ${pkg_arch}
Maintainer: ${maintainer}
Depends: liblog
Section: ${section}
Priority: optional
Description: ${description}
Android logcat tool for viewing system logs.
Useful for debugging Android-based systems.
EOF
}
# Create postinst script for logcat
function _logcat_create_postinst() {
display_alert "COMPILE-LOGCAT Creating post-installation script DEBIAN/postinst" "info"
cat > "${DEBIAN_DIR}/postinst" << EOF
#!/bin/sh
set -e
if [ "\$1" = "configure" ]; then
echo "Logcat tool installed successfully"
echo "Usage: logcat [options]"
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/postinst"
}
# Create prerm script for logcat
function _logcat_create_prerm() {
display_alert "COMPILE-LOGCAT Creating pre-removal script DEBIAN/prerm" "info"
cat > "${DEBIAN_DIR}/prerm" << EOF
#!/bin/sh
set -e
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
echo "Logcat tool being removed"
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/prerm"
}
# ------------------------------------------------------------------------------
# Step 4: Source/Vendor File Copying and Patching
# ------------------------------------------------------------------------------
# Copy logcat source files and apply patches
function _logcat_copy_and_patch_files() {
display_alert "COMPILE-LOGCAT Copying source files from ${LOGCAT_SRC_DIR}" "info"
# Check if source directory exists
if [[ ! -d "${LOGCAT_SRC_DIR}" ]]; then
display_alert "COMPILE-LOGCAT Source directory not found: ${LOGCAT_SRC_DIR}" "err"
display_alert "COMPILE-LOGCAT Trying to clone or find alternative source" "info"
# Try to find source in other locations
LOGCAT_SRC_DIR="${BASE_DIR}/aml-comp/vendor/amlogic/aml_commonlib/logcat"
if [[ ! -d "${LOGCAT_SRC_DIR}" ]]; then
display_alert "COMPILE-LOGCAT Source not found. Please ensure aml_commonlib is available" "err"
exit 1
fi
fi
# Copy source files
cp -r "${LOGCAT_SRC_DIR}" "${SOURCE_DIR}/logcat"
display_alert "COMPILE-LOGCAT Copied logcat source files" "info"
# Apply patches if available
if [[ -f "${FILES_DIR}/logcat.patch" ]]; then
display_alert "COMPILE-LOGCAT Applying logcat.patch" "info"
cd "${SOURCE_DIR}"
patch -p1 < "${FILES_DIR}/logcat.patch" || true
cd - > /dev/null
fi
}
# ------------------------------------------------------------------------------
# Step 5: Liblog Dependency Handling
# ------------------------------------------------------------------------------
# Check and build liblog dependency if needed
function _logcat_check_liblog_dependency() {
display_alert "COMPILE-LOGCAT Checking liblog dependency" "info"
# Check if liblog is already built
local liblog_dir="${SRC}/.tmp/liblog-build-${ARCH:-arm64}"
local liblog_install_dir="${liblog_dir}/install"
if [[ ! -d "${liblog_install_dir}" ]] || [[ ! -f "${liblog_install_dir}/usr/lib/liblog.so" ]]; then
display_alert "COMPILE-LOGCAT liblog not found, building liblog..." "info"
# Build liblog
if ! (
cd "${AMLBIAN_DIR}"
source "${AMLBIAN_DIR}/scripts/build-liblog.sh"
); then
display_alert "COMPILE-LOGCAT Failed to build liblog" "err"
return 1
fi
# Verify liblog was built
if [[ ! -d "${liblog_install_dir}" ]] || [[ ! -f "${liblog_install_dir}/usr/lib/liblog.so" ]]; then
display_alert "COMPILE-LOGCAT liblog still not found after build" "err"
return 1
fi
fi
display_alert "COMPILE-LOGCAT liblog dependency satisfied" "info"
return 0
}
# ------------------------------------------------------------------------------
# Step 6: Compilation
# ------------------------------------------------------------------------------
# Compile logcat tool
function _logcat_compile() {
display_alert "COMPILE-LOGCAT Compiling logcat tool" "info"
# First check and build liblog dependency
if ! _logcat_check_liblog_dependency; then
display_alert "COMPILE-LOGCAT Cannot compile without liblog" "err"
exit 1
fi
# Get liblog paths
local liblog_dir="${SRC}/.tmp/liblog-build-${ARCH:-arm64}"
local liblog_install_dir="${liblog_dir}/install"
cd "${SOURCE_DIR}/logcat"
# Set up build environment with liblog paths
export OUT_DIR="${BUILD_DIR}"
export TARGET_DIR="${INSTALL_DIR}"
export STAGING_DIR="${STAGING_DIR_TARGET:-${BASE_DIR}/staging}"
export DESTDIR="${INSTALL_DIR}"
# Add liblog include and library paths
export CFLAGS="-I${SOURCE_DIR}/logcat/include"
export LDFLAGS="-L${liblog_install_dir}/usr/lib -llog"
# Create necessary directories
mkdir -p "${BUILD_DIR}"
mkdir -p "${STAGING_DIR}"
# Create a temporary Makefile override to fix logcat_test compilation
# The original Makefile doesn't include $(CFLAGS) in the logcat_test rule
local makefile_override="${TMP_DIR}/logcat-override.mk"
cat > "${makefile_override}" << 'EOF'
# Makefile override for logcat_test compilation
# Fixes the missing $(CFLAGS) in the original rule
# Override the logcat_test rule to include $(CFLAGS)
logcat_test: logcat_test.c
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $(OUT_DIR)/$@
EOF
# Compile with liblog support, using both the original Makefile and the override
# Use -f to specify multiple makefiles: original Makefile first, then override
make -C "${SOURCE_DIR}/logcat" \
OUT_DIR="${BUILD_DIR}" \
TARGET_DIR="${INSTALL_DIR}" \
STAGING_DIR="${STAGING_DIR}" \
DESTDIR="${INSTALL_DIR}" \
CFLAGS="${CFLAGS} -DHAVE_IOCTL" \
LDFLAGS="${LDFLAGS}" \
-f Makefile -f "${makefile_override}"
cd - > /dev/null
# Check if compilation succeeded
if [[ ! -f "${BUILD_DIR}/logcat" ]]; then
display_alert "COMPILE-LOGCAT Compilation failed: logcat binary not found" "err"
exit 1
fi
display_alert "COMPILE-LOGCAT Compilation successful" "info"
}
# ------------------------------------------------------------------------------
# Step 7: Installation
# ------------------------------------------------------------------------------
# Install logcat binaries
function _logcat_install() {
display_alert "COMPILE-LOGCAT Installing binaries" "info"
# Create bin directory
mkdir -p "${INSTALL_DIR}/usr/bin"
# Install logcat binary
if [[ -f "${BUILD_DIR}/logcat" ]]; then
install -m 0755 "${BUILD_DIR}/logcat" "${INSTALL_DIR}/usr/bin/"
display_alert "COMPILE-LOGCAT Installed logcat binary" "info"
else
display_alert "COMPILE-LOGCAT logcat binary not found in ${BUILD_DIR}" "err"
exit 1
fi
# Install logcat_test if available
if [[ -f "${BUILD_DIR}/logcat_test" ]]; then
install -m 0755 "${BUILD_DIR}/logcat_test" "${INSTALL_DIR}/usr/bin/"
display_alert "COMPILE-LOGCAT Installed logcat_test binary" "info"
fi
}
# ------------------------------------------------------------------------------
# Step 8: Debian Package Building
# ------------------------------------------------------------------------------
# Build Debian package from install directory
# Parameters:
# $1 - log prefix (e.g. "COMPILE-LOGCAT")
# $2 - output deb filename (without path)
# Sets: OUTPUT_DEB
function _logcat_build_deb_package() {
local log_prefix="$1"
local output_deb_name="$2"
# 将install目录内容拷贝到deb目录
display_alert "Copying install directory to deb package directory ${log_prefix}" "info"
cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" 2>&1
# 基于deb目录打包
display_alert "Building Debian package from deb directory ${log_prefix}" "info"
dpkg-deb --build "${DEB_PACKAGE_DIR}" 2>&1
# Move to output directory
OUTPUT_DEB="${SRC}/output/${output_deb_name}"
mkdir -p "$(dirname "${OUTPUT_DEB}")"
mv "${DEB_PACKAGE_DIR}.deb" "${OUTPUT_DEB}"
display_alert "${log_prefix} Package built successfully ${output_deb_name}" "info"
display_alert "${log_prefix} Build directory preserved ${BASE_BUILD_DIR}" "info"
}
# Display build directory info
# Parameters:
# $1 - log prefix
function _logcat_display_build_info() {
local log_prefix="$1"
display_alert "${log_prefix} Source dir: ${SOURCE_DIR}" "info"
display_alert "${log_prefix} Build dir: ${BUILD_DIR}" "info"
display_alert "${log_prefix} Install dir: ${INSTALL_DIR}" "info"
display_alert "${log_prefix} Patch dir: ${PATCH_DIR}" "info"
display_alert "${log_prefix} Deb package dir: ${DEB_PACKAGE_DIR}" "info"
display_alert "${log_prefix} Logcat source dir: ${LOGCAT_SRC_DIR}" "info"
}
# ==============================================================================
# Main Build Functions (Entry Points - DO NOT RENAME)
# ==============================================================================
# ------------------------------------------------------------------------------
# Logcat Tool Build
# Entry point: amlbian_build_logcat
# ------------------------------------------------------------------------------
function amlbian_build_logcat() {
if ! is_unit_enabled "devtool" || ! is_unit_enabled "liblog"; then
echo "[SKIP] >> Logcat disable in BUILD_UNITS"
return
fi
# ---- Step 1: Version and Package Information ----
_logcat_init_package_info
# ---- Step 2: Build Directory Setup ----
_logcat_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
_logcat_create_install_dirs
# Display info
display_alert "COMPILE-LOGCAT Building logcat tool package version ${PKG_VERSION}" "info"
_logcat_display_build_info "COMPILE-LOGCAT"
# ---- Step 3: Debian Packaging Scripts ----
_logcat_create_control_file \
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
"${PKG_DESCRIPTION}" "utils"
_logcat_create_postinst
_logcat_create_prerm
# ---- Step 4: Copy Source/Vendor Files and Apply Patches ----
_logcat_copy_and_patch_files
# ---- Step 5: Check and Build Liblog Dependency ----
if ! _logcat_check_liblog_dependency; then
display_alert "COMPILE-LOGCAT Cannot proceed without liblog" "err"
return 1
fi
# ---- Step 6: Compilation ----
_logcat_compile
# ---- Step 7: Installation ----
_logcat_install
# ---- Step 8: Build Debian Package ----
_logcat_build_deb_package "COMPILE-LOGCAT" "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb"
display_alert "COMPILE-LOGCAT Logcat tool build completed successfully" "info"
return 0
}
display_alert "AMLbian Logcat Build Script" "Scriptdir: ${SCRIPT_DIR} AmlbianDir: ${AMLBIAN_DIR}" "info"
amlbian_build_logcat
display_alert "AMLOGIC: logcat build completed" "${OUTPUT_DIR}" "info"