2026-08-13 11:28:40 +08:00
#!/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"
2026-11-11 08:50:43 +08:00
MODULE_BUILD_DIR = " ${ SRC } /output/.tmp"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# 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
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "aml-comp not found: ${ aml_comp_path } "
2026-08-13 11:28:40 +08:00
exit 1
fi
local source_dir = " ${ aml_comp_path } /vendor/amlogic/aml_commonlib/ubootenv"
if [[ ! -d " ${ source_dir } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Source directory not found: ${ source_dir } "
2026-08-13 11:28:40 +08:00
exit 1
fi
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Copying source from ${ source_dir } to ${ build_dir } "
2026-08-13 11:28:40 +08:00
# 清空构建目录
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 } "
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Starting aml-ubootenv build..."
2026-08-13 11:28:40 +08:00
# 检查源目录
if [[ ! -d " ${ build_dir } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Build directory not found: ${ build_dir } "
2026-08-13 11:28:40 +08:00
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 } "
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Building aml-ubootenv library and binary..."
2026-08-13 11:28:40 +08:00
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
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Fixed Makefile linking order"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "AML-UBOOTENV" "Makefile not found, skipping fix"
2026-08-13 11:28:40 +08:00
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" || {
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Build failed"
2026-08-13 11:28:40 +08:00
return 1
}
# Verify build output
local lib_file = " ${ out_dir } /libubootenv.so"
local bin_file = " ${ out_dir } /uenv"
if [[ ! -f " ${ lib_file } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Library file not found: ${ lib_file } "
2026-08-13 11:28:40 +08:00
return 1
fi
if [[ ! -f " ${ bin_file } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Binary file not found: ${ bin_file } "
2026-08-13 11:28:40 +08:00
return 1
fi
display_alert "AML-UBOOTENV" "Build completed successfully" "success"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Library: ${ lib_file } "
aml_log debug "AML-UBOOTENV" "Binary: ${ bin_file } "
2026-08-13 11:28:40 +08:00
return 0
}
function __aml_ubootenv_install {
local build_dir = " ${ 1 } "
local install_dir = " ${ 2 } "
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Installing aml-ubootenv library and binary..."
2026-08-13 11:28:40 +08:00
local out_dir = " ${ build_dir } /output"
if [[ ! -d " ${ out_dir } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Output directory not found: ${ out_dir } "
2026-08-13 11:28:40 +08:00
return 1
fi
# 创建安装目录结构
mkdir -p " ${ install_dir } /usr/lib"
mkdir -p " ${ install_dir } /usr/bin"
mkdir -p " ${ install_dir } /usr/include"
# 安装库文件
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Installing library..."
2026-08-13 11:28:40 +08:00
local lib_file = " ${ out_dir } /libubootenv.so"
if [[ -f " ${ lib_file } " ]] ; then
install -m 0644 " ${ lib_file } " " ${ install_dir } /usr/lib/"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Installed libubootenv.so to ${ install_dir } /usr/lib/"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Library file not found: ${ lib_file } "
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装可执行文件
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Installing binary..."
2026-08-13 11:28:40 +08:00
local bin_file = " ${ out_dir } /uenv"
if [[ -f " ${ bin_file } " ]] ; then
install -m 0755 " ${ bin_file } " " ${ install_dir } /usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Installed uenv to ${ install_dir } /usr/bin/"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Binary file not found: ${ bin_file } "
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装头文件
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Installing headers..."
2026-08-13 11:28:40 +08:00
local header_file = " ${ build_dir } /ubootenv.h"
if [[ -f " ${ header_file } " ]] ; then
install -m 0644 " ${ header_file } " " ${ install_dir } /usr/include/"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Installed ubootenv.h to ${ install_dir } /usr/include/"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Header file not found: ${ header_file } "
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Installed libubootenv.so to staging directory"
2026-08-13 11:28:40 +08:00
fi
if [[ -f " ${ header_file } " ]] ; then
install -m 0644 " ${ header_file } " " ${ BUILD_DIR } /staging/usr/include/"
2026-11-11 08:50:43 +08:00
aml_log debug "AML-UBOOTENV" "Installed ubootenv.h to staging directory"
2026-08-13 11:28:40 +08:00
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 <build@armbian.com>"
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."
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Creating Debian package for ${ pkg_name } ${ version } ..."
2026-08-13 11:28:40 +08:00
# 创建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
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Copied installed files to ${ deb_dir } "
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "AML-UBOOTENV" "No installed files found in ${ install_dir } "
2026-08-13 11:28:40 +08:00
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 "AML ubootenv library and tools 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 AML ubootenv library and tools"
fi
exit 0
EOF
chmod 755 " ${ debian_dir } /prerm"
2026-11-11 08:50:43 +08:00
aml_log info "AML-UBOOTENV" "Building Debian package..."
2026-08-13 11:28:40 +08:00
# 构建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
2026-11-11 08:50:43 +08:00
aml_log error "AML-UBOOTENV" "Failed to build Debian package"
2026-08-13 11:28:40 +08:00
return 1
fi
return 0
}
function amlbian_build_aml_ubootenv {
if ! is_unit_enabled "aml-ubootenv" && ! is_unit_enabled "tvserver" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> aml-ubootenv disable in BUILD_UNITS"
2026-08-13 11:28:40 +08:00
return
fi
if ! is_feature_contains " $DISTRO_FEATURES " "tvserver" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> tvserver disable in DISTRO_FEATURES"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building aml-ubootenv ${ DEB_VERSION } ${ BOARD } "
2026-08-13 11:28:40 +08:00
# Define all directory paths (in .tmp directory)
2026-11-11 08:50:43 +08:00
local build_root = " ${ MODULE_BUILD_DIR } /aml-ubootenv-build- ${ ARCH :- arm64 } "
2026-08-13 11:28:40 +08:00
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 } "
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: build_dir ${ build_dir } "
aml_log info "AMLOGIC: install_dir ${ install_dir } "
aml_log info "AMLOGIC: output_dir ${ BUILD_DIR } /output"
2026-08-13 11:28:40 +08:00
# 准备源代码
__aml_ubootenv_prepare_source " ${ build_dir } "
# 构建
if ! __aml_ubootenv_build " ${ build_dir } " " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: aml-ubootenv build failed"
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装到安装目录
if ! __aml_ubootenv_install " ${ build_dir } " " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: aml-ubootenv installation failed"
2026-08-13 11:28:40 +08:00
return 1
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Creating Debian package..."
2026-08-13 11:28:40 +08:00
__aml_ubootenv_create_deb " ${ build_dir } " " ${ install_dir } "
__unset_aml_ubootenv_config
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLbian AML ubootenv Build Script" "Version: ${ DEB_VERSION } , Board: ${ BOARD } "
2026-08-13 11:28:40 +08:00
amlbian_build_aml_ubootenv
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: aml-ubootenv build completed" " ${ BUILD_DIR } /output"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build-aml-ubootenv.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================