443 lines
15 KiB
Bash
443 lines
15 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
#
|
||
|
|
# SPDX-License-Identifier: GPL-2.0
|
||
|
|
#
|
||
|
|
# Copyright (c) 2026 AMLOGIC
|
||
|
|
#
|
||
|
|
# Liblog build script for AMLbian
|
||
|
|
# This script builds liblog library for Amlogic SoCs
|
||
|
|
|
||
|
|
set -e
|
||
|
|
|
||
|
|
# Get script directory
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
AMLBIAN_DIR="$(dirname "${SCRIPT_DIR}")"
|
||
|
|
BUILD_DIR="${AMLBIAN_DIR}/../build"
|
||
|
|
|
||
|
|
# Source common functions
|
||
|
|
if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then
|
||
|
|
source "${AMLBIAN_DIR}/lib/function-common.sh"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# ==============================================================================
|
||
|
|
# Section 2: Default Configuration
|
||
|
|
# ==============================================================================
|
||
|
|
|
||
|
|
BOARD="${BOARD:-s6-bq201}"
|
||
|
|
DEB_VERSION="${LIBLOG_VERSION:-1.0.0}"
|
||
|
|
ARCH="${ARCH:-arm64}"
|
||
|
|
|
||
|
|
# Disable fake log device feature (default: disabled)
|
||
|
|
# Set to "y" to enable fake log device
|
||
|
|
USE_FAKE_DEVICE="${USE_FAKE_DEVICE:-}"
|
||
|
|
|
||
|
|
# Library version
|
||
|
|
LIBLOG_SO_VERSION="${LIBLOG_SO_VERSION:-1}"
|
||
|
|
|
||
|
|
# ==============================================================================
|
||
|
|
# Section 4: Liblog Build Functions
|
||
|
|
# ==============================================================================
|
||
|
|
|
||
|
|
function amlbian_checkout_toolchain() {
|
||
|
|
|
||
|
|
display_alert "Ignore check toolchain after set_external_toolchain in common setting"
|
||
|
|
return 0
|
||
|
|
|
||
|
|
# # Check default gcc version before switching toolchain
|
||
|
|
# local need_new_toolchain=1
|
||
|
|
# local default_gcc_version=""
|
||
|
|
|
||
|
|
# # Try to get default gcc version
|
||
|
|
# if command -v gcc &> /dev/null; then
|
||
|
|
# default_gcc_version=$(aarch64-linux-gnu-gcc --version | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -n1)
|
||
|
|
# display_alert "TOOLCHAIN" "Default gcc version: ${default_gcc_version}" "info"
|
||
|
|
|
||
|
|
# # Check if version is >= 5.1.0
|
||
|
|
# if [[ -n "${default_gcc_version}" ]]; then
|
||
|
|
# # Simple version comparison
|
||
|
|
# local major_version=$(echo "${default_gcc_version}" | cut -d. -f1)
|
||
|
|
# local minor_version=$(echo "${default_gcc_version}" | cut -d. -f2)
|
||
|
|
|
||
|
|
# if [[ ${major_version} -gt 5 ]] || ([[ ${major_version} -eq 5 ]] && [[ ${minor_version} -ge 1 ]]); then
|
||
|
|
# display_alert "TOOLCHAIN" "Default gcc version ${default_gcc_version} >= 5.1.0, no need for new toolchain" "info"
|
||
|
|
# need_new_toolchain=0
|
||
|
|
# else
|
||
|
|
# display_alert "TOOLCHAIN" "Default gcc version ${default_gcc_version} < 5.1.0, will try new toolchain" "info"
|
||
|
|
# fi
|
||
|
|
# fi
|
||
|
|
# else
|
||
|
|
# display_alert "TOOLCHAIN" "gcc not found in PATH" "warn"
|
||
|
|
# fi
|
||
|
|
|
||
|
|
# # Only switch to new toolchain if needed
|
||
|
|
# if [[ ${need_new_toolchain} -eq 1 ]]; then
|
||
|
|
# local new_toolchain_path="/opt/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin"
|
||
|
|
# if [[ -d "${new_toolchain_path}" ]]; then
|
||
|
|
# display_alert "TOOLCHAIN" "Using new toolchain from: ${new_toolchain_path}" "info"
|
||
|
|
# export PATH="${new_toolchain_path}:${PATH}"
|
||
|
|
# # Check if gcc exists in the new toolchain
|
||
|
|
# if [[ -f "${new_toolchain_path}/aarch64-linux-gnu-gcc" ]]; then
|
||
|
|
# display_alert "TOOLCHAIN" "Found cross-compiler: ${new_toolchain_path}/aarch64-linux-gnu-gcc" "info"
|
||
|
|
# else
|
||
|
|
# display_alert "TOOLCHAIN" "Cross-compiler not found in new toolchain, using default" "warn"
|
||
|
|
# fi
|
||
|
|
# else
|
||
|
|
# display_alert "TOOLCHAIN" "New toolchain directory not found: ${new_toolchain_path}" "warn"
|
||
|
|
# fi
|
||
|
|
# fi
|
||
|
|
}
|
||
|
|
|
||
|
|
function __unset_liblog_config {
|
||
|
|
# Clean up any environment variables set during build
|
||
|
|
unset EXTRA_OEMAKE
|
||
|
|
unset SYSROOT
|
||
|
|
unset CFLAGS
|
||
|
|
unset CXXFLAGS
|
||
|
|
unset LDFLAGS
|
||
|
|
}
|
||
|
|
|
||
|
|
function __liblog_prepare_source {
|
||
|
|
local build_dir="${1}"
|
||
|
|
|
||
|
|
local aml_comp_path="$(amlbian_get_aml_comp_dir)"
|
||
|
|
if [[ -z "${aml_comp_path}" ]] || [[ ! -d "${aml_comp_path}" ]]; then
|
||
|
|
display_alert "LIBLOG" "aml-comp not found: ${aml_comp_path}" "err"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
local source_dir="${aml_comp_path}/vendor/amlogic/aml_commonlib/liblog"
|
||
|
|
|
||
|
|
if [[ ! -d "${source_dir}" ]]; then
|
||
|
|
display_alert "LIBLOG" "Source directory not found: ${source_dir}" "err"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Copying source from ${source_dir} to ${build_dir}" "info"
|
||
|
|
|
||
|
|
# 清空构建目录
|
||
|
|
rm -rf "${build_dir}"
|
||
|
|
mkdir -p "${build_dir}"
|
||
|
|
|
||
|
|
# 复制源代码到构建目录
|
||
|
|
cp -r "${source_dir}"/* "${build_dir}/" 2>/dev/null || true
|
||
|
|
|
||
|
|
# Create necessary symlinks for build
|
||
|
|
if [[ ! -d "${build_dir}/lib" ]]; then
|
||
|
|
mkdir -p "${build_dir}/lib"
|
||
|
|
fi
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Source prepared successfully" "success"
|
||
|
|
}
|
||
|
|
|
||
|
|
function __liblog_build {
|
||
|
|
local build_dir="${1}"
|
||
|
|
local install_dir="${2}"
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Starting liblog build..." "info"
|
||
|
|
|
||
|
|
# 检查源目录
|
||
|
|
if [[ ! -d "${build_dir}" ]]; then
|
||
|
|
display_alert "LIBLOG" "Build directory not found: ${build_dir}" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 设置编译环境
|
||
|
|
ARCH="${ARCH:-arm64}"
|
||
|
|
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
|
||
|
|
|
||
|
|
export ARCH="${ARCH}"
|
||
|
|
export CROSS_COMPILE="${CROSS_COMPILE}"
|
||
|
|
|
||
|
|
amlbian_checkout_toolchain
|
||
|
|
|
||
|
|
# Set build flags based on USE_FAKE_DEVICE
|
||
|
|
local extra_flags=""
|
||
|
|
if [[ "${USE_FAKE_DEVICE}" == "y" ]]; then
|
||
|
|
extra_flags="USE_FAKE_DEVICE=y"
|
||
|
|
display_alert "LIBLOG" "Building with fake log device support" "info"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Create output directory
|
||
|
|
local out_dir="${build_dir}/output"
|
||
|
|
mkdir -p "${out_dir}"
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Building liblog library..." "info"
|
||
|
|
|
||
|
|
cd "${build_dir}"
|
||
|
|
|
||
|
|
# Build with appropriate flags
|
||
|
|
make ${extra_flags} \
|
||
|
|
OUT_DIR="${out_dir}" \
|
||
|
|
LIBLOG_SRC_DIR="${build_dir}" \
|
||
|
|
CC="${CROSS_COMPILE}gcc" \
|
||
|
|
CFLAGS="-fPIC -I${build_dir}/include" \
|
||
|
|
ARCH="${ARCH}" \
|
||
|
|
-j$(nproc) \
|
||
|
|
2>&1 | tee "${out_dir}/make.log" || {
|
||
|
|
display_alert "LIBLOG" "Build failed" "err"
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
# Verify build output
|
||
|
|
local lib_files=("liblog.so" "liblog.so.${LIBLOG_SO_VERSION}" "liblog.so.${DEB_VERSION}")
|
||
|
|
local found_lib=0
|
||
|
|
for lib_file in "${lib_files[@]}"; do
|
||
|
|
if [[ -f "${out_dir}/${lib_file}" ]]; then
|
||
|
|
display_alert "LIBLOG" "Library file found: ${out_dir}/${lib_file}" "debug"
|
||
|
|
found_lib=1
|
||
|
|
break
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
|
||
|
|
if [[ ${found_lib} -eq 0 ]]; then
|
||
|
|
display_alert "LIBLOG" "Library file not found in output directory" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Build completed successfully" "success"
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
function __liblog_install {
|
||
|
|
local build_dir="${1}"
|
||
|
|
local install_dir="${2}"
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Installing liblog library..." "info"
|
||
|
|
|
||
|
|
# Get SYSROOT from build environment or use default
|
||
|
|
local SYSROOT="${SYSROOT:-/opt/arm64-sysroot}"
|
||
|
|
|
||
|
|
local out_dir="${build_dir}/output"
|
||
|
|
|
||
|
|
if [[ ! -d "${out_dir}" ]]; then
|
||
|
|
display_alert "LIBLOG" "Output directory not found: ${out_dir}" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 创建安装目录结构
|
||
|
|
mkdir -p "${install_dir}/usr/lib"
|
||
|
|
mkdir -p "${install_dir}/usr/include/android"
|
||
|
|
mkdir -p "${install_dir}/usr/include/cutils"
|
||
|
|
|
||
|
|
# 安装库文件
|
||
|
|
display_alert "LIBLOG" "Installing libraries..." "info"
|
||
|
|
|
||
|
|
# Copy library files
|
||
|
|
local lib_files=("liblog.so" "liblog.so.${LIBLOG_SO_VERSION}" "liblog.so.${DEB_VERSION}")
|
||
|
|
for lib_file in "${lib_files[@]}"; do
|
||
|
|
if [[ -f "${out_dir}/${lib_file}" ]]; then
|
||
|
|
install -m 0644 "${out_dir}/${lib_file}" "${install_dir}/usr/lib/"
|
||
|
|
display_alert "LIBLOG" "Installed ${lib_file} to ${install_dir}/usr/lib/" "debug"
|
||
|
|
|
||
|
|
# Also install to sysroot for cross-compilation
|
||
|
|
if [[ -d "${SYSROOT}" ]]; then
|
||
|
|
install -m 0644 "${out_dir}/${lib_file}" "${SYSROOT}/usr/lib/"
|
||
|
|
display_alert "LIBLOG" "Installed ${lib_file} to sysroot ${SYSROOT}/usr/lib/" "debug"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
|
||
|
|
# Create symlinks if they don't exist
|
||
|
|
cd "${install_dir}/usr/lib"
|
||
|
|
if [[ -f "liblog.so.${DEB_VERSION}" && ! -L "liblog.so.${LIBLOG_SO_VERSION}" ]]; then
|
||
|
|
ln -sf "liblog.so.${DEB_VERSION}" "liblog.so.${LIBLOG_SO_VERSION}"
|
||
|
|
fi
|
||
|
|
if [[ -f "liblog.so.${LIBLOG_SO_VERSION}" && ! -L "liblog.so" ]]; then
|
||
|
|
ln -sf "liblog.so.${LIBLOG_SO_VERSION}" "liblog.so"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Also create symlinks in sysroot
|
||
|
|
if [[ -d "${SYSROOT}" ]] && [[ -d "${SYSROOT}/usr/lib" ]]; then
|
||
|
|
cd "${SYSROOT}/usr/lib"
|
||
|
|
if [[ -f "liblog.so.${DEB_VERSION}" && ! -L "liblog.so.${LIBLOG_SO_VERSION}" ]]; then
|
||
|
|
ln -sf "liblog.so.${DEB_VERSION}" "liblog.so.${LIBLOG_SO_VERSION}"
|
||
|
|
fi
|
||
|
|
if [[ -f "liblog.so.${LIBLOG_SO_VERSION}" && ! -L "liblog.so" ]]; then
|
||
|
|
ln -sf "liblog.so.${LIBLOG_SO_VERSION}" "liblog.so"
|
||
|
|
fi
|
||
|
|
display_alert "LIBLOG" "Created symlinks in sysroot" "debug"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 安装头文件
|
||
|
|
display_alert "LIBLOG" "Installing headers..." "info"
|
||
|
|
if [[ -d "${build_dir}/include/android" ]]; then
|
||
|
|
cp -r "${build_dir}/include/android/"* "${install_dir}/usr/include/android/" 2>/dev/null || true
|
||
|
|
|
||
|
|
# Also install to sysroot
|
||
|
|
if [[ -d "${SYSROOT}" ]] && [[ -d "${SYSROOT}/usr/include" ]]; then
|
||
|
|
mkdir -p "${SYSROOT}/usr/include/android"
|
||
|
|
cp -r "${build_dir}/include/android/"* "${SYSROOT}/usr/include/android/" 2>/dev/null || true
|
||
|
|
display_alert "LIBLOG" "Installed android headers to sysroot" "debug"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
if [[ -d "${build_dir}/include/cutils" ]]; then
|
||
|
|
cp -r "${build_dir}/include/cutils/"* "${install_dir}/usr/include/cutils/" 2>/dev/null || true
|
||
|
|
|
||
|
|
# Also install to sysroot
|
||
|
|
if [[ -d "${SYSROOT}" ]] && [[ -d "${SYSROOT}/usr/include" ]]; then
|
||
|
|
mkdir -p "${SYSROOT}/usr/include/cutils"
|
||
|
|
cp -r "${build_dir}/include/cutils/"* "${SYSROOT}/usr/include/cutils/" 2>/dev/null || true
|
||
|
|
display_alert "LIBLOG" "Installed cutils headers to sysroot" "debug"
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Installation completed" "success"
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
function __liblog_create_deb {
|
||
|
|
local build_dir="${1}"
|
||
|
|
local install_dir="${2}"
|
||
|
|
|
||
|
|
# 设置必要的变量
|
||
|
|
local pkg_name="liblog"
|
||
|
|
local version="${DEB_VERSION}"
|
||
|
|
local architecture="${ARCH}"
|
||
|
|
local maintainer="Armbian Build <build@armbian.com>"
|
||
|
|
local description="Android liblog library for Amlogic platforms"
|
||
|
|
local description_long="This package provides the Android liblog library used by Android tools on Amlogic platforms."
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Creating Debian package for ${pkg_name} ${version}..." "info"
|
||
|
|
|
||
|
|
# 创建Debian包控制文件
|
||
|
|
local deb_dir="${build_dir}/deb/${pkg_name}-${version}"
|
||
|
|
local debian_dir="${deb_dir}/DEBIAN"
|
||
|
|
|
||
|
|
mkdir -p "${debian_dir}"
|
||
|
|
|
||
|
|
# 将安装目录的内容复制到debian-root
|
||
|
|
if [[ -d "${install_dir}" ]] && [[ "$(ls -A "${install_dir}")" ]]; then
|
||
|
|
cp -r "${install_dir}"/* "${deb_dir}/" 2>/dev/null || true
|
||
|
|
display_alert "LIBLOG" "Copied installed files to ${deb_dir}" "info"
|
||
|
|
else
|
||
|
|
display_alert "LIBLOG" "No installed files found in ${install_dir}" "warn"
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 创建control文件
|
||
|
|
cat > "${debian_dir}/control" <<EOF
|
||
|
|
Package: ${pkg_name}
|
||
|
|
Version: ${version}
|
||
|
|
Architecture: ${architecture}
|
||
|
|
Maintainer: ${maintainer}
|
||
|
|
Description: ${description}
|
||
|
|
${description_long}
|
||
|
|
Depends: libc6
|
||
|
|
Section: libs
|
||
|
|
Priority: optional
|
||
|
|
EOF
|
||
|
|
|
||
|
|
# 创建postinst脚本,用于更新ldconfig
|
||
|
|
cat > "${debian_dir}/postinst" <<EOF
|
||
|
|
#!/bin/sh
|
||
|
|
set -e
|
||
|
|
|
||
|
|
if [ "\$1" = "configure" ]; then
|
||
|
|
# 更新ldconfig缓存
|
||
|
|
if command -v ldconfig >/dev/null 2>&1; then
|
||
|
|
ldconfig
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "Liblog library installed successfully"
|
||
|
|
fi
|
||
|
|
|
||
|
|
exit 0
|
||
|
|
EOF
|
||
|
|
|
||
|
|
chmod 755 "${debian_dir}/postinst"
|
||
|
|
|
||
|
|
# 创建prerm脚本
|
||
|
|
cat > "${debian_dir}/prerm" <<EOF
|
||
|
|
#!/bin/sh
|
||
|
|
set -e
|
||
|
|
|
||
|
|
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
|
||
|
|
# Update ldconfig
|
||
|
|
if command -v ldconfig >/dev/null 2>&1; then
|
||
|
|
ldconfig
|
||
|
|
fi
|
||
|
|
echo "Removing liblog library"
|
||
|
|
fi
|
||
|
|
|
||
|
|
exit 0
|
||
|
|
EOF
|
||
|
|
|
||
|
|
chmod 755 "${debian_dir}/prerm"
|
||
|
|
|
||
|
|
display_alert "LIBLOG" "Building Debian package..." "info"
|
||
|
|
|
||
|
|
# 构建Debian包
|
||
|
|
cd "${build_dir}/deb"
|
||
|
|
dpkg-deb --build "${pkg_name}-${version}"
|
||
|
|
|
||
|
|
local deb_file="${pkg_name}-${version}.deb"
|
||
|
|
if [[ -f "${deb_file}" ]]; then
|
||
|
|
local output_dir="${BUILD_DIR}/output"
|
||
|
|
mkdir -p "${output_dir}"
|
||
|
|
mv "${deb_file}" "${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb"
|
||
|
|
display_alert "LIBLOG" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success"
|
||
|
|
else
|
||
|
|
display_alert "LIBLOG" "Failed to build Debian package" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
function amlbian_build_liblog {
|
||
|
|
if ! is_unit_enabled "liblog"; then
|
||
|
|
display_alert "[SKIP] >> liblog disable in BUILD_UNITS" "" "info"
|
||
|
|
return
|
||
|
|
fi
|
||
|
|
display_alert "AMLOGIC: Building liblog ${DEB_VERSION} ${BOARD}" "info"
|
||
|
|
|
||
|
|
# Define all directory paths (in .tmp directory)
|
||
|
|
local build_root="${BUILD_DIR}/.tmp/liblog-build-${ARCH:-arm64}"
|
||
|
|
local build_dir="${build_root}/build" # Build directory with source code
|
||
|
|
local install_dir="${build_root}/install" # Install directory
|
||
|
|
|
||
|
|
rm -rf "${build_root}"
|
||
|
|
|
||
|
|
# 创建目录
|
||
|
|
mkdir -p "${build_dir}" "${install_dir}"
|
||
|
|
|
||
|
|
display_alert "AMLOGIC: build_dir ${build_dir}" "info"
|
||
|
|
display_alert "AMLOGIC: install_dir ${install_dir}" "info"
|
||
|
|
display_alert "AMLOGIC: output_dir ${BUILD_DIR}/output" "info"
|
||
|
|
|
||
|
|
# 准备源代码
|
||
|
|
__liblog_prepare_source "${build_dir}"
|
||
|
|
|
||
|
|
# 构建
|
||
|
|
if ! __liblog_build "${build_dir}" "${install_dir}"; then
|
||
|
|
display_alert "AMLOGIC: Liblog build failed" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# 安装到安装目录
|
||
|
|
if ! __liblog_install "${build_dir}" "${install_dir}"; then
|
||
|
|
display_alert "AMLOGIC: Liblog installation failed" "err"
|
||
|
|
return 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
display_alert "AMLOGIC: Creating Debian package..." "info"
|
||
|
|
__liblog_create_deb "${build_dir}" "${install_dir}"
|
||
|
|
|
||
|
|
__unset_liblog_config
|
||
|
|
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
# ==============================================================================
|
||
|
|
# Section 5: Main
|
||
|
|
# ==============================================================================
|
||
|
|
|
||
|
|
display_alert "AMLIbian Liblog Build Script" "Version: ${DEB_VERSION}, Board: ${BOARD}" "info"
|
||
|
|
|
||
|
|
amlbian_build_liblog
|
||
|
|
|
||
|
|
display_alert "AMLOGIC: Liblog build completed" "${BUILD_DIR}/output" "info"
|
||
|
|
|
||
|
|
# ==============================================================================
|
||
|
|
# End of build-liblog.sh
|
||
|
|
# ==============================================================================
|