#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2026 AMLOGIC # # DMA-allocator build script for AMLbian # This script builds dma-allocator library for Amlogic SoCs # Based on compile-dma-allocator.sh functionality set -e # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AMLBIAN_DIR="$(dirname "$(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}" DMA_ALLOCATOR_VERSION="${DMA_ALLOCATOR_VERSION:-1.0.0}" DMA_ALLOCATOR_SO_VERSION="${DMA_ALLOCATOR_SO_VERSION:-1}" ARCH="${ARCH:-arm64}" KERNEL_VERSION="${KERNEL_VERSION:-6.12}" # ============================================================================== # Section 4: DMA-allocator Build Functions # ============================================================================== function __unset_dma_allocator_config { # Clean up any environment variables set during build unset CFLAGS unset CXXFLAGS unset LDFLAGS } function __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 "DMA_ALLOCATOR" "aml-comp not found: ${aml_comp_path}" "err" exit 1 fi local source_dir="${aml_comp_path}/multimedia/gst-aml-utils/dma-allocator" if [[ ! -d "${source_dir}" ]]; then display_alert "DMA_ALLOCATOR" "Source directory not found: ${source_dir}" "err" exit 1 fi display_alert "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}/Makefile" ]] && [[ ! -f "${build_dir}/inc/dma_allocator.h" ]]; then display_alert "DMA_ALLOCATOR" "Makefile or dma_allocator.h not found in source" "err" exit 1 fi display_alert "DMA_ALLOCATOR" "Source prepared successfully" "success" } function __dma_allocator_build_depend { local build_dir="${1}" local dependencies=("liblog") # liblog local liblog_dir="${BUILD_DIR}/.tmp/liblog-build-${ARCH:-arm64}" if [ -z "${liblog_dir}" ] || [ ! -d "${liblog_dir}" ]; then display_alert "DMA_ALLOCATOR" "liblog not found in ${BUILD_DIR}/.tmp build." "info" display_alert "AMLOGIC" "TODO>> Start build liblog " "info" if ! ( source "${SCRIPT_DIR}/build-liblog.sh" ); then display_alert "ERROR" "build-liblog failed" "err" return 1 fi if [ -z "${liblog_dir}" ] || [ ! -d "${liblog_dir}" ]; then display_alert "ERROR" "liblog build finished but directory still not found" "err" return 1 fi fi display_alert "DMA_ALLOCATOR" "liblog dir: ${liblog_dir}" "info" # copy local log_head_flags="-I${liblog_dir}/build/include" export CPPFLAGS="${CPPFLAGS} ${log_head_flags}" export CFLAGS="${CFLAGS} ${log_head_flags}" export CXXFLAGS="${CXXFLAGS} ${log_head_flags}" export LDFLAGS="${LDFLAGS} -L${liblog_dir}/build -L${liblog_dir}/build/output -llog" } function __dma_allocator_build { local build_dir="${1}" display_alert "DMA_ALLOCATOR" "Starting dma-allocator build..." "info" if [[ ! -d "${build_dir}" ]]; then display_alert "DMA_ALLOCATOR" "Build directory not found: ${build_dir}" "err" return 1 fi # Set build environment ARCH="${ARCH:-arm64}" CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}" export ARCH="${ARCH}" export CROSS_COMPILE="${CROSS_COMPILE}" # amlbian_checkout_toolchain __dma_allocator_build_depend cd "${build_dir}" # Create output directory local out_dir="${build_dir}/output" mkdir -p "${out_dir}" # Set compiler export CC="${CROSS_COMPILE}gcc" export CXX="${CROSS_COMPILE}g++" export AR="${CROSS_COMPILE}ar" display_alert "DMA_ALLOCATOR" "Building using Makefile..." "info" display_alert "DMA_ALLOCATOR" "CC=${CC}" "debug" # Run make with OUT_DIR pointing to our output directory # Use CFLAGS from environment (set by __dma_allocator_build_depend) # Add include path for dma_allocator.h if not already present local current_cflags="${CFLAGS}" if [[ ! "${current_cflags}" =~ "-I${build_dir}/inc" ]]; then current_cflags="${current_cflags} -I${build_dir}/inc" fi # Add kernel headers path for dma-heap.h local aml_comp_path="$(amlbian_get_aml_comp_dir)" local kernel_headers_path="${aml_comp_path}/kernel/aml-${KERNEL_VERSION}/include" local uapi_headers_path="${aml_comp_path}/kernel/aml-${KERNEL_VERSION}/include/uapi" if [[ -d "${uapi_headers_path}" ]]; then current_cflags="${current_cflags} -I${uapi_headers_path}" display_alert "DMA_ALLOCATOR" "Added uapi headers path: ${uapi_headers_path}" "debug" fi if [[ -d "${kernel_headers_path}" ]]; then current_cflags="${current_cflags} -I${kernel_headers_path}" display_alert "DMA_ALLOCATOR" "Added kernel headers path: ${kernel_headers_path}" "debug" fi make OUT_DIR="${out_dir}" \ CC="${CC}" \ CFLAGS="${current_cflags} -fPIC -Wall" \ LDFLAGS="${LDFLAGS}" \ -j$(nproc) \ 2>&1 | tee "${out_dir}/make.log" || { display_alert "DMA_ALLOCATOR" "Build failed" "err" return 1 } # Verify the library was created if [[ -f "${out_dir}/libdma-allocator.so" ]]; then display_alert "DMA_ALLOCATOR" "Build completed successfully" "success" display_alert "DMA_ALLOCATOR" "Library: ${out_dir}/libdma-allocator.so" "info" else display_alert "DMA_ALLOCATOR" "Library file not created" "err" return 1 fi return 0 } function __dma_allocator_install { local build_dir="${1}" local install_dir="${2}" display_alert "DMA_ALLOCATOR" "Installing dma-allocator..." "info" local out_dir="${build_dir}/output" if [[ ! -d "${out_dir}" ]]; then display_alert "DMA_ALLOCATOR" "Output directory not found: ${out_dir}" "err" return 1 fi # Create installation directories mkdir -p "${install_dir}/usr/lib" mkdir -p "${install_dir}/usr/include" # Install library display_alert "DMA_ALLOCATOR" "Installing library..." "info" local lib_file="${out_dir}/libdma-allocator.so" if [[ -f "${lib_file}" ]]; then install -m 0644 "${lib_file}" "${install_dir}/usr/lib/" display_alert "DMA_ALLOCATOR" "Installed libdma-allocator.so to ${install_dir}/usr/lib/" "debug" else display_alert "DMA_ALLOCATOR" "Library file not found: ${lib_file}" "warn" fi # Install header display_alert "DMA_ALLOCATOR" "Installing header..." "info" local header_file="${build_dir}/inc/dma_allocator.h" if [[ -f "${header_file}" ]]; then install -m 0644 -D "${header_file}" "${install_dir}/usr/include/" display_alert "DMA_ALLOCATOR" "Installed dma_allocator.h to ${install_dir}/usr/include/" "debug" else display_alert "DMA_ALLOCATOR" "Header file not found: ${header_file}" "warn" fi display_alert "DMA_ALLOCATOR" "Installation completed successfully" "success" return 0 } function __dma_allocator_create_deb { local build_dir="${1}" local install_dir="${2}" # Package details local pkg_name="libdma-allocator" local version="${DMA_ALLOCATOR_VERSION}" local architecture="${ARCH:-arm64}" local maintainer="Amlbian Builder " local description="Amlogic DMA buffer allocator library" local description_long="This package provides the Amlogic DMA buffer allocator library for multimedia applications." display_alert "DMA_ALLOCATOR" "Creating Debian package for ${pkg_name} ${version}..." "info" # Create Debian package directory structure local deb_dir="${build_dir}/deb/${pkg_name}-${version}" local debian_dir="${deb_dir}/DEBIAN" mkdir -p "${debian_dir}" # Copy installed files to package directory if [[ -d "${install_dir}" ]] && [[ "$(ls -A "${install_dir}")" ]]; then cp -r "${install_dir}"/* "${deb_dir}/" 2>/dev/null || true display_alert "DMA_ALLOCATOR" "Copied installed files to ${deb_dir}" "info" else display_alert "DMA_ALLOCATOR" "No installed files found in ${install_dir}" "warning" return 0 fi # Create control file cat > "${debian_dir}/control" < "${debian_dir}/postinst" << 'POSTINST_EOF' #!/bin/sh set -e if [ "$1" = "configure" ]; then # Update ldconfig cache if command -v ldconfig >/dev/null 2>&1; then ldconfig fi fi exit 0 POSTINST_EOF chmod 755 "${debian_dir}/postinst" # Create prerm script cat > "${debian_dir}/prerm" << '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 fi exit 0 PRERM_EOF chmod 755 "${debian_dir}/prerm" display_alert "DMA_ALLOCATOR" "Building Debian package..." "info" # Build Debian package 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 "DMA_ALLOCATOR" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success" else display_alert "DMA_ALLOCATOR" "Failed to build Debian package" "err" return 1 fi return 0 } function amlbian_build_dma_allocator { if ! is_unit_enabled "dma-allocator"; then display_alert "[SKIP] >> dma-allocator disable in BUILD_UNITS" "" "info" return fi display_alert "AMLOGIC: Building dma-allocator ${DMA_ALLOCATOR_VERSION} ${BOARD}" "info" # Define all directory paths (in .tmp directory) local build_root="${BUILD_DIR}/.tmp/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" # 准备源代码 __dma_allocator_prepare_source "${build_dir}" # 构建 if ! __dma_allocator_build "${build_dir}"; then display_alert "AMLOGIC: dma-allocator build failed" "err" return 1 fi # 安装到安装目录 if ! __dma_allocator_install "${build_dir}" "${install_dir}"; then display_alert "AMLOGIC: dma-allocator installation failed" "err" return 1 fi display_alert "AMLOGIC: Creating Debian package..." "info" __dma_allocator_create_deb "${build_dir}" "${install_dir}" __unset_dma_allocator_config return 0 } # ============================================================================== # Section 5: Main # ============================================================================== display_alert "AMLIbian DMA-allocator Build Script" "Version: ${DMA_ALLOCATOR_VERSION}, Board: ${BOARD}" "info" amlbian_build_dma_allocator display_alert "AMLOGIC: dma-allocator build completed" "${BUILD_DIR}/output" "info" # ============================================================================== # End of build-dma-allocator.sh # ==============================================================================