Files
BPI-CM9-Linux/amlbian/lib/function-env.sh
T
2026-08-24 10:03:24 +08:00

278 lines
11 KiB
Bash

#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Environment configuration functions for AMLbian
function env_mount_to_docker() {
# 计算 aml-comp 目录的绝对路径
local overlay_dir="$(realpath "$(dirname "${BASH_SOURCE[0]}")/..")"
local aml_comp_dir="${overlay_dir}/../aml-comp"
local repo_dir="${overlay_dir}/../.repo"
if [[ -d "${aml_comp_dir}" ]]; then
display_alert "AMLOGIC" "Mounting local source directory: ${aml_comp_dir}" "info"
# 在容器内创建目标目录
local container_target="/aml-comp"
local repo_target="/.repo"
# 添加 Docker mount 参数
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${aml_comp_dir},target=${container_target}")
DOCKER_EXTRA_ARGS+=("--env" "AML_COMP_DIR=${container_target}")
# add .repo for git info
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${repo_dir},target=${repo_target}")
# 更新 LOCAL_UBOOT_PATH 和 LOCAL_KERNEL_PATH 指向容器内的路径
DOCKER_EXTRA_ARGS+=("--env" "LOCAL_UBOOT_PATH=${container_target}/uboot")
DOCKER_EXTRA_ARGS+=("--env" "LOCAL_KERNEL_PATH=${container_target}/kernel")
display_alert "AMLOGIC" "Local sources will be available at: ${container_target}" "info"
else
display_alert "AMLOGIC WARNING" "Local source directory not found: ${aml_comp_dir}" "warn"
display_alert "AMLOGIC" "Will fallback to remote sources if USE_LOCAL_SOURCES=yes" "info"
fi
# mount uboot依赖toolchain到docker中
display_alert "AMLOGIC TODO" "TOOLCHAIN to docker... ..." "info"
# #/opt/arm-gnu-toolchain-13.2.Rel1-x86_64-aarch64-none-elf
# local tc_gnu="/opt/arm-gnu-toolchain-13.2.Rel1-x86_64-aarch64-none-elf"
# DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${tc_gnu},target=${tc_gnu}")
# #/opt/gcc-linaro-7.3.1-2018.05-i686_aarch64-elf/bin/aarch64-elf-gcc
# local tc_linaro="/opt/gcc-linaro-7.3.1-2018.05-i686_aarch64-elf"
# DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${tc_linaro},target=${tc_linaro}")
# GNU toolchain
#arm-gnu-toolchain-11.3.rel1-x86_64-aarch64-none-linux-gnu
local ext_gnu="/opt/arm-gnu-toolchain-11.3.rel1-x86_64-aarch64-none-linux-gnu"
if [[ -d "${ext_gnu}" ]]; then
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${ext_gnu},target=${ext_gnu},readonly")
display_alert "AMLOGIC" "Mounted external GNU toolchain: ${ext_gnu}" "info"
else
display_alert "AMLOGIC WARNING" "GNU external toolchain not found: ${ext_gnu}" "warn"
fi
local tc_gnu="/opt/arm-gnu-toolchain-13.2.Rel1-x86_64-aarch64-none-elf"
if [[ -d "${tc_gnu}" ]]; then
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${tc_gnu},target=${tc_gnu},readonly")
display_alert "AMLOGIC" "Mounted GNU 13.2 toolchain: ${tc_gnu}" "info"
else
display_alert "AMLOGIC WARNING" "GNU 13.2 toolchain not found: ${tc_gnu}" "warn"
fi
# Linaro toolchain
local tc_linaro="/opt/gcc-linaro-7.3.1-2018.05-i686_aarch64-elf"
if [[ -d "${tc_linaro}" ]]; then
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${tc_linaro},target=${tc_linaro},readonly")
display_alert "AMLOGIC" "Mounted Linaro toolchain: ${tc_linaro}" "info"
else
display_alert "AMLOGIC WARNING" "Linaro toolchain not found: ${tc_linaro}" "warn"
fi
#/opt/riscv-none-gcc
local tc_riscv="/opt/riscv-none-gcc"
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${tc_riscv},target=${tc_riscv},readonly")
#add mirror mount
local sh_mirror="/mnt/fileroot/Public/SH-MIRROR"
if [ -d ${sh_mirror} ];then
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${sh_mirror},target=${sh_mirror},readonly")
display_alert "AMLOGIC DEBUG" "add mount mirror ${sh_mirror}" "debug"
fi
sh_mirror="/mnt/fileroot/jenkins/mirror"
if [ -d ${sh_mirror} ];then
DOCKER_EXTRA_ARGS+=("--mount" "type=bind,source=${sh_mirror},target=${sh_mirror},readonly")
display_alert "AMLOGIC DEBUG" "add mount mirror ${sh_mirror}" "debug"
fi
# 添加调试信息
if [[ "${SHOW_DEBUG}" == "yes" ]]; then
display_alert "AMLOGIC DEBUG" "Total mount args: ${#DOCKER_EXTRA_ARGS[@]}" "debug"
for i in "${!DOCKER_EXTRA_ARGS[@]}"; do
display_alert "AMLOGIC DEBUG" "Arg $i: ${DOCKER_EXTRA_ARGS[$i]}" "debug"
done
fi
}
# ==============================================================================
# Section 1: Source Path Configuration
# ==============================================================================
# Initialize local source paths
function amlbian_init_source_paths() {
display_alert "AMLOGIC TODO: Initializing source paths" "${BOARD}" "info"
# Calculate base directory
local amlbian_dir
amlbian_dir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
amlbian_dir="$(dirname "${amlbian_dir}")"
# Set default paths based on environment
if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then
# Docker container paths
LOCAL_UBOOT_PATH="${LOCAL_UBOOT_PATH:-/aml-comp/uboot/bl33/v2023}"
LOCAL_KERNEL_PATH="${LOCAL_KERNEL_PATH:-/aml-comp/kernel}"
LOCAL_GPU_PATH="${LOCAL_GPU_PATH:-/aml-comp/gpu}"
LOCAL_NPU_PATH="${LOCAL_NPU_PATH:-/aml-comp/npu}"
LOCAL_WIFI_PATH="${LOCAL_WIFI_PATH:-/aml-comp/wifi}"
else
# Host environment paths
LOCAL_UBOOT_PATH="${LOCAL_UBOOT_PATH:-${amlbian_dir}/../aml-comp/uboot}"
LOCAL_KERNEL_PATH="${LOCAL_KERNEL_PATH:-${amlbian_dir}/../aml-comp/kernel}"
LOCAL_GPU_PATH="${LOCAL_GPU_PATH:-${amlbian_dir}/../aml-comp/gpu}"
LOCAL_NPU_PATH="${LOCAL_NPU_PATH:-${amlbian_dir}/../aml-comp/npu}"
LOCAL_WIFI_PATH="${LOCAL_WIFI_PATH:-${amlbian_dir}/../aml-comp/wifi}"
fi
export LOCAL_UBOOT_PATH LOCAL_KERNEL_PATH LOCAL_GPU_PATH LOCAL_NPU_PATH LOCAL_WIFI_PATH
}
# ==============================================================================
# Section 2: Build Environment Setup
# ==============================================================================
# Setup build environment for local sources
function amlbian_setup_build_env() {
display_alert "AMLOGIC TODO: Setting up build environment" "${BOARD}" "info"
if [[ "${USE_LOCAL_SOURCES}" != "yes" ]]; then
display_alert "AMLOGIC TODO: Using remote sources, skipping local env setup" "${BOARD}" "info"
return 0
fi
# Verify source directories exist
if [[ ! -d "${LOCAL_UBOOT_PATH}" ]]; then
display_alert "AMLOGIC TODO: U-Boot source path not found" "${LOCAL_UBOOT_PATH}" "warn"
fi
if [[ ! -d "${LOCAL_KERNEL_PATH}" ]]; then
display_alert "AMLOGIC TODO: Kernel source path not found" "${LOCAL_KERNEL_PATH}" "warn"
fi
# Set up build flags
export SKIP_GIT_FETCH="yes"
export SKIP_GIT_FETCH_KERNEL="yes"
export GENERATE_GIT_INFO="no"
export SKIP_GIT_INFO="yes"
return 0
}
# ==============================================================================
# Section 3: Git Branch Configuration
# ==============================================================================
# Configure git branches for local sources
function amlbian_config_git_branches() {
display_alert "AMLOGIC TODO: Configuring git branches" "${BOARD}" "info"
if [[ "${USE_LOCAL_SOURCES}" != "yes" ]]; then
display_alert "AMLOGIC TODO: Using remote sources, skipping branch config" "${BOARD}" "info"
return 0
fi
# Get actual commit hashes from local repositories
local uboot_commit="amlogic-dev-2023"
local kernel_commit="android16-6.12"
if [[ -d "${LOCAL_UBOOT_PATH}/.git" ]]; then
uboot_commit=$(cd "${LOCAL_UBOOT_PATH}" && git rev-parse HEAD 2>/dev/null | cut -c1-12 || echo "amlogic-dev-2023")
fi
if [[ -d "${LOCAL_KERNEL_PATH}/.git" ]]; then
kernel_commit=$(cd "${LOCAL_KERNEL_PATH}" && git rev-parse HEAD 2>/dev/null | cut -c1-12 || echo "android16-6.12")
fi
# Set branches using commit hashes to avoid validation errors
export BOOTBRANCH="commit:${uboot_commit}"
export KERNELBRANCH="commit:${kernel_commit}"
display_alert "AMLOGIC TODO: Git branches configured" "UBoot: ${BOOTBRANCH}, Kernel: ${KERNELBRANCH}" "info"
return 0
}
# ==============================================================================
# Section 4: Docker Environment Setup
# ==============================================================================
# Setup Docker-specific environment
function amlbian_setup_docker_env() {
display_alert "AMLOGIC TODO: Setting up Docker environment" "${BOARD}" "info"
if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" != "yes" ]]; then
return 0
fi
# Update paths for Docker mount points
local aml_comp_base="/aml-comp"
if [[ -d "${aml_comp_base}" ]]; then
export LOCAL_UBOOT_PATH="${aml_comp_base}/uboot/bl33/v2023"
export LOCAL_KERNEL_PATH="${aml_comp_base}/kernel"
export LOCAL_GPU_PATH="${aml_comp_base}/gpu"
export LOCAL_NPU_PATH="${aml_comp_base}/npu"
export LOCAL_WIFI_PATH="${aml_comp_base}/wifi"
fi
return 0
}
# ==============================================================================
# Section 5: Compilation Environment
# ==============================================================================
# Setup compilation toolchain paths
function amlbian_setup_toolchain_env() {
display_alert "AMLOGIC TODO: Setting up toolchain environment" "${BOARD}" "info"
# AMLOGIC TODO: Add toolchain paths if needed
# export PATH="/path/to/toolchain/bin:${PATH}"
# export CROSS_COMPILE="/path/to/toolchain/bin/aarch64-linux-gnu-"
return 0
}
# ==============================================================================
# Section 6: Environment Validation
# ==============================================================================
# Validate environment before build
function amlbian_validate_env() {
display_alert "AMLOGIC TODO: Validating build environment" "${BOARD}" "info"
local errors=0
# Check required environment variables
if [[ -z "${BOARD}" ]]; then
display_alert "AMLOGIC TODO: BOARD not set" "" "err"
((errors++))
fi
# Check local sources if enabled
if [[ "${USE_LOCAL_SOURCES}" == "yes" ]]; then
if [[ ! -d "${LOCAL_UBOOT_PATH}" ]] && [[ -n "${LOCAL_UBOOT_PATH}" ]]; then
display_alert "AMLOGIC TODO: U-Boot source not found" "${LOCAL_UBOOT_PATH}" "err"
((errors++))
fi
if [[ ! -d "${LOCAL_KERNEL_PATH}" ]] && [[ -n "${LOCAL_KERNEL_PATH}" ]]; then
display_alert "AMLOGIC TODO: Kernel source not found" "${LOCAL_KERNEL_PATH}" "err"
((errors++))
fi
fi
if [[ ${errors} -gt 0 ]]; then
display_alert "AMLOGIC TODO: Environment validation failed" "${errors} errors" "err"
return 1
fi
display_alert "AMLOGIC TODO: Environment validation passed" "${BOARD}" "info"
return 0
}
# ==============================================================================
# End of function-env.sh
# ==============================================================================