Files
BPI-CM9-Linux/amlbian/scripts/multimedia/build-screencapture.sh
T

373 lines
12 KiB
Bash

#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Screencapture build script for AMLbian
# This script builds screencapture utility for Amlogic SoCs
# Based on aml-screencapture.bb functionality
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")"
BUILD_DIR="${SRC}"
MODULE_BUILD_DIR="${SRC}/output/.tmp"
# ==============================================================================
# Section 2: Default Configuration
# ==============================================================================
BOARD="${BOARD:-s6-bq201}"
SCREENCAPTURE_VERSION="${SCREENCAPTURE_VERSION:-1.0.0}"
: "${ARCH:=arm64}"
KERNEL_VERSION="${KERNEL_VERSION:-6.12}"
# ==============================================================================
# Section 4: Screencapture Build Functions
# ==============================================================================
function __unset_screencapture_config {
# Clean up any environment variables set during build
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
unset CPPFLAGS
unset SYSROOT
unset PKG_CONFIG
unset PKG_CONFIG_SYSROOT_DIR
unset PKG_CONFIG_LIBDIR
unset PKG_CONFIG_PATH
}
function __screencapture_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 "SCREENCAPTURE" "aml-comp not found: ${aml_comp_path}"
exit 1
fi
local source_dir="${aml_comp_path}/vendor/amlogic/linux_sample_app/aml-screencapture"
if [[ ! -d "${source_dir}" ]]; then
aml_log error "SCREENCAPTURE" "Source directory not found: ${source_dir}"
exit 1
fi
aml_log info "SCREENCAPTURE" "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}/ScreenCapture.cpp" ]]; then
aml_log error "SCREENCAPTURE" "Makefile or ScreenCapture.cpp not found in source"
exit 1
fi
display_alert "SCREENCAPTURE" "Source prepared successfully" "success"
}
function __screencapture_build {
local build_dir="${1}"
aml_log info "SCREENCAPTURE" "Starting screencapture build..."
if [[ ! -d "${build_dir}" ]]; then
aml_log error "SCREENCAPTURE" "Build directory not found: ${build_dir}"
return 1
fi
# Set build environment
: "${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}"
aml_log info "SCREENCAPTURE" "Using ROOTFS_TEMP as sysroot: ${SYSROOT}"
else
aml_log error "SCREENCAPTURE" "ROOTFS_TEMP not found or invalid: ${ROOTFS_TEMP}"
aml_log error "SCREENCAPTURE" "ROOTFS_TEMP should be created by create_base_rootfs function"
return 1
fi
# Use sysroot configuration
aml_log info "SCREENCAPTURE" "Using sysroot: ${SYSROOT}"
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}"
# 根据 bb 文件中的 EXTRA_OEMAKE,添加额外的编译标志
local extra_cflags="-Wno-unused-result -Wno-unused-variable -Wno-unused-function -Os"
export CFLAGS="${CFLAGS} ${extra_cflags}"
export CXXFLAGS="${CXXFLAGS} ${extra_cflags}"
# 链接标志
export LDFLAGS="${LDFLAGS} -lstdc++ -lm -lpthread"
# 添加 sysroot 中的头文件路径
include_flags="-I${SYSROOT}/usr/include/aarch64-linux-gnu"
export CPPFLAGS="${CPPFLAGS} ${include_flags}"
export CFLAGS="${CFLAGS} ${include_flags}"
export CXXFLAGS="${CXXFLAGS} ${include_flags}"
export LDFLAGS="${LDFLAGS} -L${SYSROOT}/usr/lib/aarch64-linux-gnu -L${SYSROOT}/usr/aarch64-linux-gnu/lib"
export LDFLAGS="${LDFLAGS} -Wl,-rpath-link,${SYSROOT}/usr/lib/aarch64-linux-gnu"
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 "SCREENCAPTURE" "Building using Makefile..."
aml_log debug "SCREENCAPTURE" "CC=${CC}"
# 运行 make,根据源码中的 Makefile
make OUT_DIR="${out_dir}" \
CC="${CC}" \
CXX="${CXX}" \
CXXFLAGS="${CXXFLAGS} -std=c++14 -Wall -Wextra -fPIC -Os -D_FILE_OFFSET_BITS=64" \
LDFLAGS="${LDFLAGS}" \
-j$(nproc) \
2>&1 | tee "${out_dir}/make.log" || {
aml_log error "SCREENCAPTURE" "Build failed"
return 1
}
# Verify the executable was created
if [[ -f "${out_dir}/screencapture" ]]; then
display_alert "SCREENCAPTURE" "Build completed successfully" "success"
aml_log info "SCREENCAPTURE" "Executable: ${out_dir}/screencapture"
else
aml_log error "SCREENCAPTURE" "Executable file not created"
return 1
fi
return 0
}
function __screencapture_install {
local build_dir="${1}"
local install_dir="${2}"
aml_log info "SCREENCAPTURE" "Installing screencapture..."
local out_dir="${build_dir}/output"
if [[ ! -d "${out_dir}" ]]; then
aml_log error "SCREENCAPTURE" "Output directory not found: ${out_dir}"
return 1
fi
# Create installation directories
# 根据 bb 文件中的 do_install(),只安装到 bindir
mkdir -p "${install_dir}/usr/bin"
# Install executable
aml_log info "SCREENCAPTURE" "Installing executable..."
local exec_file="${out_dir}/screencapture"
if [[ -f "${exec_file}" ]]; then
install -m 0755 "${exec_file}" "${install_dir}/usr/bin/"
aml_log debug "SCREENCAPTURE" "Installed screencapture to ${install_dir}/usr/bin/"
else
aml_log warn "SCREENCAPTURE" "Executable file not found: ${exec_file}"
fi
display_alert "SCREENCAPTURE" "Installation completed successfully" "success"
return 0
}
function __screencapture_create_deb {
local build_dir="${1}"
local install_dir="${2}"
# Package details
local pkg_name="screencapture"
local version="${SCREENCAPTURE_VERSION}"
local architecture="${ARCH:-arm64}"
local maintainer="Amlbian Builder <build@amlbian.com>"
local description="Amlogic screencapture utility"
local description_long="This package provides the Amlogic screencapture utility for capturing screen content."
aml_log info "SCREENCAPTURE" "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 "SCREENCAPTURE" "Copied installed files to ${deb_dir}"
else
aml_log error "SCREENCAPTURE" "No installed files found in ${install_dir}"
return 1
fi
# Create control file
cat > "${debian_dir}/control" <<EOF
Package: ${pkg_name}
Version: ${version}
Architecture: ${architecture}
Maintainer: ${maintainer}
Description: ${description}
${description_long}
Depends: libc6, libstdc++6
Section: utils
Priority: optional
EOF
# Create postinst script (not needed for simple executable)
cat > "${debian_dir}/postinst" << 'POSTINST_EOF'
#!/bin/sh
set -e
# No special post-installation steps needed
exit 0
POSTINST_EOF
chmod 755 "${debian_dir}/postinst"
# Create prerm script
cat > "${debian_dir}/prerm" << 'PRERM_EOF'
#!/bin/sh
set -e
# No special pre-removal steps needed
exit 0
PRERM_EOF
chmod 755 "${debian_dir}/prerm"
aml_log info "SCREENCAPTURE" "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}"
local final_deb_name="linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb"
mv "${deb_file}" "${output_dir}/${final_deb_name}"
chmod 755 "${output_dir}/${final_deb_name}"
# 验证文件确实存在
if [[ -f "${output_dir}/${final_deb_name}" ]]; then
display_alert "SCREENCAPTURE" "Debian package created: ${output_dir}/${final_deb_name}" "success"
# 简单的文件大小检查,避免复杂的命令
if [[ -s "${output_dir}/${final_deb_name}" ]]; then
aml_log debug "SCREENCAPTURE" "File created successfully (non-empty)"
else
aml_log warn "SCREENCAPTURE" "WARNING: File is empty"
fi
else
aml_log error "SCREENCAPTURE" "ERROR: Debian package not found after move: ${output_dir}/${final_deb_name}"
return 1
fi
else
aml_log error "SCREENCAPTURE" "Failed to build Debian package"
return 1
fi
return 0
}
function amlbian_build_screencapture {
if ! is_unit_enabled "screencapture"; then
aml_log info "[SKIP] >> screencapture disable in BUILD_UNITS"
return
fi
aml_log info "AMLOGIC: Building screencapture ${SCREENCAPTURE_VERSION} ${BOARD}"
# Define all directory paths (in .tmp directory)
local build_root="${MODULE_BUILD_DIR}/screencapture-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 info "AMLOGIC: build_dir ${build_dir}"
aml_log info "AMLOGIC: install_dir ${install_dir}"
aml_log info "AMLOGIC: output_dir ${BUILD_DIR}/output"
# 准备源代码
__screencapture_prepare_source "${build_dir}"
# 构建
if ! __screencapture_build "${build_dir}"; then
aml_log error "AMLOGIC: screencapture build failed"
return 1
fi
# 安装到安装目录
if ! __screencapture_install "${build_dir}" "${install_dir}"; then
aml_log error "AMLOGIC: screencapture installation failed"
return 1
fi
aml_log info "AMLOGIC: Creating Debian package..."
__screencapture_create_deb "${build_dir}" "${install_dir}"
__unset_screencapture_config
chmod 777 "${build_root}"
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
aml_log info "AMLIbian Screencapture Build Script" "Version: ${SCREENCAPTURE_VERSION}, Board: ${BOARD}"
amlbian_build_screencapture
aml_log info "AMLOGIC: screencapture build completed" "${BUILD_DIR}/output"
# ==============================================================================
# End of build-screencapture.sh
# ==============================================================================