#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2026 AMLOGIC # # AML ubootenv build script for AMLbian # This script builds aml-ubootenv library for Amlogic SoCs set -e # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AMLBIAN_DIR="$(dirname "${SCRIPT_DIR}")" AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # Go up one more level from multimedia/tvserver AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # Go up one more level from scripts 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="${AML_UBOOTENV_VERSION:-1.0.0}" ARCH="${ARCH:-arm64}" # ============================================================================== # Section 4: AML ubootenv Build Functions # ============================================================================== function __unset_aml_ubootenv_config { # Clean up any environment variables set during build unset CFLAGS unset CXXFLAGS unset LDFLAGS } function __aml_ubootenv_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 "AML-UBOOTENV" "aml-comp not found: ${aml_comp_path}" "err" exit 1 fi local source_dir="${aml_comp_path}/vendor/amlogic/aml_commonlib/ubootenv" if [[ ! -d "${source_dir}" ]]; then display_alert "AML-UBOOTENV" "Source directory not found: ${source_dir}" "err" exit 1 fi display_alert "AML-UBOOTENV" "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 display_alert "AML-UBOOTENV" "Source prepared successfully" "success" } function __aml_ubootenv_build { local build_dir="${1}" local install_dir="${2}" display_alert "AML-UBOOTENV" "Starting aml-ubootenv build..." "info" # 检查源目录 if [[ ! -d "${build_dir}" ]]; then display_alert "AML-UBOOTENV" "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 # Create output directory local out_dir="${build_dir}/output" mkdir -p "${out_dir}" display_alert "AML-UBOOTENV" "Building aml-ubootenv library and binary..." "info" cd "${build_dir}" # Fix Makefile linking order: move -L$(OUT_DIR) -lubootenv after object files # Original: $(CC) -L$(OUT_DIR) -lubootenv $(patsubst %.o, $(OUT_DIR)/%.o, $<) -o $(OUT_DIR)/$@ # Fixed: $(CC) $(patsubst %.o, $(OUT_DIR)/%.o, $<) -L$(OUT_DIR) -lubootenv -o $(OUT_DIR)/$@ if [[ -f "Makefile" ]]; then sed -i 's/\$(CC) -L\$(OUT_DIR) -lubootenv \$(patsubst \%.o, \$(OUT_DIR)\/%.o, \$<)/\$(CC) \$(patsubst \%.o, \$(OUT_DIR)\/%.o, \$<) -L\$(OUT_DIR) -lubootenv/g' Makefile display_alert "AML-UBOOTENV" "Fixed Makefile linking order" "debug" else display_alert "AML-UBOOTENV" "Makefile not found, skipping fix" "warn" fi # Build with appropriate flags make OUT_DIR="${out_dir}" \ CC="${CROSS_COMPILE}gcc" \ CFLAGS="-fPIC" \ TARGET_DIR="${install_dir}" \ STAGING_DIR="${BUILD_DIR}/staging" \ -j$(nproc) \ all 2>&1 | tee "${out_dir}/make.log" || { display_alert "AML-UBOOTENV" "Build failed" "err" return 1 } # Verify build output local lib_file="${out_dir}/libubootenv.so" local bin_file="${out_dir}/uenv" if [[ ! -f "${lib_file}" ]]; then display_alert "AML-UBOOTENV" "Library file not found: ${lib_file}" "err" return 1 fi if [[ ! -f "${bin_file}" ]]; then display_alert "AML-UBOOTENV" "Binary file not found: ${bin_file}" "err" return 1 fi display_alert "AML-UBOOTENV" "Build completed successfully" "success" display_alert "AML-UBOOTENV" "Library: ${lib_file}" "debug" display_alert "AML-UBOOTENV" "Binary: ${bin_file}" "debug" return 0 } function __aml_ubootenv_install { local build_dir="${1}" local install_dir="${2}" display_alert "AML-UBOOTENV" "Installing aml-ubootenv library and binary..." "info" local out_dir="${build_dir}/output" if [[ ! -d "${out_dir}" ]]; then display_alert "AML-UBOOTENV" "Output directory not found: ${out_dir}" "err" return 1 fi # 创建安装目录结构 mkdir -p "${install_dir}/usr/lib" mkdir -p "${install_dir}/usr/bin" mkdir -p "${install_dir}/usr/include" # 安装库文件 display_alert "AML-UBOOTENV" "Installing library..." "info" local lib_file="${out_dir}/libubootenv.so" if [[ -f "${lib_file}" ]]; then install -m 0644 "${lib_file}" "${install_dir}/usr/lib/" display_alert "AML-UBOOTENV" "Installed libubootenv.so to ${install_dir}/usr/lib/" "debug" else display_alert "AML-UBOOTENV" "Library file not found: ${lib_file}" "err" return 1 fi # 安装可执行文件 display_alert "AML-UBOOTENV" "Installing binary..." "info" local bin_file="${out_dir}/uenv" if [[ -f "${bin_file}" ]]; then install -m 0755 "${bin_file}" "${install_dir}/usr/bin/" display_alert "AML-UBOOTENV" "Installed uenv to ${install_dir}/usr/bin/" "debug" else display_alert "AML-UBOOTENV" "Binary file not found: ${bin_file}" "err" return 1 fi # 安装头文件 display_alert "AML-UBOOTENV" "Installing headers..." "info" local header_file="${build_dir}/ubootenv.h" if [[ -f "${header_file}" ]]; then install -m 0644 "${header_file}" "${install_dir}/usr/include/" display_alert "AML-UBOOTENV" "Installed ubootenv.h to ${install_dir}/usr/include/" "debug" else display_alert "AML-UBOOTENV" "Header file not found: ${header_file}" "err" return 1 fi # Also install to staging directory for cross-compilation if [[ -d "${BUILD_DIR}/staging" ]]; then mkdir -p "${BUILD_DIR}/staging/usr/lib" mkdir -p "${BUILD_DIR}/staging/usr/include" if [[ -f "${lib_file}" ]]; then install -m 0644 "${lib_file}" "${BUILD_DIR}/staging/usr/lib/" display_alert "AML-UBOOTENV" "Installed libubootenv.so to staging directory" "debug" fi if [[ -f "${header_file}" ]]; then install -m 0644 "${header_file}" "${BUILD_DIR}/staging/usr/include/" display_alert "AML-UBOOTENV" "Installed ubootenv.h to staging directory" "debug" fi fi display_alert "AML-UBOOTENV" "Installation completed" "success" return 0 } function __aml_ubootenv_create_deb { local build_dir="${1}" local install_dir="${2}" # 设置必要的变量 local pkg_name="aml-ubootenv" local version="${DEB_VERSION}" local architecture="${ARCH}" local maintainer="Armbian Build " local description="Amlogic U-Boot environment library and tools" local description_long="This package provides the Amlogic U-Boot environment library (libubootenv) and utility (uenv) for managing U-Boot environment variables on Amlogic platforms." display_alert "AML-UBOOTENV" "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 "AML-UBOOTENV" "Copied installed files to ${deb_dir}" "info" else display_alert "AML-UBOOTENV" "No installed files found in ${install_dir}" "warn" return 0 fi # 创建control文件 cat > "${debian_dir}/control" < "${debian_dir}/postinst" </dev/null 2>&1; then ldconfig fi echo "AML ubootenv library and tools installed successfully" fi exit 0 EOF chmod 755 "${debian_dir}/postinst" # 创建prerm脚本 cat > "${debian_dir}/prerm" </dev/null 2>&1; then ldconfig fi echo "Removing AML ubootenv library and tools" fi exit 0 EOF chmod 755 "${debian_dir}/prerm" display_alert "AML-UBOOTENV" "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 "AML-UBOOTENV" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success" else display_alert "AML-UBOOTENV" "Failed to build Debian package" "err" return 1 fi return 0 } function amlbian_build_aml_ubootenv { if ! is_unit_enabled "aml-ubootenv" && ! is_unit_enabled "tvserver"; then display_alert "[SKIP] >> aml-ubootenv disable in BUILD_UNITS" "" "info" return fi if ! is_feature_contains "$DISTRO_FEATURES" "tvserver"; then display_alert "[SKIP] >> tvserver disable in DISTRO_FEATURES" "" "info" return fi display_alert "AMLOGIC: Building aml-ubootenv ${DEB_VERSION} ${BOARD}" "info" # Define all directory paths (in .tmp directory) local build_root="${BUILD_DIR}/.tmp/aml-ubootenv-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" # 准备源代码 __aml_ubootenv_prepare_source "${build_dir}" # 构建 if ! __aml_ubootenv_build "${build_dir}" "${install_dir}"; then display_alert "AMLOGIC: aml-ubootenv build failed" "err" return 1 fi # 安装到安装目录 if ! __aml_ubootenv_install "${build_dir}" "${install_dir}"; then display_alert "AMLOGIC: aml-ubootenv installation failed" "err" return 1 fi display_alert "AMLOGIC: Creating Debian package..." "info" __aml_ubootenv_create_deb "${build_dir}" "${install_dir}" __unset_aml_ubootenv_config return 0 } # ============================================================================== # Section 5: Main # ============================================================================== display_alert "AMLbian AML ubootenv Build Script" "Version: ${DEB_VERSION}, Board: ${BOARD}" "info" amlbian_build_aml_ubootenv display_alert "AMLOGIC: aml-ubootenv build completed" "${BUILD_DIR}/output" "info" # ============================================================================== # End of build-aml-ubootenv.sh # ==============================================================================