298 lines
9.0 KiB
Bash
298 lines
9.0 KiB
Bash
#!/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"
|
||
MODULE_BUILD_DIR="${SRC}/output/.tmp"
|
||
|
||
|
||
# ==============================================================================
|
||
# Section 2: Default Configuration
|
||
# ==============================================================================
|
||
|
||
BOARD="${BOARD:-s6-bq201}"
|
||
DEB_VERSION="1.0"
|
||
ARCH="${ARCH:-arm64}"
|
||
|
||
# ==============================================================================
|
||
# Section 4: Libion Build Functions
|
||
# ==============================================================================
|
||
|
||
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
|
||
aml_log error "LIBION" "aml-comp not found: ${aml_comp_path}"
|
||
exit 1
|
||
fi
|
||
|
||
local source_dir="${aml_comp_path}/hardware/amlogic/libion"
|
||
|
||
if [[ ! -d "${source_dir}" ]]; then
|
||
aml_log error "LIBION" "Source directory not found: ${source_dir}"
|
||
exit 1
|
||
fi
|
||
|
||
aml_log debug "LIBION" "Copying source from ${source_dir} to ${build_dir}"
|
||
|
||
# 清空构建目录
|
||
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}"
|
||
|
||
aml_log info "LIBION" "Starting libion build..."
|
||
|
||
# 检查源目录
|
||
if [[ ! -d "${build_dir}" ]]; then
|
||
aml_log error "LIBION" "Build directory not found: ${build_dir}"
|
||
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}"
|
||
|
||
aml_log info "LIBION" "Building libion library..."
|
||
|
||
# 编译命令,使用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 || {
|
||
aml_log error "LIBION" "Build failed"
|
||
return 1
|
||
}
|
||
|
||
display_alert "LIBION" "Build completed successfully" "success"
|
||
return 0
|
||
}
|
||
|
||
function __libion_install {
|
||
local build_dir="${1}"
|
||
local install_dir="${2}"
|
||
|
||
aml_log info "LIBION" "Installing libion library..."
|
||
|
||
# 创建安装目录结构
|
||
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/"
|
||
aml_log debug "LIBION" "Headers installed to ${install_dir}/usr/include"
|
||
fi
|
||
|
||
# 安装库文件
|
||
if [[ -f "${build_dir}/libion.so" ]]; then
|
||
install -m 0644 -D "${build_dir}/libion.so" "${install_dir}/usr/lib/${LIB_DIR}/"
|
||
aml_log debug "LIBION" "Library installed to ${install_dir}/usr/lib/${LIB_DIR}"
|
||
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}/"
|
||
aml_log info "LIBION" "Library installed from ${lib_file}"
|
||
else
|
||
aml_log error "LIBION" "libion.so not found in build directory"
|
||
return 1
|
||
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 <[email protected]>"
|
||
local description="ION (I/O Memory Manager) library for Amlogic SoCs"
|
||
|
||
aml_log info "LIBION" "Creating Debian package for ${pkg_name} ${version}..."
|
||
|
||
# 创建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
|
||
aml_log info "LIBION" "Copied installed files to ${deb_dir}"
|
||
else
|
||
aml_log error "LIBION" "No installed files found in ${install_dir}"
|
||
return 1
|
||
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"
|
||
|
||
aml_log info "LIBION" "Building Debian package..."
|
||
|
||
# 构建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
|
||
aml_log error "LIBION" "Failed to build Debian package"
|
||
return 1
|
||
fi
|
||
|
||
return 0
|
||
}
|
||
|
||
function amlbian_build_libion {
|
||
if ! is_unit_enabled "libion"; then
|
||
aml_log info "[SKIP] >> libion disable in BUILD_UNITS"
|
||
return
|
||
fi
|
||
aml_log info "AMLOGIC: Building libion ${DEB_VERSION} ${BOARD}"
|
||
|
||
# Define all directory paths (in .tmp directory)
|
||
local build_root="${MODULE_BUILD_DIR}/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}"
|
||
|
||
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"
|
||
|
||
# 准备源代码
|
||
__libion_prepare_source "${build_dir}"
|
||
|
||
# 构建
|
||
if ! __libion_build "${build_dir}" "${install_dir}"; then
|
||
aml_log error "AMLOGIC: Libion build failed"
|
||
return 1
|
||
fi
|
||
|
||
# 安装到安装目录
|
||
if ! __libion_install "${build_dir}" "${install_dir}"; then
|
||
aml_log error "AMLOGIC: Libion installation failed"
|
||
return 1
|
||
fi
|
||
|
||
aml_log info "AMLOGIC: Creating Debian package..."
|
||
__libion_create_deb "${build_dir}" "${install_dir}"
|
||
|
||
__unset_libion_config
|
||
|
||
return 0
|
||
}
|
||
|
||
# ==============================================================================
|
||
# Section 5: Main
|
||
# ==============================================================================
|
||
|
||
aml_log info "AMLIbian Libion Build Script" "Version: ${PKG_VERSION}, Board: ${BOARD}"
|
||
|
||
amlbian_build_libion
|
||
|
||
aml_log info "AMLOGIC: Libion build completed" "${BUILD_DIR}/output"
|
||
|
||
# ==============================================================================
|
||
# End of build-libion.sh
|
||
# ==============================================================================
|