Files
BPI-CM9-Linux/amlbian/scripts/multimedia/libencoder/build_libamljpegenc.sh
T

387 lines
12 KiB
Bash
Raw Normal View History

2026-08-13 11:28:40 +08:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Libamljpegenc build script for AMLbian
# This script builds libamljpegenc library for Amlogic SoCs
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "$(dirname "$(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="${LIBAMLJPEGENC_VERSION:-1.0.0}"
ARCH="${ARCH:-arm64}"
# ==============================================================================
# Section 4: Libamljpegenc Build Functions
# ==============================================================================
function __unset_libamljpegenc_config {
# Clean up any environment variables set during build
unset EXTRA_OEMAKE
unset SYSROOT
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
#unset CROSS_CC_PREFIX
#unset CC
#unset CXX
#unset AR
}
function __libamljpegenc_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 "LIBAMLJPEGENC" "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/libencoder/jpeg"
if [[ ! -d "${source_dir}" ]]; then
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "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 [[ ! -d "${build_dir}/jpeg_enc" ]]; then
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "jpeg_enc directory not found in source"
2026-08-13 11:28:40 +08:00
exit 1
fi
display_alert "LIBAMLJPEGENC" "Source prepared successfully" "success"
}
function __libamljpegenc_build {
local build_dir="${1}"
local install_dir="${2}"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Starting libamljpegenc 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 "LIBAMLJPEGENC" "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
# 创建输出目录
local out_dir="${build_dir}/output"
mkdir -p "${out_dir}"
# Set environment variables for Makefile
export CROSS_CC_PREFIX="${CROSS_COMPILE}"
export CC="${CROSS_COMPILE}gcc"
export CXX="${CROSS_COMPILE}g++"
export AR="${CROSS_COMPILE}ar"
# Build libjpegenc_api library in jpeg_enc directory
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Building libjpegenc_api.so library..."
2026-08-13 11:28:40 +08:00
# Build library in jpeg_enc directory with OUT_DIR set to output/jpeg_enc
make OUT_DIR="${out_dir}/jpeg_enc" \
CC="${CC}" \
CXX="${CXX}" \
CFLAGS="${CFLAGS} -I. -I./jpeg_enc/include -fPIC" \
LDFLAGS="${LDFLAGS} -shared -lm -lrt" \
-C "${build_dir}/jpeg_enc" \
-j$(nproc) \
2>&1 | tee "${out_dir}/make_lib.log" || {
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "Library build failed"
2026-08-13 11:28:40 +08:00
return 1
}
# Copy library to output directory
mkdir -p "${out_dir}/lib"
local lib_file="${out_dir}/jpeg_enc/libjpegenc_api.so"
if [[ -f "${lib_file}" ]]; then
cp "${lib_file}" "${out_dir}/lib/"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Library built: ${out_dir}/lib/libjpegenc_api.so"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "Library file not found: ${lib_file}"
2026-08-13 11:28:40 +08:00
return 1
fi
# Build jpeg_enc_test in root directory
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Building jpeg_enc_test..."
2026-08-13 11:28:40 +08:00
# Build test program in root directory with OUT_DIR set to output
make OUT_DIR="${out_dir}" \
CC="${CC}" \
CXX="${CXX}" \
CFLAGS="${CFLAGS} -I. -I./jpeg_enc/include" \
LDFLAGS="${LDFLAGS} -lm -lrt -L${out_dir}/lib -ljpegenc_api" \
-C "${build_dir}" \
-j$(nproc) \
2>&1 | tee "${out_dir}/make_test.log" || {
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "Test program build failed"
2026-08-13 11:28:40 +08:00
return 1
}
# Copy test program to output directory
mkdir -p "${out_dir}/bin"
local test_program="${out_dir}/jpeg_enc_test"
if [[ -f "${test_program}" ]]; then
cp "${test_program}" "${out_dir}/bin/"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Test program built: ${out_dir}/bin/jpeg_enc_test"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "LIBAMLJPEGENC" "Test program not found: ${test_program}"
2026-08-13 11:28:40 +08:00
return 1
fi
display_alert "LIBAMLJPEGENC" "Build completed successfully" "success"
return 0
}
function __libamljpegenc_install {
local build_dir="${1}"
local install_dir="${2}"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Installing libamljpegenc 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 "LIBAMLJPEGENC" "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"
# 安装库文件
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Installing library..."
2026-08-13 11:28:40 +08:00
local lib_file="${out_dir}/lib/libjpegenc_api.so"
if [[ -f "${lib_file}" ]]; then
install -m 0644 -D "${lib_file}" "${install_dir}/usr/lib/"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "Installing header..."
2026-08-13 11:28:40 +08:00
local header_file="${build_dir}/jpeg_enc/include/jpegenc_api.h"
if [[ -f "${header_file}" ]]; then
install -m 0644 -D "${header_file}" "${install_dir}/usr/include/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBAMLJPEGENC" "Header installed: jpegenc_api.h"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "LIBAMLJPEGENC" "Header file not found: ${header_file}"
2026-08-13 11:28:40 +08:00
fi
# 安装测试程序
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "Installing test program..."
2026-08-13 11:28:40 +08:00
local test_program="${out_dir}/bin/jpeg_enc_test"
if [[ -f "${test_program}" ]]; then
install -m 0755 "${test_program}" "${install_dir}/usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "LIBAMLJPEGENC" "Binary installed: jpeg_enc_test"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "LIBAMLJPEGENC" "Test program not found: ${test_program}"
2026-08-13 11:28:40 +08:00
fi
display_alert "LIBAMLJPEGENC" "Installation completed successfully" "success"
return 0
}
function __libamljpegenc_create_deb {
local build_dir="${1}"
local install_dir="${2}"
# 设置必要的变量
local pkg_name="libamljpegenc"
local version="${DEB_VERSION}"
local architecture="${ARCH}"
local maintainer="Armbian Build <build@armbian.com>"
local description="Amlogic libamljpegenc library (JPEG encoder hal) for Amlogic platforms"
local description_long="This package provides the Amlogic libamljpegenc library used by JPEG encoding on Amlogic platforms."
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "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 "Libamljpegenc 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 libamljpegenc library"
fi
exit 0
EOF
chmod 755 "${debian_dir}/prerm"
2026-11-11 08:50:43 +08:00
aml_log info "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "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 "LIBAMLJPEGENC" "Failed to build Debian package"
2026-08-13 11:28:40 +08:00
return 1
fi
return 0
}
function amlbian_build_libamljpegenc {
if ! is_unit_enabled "libamljpegenc" && ! is_unit_enabled "libencoder"; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> libamljpegenc disable in BUILD_UNITS"
2026-08-13 11:28:40 +08:00
return
fi
if ! is_feature_contains "$MEDIA_FEATURES" "libamljpegenc"; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> libamljpegenc disable in MEDIA_FEATURES"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building libamljpegenc ${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}/libamljpegenc-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
# 准备源代码
__libamljpegenc_prepare_source "${build_dir}"
# 构建
if ! __libamljpegenc_build "${build_dir}" "${install_dir}"; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Libamljpegenc build failed"
2026-08-13 11:28:40 +08:00
return 1
fi
# 安装到安装目录
if ! __libamljpegenc_install "${build_dir}" "${install_dir}"; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Libamljpegenc 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
__libamljpegenc_create_deb "${build_dir}" "${install_dir}"
__unset_libamljpegenc_config
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLIbian Libamljpegenc Build Script" "Version: ${DEB_VERSION}, Board: ${BOARD}"
2026-08-13 11:28:40 +08:00
amlbian_build_libamljpegenc
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Libamljpegenc build completed" "${BUILD_DIR}/output"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build_libamljpegenc.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================