2026-08-13 11:28:40 +08:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Libge2d build script for AMLbian
# This script builds libge2d 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"
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 = " ${ LIBGE2D_VERSION :- 1 .0 } "
ARCH = " ${ ARCH :- arm64 } "
# ==============================================================================
# Section 4: Libge2d Build Functions
# ==============================================================================
function __unset_libge2d_config {
# Clean up any environment variables set during build
unset EXTRA_OEMAKE
unset SYSROOT
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
}
function __libge2d_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 "LIBGE2D" "aml-comp not found: ${ aml_comp_path } "
2026-08-13 11:28:40 +08:00
exit 1
fi
local source_dir = " ${ aml_comp_path } /hardware/amlogic/libge2d"
if [[ ! -d " ${ source_dir } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "LIBGE2D" "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 debug "LIBGE2D" "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
# Check for necessary source files
if [[ ! -f " ${ build_dir } /Makefile" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "LIBGE2D" "Makefile not found in source"
2026-08-13 11:28:40 +08:00
exit 1
fi
display_alert "LIBGE2D" "Source prepared successfully" "success"
}
function __libge2d_build_depend {
local build_dir = " ${ 1 } "
local dependencies =( "libion" )
# libion
2026-11-11 08:50:43 +08:00
local libion_dir = " ${ MODULE_BUILD_DIR } /libion-build- ${ ARCH :- arm64 } "
2026-08-13 11:28:40 +08:00
if [ -z " ${ libion_dir } " ] || [ ! -d " ${ libion_dir } " ] ; then
2026-11-11 08:50:43 +08:00
aml_log warning "LIBGE2D" "libion not found in ${ MODULE_BUILD_DIR } build."
aml_log info "AMLOGIC" "TODO>> Start build libion "
2026-08-13 11:28:40 +08:00
if ! (
source " ${ SCRIPT_DIR } /build-libion.sh"
) ; then
2026-11-11 08:50:43 +08:00
aml_log error "ERROR" "build-libion failed"
2026-08-13 11:28:40 +08:00
return 1
fi
if [ -z " ${ libion_dir } " ] || [ ! -d " ${ libion_dir } " ] ; then
2026-11-11 08:50:43 +08:00
aml_log error "ERROR" "libion build finished but directory still not found"
2026-08-13 11:28:40 +08:00
return 1
fi
fi
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "libion dir: ${ libion_dir } "
2026-08-13 11:28:40 +08:00
# copy
local ionmem_head_flags = "-I ${ libion_dir } /build/include -I ${ libion_dir } /build/include/ion"
export CPPFLAGS = " ${ CPPFLAGS } -I ${ libion_dir } /build/include -I ${ libion_dir } /build/include/ion"
export CFLAGS = " ${ CFLAGS } -I ${ libion_dir } /build/include -I ${ libion_dir } /build/include/ion"
export CXXFLAGS = " ${ CXXFLAGS } -I ${ libion_dir } /build/include -I ${ libion_dir } /build/include/ion"
export LDFLAGS = " ${ LDFLAGS } -L ${ libion_dir } /build"
}
function __libge2d_build {
local build_dir = " ${ 1 } "
local install_dir = " ${ 2 } "
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "Starting libge2d 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 "LIBGE2D" "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- } "
# SYSROOT="${SYSROOT:-/opt/arm64-sysroot}"
export ARCH = " ${ ARCH } "
export CROSS_COMPILE = " ${ CROSS_COMPILE } "
amlbian_checkout_toolchain
__libge2d_build_depend
export CFLAGS = " ${ CFLAGS } \
-I ${ build_dir } /libge2d/include \
-I ${ build_dir } /libge2d/kernel-headers/linux \
-fPIC"
export CXXFLAGS = " ${ CFLAGS } "
# 创建输出目录
local out_dir = " ${ build_dir } /output"
mkdir -p " ${ out_dir } "
# Fix Makefile linking order (as in original script)
cd " ${ build_dir } "
if [[ -f "Makefile" ]] ; then
# Fix linking order and add LDFLAGS to the test program linking commands
sed -i 's/\$(CC) \$(CFLAGS) -L\$(LIBDIR) -L\$(OUT_DIR) -lpthread -lge2d/\$(CC) \$(CFLAGS) \$(LDFLAGS) -L\$(LIBDIR) -L\$(OUT_DIR) -lpthread -lge2d/' Makefile
sed -i 's/-lpthread -lge2d \$(addsuffix .c,\$(FEATURE_TEST))/\$(addsuffix .c,$(FEATURE_TEST)) -lpthread -lge2d -lion/' Makefile
sed -i 's/-lpthread -lge2d \$(addsuffix .c,\$(CHIP_CHECK))/\$(addsuffix .c,$(CHIP_CHECK)) -lpthread -lge2d -lion/' Makefile
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Fixed Makefile linking order and added LDFLAGS"
2026-08-13 11:28:40 +08:00
fi
# Fix libge2d/Makefile to use LDFLAGS and add library search paths
if [[ -f " ${ build_dir } /libge2d/Makefile" ]] ; then
# Add LDFLAGS to the linking command
sed -i 's/\$(CC) \$(CFLAGS) -shared -fPIC/\$(CC) \$(CFLAGS) \$(LDFLAGS) -shared -fPIC/' " ${ build_dir } /libge2d/Makefile"
# Fix linking order: move -lion after object files
sed -i 's/-lion -o \$(OUT_DIR)\/\$(LIB) \$(OBJS)/-o \$(OUT_DIR)\/\$(LIB) \$(OBJS) -lion/' " ${ build_dir } /libge2d/Makefile"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Added LDFLAGS and fixed linking order in libge2d/Makefile"
2026-08-13 11:28:40 +08:00
fi
# Set build flags based on EXTRA_OEMAKE from .bb file
local EXTRA_OEMAKE = "OUT_DIR= ${ out_dir } LIBGE2D_STAGING_DIR= ${ out_dir } CROSS= ${ CROSS_COMPILE } TARGET_DIR= ${ out_dir } STAGING_DIR= ${ out_dir } DESTDIR= ${ out_dir } INSTALL_DIR= ${ out_dir } /usr/lib"
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "Building libge2d library..."
2026-08-13 11:28:40 +08:00
cd " ${ build_dir } "
# 编译命令,使用EXTRA_OEMAKE参数
make ${ EXTRA_OEMAKE } \
CC = " ${ CROSS_COMPILE } gcc" \
CFLAGS = " ${ CFLAGS } " \
LDFLAGS = " ${ LDFLAGS } " \
ARCH = " ${ ARCH } " \
-j$( nproc) \
all 2>& 1 | tee " ${ out_dir } /make.log" || {
2026-11-11 08:50:43 +08:00
aml_log error "LIBGE2D" "Build failed"
2026-08-13 11:28:40 +08:00
return 1
}
# Verify build output
if [[ ! -f " ${ out_dir } /libge2d.so" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "LIBGE2D" "Library file not found: ${ out_dir } /libge2d.so"
2026-08-13 11:28:40 +08:00
return 1
fi
if [[ ! -f " ${ out_dir } /ge2d_feature_test" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log warn "LIBGE2D" "Test program not found: ${ out_dir } /ge2d_feature_test"
2026-08-13 11:28:40 +08:00
fi
display_alert "LIBGE2D" "Build completed successfully" "success"
return 0
}
function __libge2d_install {
local build_dir = " ${ 1 } "
local install_dir = " ${ 2 } "
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "Installing libge2d library..."
2026-08-13 11:28:40 +08:00
# 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
2026-11-11 08:50:43 +08:00
aml_log error "LIBGE2D" "Output directory not found: ${ out_dir } "
2026-08-13 11:28:40 +08:00
return 1
fi
# 创建安装目录结构
mkdir -p " ${ install_dir } /usr/include"
mkdir -p " ${ install_dir } /usr/lib"
mkdir -p " ${ install_dir } /usr/bin"
# 安装库文件
if [[ -f " ${ out_dir } /libge2d.so" ]] ; then
install -m 0644 -D " ${ out_dir } /libge2d.so" " ${ install_dir } /usr/lib/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Library installed 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 "LIBGE2D" "libge2d.so not found in output directory"
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装头文件
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "Installing headers..."
2026-08-13 11:28:40 +08:00
local header_files =( "aml_ge2d.h" "ge2d_port.h" )
for header_file in " ${ header_files [@] } " ; do
if [[ -f " ${ build_dir } /libge2d/include/ ${ header_file } " ]] ; then
install -m 0644 -D " ${ build_dir } /libge2d/include/ ${ header_file } " " ${ install_dir } /usr/include/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Header installed: ${ header_file } "
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "LIBGE2D" "Header file not found: ${ build_dir } /libge2d/include/ ${ header_file } "
2026-08-13 11:28:40 +08:00
fi
done
# 安装二进制文件
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "Installing binaries..."
2026-08-13 11:28:40 +08:00
if [[ -f " ${ out_dir } /ge2d_feature_test" ]] ; then
install -m 0755 -D " ${ out_dir } /ge2d_feature_test" " ${ install_dir } /usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Binary installed: ge2d_feature_test"
2026-08-13 11:28:40 +08:00
fi
if [[ -f " ${ out_dir } /ge2d_chip_check" ]] ; then
install -m 0755 -D " ${ out_dir } /ge2d_chip_check" " ${ install_dir } /usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBGE2D" "Binary installed: ge2d_chip_check"
2026-08-13 11:28:40 +08:00
fi
display_alert "LIBGE2D" "Installation completed" "success"
return 0
}
function __libge2d_create_deb {
local build_dir = " ${ 1 } "
local install_dir = " ${ 2 } "
# 设置必要的变量
local pkg_name = "libge2d"
local version = " ${ DEB_VERSION } "
local architecture = " ${ ARCH } "
local maintainer = "Armbian Build <build@armbian.com>"
local description = "GE2D (Graphics Engine 2D) library for Amlogic SoCs"
local description_long = "This package provides the GE2D (Graphics Engine 2D) library for Amlogic SoCs. GE2D is a hardware-accelerated 2D graphics engine that supports various 2D operations like rotation, scaling, blending, and format conversion."
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "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 "LIBGE2D" "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 "LIBGE2D" "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, libion
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 "Libge2d 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 libge2d library"
fi
exit 0
EOF
chmod 755 " ${ debian_dir } /prerm"
2026-11-11 08:50:43 +08:00
aml_log info "LIBGE2D" "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 "LIBGE2D" "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 "LIBGE2D" "Failed to build Debian package"
2026-08-13 11:28:40 +08:00
return 1
fi
return 0
}
function amlbian_build_libge2d {
if ! is_unit_enabled "libge2d" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> libge2d disable in BUILD_UNITS"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building libge2d ${ 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 } /libge2d-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
# 准备源代码
__libge2d_prepare_source " ${ build_dir } "
# 构建
if ! __libge2d_build " ${ build_dir } " " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Libge2d build failed"
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装到安装目录
if ! __libge2d_install " ${ build_dir } " " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Libge2d 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
__libge2d_create_deb " ${ build_dir } " " ${ install_dir } "
__unset_libge2d_config
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLIbian Libge2d Build Script" "Version: ${ DEB_VERSION } , Board: ${ BOARD } "
2026-08-13 11:28:40 +08:00
amlbian_build_libge2d
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Libge2d build completed" " ${ BUILD_DIR } /output"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build-libge2d.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================