Files
BPI-CM9-Linux/amlbian/scripts/build-libion.sh
T
2026-08-24 10:03:24 +08:00

349 lines
12 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Libion build script for AMLbian
# This script builds libion library for Amlogic SoCs
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(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}"
DEB_VERSION="1.0"
ARCH="${ARCH:-arm64}"
# ==============================================================================
# Section 4: Libion Build Functions
# ==============================================================================
function amlbian_checkout_toolchain() {
display_alert "Ignore check toolchain after set_external_toolchain in common setting"
return 0
# # Check default gcc version before switching toolchain
# local need_new_toolchain=1
# local default_gcc_version=""
# # Try to get default gcc version
# if command -v gcc &> /dev/null; then
# default_gcc_version=$(aarch64-linux-gnu-gcc --version | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -n1)
# display_alert "TOOLCHAIN" "Default gcc version: ${default_gcc_version}" "info"
# # Check if version is >= 5.1.0
# if [[ -n "${default_gcc_version}" ]]; then
# # Simple version comparison
# local major_version=$(echo "${default_gcc_version}" | cut -d. -f1)
# local minor_version=$(echo "${default_gcc_version}" | cut -d. -f2)
# if [[ ${major_version} -gt 5 ]] || ([[ ${major_version} -eq 5 ]] && [[ ${minor_version} -ge 1 ]]); then
# display_alert "TOOLCHAIN" "Default gcc version ${default_gcc_version} >= 5.1.0, no need for new toolchain" "info"
# need_new_toolchain=0
# else
# display_alert "TOOLCHAIN" "Default gcc version ${default_gcc_version} < 5.1.0, will try new toolchain" "info"
# fi
# fi
# else
# display_alert "TOOLCHAIN" "gcc not found in PATH" "warn"
# fi
# # Only switch to new toolchain if needed
# if [[ ${need_new_toolchain} -eq 1 ]]; then
# #local new_toolchain_path="/opt/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin"
# local new_toolchain_path="/opt/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin"
# if [[ -d "${new_toolchain_path}" ]]; then
# display_alert "TOOLCHAIN" "Using new toolchain from: ${new_toolchain_path}" "info"
# export PATH="${new_toolchain_path}:${PATH}"
# # Check if gcc exists in the new toolchain
# if [[ -f "${new_toolchain_path}/aarch64-linux-gnu-gcc" ]]; then
# display_alert "TOOLCHAIN" "Found cross-compiler: ${new_toolchain_path}/aarch64-linux-gnu-gcc" "info"
# else
# display_alert "TOOLCHAIN" "Cross-compiler not found in new toolchain, using default" "warn"
# fi
# else
# display_alert "TOOLCHAIN" "New toolchain directory not found: ${new_toolchain_path}" "warn"
# fi
# fi
}
function __unset_libion_config {
# Clean up any environment variables set during build
unset EXTRA_OEMAKE
unset SYSROOT
}
function __libion_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 "LIBION" "aml-comp not found: ${aml_comp_path}" "err"
exit 1
fi
local source_dir="${aml_comp_path}/hardware/amlogic/libion"
if [[ ! -d "${source_dir}" ]]; then
display_alert "LIBION" "Source directory not found: ${source_dir}" "err"
exit 1
fi
display_alert "LIBION" "Copying source from ${source_dir} to ${build_dir}" "info"
# 清空构建目录
rm -rf "${build_dir}"
mkdir -p "${build_dir}"
# 复制源代码到构建目录
cp -r "${source_dir}"/* "${build_dir}/"
display_alert "LIBION" "Source prepared successfully" "success"
}
function __libion_build {
local build_dir="${1}"
local install_dir="${2}"
display_alert "LIBION" "Starting libion build..." "info"
# 检查源目录
if [[ ! -d "${build_dir}" ]]; then
display_alert "LIBION" "Build directory not found: ${build_dir}" "err"
return 1
fi
# 设置编译环境
export ARCH=arm64
export CROSS_COMPILE=${CROSS_COMPILE:-aarch64-linux-gnu-}
amlbian_checkout_toolchain
# 设置EXTRA_OEMAKE参数(参考libion.bb)
local EXTRA_OEMAKE="OUT_DIR=${build_dir} LIBION_STAGING_DIR=${install_dir} CROSS=${CROSS_COMPILE} TARGET_DIR=${install_dir} STAGING_DIR=${install_dir} DESTDIR=${install_dir} INSTALL_DIR=${install_dir}/usr/lib"
# 进入源码目录
cd "${build_dir}"
display_alert "LIBION" "Building libion library..." "info"
# 编译命令,使用EXTRA_OEMAKE参数
make -j$(nproc) ${EXTRA_OEMAKE} \
CROSS_COMPILE=${CROSS_COMPILE:-aarch64-linux-gnu-} \
CC=${CC:-${CROSS_COMPILE}gcc} \
ARCH=arm64 2>&1 | tee build.log || {
display_alert "LIBION" "Build failed" "err"
return 1
}
display_alert "LIBION" "Build completed successfully" "success"
return 0
}
function __libion_install {
local build_dir="${1}"
local install_dir="${2}"
display_alert "LIBION" "Installing libion library..." "info"
# 创建安装目录结构
mkdir -p "${install_dir}/usr/include"
mkdir -p "${install_dir}/usr/lib/${LIB_DIR}"
# 安装头文件
if [[ -d "${build_dir}/include" ]]; then
install -m 0644 -D "${build_dir}/include/ion/ion.h" "${install_dir}/usr/include/"
install -m 0644 -D "${build_dir}/include/ion/IONmem.h" "${install_dir}/usr/include/"
display_alert "LIBION" "Headers installed to ${install_dir}/usr/include" "info"
fi
# 安装库文件
if [[ -f "${build_dir}/libion.so" ]]; then
install -m 0644 -D "${build_dir}/libion.so" "${install_dir}/usr/lib/${LIB_DIR}/"
display_alert "LIBION" "Library installed to ${install_dir}/usr/lib/${LIB_DIR}" "info"
else
# 尝试在其他位置查找库文件
local lib_file=$(find "${build_dir}" -name "libion.so" -type f | head -1)
if [[ -f "${lib_file}" ]]; then
install -m 0644 -D "${lib_file}" "${install_dir}/usr/lib/${LIB_DIR}/"
display_alert "LIBION" "Library installed from ${lib_file}" "info"
else
display_alert "LIBION" "libion.so not found in build directory" "warn"
fi
fi
display_alert "LIBION" "Installation completed" "success"
return 0
}
function __libion_create_deb {
local build_dir="${1}"
local install_dir="${2}"
# 设置必要的变量
local pkg_name="libion"
local version="${DEB_VERSION}"
local architecture="${ARCH}"
local maintainer="Armbian Build <build@armbian.com>"
local description="ION (I/O Memory Manager) library for Amlogic SoCs"
display_alert "LIBION" "Creating Debian package for ${pkg_name} ${version}..." "info"
# 创建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
display_alert "LIBION" "Copied installed files to ${deb_dir}" "info"
else
display_alert "LIBION" "No installed files found in ${install_dir}" "warn"
return 0
fi
# 创建control文件
cat > "${debian_dir}/control" <<EOF
Package: ${pkg_name}
Version: ${version}
Architecture: ${architecture}
Maintainer: ${maintainer}
Description: ${description}
This package provides ION (I/O Memory Manager) library for Amlogic SoCs.
ION is a memory manager that provides a way to share memory between
userspace and kernel space, and between different hardware components.
Includes:
* libion.so - ION library
* ion.h - ION header file
* IONmem.h - ION memory header file
Depends:
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 "Libion 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
echo "Removing libion library"
fi
exit 0
EOF
chmod 755 "${debian_dir}/prerm"
display_alert "LIBION" "Building Debian package..." "info"
# 构建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 "LIBION" "Debian package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${version}_${architecture}.deb" "success"
else
display_alert "LIBION" "Failed to build Debian package" "err"
return 1
fi
return 0
}
function amlbian_build_libion {
if ! is_unit_enabled "libion"; then
display_alert "[SKIP] >> libion disable in BUILD_UNITS" "" "info"
return
fi
display_alert "AMLOGIC: Building libion ${DEB_VERSION} ${BOARD}" "info"
# Define all directory paths (in .tmp directory)
local build_root="${BUILD_DIR}/.tmp/libion-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"
# 准备源代码
__libion_prepare_source "${build_dir}"
# 构建
if ! __libion_build "${build_dir}" "${install_dir}"; then
display_alert "AMLOGIC: Libion build failed" "err"
return 1
fi
# 安装到安装目录
if ! __libion_install "${build_dir}" "${install_dir}"; then
display_alert "AMLOGIC: Libion installation failed" "err"
return 1
fi
display_alert "AMLOGIC: Creating Debian package..." "info"
__libion_create_deb "${build_dir}" "${install_dir}"
__unset_libion_config
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
display_alert "AMLIbian Libion Build Script" "Version: ${PKG_VERSION}, Board: ${BOARD}" "info"
amlbian_build_libion
display_alert "AMLOGIC: Libion build completed" "${BUILD_DIR}/output" "info"
# ==============================================================================
# End of build-libion.sh
# ==============================================================================