375 lines
12 KiB
Bash
375 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}"
|
|
|
|
# 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}"
|
|
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
|
|
display_alert "SCREENCAPTURE" "aml-comp not found: ${aml_comp_path}" "err"
|
|
exit 1
|
|
fi
|
|
|
|
local source_dir="${aml_comp_path}/vendor/amlogic/linux_sample_app/aml-screencapture"
|
|
|
|
if [[ ! -d "${source_dir}" ]]; then
|
|
display_alert "SCREENCAPTURE" "Source directory not found: ${source_dir}" "err"
|
|
exit 1
|
|
fi
|
|
|
|
display_alert "SCREENCAPTURE" "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}/ScreenCapture.cpp" ]]; then
|
|
display_alert "SCREENCAPTURE" "Makefile or ScreenCapture.cpp not found in source" "err"
|
|
exit 1
|
|
fi
|
|
|
|
display_alert "SCREENCAPTURE" "Source prepared successfully" "success"
|
|
}
|
|
|
|
function __screencapture_build {
|
|
local build_dir="${1}"
|
|
|
|
display_alert "SCREENCAPTURE" "Starting screencapture build..." "info"
|
|
|
|
if [[ ! -d "${build_dir}" ]]; then
|
|
display_alert "SCREENCAPTURE" "Build directory not found: ${build_dir}" "err"
|
|
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}"
|
|
display_alert "SCREENCAPTURE" "Using ROOTFS_TEMP as sysroot: ${SYSROOT}" "info"
|
|
else
|
|
display_alert "SCREENCAPTURE" "ROOTFS_TEMP not found or invalid: ${ROOTFS_TEMP}" "err"
|
|
display_alert "SCREENCAPTURE" "ROOTFS_TEMP should be created by create_base_rootfs function" "err"
|
|
return 1
|
|
fi
|
|
|
|
# Use sysroot configuration
|
|
display_alert "SCREENCAPTURE" "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}"
|
|
|
|
# 根据 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"
|
|
|
|
display_alert "SCREENCAPTURE" "Building using Makefile..." "info"
|
|
display_alert "SCREENCAPTURE" "CC=${CC}" "debug"
|
|
|
|
# 运行 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" || {
|
|
display_alert "SCREENCAPTURE" "Build failed" "err"
|
|
return 1
|
|
}
|
|
|
|
# Verify the executable was created
|
|
if [[ -f "${out_dir}/screencapture" ]]; then
|
|
display_alert "SCREENCAPTURE" "Build completed successfully" "success"
|
|
display_alert "SCREENCAPTURE" "Executable: ${out_dir}/screencapture" "info"
|
|
else
|
|
display_alert "SCREENCAPTURE" "Executable file not created" "err"
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
function __screencapture_install {
|
|
local build_dir="${1}"
|
|
local install_dir="${2}"
|
|
|
|
display_alert "SCREENCAPTURE" "Installing screencapture..." "info"
|
|
|
|
local out_dir="${build_dir}/output"
|
|
|
|
if [[ ! -d "${out_dir}" ]]; then
|
|
display_alert "SCREENCAPTURE" "Output directory not found: ${out_dir}" "err"
|
|
return 1
|
|
fi
|
|
|
|
# Create installation directories
|
|
# 根据 bb 文件中的 do_install(),只安装到 bindir
|
|
mkdir -p "${install_dir}/usr/bin"
|
|
|
|
# Install executable
|
|
display_alert "SCREENCAPTURE" "Installing executable..." "info"
|
|
|
|
local exec_file="${out_dir}/screencapture"
|
|
if [[ -f "${exec_file}" ]]; then
|
|
install -m 0755 "${exec_file}" "${install_dir}/usr/bin/"
|
|
display_alert "SCREENCAPTURE" "Installed screencapture to ${install_dir}/usr/bin/" "debug"
|
|
else
|
|
display_alert "SCREENCAPTURE" "Executable file not found: ${exec_file}" "warn"
|
|
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 <[email protected]>"
|
|
local description="Amlogic screencapture utility"
|
|
local description_long="This package provides the Amlogic screencapture utility for capturing screen content."
|
|
|
|
display_alert "SCREENCAPTURE" "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 "SCREENCAPTURE" "Copied installed files to ${deb_dir}" "info"
|
|
else
|
|
display_alert "SCREENCAPTURE" "No installed files found in ${install_dir}" "warning"
|
|
return 0
|
|
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"
|
|
|
|
display_alert "SCREENCAPTURE" "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}"
|
|
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
|
|
display_alert "SCREENCAPTURE" "File created successfully (non-empty)" "debug"
|
|
else
|
|
display_alert "SCREENCAPTURE" "WARNING: File is empty" "warn"
|
|
fi
|
|
else
|
|
display_alert "SCREENCAPTURE" "ERROR: Debian package not found after move: ${output_dir}/${final_deb_name}" "err"
|
|
return 1
|
|
fi
|
|
else
|
|
display_alert "SCREENCAPTURE" "Failed to build Debian package" "err"
|
|
return 1
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
function amlbian_build_screencapture {
|
|
if ! is_unit_enabled "screencapture"; then
|
|
display_alert "[SKIP] >> screencapture disable in BUILD_UNITS" "" "info"
|
|
return
|
|
fi
|
|
display_alert "AMLOGIC: Building screencapture ${SCREENCAPTURE_VERSION} ${BOARD}" "info"
|
|
|
|
# Define all directory paths (in .tmp directory)
|
|
local build_root="${BUILD_DIR}/.tmp/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}"
|
|
|
|
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"
|
|
|
|
# 准备源代码
|
|
__screencapture_prepare_source "${build_dir}"
|
|
|
|
# 构建
|
|
if ! __screencapture_build "${build_dir}"; then
|
|
display_alert "AMLOGIC: screencapture build failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
# 安装到安装目录
|
|
if ! __screencapture_install "${build_dir}" "${install_dir}"; then
|
|
display_alert "AMLOGIC: screencapture installation failed" "err"
|
|
return 1
|
|
fi
|
|
|
|
display_alert "AMLOGIC: Creating Debian package..." "info"
|
|
__screencapture_create_deb "${build_dir}" "${install_dir}"
|
|
|
|
__unset_screencapture_config
|
|
|
|
chmod 777 "${build_root}"
|
|
|
|
return 0
|
|
}
|
|
|
|
# ==============================================================================
|
|
# Section 5: Main
|
|
# ==============================================================================
|
|
|
|
display_alert "AMLIbian Screencapture Build Script" "Version: ${SCREENCAPTURE_VERSION}, Board: ${BOARD}" "info"
|
|
|
|
amlbian_build_screencapture
|
|
|
|
display_alert "AMLOGIC: screencapture build completed" "${BUILD_DIR}/output" "info"
|
|
|
|
# ==============================================================================
|
|
# End of build-screencapture.sh
|
|
# ============================================================================== |