Files
BPI-CM9-Linux/amlbian/scripts/multimedia/gstreamer/build-gst-aml-dma-allocator.sh
T
2026-08-24 10:03:24 +08:00

399 lines
14 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# gst-aml-dma-allocator build script for AMLbian
# This script builds gst-aml-dma-allocator library for Amlogic SoCs
set -e
umask 022
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "${SCRIPT_DIR}")" # amlbian/scripts/multimedia
AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # amlbian/scripts
AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # amlbian
BUILD_DIR="${SRC}"
# 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="${GST_AML_DMA_ALLOCATOR_VERSION:-1.0}"
#ARCH="${ARCH:-arm64}"
: "${ARCH:=arm64}"
# ==============================================================================
# Section 4: gst-aml-dma-allocator Build Functions
# ==============================================================================
function __unset_gst_aml_dma_allocator_config {
# Clean up any environment variables set during build
unset EXTRA_OEMAKE
unset SYSROOT
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
unset CPPFLAGS
}
function __gst_aml_dma_allocator_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 "GST-AML-DMA-ALLOCATOR" "aml-comp not found: ${aml_comp_path}" "err"
exit 1
fi
local source_dir="${aml_comp_path}/multimedia/gst-aml-utils/gst-aml-dma-allocator"
if [[ ! -d "${source_dir}" ]]; then
display_alert "GST-AML-DMA-ALLOCATOR" "Source directory not found: ${source_dir}" "err"
exit 1
fi
display_alert "GST-AML-DMA-ALLOCATOR" "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
# Check for necessary source files
if [[ ! -f "${build_dir}/gstamldmaallocator.c" ]] || [[ ! -f "${build_dir}/Makefile" ]]; then
display_alert "GST-AML-DMA-ALLOCATOR" "Source files not found in build directory" "warning"
fi
display_alert "GST-AML-DMA-ALLOCATOR" "Source prepared successfully" "success"
}
function __gst_aml_dma_allocator_build {
local build_dir="${1}"
local install_dir="${2}"
display_alert "GST-AML-DMA-ALLOCATOR" "Starting gst-aml-dma-allocator build..." "info"
# 检查源目录
if [[ ! -d "${build_dir}" ]]; then
display_alert "GST-AML-DMA-ALLOCATOR" "Build directory not found: ${build_dir}" "err"
return 1
fi
# 设置编译环境
#ARCH="${ARCH:-arm64}"
: "${ARCH:=arm64}"
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
export ARCH
export CROSS_COMPILE="${CROSS_COMPILE}"
# amlbian_checkout_toolchain
# Check if ROOTFS_TEMP exists, use it as sysroot, otherwise error
local SYSROOT=""
if [[ -n "${ROOTFS_TEMP}" ]] && [[ -d "${ROOTFS_TEMP}" ]] && [[ -d "${ROOTFS_TEMP}/usr/lib/aarch64-linux-gnu" ]]; then
SYSROOT="${ROOTFS_TEMP}"
display_alert "GST-AML-DMA-ALLOCATOR" "Using ROOTFS_TEMP as sysroot: ${SYSROOT}" "info"
else
display_alert "GST-AML-DMA-ALLOCATOR" "ROOTFS_TEMP not found or invalid: ${ROOTFS_TEMP}" "err"
display_alert "GST-AML-DMA-ALLOCATOR" "ROOTFS_TEMP should be created by create_base_rootfs function" "err"
return 1
fi
# Use sysroot configuration
display_alert "GST-AML-DMA-ALLOCATOR" "Using sysroot: ${SYSROOT}" "info"
export PKG_CONFIG=/usr/bin/pkg-config
export PKG_CONFIG_SYSROOT_DIR="${SYSROOT}"
export PKG_CONFIG_LIBDIR="${SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig:${SYSROOT}/usr/share/pkgconfig"
unset PKG_CONFIG_PATH
# Set up flags with sysroot
export CPPFLAGS="${CPPFLAGS} --sysroot=${SYSROOT}"
export CFLAGS="${CFLAGS} --sysroot=${SYSROOT} -fPIC"
export CXXFLAGS="${CXXFLAGS} --sysroot=${SYSROOT} -fPIC"
export LDFLAGS="${LDFLAGS} --sysroot=${SYSROOT}"
# Add sysroot-specific include paths
local include_flags="-I${SYSROOT}/usr/include/aarch64-linux-gnu \
-I${SYSROOT}/usr/include/gstreamer-1.0 \
-I${SYSROOT}/usr/include/glib-2.0 \
-I${SYSROOT}/usr/lib/aarch64-linux-gnu/glib-2.0/include \
-I${SYSROOT}/usr/include/libdrm \
-I${SYSROOT}/usr/include/gstreamer-1.0/gst/allocators \
-I${build_dir}/inc"
export CPPFLAGS="${CPPFLAGS} ${include_flags}"
export CFLAGS="${CFLAGS} ${include_flags}"
export CXXFLAGS="${CXXFLAGS} ${include_flags}"
# Add sysroot-specific library paths
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib/aarch64-linux-gnu -L${SYSROOT}/usr/aarch64-linux-gnu/lib"
# 进入源代码目录
cd "${build_dir}" || {
display_alert "GST-AML-DMA-ALLOCATOR" "Cannot enter build directory: ${build_dir}" "err"
return 1
}
# 设置 Makefile 需要的变量
export OUT_DIR="${build_dir}"
export STAGING_DIR="${SYSROOT}"
export TARGET_DIR="${install_dir}"
export OBJDIR="."
# 设置 PKG_CONFIG 路径
export PKG_CONFIG="${STAGING_DIR}/usr/bin/pkg-config"
# 设置 GST_CFLAGS 和 GST_LIBS(可选,Makefile 已经使用 pkg-config)
export GST_CFLAGS="$(pkg-config --cflags gstreamer-1.0 gstreamer-base-1.0 gstreamer-allocators-1.0 gstreamer-video-1.0)"
export GST_LIBS="$(pkg-config --libs gstreamer-1.0 gstreamer-base-1.0 gstreamer-allocators-1.0 gstreamer-video-1.0)"
# 设置编译器
export CC="${CROSS_COMPILE}gcc"
export LD="${CROSS_COMPILE}ld"
export AR="${CROSS_COMPILE}ar"
export STRIP="${CROSS_COMPILE}strip"
# 清理之前的构建
display_alert "GST-AML-DMA-ALLOCATOR" "Cleaning previous build..." "info"
make clean 2>/dev/null || true
# 构建
display_alert "GST-AML-DMA-ALLOCATOR" "Building with Makefile..." "info"
# 使用 Makefile 构建
make -j$(nproc) \
CC="${CC}" \
CFLAGS="${CFLAGS}" \
LDFLAGS="${LDFLAGS}" \
PKG_CONFIG="${PKG_CONFIG}" \
STAGING_DIR="${STAGING_DIR}" \
TARGET_DIR="${TARGET_DIR}" \
OUT_DIR="${OUT_DIR}" \
2>&1 | tee make.log || {
display_alert "GST-AML-DMA-ALLOCATOR" "make failed" "err"
return 1
}
# 验证构建输出
local target_lib="${build_dir}/libgst-aml-dma-allocator.so"
if [[ ! -f "${target_lib}" ]]; then
display_alert "GST-AML-DMA-ALLOCATOR" "Target library not created: ${target_lib}" "err"
return 1
fi
display_alert "GST-AML-DMA-ALLOCATOR" "Build completed successfully" "success"
return 0
}
function __gst_aml_dma_allocator_install {
local build_dir="${1}"
local install_dir="${2}"
display_alert "GST-AML-DMA-ALLOCATOR" \
"Installing gst-aml-dma-allocator library..." "info"
mkdir -p "${install_dir}/usr/include/gst/allocators"
mkdir -p "${install_dir}/usr/lib/aarch64-linux-gnu"
# install headers
if [[ -d "${build_dir}/inc" ]]; then
install -m 0644 \
"${build_dir}"/inc/*.h \
"${install_dir}/usr/include/gst/allocators/"
fi
# install shared library
if [[ -f "${build_dir}/libgst-aml-dma-allocator.so" ]]; then
install -m 0755 \
"${build_dir}/libgst-aml-dma-allocator.so" \
"${install_dir}/usr/lib/aarch64-linux-gnu/"
else
display_alert "GST-AML-DMA-ALLOCATOR" \
"Shared library not found" "err"
return 1
fi
display_alert "GST-AML-DMA-ALLOCATOR" \
"Installation completed" "success"
return 0
}
function __gst_aml_dma_allocator_create_deb {
local build_dir="${1}"
local install_dir="${2}"
# 设置必要的变量
local pkg_name="gst-aml-dma-allocator"
local version="${DEB_VERSION}"
local architecture="${ARCH}"
local maintainer="Armbian Build <[email protected]>"
local description="Amlogic DMA buffer allocator GStreamer library and headers"
local description_long="This package provides Amlogic-specific DMA buffer allocator libraries for GStreamer. These plugins enable GStreamer to use Amlogic's DMA buffer allocation and management for hardware-accelerated multimedia processing."
display_alert "GST-AML-DMA-ALLOCATOR" "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 "GST-AML-DMA-ALLOCATOR" "Copied installed files to ${deb_dir}" "info"
else
display_alert "GST-AML-DMA-ALLOCATOR" "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: libgstreamer1.0-0 (>= 1.0.0), gstreamer1.0-plugins-base (>= 1.0.0), libdma-allocator (>= 1.0.0)
Section: libs
Priority: optional
EOF
# 创建postinst脚本,用于更新ldconfig和GStreamer插件缓存
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
# 更新GStreamer插件缓存
if command -v gst-inspect-1.0 >/dev/null 2>&1; then
gst-inspect-1.0 --gst-info >/dev/null 2>&1 || true
fi
echo "gst-aml-dma-allocator 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 gst-aml-dma-allocator"
fi
exit 0
EOF
chmod 755 "${debian_dir}/prerm"
display_alert "GST-AML-DMA-ALLOCATOR" "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"
chmod 755 "${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb"
display_alert "GST-AML-DMA-ALLOCATOR" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success"
else
display_alert "GST-AML-DMA-ALLOCATOR" "Failed to build Debian package" "err"
return 1
fi
return 0
}
function amlbian_build_gst_aml_dma_allocator {
if ! is_unit_enabled "gst-aml-dma-allocator" && ! is_unit_enabled "gst-aml"; then
display_alert "[SKIP] >> gst-aml-dma-allocator disable in BUILD_UNITS" "" "info"
return
fi
display_alert "AMLOGIC: Building gst-aml-dma-allocator ${DEB_VERSION} ${BOARD}" "info"
# Define all directory paths (in .tmp directory)
local build_root="${BUILD_DIR}/.tmp/gst-aml-dma-allocator-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"
# 准备源代码
__gst_aml_dma_allocator_prepare_source "${build_dir}"
# 构建
if ! __gst_aml_dma_allocator_build "${build_dir}" "${install_dir}"; then
display_alert "AMLOGIC: gst-aml-dma-allocator build failed" "err"
return 1
fi
# 安装到安装目录
if ! __gst_aml_dma_allocator_install "${build_dir}" "${install_dir}"; then
display_alert "AMLOGIC: gst-aml-dma-allocator installation failed" "err"
return 1
fi
display_alert "AMLOGIC: Creating Debian package..." "info"
__gst_aml_dma_allocator_create_deb "${build_dir}" "${install_dir}"
__unset_gst_aml_dma_allocator_config
#chmod -R 777 "${build_root}"
rm -rf "${build_root}"
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
display_alert "AMLbian gst-aml-dma-allocator Build Script" "Version: ${DEB_VERSION}, Board: ${BOARD}" "info"
amlbian_build_gst_aml_dma_allocator
display_alert "AMLOGIC: gst-aml-dma-allocator build completed" "${BUILD_DIR}/output" "info"
# ==============================================================================
# End of build-gst-aml-dma-allocator.sh
# ==============================================================================