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

535 lines
18 KiB
Bash
Raw Normal View History

2026-08-13 11:28:40 +08:00
#!/usr/bin/env bash
# SPDX-License-Identifier: (GPL-2.0-only OR MIT)
# Copyright (C) 2026 Amlogic, Inc. All rights reserved
################################################################################
# Amlbian NPU Compilation Module
#
# Purpose: NPU compilation with Hook support and aml-comp integration
# Provides: NPU compilation, driver and library preparation
################################################################################
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")"
BASE_DIR="$(dirname "$(dirname "${AMLBIAN_DIR}")")"
2026-11-11 08:50:43 +08:00
MODULE_BUILD_DIR="${SRC}/output/.tmp"
2026-08-13 11:28:40 +08:00
2026-11-11 08:50:43 +08:00
aml_log debug "SCRIPT_DIR ${SCRIPT_DIR} AMLBIAN_DIR ${AMLBIAN_DIR} BASE_DIR ${BASE_DIR}"
2026-08-13 11:28:40 +08:00
# Source ADLA build script if it exists
ADLA_SCRIPT="${SCRIPT_DIR}/build-adla.sh"
if [[ -f "${ADLA_SCRIPT}" ]]; then
2026-11-11 08:50:43 +08:00
aml_log debug "Loading ADLA build script" "${ADLA_SCRIPT}"
2026-08-13 11:28:40 +08:00
source "${ADLA_SCRIPT}"
else
2026-11-11 08:50:43 +08:00
aml_log error "ADLA build script not found" "${ADLA_SCRIPT}"
2026-08-13 11:28:40 +08:00
fi
# ==============================================================================
# Shared Helper Functions
# ==============================================================================
# ------------------------------------------------------------------------------
# Step 1: Version and Package Information Definition
# ------------------------------------------------------------------------------
# Initialize common package metadata for NPU common library
function _npu_common_init_package_info() {
PKG_MAINTAINER="Armbian Build <build@armbian.com>"
PKG_DESCRIPTION="Common library for Amlogic NPU"
PKG_NAME="aml-npu-common"
PKG_VERSION="1.0"
PKG_ARCH="arm64"
LIB_DIR="aarch64-linux-gnu"
NPU_COMM_DIR="${BASE_DIR}/aml-comp/vendor/amlogic/aml_npu_common"
}
# Initialize common package metadata for NPU driver
function _npu_driver_init_package_info() {
PKG_MAINTAINER="Armbian Build <build@armbian.com>"
PKG_DESCRIPTION="Amlogic NPU driver and libraries"
PKG_NAME="aml-npu"
PKG_VERSION="1.0"
PKG_ARCH="arm64"
NPU_MODULE="galcore.ko"
# Cross compilation settings
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
}
# ------------------------------------------------------------------------------
# Step 2: Build Directory Structure Setup
# ------------------------------------------------------------------------------
# Create layered build directory structure
# Parameters:
# $1 - base name (e.g. "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}")
# Sets: TMP_DIR, BASE_BUILD_DIR, SOURCE_DIR, BUILD_DIR, INSTALL_DIR, PATCH_DIR,
# DEB_PACKAGE_DIR, DEBIAN_DIR
function _npu_create_build_dirs() {
local base_name="$1"
2026-11-11 08:50:43 +08:00
mkdir -p "${MODULE_BUILD_DIR}"
2026-08-13 11:28:40 +08:00
# 基础构建目录
2026-11-11 08:50:43 +08:00
BASE_BUILD_DIR="${MODULE_BUILD_DIR}/${base_name}"
2026-08-13 11:28:40 +08:00
rm -rf ${BASE_BUILD_DIR}
# 分层目录
SOURCE_DIR="${BASE_BUILD_DIR}/source" # 源码目录
BUILD_DIR="${BASE_BUILD_DIR}/build" # 编译目录
INSTALL_DIR="${BASE_BUILD_DIR}/install" # 安装目录
PATCH_DIR="${BASE_BUILD_DIR}/patch" # 补丁目录
# 创建所有目录
mkdir -p "${SOURCE_DIR}"
mkdir -p "${BUILD_DIR}"
mkdir -p "${INSTALL_DIR}"
mkdir -p "${PATCH_DIR}"
# 在BUILD_DIR下创建deb/包名目录
DEB_PACKAGE_DIR="${BUILD_DIR}/deb/${base_name}"
mkdir -p "${DEB_PACKAGE_DIR}"
# 安装目录的子目录
DEBIAN_DIR="${INSTALL_DIR}/DEBIAN"
mkdir -p "${DEBIAN_DIR}"
}
# Create standard install subdirectories for NPU common library
function _npu_common_create_install_dirs() {
mkdir -p "${INSTALL_DIR}/usr/include"
mkdir -p "${INSTALL_DIR}/usr/lib"
}
# Create standard install subdirectories for NPU driver
function _npu_driver_create_install_dirs() {
local kernel_version="$1"
mkdir -p "${INSTALL_DIR}/lib/modules/${kernel_version}/kernel/amlogic/npu/"
mkdir -p "${INSTALL_DIR}/usr/lib/"
mkdir -p "${INSTALL_DIR}/etc/npu"
}
# ------------------------------------------------------------------------------
# Step 3: Debian Packaging Scripts Creation
# ------------------------------------------------------------------------------
# Create Debian control file
# Parameters:
# $1 - package name
# $2 - package version
# $3 - package architecture
# $4 - maintainer
# $5 - description
# $6 - dependencies (optional)
# $7 - conflicts (optional)
# $8 - provides (optional)
# $9 - section (optional, default "libs")
function _npu_create_control_file() {
local pkg_name="$1"
local pkg_version="$2"
local pkg_arch="$3"
local maintainer="$4"
local description="$5"
local depends="${6:-}"
local conflicts="${7:-}"
local provides="${8:-}"
local section="${9:-libs}"
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU Creating control file DEBIAN/control"
2026-08-13 11:28:40 +08:00
cat > "${DEBIAN_DIR}/control" <<EOF
Package: ${pkg_name}
Version: ${pkg_version}
Architecture: ${pkg_arch}
Maintainer: ${maintainer}
Depends: ${depends}
Conflicts: ${conflicts}
Provides: ${provides}
Section: ${section}
Priority: optional
Description: ${description}
EOF
}
# Create postinst script for NPU common library
function _npu_common_create_postinst() {
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-COMMON Creating post-installation script DEBIAN/postinst"
2026-08-13 11:28:40 +08:00
cat > "${DEBIAN_DIR}/postinst" << EOF
#!/bin/sh
set -e
if [ "\$1" = "configure" ]; then
# Run ldconfig to update library cache
ldconfig
echo "NPU common library installed successfully."
echo "Header files are installed in /usr/include/"
echo "Library files are installed in /usr/lib/"
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/postinst"
}
# Create prerm script for NPU common library
function _npu_common_create_prerm() {
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-COMMON Creating pre-removal script DEBIAN/prerm"
2026-08-13 11:28:40 +08:00
cat > "${DEBIAN_DIR}/prerm" << EOF
#!/bin/sh
set -e
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
# Run ldconfig to update library cache
ldconfig
echo "NPU common library removed successfully."
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/prerm"
}
# Create postinst script for NPU driver
function _npu_driver_create_postinst() {
local kernel_version="$1"
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Creating post-installation script DEBIAN/postinst"
2026-08-13 11:28:40 +08:00
cat > "${DEBIAN_DIR}/postinst" << EOF
#!/bin/bash
set -e
# Run depmod to update module dependencies
# Clear any KERNELDIR environment variable that might interfere
unset KERNELDIR
unset STAGING_KERNEL_DIR
if [ -d /lib/modules ]; then
for kernel in /lib/modules/*; do
if [ -d "\${kernel}" ]; then
# Use depmod with explicit kernel version
depmod -a "${kernel_version}" 2>/dev/null || {
echo "Warning: depmod failed for kernel "${kernel_version}")"
echo "This is normal if the kernel is not currently running"
}
fi
done
fi
# Update library cache
ldconfig
echo "Amlogic NPU driver installed successfully."
EOF
chmod 0755 "${DEBIAN_DIR}/postinst"
}
# Create prerm script for NPU driver
function _npu_driver_create_prerm() {
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Creating pre-removal script DEBIAN/prerm"
2026-08-13 11:28:40 +08:00
cat > "${DEBIAN_DIR}/prerm" << 'EOF'
#!/bin/bash
set -e
echo "Removing Amlogic NPU driver..."
EOF
chmod 0755 "${DEBIAN_DIR}/prerm"
}
# ------------------------------------------------------------------------------
# Step 4: Source/Vendor File Copying
# ------------------------------------------------------------------------------
# Copy NPU common vendor files to install directory
function _npu_common_copy_files() {
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-COMMON Copying files from vendor binaries ${NPU_COMM_DIR}"
2026-08-13 11:28:40 +08:00
# Determine the correct library path based on configuration
local AML_CUST_SPEC="${AML_CUST_SPEC:-}"
local HOST_ARCH="${HOST_ARCH:-aarch64}"
# Set ARM_TARGET based on architecture
local ARM_TARGET="lib32"
if [ "${HOST_ARCH}" = "aarch64" ]; then
ARM_TARGET="lib64"
fi
# Install header files
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-COMMON Installing NPU common header files"
2026-08-13 11:28:40 +08:00
if [ -d "${NPU_COMM_DIR}/nnsdk/include" ]; then
install -m 0644 "${NPU_COMM_DIR}"/nnsdk/include/* "${INSTALL_DIR}/usr/include/"
fi
if [ -d "${NPU_COMM_DIR}/llmsdk/include" ]; then
install -m 0644 "${NPU_COMM_DIR}"/llmsdk/include/* "${INSTALL_DIR}/usr/include/"
fi
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-COMMON Library source path: ${LIB_SOURCE_PATH}"
2026-08-13 11:28:40 +08:00
if [ "${AML_CUST_SPEC}" = "CUST_L_HUB_F" ]; then
LIB_SOURCE_PATH="${NPU_COMM_DIR}/nnsdk/linux/starf/${ARM_TARGET}"
safe_install -m 0644 "${LIB_SOURCE_PATH}"/*.so* "${INSTALL_DIR}/usr/lib/"
else
LIB_SOURCE_PATH="${NPU_COMM_DIR}/nnsdk/linux/yocto/${ARM_TARGET}"
safe_install -m 0644 "${LIB_SOURCE_PATH}"/*.so* "${INSTALL_DIR}/usr/lib/"
safe_install -m 0644 "${NPU_COMM_DIR}"/llmsdk/linux/yocto/${ARM_TARGET}/*.so* "${INSTALL_DIR}/usr/lib/"
fi
}
# Copy NPU driver files to install directory
function _npu_driver_copy_files() {
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Copying NPU driver files"
2026-08-13 11:28:40 +08:00
local kernel_version="$1"
local module_dir="${INSTALL_DIR}/lib/modules/${kernel_version}/kernel/amlogic/npu/"
# Check if NPU module was built
if [[ -f "${SOURCE_DIR}/build/sdk/drivers/${NPU_MODULE}" ]]; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER NPU module built successfully: ${NPU_MODULE}"
2026-08-13 11:28:40 +08:00
# Install NPU module to INSTALL_DIR
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-DRIVER Installing NPU module to install directory ${module_dir}"
2026-08-13 11:28:40 +08:00
install -m 0644 "${SOURCE_DIR}/build/sdk/drivers/${NPU_MODULE}" "${module_dir}/"
# Install NPU libraries based on architecture to INSTALL_DIR
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Installing NPU libraries for ${PKG_ARCH} architecture"
2026-08-13 11:28:40 +08:00
if [[ "${PKG_ARCH}" == "arm64" ]]; then
# Install 64-bit libraries
LIB_SRC_DIR="${SOURCE_DIR}/sharelib/lib64"
if [[ -d "${LIB_SRC_DIR}" ]]; then
for lib in "${LIB_SRC_DIR}"/*.so*; do
if [[ -f "${lib}" ]]; then
install -m 0644 "${lib}" "${INSTALL_DIR}/usr/lib/"
fi
done
fi
else
# Install 32-bit libraries
LIB_SRC_DIR="${SOURCE_DIR}/sharelib/lib32"
if [[ -d "${LIB_SRC_DIR}" ]]; then
for lib in "${LIB_SRC_DIR}"/*.so*; do
if [[ -f "${lib}" ]]; then
install -m 0644 "${lib}" "${INSTALL_DIR}/usr/lib/"
fi
done
fi
fi
# Create NPU configuration directory in INSTALL_DIR
CONFIG_DIR="${INSTALL_DIR}/etc/npu"
mkdir -p "${CONFIG_DIR}"
# Create a sample configuration file
cat > "${CONFIG_DIR}/npu.conf" << EOF
# Amlogic NPU Configuration
NPU_ENABLE=1
NPU_MODE=performance
NPU_FREQUENCY=800
EOF
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER NPU configuration files created"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-NPU-DRIVER NPU module build failed. Could not find ${NPU_MODULE}"
2026-08-13 11:28:40 +08:00
exit 1
fi
}
# ------------------------------------------------------------------------------
# Step 5: Debian Package Building
# ------------------------------------------------------------------------------
# Build Debian package from install directory
# Parameters:
# $1 - log prefix (e.g. "COMPILE-NPU-COMMON" or "COMPILE-NPU-DRIVER")
# $2 - output deb filename (without path)
# Sets: OUTPUT_DEB
function _npu_build_deb_package() {
local log_prefix="$1"
local output_deb_name="$2"
# 将install目录内容拷贝到deb目录
2026-11-11 08:50:43 +08:00
aml_log debug "Copying install directory to deb package directory ${log_prefix}"
2026-08-13 11:28:40 +08:00
cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" 2>&1
# 基于deb目录打包
2026-11-11 08:50:43 +08:00
aml_log info "Building Debian package from deb directory ${log_prefix}"
2026-08-13 11:28:40 +08:00
dpkg-deb --build "${DEB_PACKAGE_DIR}" 2>&1
# Move to output directory
OUTPUT_DEB="${SRC}/output/${output_deb_name}"
mkdir -p "$(dirname "${OUTPUT_DEB}")"
mv "${DEB_PACKAGE_DIR}.deb" "${OUTPUT_DEB}"
2026-11-11 08:50:43 +08:00
aml_log info "${log_prefix} Package built successfully ${output_deb_name}"
aml_log debug "${log_prefix} Build directory preserved ${BASE_BUILD_DIR}"
2026-08-13 11:28:40 +08:00
}
# Display build directory info
# Parameters:
# $1 - log prefix
function _npu_display_build_info() {
local log_prefix="$1"
2026-11-11 08:50:43 +08:00
aml_log debug "${log_prefix} Source dir: ${SOURCE_DIR}"
aml_log debug "${log_prefix} Build dir: ${BUILD_DIR}"
aml_log debug "${log_prefix} Install dir: ${INSTALL_DIR}"
aml_log debug "${log_prefix} Patch dir: ${PATCH_DIR}"
aml_log debug "${log_prefix} Deb package dir: ${DEB_PACKAGE_DIR}"
2026-08-13 11:28:40 +08:00
}
# ==============================================================================
# Main Build Functions (Entry Points)
# ==============================================================================
# ------------------------------------------------------------------------------
# NPU Common Library Build
# Entry point: amlbian_build_npu_common
# ------------------------------------------------------------------------------
function amlbian_build_npu_common() {
# ---- Step 1: Version and Package Information ----
_npu_common_init_package_info
# amlbian_checkout_toolchain
# Get kernel version for packaging
local KERNEL_VERSION=$(make -s -C "${BASE_DIR}/aml-comp/kernel/aml-6.12/build" kernelrelease 2>/dev/null || true)
if [[ -z "${KERNEL_VERSION}" ]]; then
KERNEL_VERSION="unknown-$(date +%Y%m%d)"
fi
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-COMMON Building NPU common library for kernel ${KERNEL_VERSION}"
2026-08-13 11:28:40 +08:00
# ---- Step 2: Build Directory Setup ----
_npu_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
_npu_common_create_install_dirs
# Display info
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-COMMON Building NPU common library version ${PKG_VERSION} for ${PKG_ARCH}"
2026-08-13 11:28:40 +08:00
_npu_display_build_info "COMPILE-NPU-COMMON"
# ---- Step 3: Debian Packaging Scripts ----
_npu_create_control_file \
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
"${PKG_DESCRIPTION}" "" "" "" "libs"
_npu_common_create_postinst
_npu_common_create_prerm
# ---- Step 4: Copy Source/Vendor Files ----
_npu_common_copy_files
# ---- Step 5: Build Debian Package ----
_npu_build_deb_package "COMPILE-NPU-COMMON" "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb"
return 0
}
# ------------------------------------------------------------------------------
# NPU Driver Build
# Entry point: amlbian_build_npu_driver
# ------------------------------------------------------------------------------
function amlbian_build_npu_driver() {
set -e
# ---- Step 1: Version and Package Information ----
_npu_driver_init_package_info
# amlbian_checkout_toolchain
# Get kernel version for packaging
local KERNEL_VERSION=$(make -s -C "${BASE_DIR}/aml-comp/kernel/aml-6.12/build" kernelrelease 2>/dev/null || true)
if [[ -z "${KERNEL_VERSION}" ]]; then
KERNEL_VERSION="unknown-$(date +%Y%m%d)"
fi
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Building NPU driver for kernel ${KERNEL_VERSION}"
2026-08-13 11:28:40 +08:00
# Source directory
local SRC_DIR="${BASE_DIR}/aml-comp/hardware/amlogic/npu/nanoq"
# Check if source exists
if [[ ! -d "${SRC_DIR}" ]]; then
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-NPU-DRIVER NPU driver source code not found. Expected at: ${SRC_DIR}"
2026-08-13 11:28:40 +08:00
exit 1
fi
# Check for build script
if [[ ! -f "${SRC_DIR}/aml_buildroot.sh" ]]; then
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-NPU-DRIVER NPU build script not found. Expected: ${SRC_DIR}/aml_buildroot.sh"
2026-08-13 11:28:40 +08:00
exit 1
fi
# ---- Step 2: Build Directory Setup ----
_npu_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
_npu_driver_create_install_dirs "${KERNEL_VERSION}"
_npu_display_build_info "COMPILE-NPU-DRIVER"
# Copy source to source directory
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-NPU-DRIVER Copying source files to source directory"
2026-08-13 11:28:40 +08:00
cp -r "${SRC_DIR}/"* "${SOURCE_DIR}/"
# Set up environment variables for build
export ARCH="${PKG_ARCH}"
export CROSS_COMPILE="${CROSS_COMPILE}"
export KERNELDIR="${BASE_DIR}/aml-comp/kernel/aml-6.12"
export STAGING_KERNEL_DIR="${KERNELDIR}"
# Check for kernel headers
if [[ ! -d "${KERNELDIR}" ]]; then
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-NPU-DRIVER Kernel headers directory not found at: ${KERNELDIR}"
2026-08-13 11:28:40 +08:00
exit 1
fi
# Change to source directory
cd "${SOURCE_DIR}"
# Build the NPU driver
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER Building NPU kernel module and libraries"
2026-08-13 11:28:40 +08:00
# Execute the build script with appropriate parameters
./aml_buildroot.sh "${PKG_ARCH}" "${KERNELDIR}/build" "/usr/bin/aarch64-linux-gnu-"
# ---- Step 3: Debian Packaging Scripts ----
_npu_create_control_file \
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
"${PKG_DESCRIPTION}" "" "" "" "kernel"
_npu_driver_create_postinst "${KERNEL_VERSION}"
_npu_driver_create_prerm
# ---- Step 4: Copy Source/Vendor Files ----
_npu_driver_copy_files "${KERNEL_VERSION}"
# ---- Step 5: Build Debian Package ----
_npu_build_deb_package "COMPILE-NPU-DRIVER" "${PKG_NAME}_${PKG_VERSION}_${KERNEL_VERSION}_${PKG_ARCH}.deb"
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-NPU-DRIVER NPU driver build completed. Successfully built for ${PKG_ARCH} with kernel ${KERNEL_VERSION}"
2026-08-13 11:28:40 +08:00
return 0
}
# ------------------------------------------------------------------------------
# Main NPU Build Function
# ------------------------------------------------------------------------------
function amlbian_build_npu() {
if ! is_unit_enabled "npu"; then
echo "[SKIP] >> NPU disable in BUILD_UNITS"
return
fi
amlbian_build_npu_common
amlbian_build_adla
}
# ==============================================================================
# Main Execution
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLbian NPU Build Script" "Starting NPU build"
2026-08-13 11:28:40 +08:00
amlbian_build_npu
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: NPU build completed" "Check output directory for packages"