From 9172f4f2f5def81197820923cc7614ddd06a5df3 Mon Sep 17 00:00:00 2001 From: "dian.yuan" Date: Wed, 25 Feb 2026 15:46:48 +0800 Subject: [PATCH] feat: Add Yocto build support for 32-bit and 64-bit architectures to examples, including new build scripts, CMake logic, and documentation. --- README.md | 57 +++++++ cmake/FindAMLNN.cmake | 6 +- examples/build-linux-all.sh | 209 ++++++++++++++++++++++++++ examples/clean-linux-all.sh | 74 +++++++++ examples/cmake/yocto-toolchain.cmake | 71 +++++++++ examples/ppocr-det/cpp/build-linux.sh | 100 +++++++++++- examples/yoloe/cpp/build-linux.sh | 100 +++++++++++- examples/yolov8/cpp/build-linux.sh | 116 ++++++++++++-- examples/yoloworld/cpp/build-linux.sh | 100 +++++++++++- examples/yolox/cpp/build-linux.sh | 103 ++++++++++++- 10 files changed, 900 insertions(+), 36 deletions(-) create mode 100755 examples/build-linux-all.sh create mode 100755 examples/clean-linux-all.sh create mode 100644 examples/cmake/yocto-toolchain.cmake diff --git a/README.md b/README.md index e65e820..ddbc0c4 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,24 @@ git clone https://github.com/Amlogic-NN/amlnn-toolkit.git ../amlnn-toolkit ​ Each **example** directory contains a **build-android.sh** and **build-linux.sh** script. For compilation steps, refer to **Chapter 4** of the **README.md** file in the corresponding example directory. +## Android Complication + +Android compilation requires the NDK toolchain. The build scripts look for the NDK path via the following environment variables (in priority order): + +| Variable | Description | +| -------- | ----------- | +| `ANDROID_NDK_PATH` | Preferred variable | +| `ANDROID_NDK` | Fallback | +| `ANDROID_NDK_HOME` | Fallback | + +Set one of them before building, for example: + +```bash +export ANDROID_NDK_PATH=/path/to/android-ndk-r25c +``` + +> **Note:** NDK **r25c** is recommended. Download: https://github.com/android/ndk/wiki/Unsupported-Downloads + To build **all examples at once**, use the top-level batch script: ```bash @@ -98,7 +116,46 @@ AMLNN_HOME=/path/to/amlnn-toolkit ./build-android-all.sh The script automatically cleans the previous build, resolves the AMLNN SDK via the priority rules above, and prints a build summary at the end. +## Yocto Compilation +Each example's `build-linux.sh` also supports **Yocto** mode via the `-m yocto` flag. + +**Dependency:** A Yocto SDK (Poky). Set the path via environment variable or `-s` flag: + +```bash +export YOCTO_SDK_ROOT=/path/to/poky/sdk +``` + +The toolchain file is shared across all demos at `examples/cmake/yocto-toolchain.cmake`. + +**Build a single demo:** + +```bash +cd examples/yolox/cpp + +# 64-bit (default) +./build-linux.sh -m yocto -s /path/to/poky/sdk + +# 32-bit +./build-linux.sh -m yocto -b 32 -s /path/to/poky/32bit-sdk +``` + +**Build all demos at once:** + +```bash +cd examples + +# 64-bit +./build-linux-all.sh -m yocto -s /path/to/poky/sdk + +# 32-bit +./build-linux-all.sh -m yocto -b 32 -s /path/to/poky/32bit-sdk + +# Clean yocto build artifacts +./clean-linux-all.sh -m yocto +``` + +> **Note:** The `LLMs` demo is automatically excluded from the batch build scripts. # **Release Notes** diff --git a/cmake/FindAMLNN.cmake b/cmake/FindAMLNN.cmake index 16601dc..fbe5d33 100644 --- a/cmake/FindAMLNN.cmake +++ b/cmake/FindAMLNN.cmake @@ -43,7 +43,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Android") set(AMLNN_LIBRARY_DIR "${AMLNN_NNSDK_ROOT}/android/armeabi-v7a") endif() elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") - set(AMLNN_LIBRARY_DIR "${AMLNN_NNSDK_ROOT}/linux/yocto/aarch64-poky-linux") + if(DEFINED ARCH_BITS AND ARCH_BITS STREQUAL "32") + set(AMLNN_LIBRARY_DIR "${AMLNN_NNSDK_ROOT}/linux/yocto/arm-poky-linux") + else() + set(AMLNN_LIBRARY_DIR "${AMLNN_NNSDK_ROOT}/linux/yocto/aarch64-poky-linux") + endif() else() set(AMLNN_LIBRARY_DIR "${AMLNN_NNSDK_ROOT}/linux/yocto/aarch64-poky-linux") endif() diff --git a/examples/build-linux-all.sh b/examples/build-linux-all.sh new file mode 100755 index 0000000..e9df82f --- /dev/null +++ b/examples/build-linux-all.sh @@ -0,0 +1,209 @@ +#!/bin/bash + +# +# Copyright (C) 2024–2025 Amlogic, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +usage() { + echo "Usage: $0 [-m ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" + exit 1 +} + +# Default values +BUILD_MODE=linux +TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" + +# Parse arguments +while getopts 'm:b:s:t:h' opt; do + case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; + a) + TARGET_ARCH=$OPTARG + ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; + h) + usage + ;; + *) + usage + ;; + esac +done + +SCRIPT_DIR=$(cd "$(dirname $0)" && pwd) + +# --------------------------------------------------------------------------- +# AMLNN SDK discovery (same logic as build-android-all.sh) +# --------------------------------------------------------------------------- +if [ -n "$AMLNN_HOME" ]; then + if [ ! -d "$AMLNN_HOME/nn_runtime" ]; then + echo "Error: AMLNN_HOME is set to '$AMLNN_HOME' but nn_runtime was not found there." + echo "Please check your AMLNN_HOME path." + exit 1 + fi + echo "Priority 1: Using AMLNN_HOME from environment: $AMLNN_HOME" +elif [ -d "${SCRIPT_DIR}/../../amlnn-toolkit/nn_runtime" ]; then + export AMLNN_HOME="$(cd "${SCRIPT_DIR}/../../amlnn-toolkit" && pwd)" + echo "Priority 3: Using sibling amlnn-toolkit as fallback: $AMLNN_HOME" +elif [ -d "${SCRIPT_DIR}/../../amlnn-toolkit-a/nn_runtime" ]; then + export AMLNN_HOME="$(cd "${SCRIPT_DIR}/../../amlnn-toolkit-a" && pwd)" + echo "Priority 3: Using sibling amlnn-toolkit-a as fallback: $AMLNN_HOME" +else + echo "" + echo "Error: AMLNN SDK not found." + echo "" + echo "Please do one of the following:" + echo "" + echo " Option A (recommended) – set AMLNN_HOME:" + echo " export AMLNN_HOME=/path/to/amlnn-toolkit" + echo " ./build-linux-all.sh" + echo "" + echo " Option B – clone amlnn-toolkit as a sibling directory:" + echo " git clone https://github.com/Amlogic-NN/amlnn-toolkit.git ../../amlnn-toolkit" + echo " ./build-linux-all.sh" + echo "" + exit 1 +fi + +# --------------------------------------------------------------------------- +# Demos to exclude (directory name under examples/) +# --------------------------------------------------------------------------- +EXCLUDE_DIRS=("LLMs") + +is_excluded() { + local demo="$1" + for excl in "${EXCLUDE_DIRS[@]}"; do + if [[ "$demo" == *"${excl}"* ]]; then + return 0 + fi + done + return 1 +} + +echo "============================================" +echo "Building all Linux examples" +echo "BUILD_MODE: ${BUILD_MODE}" +if [[ "${BUILD_MODE}" == "yocto" ]]; then + echo "ARCH_BITS: ${ARCH_BITS}-bit" + echo "YOCTO_SDK_ROOT: ${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" +else + echo "TARGET_ARCH: ${TARGET_ARCH}" +fi +echo "EXCLUDED: ${EXCLUDE_DIRS[*]}" +echo "============================================" + +# Dynamically discover all examples that have a build-linux.sh +mapfile -t BUILD_SCRIPTS < <(find "${SCRIPT_DIR}" -mindepth 3 -maxdepth 3 -name "build-linux.sh" | sort) + +EXAMPLES=() +for script in "${BUILD_SCRIPTS[@]}"; do + rel=$(realpath --relative-to="${SCRIPT_DIR}" "$(dirname "$script")") + EXAMPLES+=("$rel") +done + +FAILED=() +SUCCEEDED=() +SKIPPED=() + +for EXAMPLE in "${EXAMPLES[@]}"; do + # Skip excluded demos + if is_excluded "${EXAMPLE}"; then + SKIPPED+=("${EXAMPLE}") + echo "" + echo "[SKIP] ${EXAMPLE} (excluded)" + continue + fi + + EXAMPLE_DIR="${SCRIPT_DIR}/${EXAMPLE}" + BUILD_SCRIPT="${EXAMPLE_DIR}/build-linux.sh" + + if [ ! -f "${BUILD_SCRIPT}" ]; then + echo "WARNING: No build-linux.sh found for ${EXAMPLE}, skipping." + continue + fi + + echo "" + echo "--------------------------------------------" + echo "Building: ${EXAMPLE}" + echo "--------------------------------------------" + + # Clean previous build to avoid stale CMake cache + if [[ "${BUILD_MODE}" == "yocto" ]]; then + echo "Cleaning: ${EXAMPLE_DIR}/build/yocto/${ARCH_BITS}" + rm -rf "${EXAMPLE_DIR}/build/yocto/${ARCH_BITS}" + else + echo "Cleaning: ${EXAMPLE_DIR}/build/linux" + rm -rf "${EXAMPLE_DIR}/build/linux" + fi + + # Build the extra args to forward to individual build-linux.sh + EXTRA_ARGS=(-m "${BUILD_MODE}") + if [[ "${BUILD_MODE}" == "yocto" ]]; then + EXTRA_ARGS+=(-b "${ARCH_BITS}") + [[ -n "${CLI_SDK_ROOT}" ]] && EXTRA_ARGS+=(-s "${CLI_SDK_ROOT}") + [[ -n "${CLI_TOOLCHAIN_FILE}" ]] && EXTRA_ARGS+=(-t "${CLI_TOOLCHAIN_FILE}") + else + EXTRA_ARGS+=(-a "${TARGET_ARCH}") + fi + + if bash "${BUILD_SCRIPT}" "${EXTRA_ARGS[@]}"; then + SUCCEEDED+=("${EXAMPLE}") + echo "[SUCCESS] ${EXAMPLE}" + else + FAILED+=("${EXAMPLE}") + echo "[FAILED] ${EXAMPLE}" + fi +done + +echo "" +echo "============================================" +echo "Build Summary" +echo "============================================" +echo "Succeeded (${#SUCCEEDED[@]}):" +for E in "${SUCCEEDED[@]}"; do echo " - $E"; done + +if [ ${#SKIPPED[@]} -gt 0 ]; then + echo "" + echo "Skipped (${#SKIPPED[@]}):" + for E in "${SKIPPED[@]}"; do echo " - $E"; done +fi + +if [ ${#FAILED[@]} -gt 0 ]; then + echo "" + echo "Failed (${#FAILED[@]}):" + for E in "${FAILED[@]}"; do echo " - $E"; done + exit 1 +fi + +echo "" +echo "All examples built successfully!" diff --git a/examples/clean-linux-all.sh b/examples/clean-linux-all.sh new file mode 100755 index 0000000..5d3fbdc --- /dev/null +++ b/examples/clean-linux-all.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +# +# Copyright (C) 2024–2025 Amlogic, Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +usage() { + echo "Usage: $0 [-m ]" + echo " -m : What to clean: 'linux', 'yocto', or 'all' (default: all)" + echo " -h : Show this help message" + exit 1 +} + +# Default values +CLEAN_MODE=all + +# Parse arguments +while getopts 'm:h' opt; do + case "$opt" in + m) + CLEAN_MODE=$OPTARG + ;; + h) + usage + ;; + *) + usage + ;; + esac +done + +SCRIPT_DIR=$(cd "$(dirname $0)" && pwd) + +case "${CLEAN_MODE}" in + linux) + echo "Cleaning all Linux build directories..." + find "${SCRIPT_DIR}" -type d -path "*/build/linux" | while read dir; do + echo " rm -rf ${dir}" + rm -rf "${dir}" + done + ;; + yocto) + echo "Cleaning all Yocto build directories..." + find "${SCRIPT_DIR}" -type d -path "*/build/yocto" | while read dir; do + echo " rm -rf ${dir}" + rm -rf "${dir}" + done + ;; + all) + echo "Cleaning all Linux and Yocto build directories..." + find "${SCRIPT_DIR}" -type d \( -path "*/build/linux" -o -path "*/build/yocto" \) | while read dir; do + echo " rm -rf ${dir}" + rm -rf "${dir}" + done + ;; + *) + echo "Unknown mode: ${CLEAN_MODE}" >&2 + usage + ;; +esac + +echo "Done." diff --git a/examples/cmake/yocto-toolchain.cmake b/examples/cmake/yocto-toolchain.cmake new file mode 100644 index 0000000..2c4e775 --- /dev/null +++ b/examples/cmake/yocto-toolchain.cmake @@ -0,0 +1,71 @@ +# Yocto cross-compilation toolchain +# +# Required cache variables (pass via -D): +# YOCTO_SDK_ROOT - Yocto SDK root dir (contains sysroots/) +# ARCH_BITS - 32 or 64 +# +# Example: +# cmake -DCMAKE_TOOLCHAIN_FILE=cmake/yocto-toolchain.cmake \ +# -DYOCTO_SDK_ROOT=/opt/poky/4.0.20 \ +# -DARCH_BITS=64 ... + +set(CMAKE_SYSTEM_NAME Linux) + +# Fallback to environment variables if not defined via -D +if(NOT DEFINED YOCTO_SDK_ROOT AND DEFINED ENV{YOCTO_SDK_ROOT}) + set(YOCTO_SDK_ROOT "$ENV{YOCTO_SDK_ROOT}") +endif() + +if(NOT DEFINED ARCH_BITS AND DEFINED ENV{ARCH_BITS}) + set(ARCH_BITS "$ENV{ARCH_BITS}") +endif() + +# Mandatory check +if(NOT DEFINED YOCTO_SDK_ROOT) + message(FATAL_ERROR "YOCTO_SDK_ROOT must point to the Yocto SDK root directory") +endif() + +if(NOT DEFINED ARCH_BITS) + message(FATAL_ERROR "ARCH_BITS must be set to 32 or 64") +endif() + +# Propagate these variables to try_compile sub-projects +list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES YOCTO_SDK_ROOT ARCH_BITS) + +set(_HOST_SYSROOT "${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux") +if(NOT IS_DIRECTORY "${_HOST_SYSROOT}") + message(FATAL_ERROR "Yocto host sysroot not found: ${_HOST_SYSROOT}") +endif() + +if(ARCH_BITS EQUAL 32) + set(_TARGET_SYSROOT "${YOCTO_SDK_ROOT}/sysroots/armv7at2hf-neon-poky-linux-gnueabi") + set(_TRIPLE "arm-poky-linux-gnueabi") + set(_ARCH_FLAGS "-march=armv7-a -marm -mfpu=vfp -mfloat-abi=hard") +elseif(ARCH_BITS EQUAL 64) + set(_TARGET_SYSROOT "${YOCTO_SDK_ROOT}/sysroots/armv8a-poky-linux") + set(_TRIPLE "aarch64-poky-linux") + set(_ARCH_FLAGS "") +else() + message(FATAL_ERROR "Unsupported ARCH_BITS: ${ARCH_BITS} (expected 32 or 64)") +endif() + +if(NOT IS_DIRECTORY "${_TARGET_SYSROOT}") + message(FATAL_ERROR "Yocto target sysroot not found: ${_TARGET_SYSROOT}") +endif() + +set(CMAKE_C_COMPILER "${_HOST_SYSROOT}/usr/bin/${_TRIPLE}/${_TRIPLE}-gcc") +set(CMAKE_CXX_COMPILER "${_HOST_SYSROOT}/usr/bin/${_TRIPLE}/${_TRIPLE}-g++") + +set(CMAKE_SYSROOT "${_TARGET_SYSROOT}") +set(CMAKE_C_FLAGS_INIT "--sysroot=${_TARGET_SYSROOT} ${_ARCH_FLAGS}") +set(CMAKE_CXX_FLAGS_INIT "--sysroot=${_TARGET_SYSROOT} ${_ARCH_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS_INIT "--sysroot=${_TARGET_SYSROOT}") +set(CMAKE_SHARED_LINKER_FLAGS_INIT "--sysroot=${_TARGET_SYSROOT}") + +set(CMAKE_FIND_ROOT_PATH "${_TARGET_SYSROOT}") +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) + +# Expose ARCH_BITS and triple for CMakeLists.txt to use +set(YOCTO_CROSS_TRIPLE "${_TRIPLE}" CACHE INTERNAL "") diff --git a/examples/ppocr-det/cpp/build-linux.sh b/examples/ppocr-det/cpp/build-linux.sh index 8b3d782..c962d1b 100755 --- a/examples/ppocr-det/cpp/build-linux.sh +++ b/examples/ppocr-det/cpp/build-linux.sh @@ -18,21 +18,41 @@ set -e # usage() { - echo "Usage: $0 [-a ]" - echo " -a : Target architecture (default: aarch64)" - echo " -h : Show this help message" + echo "Usage: $0 [-m ] [-a ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -a : Target arch for linux mode (default: aarch64)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" exit 1 } # Default values +BUILD_MODE=linux TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" # Parse arguments -while getopts 'a:h' opt; do +while getopts 'm:a:b:s:t:h' opt; do case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; a) TARGET_ARCH=$OPTARG ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; h) usage ;; @@ -42,6 +62,77 @@ while getopts 'a:h' opt; do esac done +ROOT_PWD=$(cd "$(dirname $0)" && pwd) + +# =========================================================================== +# Yocto build +# =========================================================================== +if [[ "${BUILD_MODE}" == "yocto" ]]; then + + if [[ "${ARCH_BITS}" != "32" && "${ARCH_BITS}" != "64" ]]; then + echo "Unsupported ARCH_BITS \"${ARCH_BITS}\". Must be 32 or 64." >&2 + exit 1 + fi + + # Configurable via environment variables (CLI args > env vars > defaults) + CMAKE_BIN="${CMAKE_BIN:-cmake}" + YOCTO_SDK_ROOT="${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" + TOOLCHAIN_FILE="${CLI_TOOLCHAIN_FILE:-${TOOLCHAIN_FILE:-${ROOT_PWD}/../../cmake/yocto-toolchain.cmake}}" + + # Export variables for CMake + export YOCTO_SDK_ROOT + export ARCH_BITS + + BUILD_DIR="${ROOT_PWD}/build/yocto/${ARCH_BITS}" + + echo "==> Building Yocto ${ARCH_BITS}-bit" + echo " toolchain : ${TOOLCHAIN_FILE}" + echo " SDK root : ${YOCTO_SDK_ROOT}" + echo " BUILD_DIR : ${BUILD_DIR}" + + mkdir -p "${BUILD_DIR}" + rm -rf "${BUILD_DIR}" + + # Select OpenCV based on target architecture + if [[ "${ARCH_BITS}" == "32" ]]; then + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-armhf/share/OpenCV" + else + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV" + fi + + "${CMAKE_BIN}" \ + -S "${ROOT_PWD}/src" \ + -B "${BUILD_DIR}" \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ + -DYOCTO_SDK_ROOT="${YOCTO_SDK_ROOT}" \ + -DARCH_BITS="${ARCH_BITS}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DOpenCV_DIR="${OPENCV_DIR}" + + "${CMAKE_BIN}" --build "${BUILD_DIR}" --config Release + + # Strip (best-effort) + HOST_SYSROOT="${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux" + if [[ "${ARCH_BITS}" == "32" ]]; then + CROSS_TRIPLE="arm-poky-linux-gnueabi" + else + CROSS_TRIPLE="aarch64-poky-linux" + fi + STRIP_TOOL="${HOST_SYSROOT}/usr/bin/${CROSS_TRIPLE}/${CROSS_TRIPLE}-strip" + if [[ -x "${STRIP_TOOL}" ]]; then + "${STRIP_TOOL}" --strip-unneeded "${BUILD_DIR}/paddleocr_det_demo" + else + echo "warning: strip tool not found; keeping debug info." >&2 + fi + + echo "Build complete. Executable in ${BUILD_DIR}/paddleocr_det_demo" + exit 0 +fi + +# =========================================================================== +# Standard Linux cross-compile build +# =========================================================================== + # Default to aarch64-linux-gnu if GCC_COMPILER is not set GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu} @@ -57,7 +148,6 @@ if ! command -v ${CC} &> /dev/null; then exit 1 fi -ROOT_PWD=$(cd "$(dirname $0)" && pwd) BUILD_DIR=${ROOT_PWD}/build/linux echo "Building for Linux..." diff --git a/examples/yoloe/cpp/build-linux.sh b/examples/yoloe/cpp/build-linux.sh index 71cb490..5a81059 100755 --- a/examples/yoloe/cpp/build-linux.sh +++ b/examples/yoloe/cpp/build-linux.sh @@ -18,21 +18,41 @@ set -e # usage() { - echo "Usage: $0 [-a ]" - echo " -a : Target architecture (default: aarch64)" - echo " -h : Show this help message" + echo "Usage: $0 [-m ] [-a ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -a : Target arch for linux mode (default: aarch64)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" exit 1 } # Default values +BUILD_MODE=linux TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" # Parse arguments -while getopts 'a:h' opt; do +while getopts 'm:a:b:s:t:h' opt; do case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; a) TARGET_ARCH=$OPTARG ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; h) usage ;; @@ -42,6 +62,77 @@ while getopts 'a:h' opt; do esac done +ROOT_PWD=$(cd "$(dirname $0)" && pwd) + +# =========================================================================== +# Yocto build +# =========================================================================== +if [[ "${BUILD_MODE}" == "yocto" ]]; then + + if [[ "${ARCH_BITS}" != "32" && "${ARCH_BITS}" != "64" ]]; then + echo "Unsupported ARCH_BITS \"${ARCH_BITS}\". Must be 32 or 64." >&2 + exit 1 + fi + + # Configurable via environment variables (CLI args > env vars > defaults) + CMAKE_BIN="${CMAKE_BIN:-cmake}" + YOCTO_SDK_ROOT="${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" + TOOLCHAIN_FILE="${CLI_TOOLCHAIN_FILE:-${TOOLCHAIN_FILE:-${ROOT_PWD}/../../cmake/yocto-toolchain.cmake}}" + + # Export variables for CMake + export YOCTO_SDK_ROOT + export ARCH_BITS + + BUILD_DIR="${ROOT_PWD}/build/yocto/${ARCH_BITS}" + + echo "==> Building Yocto ${ARCH_BITS}-bit" + echo " toolchain : ${TOOLCHAIN_FILE}" + echo " SDK root : ${YOCTO_SDK_ROOT}" + echo " BUILD_DIR : ${BUILD_DIR}" + + mkdir -p "${BUILD_DIR}" + rm -rf "${BUILD_DIR}" + + # Select OpenCV based on target architecture + if [[ "${ARCH_BITS}" == "32" ]]; then + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-armhf/share/OpenCV" + else + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV" + fi + + "${CMAKE_BIN}" \ + -S "${ROOT_PWD}/src" \ + -B "${BUILD_DIR}" \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ + -DYOCTO_SDK_ROOT="${YOCTO_SDK_ROOT}" \ + -DARCH_BITS="${ARCH_BITS}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DOpenCV_DIR="${OPENCV_DIR}" + + "${CMAKE_BIN}" --build "${BUILD_DIR}" --config Release + + # Strip (best-effort) + HOST_SYSROOT="${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux" + if [[ "${ARCH_BITS}" == "32" ]]; then + CROSS_TRIPLE="arm-poky-linux-gnueabi" + else + CROSS_TRIPLE="aarch64-poky-linux" + fi + STRIP_TOOL="${HOST_SYSROOT}/usr/bin/${CROSS_TRIPLE}/${CROSS_TRIPLE}-strip" + if [[ -x "${STRIP_TOOL}" ]]; then + "${STRIP_TOOL}" --strip-unneeded "${BUILD_DIR}/yoloe_demo" + else + echo "warning: strip tool not found; keeping debug info." >&2 + fi + + echo "Build complete. Executable in ${BUILD_DIR}/yoloe_demo" + exit 0 +fi + +# =========================================================================== +# Standard Linux cross-compile build +# =========================================================================== + # Default to aarch64-linux-gnu if GCC_COMPILER is not set GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu} @@ -57,7 +148,6 @@ if ! command -v ${CC} &> /dev/null; then exit 1 fi -ROOT_PWD=$(cd "$(dirname $0)" && pwd) BUILD_DIR=${ROOT_PWD}/build/linux echo "Building for Linux..." diff --git a/examples/yolov8/cpp/build-linux.sh b/examples/yolov8/cpp/build-linux.sh index 4e62922..25320b2 100755 --- a/examples/yolov8/cpp/build-linux.sh +++ b/examples/yolov8/cpp/build-linux.sh @@ -18,21 +18,41 @@ set -e # usage() { - echo "Usage: $0 [-a ]" - echo " -a : Target architecture (default: aarch64)" - echo " -h : Show this help message" + echo "Usage: $0 [-m ] [-a ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -a : Target arch for linux mode (default: aarch64)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" exit 1 } # Default values +BUILD_MODE=linux TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" # Parse arguments -while getopts 'a:h' opt; do +while getopts 'm:a:b:s:t:h' opt; do case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; a) TARGET_ARCH=$OPTARG ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; h) usage ;; @@ -42,6 +62,77 @@ while getopts 'a:h' opt; do esac done +ROOT_PWD=$(cd "$(dirname $0)" && pwd) + +# =========================================================================== +# Yocto build +# =========================================================================== +if [[ "${BUILD_MODE}" == "yocto" ]]; then + + if [[ "${ARCH_BITS}" != "32" && "${ARCH_BITS}" != "64" ]]; then + echo "Unsupported ARCH_BITS \"${ARCH_BITS}\". Must be 32 or 64." >&2 + exit 1 + fi + + # Configurable via environment variables (CLI args > env vars > defaults) + CMAKE_BIN="${CMAKE_BIN:-cmake}" + YOCTO_SDK_ROOT="${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" + TOOLCHAIN_FILE="${CLI_TOOLCHAIN_FILE:-${TOOLCHAIN_FILE:-${ROOT_PWD}/../../cmake/yocto-toolchain.cmake}}" + + # Export variables for CMake + export YOCTO_SDK_ROOT + export ARCH_BITS + + BUILD_DIR="${ROOT_PWD}/build/yocto/${ARCH_BITS}" + + # Select OpenCV based on target architecture + if [[ "${ARCH_BITS}" == "32" ]]; then + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-armhf/share/OpenCV" + else + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV" + fi + + echo "==> Building Yocto ${ARCH_BITS}-bit" + echo " toolchain : ${TOOLCHAIN_FILE}" + echo " SDK root : ${YOCTO_SDK_ROOT}" + echo " BUILD_DIR : ${BUILD_DIR}" + + mkdir -p "${BUILD_DIR}" + rm -rf "${BUILD_DIR}" + + "${CMAKE_BIN}" \ + -S "${ROOT_PWD}/src" \ + -B "${BUILD_DIR}" \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ + -DYOCTO_SDK_ROOT="${YOCTO_SDK_ROOT}" \ + -DARCH_BITS="${ARCH_BITS}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DOpenCV_DIR="${OPENCV_DIR}" + + "${CMAKE_BIN}" --build "${BUILD_DIR}" --config Release + + # Strip (best-effort) + HOST_SYSROOT="${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux" + if [[ "${ARCH_BITS}" == "32" ]]; then + CROSS_TRIPLE="arm-poky-linux-gnueabi" + else + CROSS_TRIPLE="aarch64-poky-linux" + fi + STRIP_TOOL="${HOST_SYSROOT}/usr/bin/${CROSS_TRIPLE}/${CROSS_TRIPLE}-strip" + if [[ -x "${STRIP_TOOL}" ]]; then + "${STRIP_TOOL}" --strip-unneeded "${BUILD_DIR}/yolov8_demo" + else + echo "warning: strip tool not found; keeping debug info." >&2 + fi + + echo "Build complete. Executable in ${BUILD_DIR}/yolov8_demo" + exit 0 +fi + +# =========================================================================== +# Standard Linux cross-compile build +# =========================================================================== + # Default to aarch64-linux-gnu if GCC_COMPILER is not set GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu} @@ -65,14 +156,6 @@ fi ROOT_PWD=$(cd "$(dirname $0)" && pwd) BUILD_DIR=${ROOT_PWD}/build/linux -echo "Building for Linux..." -echo "COMPILER: ${CC}" -echo "TARGET_ARCH: ${TARGET_ARCH}" -echo "BUILD_DIR: ${BUILD_DIR}" - -mkdir -p ${BUILD_DIR} -cd ${BUILD_DIR} - # Set OpenCV path if [ "${TARGET_ARCH}" == "aarch64" ]; then OPENCV_DIR_NAME="opencv-linux-aarch64" @@ -82,11 +165,18 @@ else echo "Warning: No OpenCV prebuilt for architecture ${TARGET_ARCH}" fi +echo "Building for Linux..." +echo "COMPILER: ${CC}" +echo "TARGET_ARCH: ${TARGET_ARCH}" +echo "BUILD_DIR: ${BUILD_DIR}" + +mkdir -p ${BUILD_DIR} +cd ${BUILD_DIR} + cmake ../../src \ -DCMAKE_SYSTEM_NAME=Linux \ -DCMAKE_SYSTEM_PROCESSOR=${TARGET_ARCH} \ -DCMAKE_BUILD_TYPE=Release \ - -DNNSDK_DIR=${ROOT_PWD}/../../../../amlnn-toolkit/nn_runtime/nnsdk \ -DOpenCV_DIR=${ROOT_PWD}/../../../dependency/opencv/${OPENCV_DIR_NAME}/share/OpenCV make -j4 diff --git a/examples/yoloworld/cpp/build-linux.sh b/examples/yoloworld/cpp/build-linux.sh index 85175a0..a52d4aa 100755 --- a/examples/yoloworld/cpp/build-linux.sh +++ b/examples/yoloworld/cpp/build-linux.sh @@ -18,21 +18,41 @@ set -e # usage() { - echo "Usage: $0 [-a ]" - echo " -a : Target architecture (default: aarch64)" - echo " -h : Show this help message" + echo "Usage: $0 [-m ] [-a ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -a : Target arch for linux mode (default: aarch64)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" exit 1 } # Default values +BUILD_MODE=linux TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" # Parse arguments -while getopts 'a:h' opt; do +while getopts 'm:a:b:s:t:h' opt; do case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; a) TARGET_ARCH=$OPTARG ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; h) usage ;; @@ -42,6 +62,77 @@ while getopts 'a:h' opt; do esac done +ROOT_PWD=$(cd "$(dirname $0)" && pwd) + +# =========================================================================== +# Yocto build +# =========================================================================== +if [[ "${BUILD_MODE}" == "yocto" ]]; then + + if [[ "${ARCH_BITS}" != "32" && "${ARCH_BITS}" != "64" ]]; then + echo "Unsupported ARCH_BITS \"${ARCH_BITS}\". Must be 32 or 64." >&2 + exit 1 + fi + + # Configurable via environment variables (CLI args > env vars > defaults) + CMAKE_BIN="${CMAKE_BIN:-cmake}" + YOCTO_SDK_ROOT="${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" + TOOLCHAIN_FILE="${CLI_TOOLCHAIN_FILE:-${TOOLCHAIN_FILE:-${ROOT_PWD}/../../cmake/yocto-toolchain.cmake}}" + + # Export variables for CMake + export YOCTO_SDK_ROOT + export ARCH_BITS + + BUILD_DIR="${ROOT_PWD}/build/yocto/${ARCH_BITS}" + + echo "==> Building Yocto ${ARCH_BITS}-bit" + echo " toolchain : ${TOOLCHAIN_FILE}" + echo " SDK root : ${YOCTO_SDK_ROOT}" + echo " BUILD_DIR : ${BUILD_DIR}" + + mkdir -p "${BUILD_DIR}" + rm -rf "${BUILD_DIR}" + + # Select OpenCV based on target architecture + if [[ "${ARCH_BITS}" == "32" ]]; then + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-armhf/share/OpenCV" + else + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV" + fi + + "${CMAKE_BIN}" \ + -S "${ROOT_PWD}/src" \ + -B "${BUILD_DIR}" \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ + -DYOCTO_SDK_ROOT="${YOCTO_SDK_ROOT}" \ + -DARCH_BITS="${ARCH_BITS}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DOpenCV_DIR="${OPENCV_DIR}" + + "${CMAKE_BIN}" --build "${BUILD_DIR}" --config Release + + # Strip (best-effort) + HOST_SYSROOT="${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux" + if [[ "${ARCH_BITS}" == "32" ]]; then + CROSS_TRIPLE="arm-poky-linux-gnueabi" + else + CROSS_TRIPLE="aarch64-poky-linux" + fi + STRIP_TOOL="${HOST_SYSROOT}/usr/bin/${CROSS_TRIPLE}/${CROSS_TRIPLE}-strip" + if [[ -x "${STRIP_TOOL}" ]]; then + "${STRIP_TOOL}" --strip-unneeded "${BUILD_DIR}/yolo_world_demo" + else + echo "warning: strip tool not found; keeping debug info." >&2 + fi + + echo "Build complete. Executable in ${BUILD_DIR}/yolo_world_demo" + exit 0 +fi + +# =========================================================================== +# Standard Linux cross-compile build +# =========================================================================== + # Default to aarch64-linux-gnu if GCC_COMPILER is not set GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu} @@ -57,7 +148,6 @@ if ! command -v ${CC} &> /dev/null; then exit 1 fi -ROOT_PWD=$(cd "$(dirname $0)" && pwd) BUILD_DIR=${ROOT_PWD}/build/linux echo "Building for Linux..." diff --git a/examples/yolox/cpp/build-linux.sh b/examples/yolox/cpp/build-linux.sh index 317420a..6463fcd 100755 --- a/examples/yolox/cpp/build-linux.sh +++ b/examples/yolox/cpp/build-linux.sh @@ -1,23 +1,42 @@ -#TODO #!/bin/bash set -e usage() { - echo "Usage: $0 [-a ]" - echo " -a : Target architecture (default: aarch64)" - echo " -h : Show this help message" + echo "Usage: $0 [-m ] [-a ] [-b ] [-s ] [-t ]" + echo " -m : Build mode: 'linux' or 'yocto' (default: linux)" + echo " -a : Target arch for linux mode (default: aarch64)" + echo " -b : Arch bits for yocto mode: 32 or 64 (default: 64)" + echo " -s : Yocto SDK root path (overrides YOCTO_SDK_ROOT env var)" + echo " -t : CMake toolchain file (overrides TOOLCHAIN_FILE env var)" + echo " -h : Show this help message" exit 1 } # Default values +BUILD_MODE=linux TARGET_ARCH=aarch64 +ARCH_BITS=64 +CLI_SDK_ROOT="" +CLI_TOOLCHAIN_FILE="" # Parse arguments -while getopts 'a:h' opt; do +while getopts 'm:a:b:s:t:h' opt; do case "$opt" in + m) + BUILD_MODE=$OPTARG + ;; a) TARGET_ARCH=$OPTARG ;; + b) + ARCH_BITS=$OPTARG + ;; + s) + CLI_SDK_ROOT=$OPTARG + ;; + t) + CLI_TOOLCHAIN_FILE=$OPTARG + ;; h) usage ;; @@ -27,6 +46,77 @@ while getopts 'a:h' opt; do esac done +ROOT_PWD=$(cd "$(dirname $0)" && pwd) + +# =========================================================================== +# Yocto build +# =========================================================================== +if [[ "${BUILD_MODE}" == "yocto" ]]; then + + if [[ "${ARCH_BITS}" != "32" && "${ARCH_BITS}" != "64" ]]; then + echo "Unsupported ARCH_BITS \"${ARCH_BITS}\". Must be 32 or 64." >&2 + exit 1 + fi + + # Configurable via environment variables (CLI args > env vars > defaults) + CMAKE_BIN="${CMAKE_BIN:-cmake}" + YOCTO_SDK_ROOT="${CLI_SDK_ROOT:-${YOCTO_SDK_ROOT:-/data/yuandian/tools/poky/4.0.20}}" + TOOLCHAIN_FILE="${CLI_TOOLCHAIN_FILE:-${TOOLCHAIN_FILE:-${ROOT_PWD}/../../cmake/yocto-toolchain.cmake}}" + + # Export variables for CMake (important for try_compile and toolchain fallbacks) + export YOCTO_SDK_ROOT + export ARCH_BITS + + BUILD_DIR="${ROOT_PWD}/build/yocto/${ARCH_BITS}" + + echo "==> Building Yocto ${ARCH_BITS}-bit" + echo " toolchain : ${TOOLCHAIN_FILE}" + echo " SDK root : ${YOCTO_SDK_ROOT}" + echo " BUILD_DIR : ${BUILD_DIR}" + + mkdir -p "${BUILD_DIR}" + rm -rf "${BUILD_DIR}" + + # Select OpenCV based on target architecture + if [[ "${ARCH_BITS}" == "32" ]]; then + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-armhf/share/OpenCV" + else + OPENCV_DIR="${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV" + fi + + "${CMAKE_BIN}" \ + -S "${ROOT_PWD}/src" \ + -B "${BUILD_DIR}" \ + -DCMAKE_TOOLCHAIN_FILE="${TOOLCHAIN_FILE}" \ + -DYOCTO_SDK_ROOT="${YOCTO_SDK_ROOT}" \ + -DARCH_BITS="${ARCH_BITS}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DOpenCV_DIR="${OPENCV_DIR}" + + "${CMAKE_BIN}" --build "${BUILD_DIR}" --config Release + + # Strip (best-effort) + HOST_SYSROOT="${YOCTO_SDK_ROOT}/sysroots/x86_64-pokysdk-linux" + if [[ "${ARCH_BITS}" == "32" ]]; then + CROSS_TRIPLE="arm-poky-linux-gnueabi" + else + CROSS_TRIPLE="aarch64-poky-linux" + fi + STRIP_TOOL="${HOST_SYSROOT}/usr/bin/${CROSS_TRIPLE}/${CROSS_TRIPLE}-strip" + if [[ -x "${STRIP_TOOL}" ]]; then + "${STRIP_TOOL}" --strip-unneeded "${BUILD_DIR}/yolox_demo" + else + echo "warning: strip tool not found; keeping debug info." >&2 + fi + + echo "Build complete. Executable in ${BUILD_DIR}/yolox_demo" + exit 0 +fi + +# =========================================================================== +# Standard Linux cross-compile build +# =========================================================================== + # Default to aarch64-linux-gnu if GCC_COMPILER is not set GCC_COMPILER=${GCC_COMPILER:-aarch64-linux-gnu} @@ -44,7 +134,6 @@ else echo "Using compiler: ${CC}" fi -ROOT_PWD=$(cd "$(dirname $0)" && pwd) BUILD_DIR=${ROOT_PWD}/build/linux echo "Building for Linux..." @@ -62,4 +151,4 @@ cmake ../../src \ -DOpenCV_DIR=${ROOT_PWD}/../../../dependency/opencv/opencv-linux-aarch64/share/OpenCV make -j4 -echo "Build complete. Executable in ${BUILD_DIR}/yolo11_demo" +echo "Build complete. Executable in ${BUILD_DIR}/yolox_demo"