#!/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" MODULE_BUILD_DIR="${SRC}/output/.tmp" # ============================================================================== # 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 aml_log error "DMA_ALLOCATOR" "aml-comp not found: ${aml_comp_path}" exit 1 fi local source_dir="${aml_comp_path}/multimedia/gst-aml-utils/dma-allocator" if [[ ! -d "${source_dir}" ]]; then aml_log error "DMA_ALLOCATOR" "Source directory not found: ${source_dir}" exit 1 fi aml_log info "DMA_ALLOCATOR" "Copying source from ${source_dir} to ${build_dir}" # 清空构建目录 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 aml_log error "DMA_ALLOCATOR" "Makefile or dma_allocator.h not found in source" 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="${MODULE_BUILD_DIR}/liblog-build-${ARCH:-arm64}" if [ -z "${liblog_dir}" ] || [ ! -d "${liblog_dir}" ]; then aml_log debug "DMA_ALLOCATOR" "liblog not found in ${MODULE_BUILD_DIR} build." aml_log debug "AMLOGIC" "TODO>> Start build liblog " if ! ( source "${SCRIPT_DIR}/build-liblog.sh" ); then aml_log error "ERROR" "build-liblog failed" return 1 fi if [ -z "${liblog_dir}" ] || [ ! -d "${liblog_dir}" ]; then aml_log error "ERROR" "liblog build finished but directory still not found" return 1 fi fi aml_log info "DMA_ALLOCATOR" "liblog dir: ${liblog_dir}" # 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}" aml_log info "DMA_ALLOCATOR" "Starting dma-allocator build..." if [[ ! -d "${build_dir}" ]]; then aml_log error "DMA_ALLOCATOR" "Build directory not found: ${build_dir}" 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" aml_log info "DMA_ALLOCATOR" "Building using Makefile..." aml_log debug "DMA_ALLOCATOR" "CC=${CC}" # 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}" aml_log debug "DMA_ALLOCATOR" "Added uapi headers path: ${uapi_headers_path}" fi if [[ -d "${kernel_headers_path}" ]]; then current_cflags="${current_cflags} -I${kernel_headers_path}" aml_log debug "DMA_ALLOCATOR" "Added kernel headers path: ${kernel_headers_path}" fi make OUT_DIR="${out_dir}" \ CC="${CC}" \ CFLAGS="${current_cflags} -fPIC -Wall" \ LDFLAGS="${LDFLAGS}" \ -j$(nproc) \ 2>&1 | tee "${out_dir}/make.log" || { aml_log error "DMA_ALLOCATOR" "Build failed" return 1 } # Verify the library was created if [[ -f "${out_dir}/libdma-allocator.so" ]]; then display_alert "DMA_ALLOCATOR" "Build completed successfully" "success" aml_log info "DMA_ALLOCATOR" "Library: ${out_dir}/libdma-allocator.so" else aml_log error "DMA_ALLOCATOR" "Library file not created" return 1 fi return 0 } function __dma_allocator_install { local build_dir="${1}" local install_dir="${2}" aml_log info "DMA_ALLOCATOR" "Installing dma-allocator..." local out_dir="${build_dir}/output" if [[ ! -d "${out_dir}" ]]; then aml_log error "DMA_ALLOCATOR" "Output directory not found: ${out_dir}" return 1 fi # Create installation directories mkdir -p "${install_dir}/usr/lib" mkdir -p "${install_dir}/usr/include" # Install library aml_log info "DMA_ALLOCATOR" "Installing library..." local lib_file="${out_dir}/libdma-allocator.so" if [[ -f "${lib_file}" ]]; then install -m 0644 "${lib_file}" "${install_dir}/usr/lib/" aml_log debug "DMA_ALLOCATOR" "Installed libdma-allocator.so to ${install_dir}/usr/lib/" else aml_log error "DMA_ALLOCATOR" "Library file not found: ${lib_file}" return 1 fi # Install header aml_log info "DMA_ALLOCATOR" "Installing header..." local header_file="${build_dir}/inc/dma_allocator.h" if [[ -f "${header_file}" ]]; then install -m 0644 -D "${header_file}" "${install_dir}/usr/include/" aml_log debug "DMA_ALLOCATOR" "Installed dma_allocator.h to ${install_dir}/usr/include/" else aml_log error "DMA_ALLOCATOR" "Header file not found: ${header_file}" return 1 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." aml_log info "DMA_ALLOCATOR" "Creating Debian package for ${pkg_name} ${version}..." # 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 aml_log info "DMA_ALLOCATOR" "Copied installed files to ${deb_dir}" else aml_log error "DMA_ALLOCATOR" "No installed files found in ${install_dir}" return 1 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" aml_log info "DMA_ALLOCATOR" "Building Debian package..." # 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 aml_log error "DMA_ALLOCATOR" "Failed to build Debian package" return 1 fi return 0 } function amlbian_build_dma_allocator { if ! is_unit_enabled "dma-allocator"; then aml_log info "[SKIP] >> dma-allocator disable in BUILD_UNITS" return fi aml_log info "AMLOGIC: Building dma-allocator ${DMA_ALLOCATOR_VERSION} ${BOARD}" # Define all directory paths (in .tmp directory) local build_root="${MODULE_BUILD_DIR}/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}" aml_log debug "AMLOGIC: build_dir ${build_dir}" aml_log debug "AMLOGIC: install_dir ${install_dir}" aml_log debug "AMLOGIC: output_dir ${BUILD_DIR}/output" # 准备源代码 __dma_allocator_prepare_source "${build_dir}" # 构建 if ! __dma_allocator_build "${build_dir}"; then aml_log error "AMLOGIC: dma-allocator build failed" return 1 fi # 安装到安装目录 if ! __dma_allocator_install "${build_dir}" "${install_dir}"; then aml_log error "AMLOGIC: dma-allocator installation failed" return 1 fi aml_log info "AMLOGIC: Creating Debian package..." __dma_allocator_create_deb "${build_dir}" "${install_dir}" __unset_dma_allocator_config return 0 } # ============================================================================== # Section 5: Main # ============================================================================== aml_log info "AMLIbian DMA-allocator Build Script" "Version: ${DMA_ALLOCATOR_VERSION}, Board: ${BOARD}" amlbian_build_dma_allocator aml_log info "AMLOGIC: dma-allocator build completed" "${BUILD_DIR}/output" # ============================================================================== # End of build-dma-allocator.sh # ==============================================================================