Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions build_scripts/berry_prebuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@echo off
setlocal EnableDelayedExpansion

:: ========================================================
:: OpenBeken Berry prebuild script
:: Can be called from any directory - resolves repo root
:: from the script's own location.
:: ========================================================

:: Resolve repo root (one level up from build_scripts)
set "REPO_ROOT=%~dp0.."
pushd "!REPO_ROOT!"
set "REPO_ROOT=%CD%"
popd

echo [INFO] Running Berry prebuild...
echo [INFO] Repo root: !REPO_ROOT!

set PYTHON_CMD=
where python >nul 2>nul
if %errorlevel% equ 0 (
set "PYTHON_CMD=python"
) else (
where python3 >nul 2>nul
if !errorlevel! equ 0 (
set "PYTHON_CMD=python3"
)
)

if "%PYTHON_CMD%"=="" (
echo [WARNING] Neither 'python' nor 'python3' was found in PATH!
echo [WARNING] Berry prebuild step will be skipped. Please install Python to generate Berry bindings.
exit /b 0
)

echo [INFO] Using Python command: !PYTHON_CMD!

if not exist "!REPO_ROOT!\libraries\berry\generate" (
echo [INFO] Creating libraries\berry\generate directory...
mkdir "!REPO_ROOT!\libraries\berry\generate"
)

echo [INFO] Executing Berry C-Object-Compiler (coc)...
!PYTHON_CMD! "!REPO_ROOT!\libraries\berry\tools\coc\coc" -o "!REPO_ROOT!\libraries\berry\generate" "!REPO_ROOT!\libraries\berry\src" "!REPO_ROOT!\src\berry\modules" -c "!REPO_ROOT!\include\berry_conf.h"

if !errorlevel! neq 0 (
echo [ERROR] Berry prebuild failed with exit code !errorlevel!!
exit /b !errorlevel!
)

echo [INFO] Berry prebuild completed successfully.
exit /b 0
185 changes: 185 additions & 0 deletions build_scripts/build_bk7231n.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
@echo off
setlocal EnableDelayedExpansion
pushd "%~dp0\.."

:: ========================================================
:: OpenBeken BK7231N Windows build script
:: Uses Git Bash + bundled ARM GCC toolchain + Windows
:: packaging tools that are all already in the SDK.
:: ========================================================

set APP_NAME=OpenBK7231N_App
set APP_VERSION=dev_bk7231n
set SDK_DIR=sdk\OpenBK7231N
set OBK_VARIANT=2
set ACTION=build

:: Allow overriding version from command line
if not "%~1"=="" set APP_VERSION=%~1
if not "%~2"=="" set ACTION=%~2

echo ==============================================
echo Building OpenBeken for BK7231N on Windows
echo App Name: %APP_NAME%
echo App Version: %APP_VERSION%
echo Variant: %OBK_VARIANT%
echo SDK Dir: %SDK_DIR%
echo Action: %ACTION%
echo ==============================================

:: --- Check prerequisites ---

:: 1. Git Bash
set GIT_BASH=
for %%P in (
"C:\Program Files\Git\bin\bash.exe"
"C:\Program Files (x86)\Git\bin\bash.exe"
) do (
if exist %%P (
set "GIT_BASH=%%~P"
goto :found_bash
)
)
echo [ERROR] Git Bash not found! Please install Git for Windows.
exit /b 1

:found_bash
echo [INFO] Using Git Bash: %GIT_BASH%

:: 2. Check that SDK submodule is checked out
if not exist "%SDK_DIR%\platforms\BK7231N\BK7231N_os\build.sh" (
echo [INFO] SDK submodule not found, checking out...
git submodule update --init --recursive --depth=1 %SDK_DIR%
if !errorlevel! neq 0 (
echo [ERROR] Failed to checkout SDK submodule!
exit /b 1
)
)
echo [INFO] SDK submodule OK.

:: 2b. Check that Berry submodule is checked out
if not exist "libraries\berry\src\be_api.c" (
echo [INFO] Berry submodule not found, checking out...
git submodule update --init --depth=1 libraries\berry
if !errorlevel! neq 0 (
echo [ERROR] Failed to checkout Berry submodule!
exit /b 1
)
)
echo [INFO] Berry submodule OK.

:: 3. Check that Windows ARM GCC toolchain exists
if not exist "%SDK_DIR%\platforms\BK7231N\toolchain\windows\gcc-arm-none-eabi-4_9-2015q1\bin\arm-none-eabi-gcc.exe" (
echo [ERROR] Windows ARM GCC toolchain not found in SDK!
echo Expected at: %SDK_DIR%\platforms\BK7231N\toolchain\windows\gcc-arm-none-eabi-4_9-2015q1\bin\
exit /b 1
)
echo [INFO] ARM GCC toolchain OK.

:: 4. Ensure our app symlink/junction exists in SDK apps folder
if not exist "%SDK_DIR%\apps\%APP_NAME%" (
echo [INFO] Creating junction for app in SDK apps folder...
mklink /J "%SDK_DIR%\apps\%APP_NAME%" "%CD%"
if !errorlevel! neq 0 (
echo [ERROR] Failed to create junction. Try running as Administrator.
exit /b 1
)
)
echo [INFO] App link OK.

:: 5. Ensure mbedtls is downloaded (needed by the top-level Makefile)
if not exist "output\mbedtls-2.28.5" (
echo [INFO] Downloading mbedtls...
"%GIT_BASH%" -c "mkdir -p output && cd output && curl -sL https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/v2.28.5.tar.gz -o v2.28.5.tar.gz && tar -xf v2.28.5.tar.gz && rm -f v2.28.5.tar.gz && mv mbedtls-2.28.5/library/base64.c mbedtls-2.28.5/library/base64_mbedtls.c"
if !errorlevel! neq 0 (
echo [WARN] mbedtls download may have failed, build might still work.
)
)

:: 6. Ensure output directory exists
if not exist "output\%APP_VERSION%" mkdir "output\%APP_VERSION%"


:: --- Berry prebuild ---
call build_scripts\berry_prebuild.bat

:: --- Build via Git Bash ---
:: The build.sh and application.mk already handle Windows vs Linux toolchain paths.
:: The trick is: application.mk uses `uname` to decide toolchain dir, and build.sh
:: uses `uname` to pick the Windows package tools.
:: Under Git Bash, uname returns MINGW64_NT-... which is NOT "Linux", so it picks
:: the Windows paths. We just need to provide `make` to Git Bash.

echo [INFO] Starting %ACTION% via Git Bash...
echo.

:: Run the inner build script, passing parameters as arguments.
:: This avoids the fragile inline echo approach that loses $variables.
"%GIT_BASH%" --login -i "%CD%\build_scripts\build_bk7231n_inner.sh" "%APP_NAME%" "%APP_VERSION%" "%SDK_DIR%" "%OBK_VARIANT%" "%ACTION%"
set BUILD_RESULT=%errorlevel%

if %BUILD_RESULT% neq 0 (
echo.
echo [ERROR] BK7231N build failed with exit code %BUILD_RESULT%!
echo.
echo [INFO] Checking for partial build artifacts...
echo [INFO] Looking in: %SDK_DIR%\platforms\BK7231N\BK7231N_os\tools\generate\
if exist "%SDK_DIR%\platforms\BK7231N\BK7231N_os\tools\generate" (
dir /b "%SDK_DIR%\platforms\BK7231N\BK7231N_os\tools\generate\*.bin" 2>nul
dir /b "%SDK_DIR%\platforms\BK7231N\BK7231N_os\tools\generate\*.rbl" 2>nul
)
echo.
echo [INFO] Checking output dir: output\%APP_VERSION%\
if exist "output\%APP_VERSION%" (
dir /b "output\%APP_VERSION%\*.bin" 2>nul
dir /b "output\%APP_VERSION%\*.rbl" 2>nul
dir /b "output\%APP_VERSION%\*.axf" 2>nul
) else (
echo [INFO] Output dir does not exist yet.
)
exit /b %BUILD_RESULT%
)

:: --- Copy output ---
echo.
echo [INFO] Checking output files...
set GEN_DIR=%SDK_DIR%\platforms\BK7231N\BK7231N_os\tools\generate

set COPIED_COUNT=0
if exist "%GEN_DIR%\%APP_NAME%_%APP_VERSION%.rbl" (
copy /y "%GEN_DIR%\%APP_NAME%_%APP_VERSION%.rbl" "output\%APP_VERSION%\" >nul 2>nul
echo [INFO] Copied: %APP_NAME%_%APP_VERSION%.rbl
set /a COPIED_COUNT+=1
)
if exist "%GEN_DIR%\%APP_NAME%_UG_%APP_VERSION%.bin" (
copy /y "%GEN_DIR%\%APP_NAME%_UG_%APP_VERSION%.bin" "output\%APP_VERSION%\" >nul 2>nul
echo [INFO] Copied: %APP_NAME%_UG_%APP_VERSION%.bin
set /a COPIED_COUNT+=1
)
if exist "%GEN_DIR%\%APP_NAME%_UA_%APP_VERSION%.bin" (
copy /y "%GEN_DIR%\%APP_NAME%_UA_%APP_VERSION%.bin" "output\%APP_VERSION%\" >nul 2>nul
echo [INFO] Copied: %APP_NAME%_UA_%APP_VERSION%.bin
set /a COPIED_COUNT+=1
)
if exist "%GEN_DIR%\%APP_NAME%_QIO_%APP_VERSION%.bin" (
copy /y "%GEN_DIR%\%APP_NAME%_QIO_%APP_VERSION%.bin" "output\%APP_VERSION%\" >nul 2>nul
echo [INFO] Copied: %APP_NAME%_QIO_%APP_VERSION%.bin
set /a COPIED_COUNT+=1
)

echo.
echo ==============================================
echo BUILD SUCCESSFUL! (%COPIED_COUNT% files copied)
echo ==============================================
echo.
echo Output directory:
echo %CD%\output\%APP_VERSION%\
echo.
echo Build files:
echo ----------------------------------------------
for %%F in (output\%APP_VERSION%\*.bin output\%APP_VERSION%\*.rbl) do (
echo %%~nxF (%%~zF bytes)
)
echo ==============================================
exit /b 0

73 changes: 73 additions & 0 deletions build_scripts/build_bk7231n_inner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash
# Inner build script called by build_BK7231N.bat
# Arguments: APP_NAME APP_VERSION SDK_DIR OBK_VARIANT
set -e

APP_NAME="$1"
APP_VERSION="$2"
SDK_DIR="$3"
export OBK_VARIANT="$4"

echo "[INFO] Inner build script started"
echo "[INFO] APP_NAME=$APP_NAME"
echo "[INFO] APP_VERSION=$APP_VERSION"
echo "[INFO] SDK_DIR=$SDK_DIR"
echo "[INFO] OBK_VARIANT=$OBK_VARIANT"

# First, check if make is available
if type make > /dev/null 2>&1; then
echo "[INFO] GNU make is available."
else
echo "[INFO] GNU make not found in Git Bash. Attempting to get it..."
MAKE_FOUND=0
for MAKE_PATH in /c/ProgramData/chocolatey/bin/make.exe /c/msys64/usr/bin/make.exe /w/TOOLS/msys64/usr/bin/make.exe; do
if [ -f "$MAKE_PATH" ]; then
export PATH="$(dirname "$MAKE_PATH"):$PATH"
MAKE_FOUND=1
echo "[INFO] Found make at $MAKE_PATH"
break
fi
done
if [ $MAKE_FOUND -eq 0 ]; then
echo "[ERROR] GNU make not found!"
echo "Please install make via one of:"
echo " choco install make"
echo " pacman -S make (in MSYS2)"
echo " Or download from https://gnuwin32.sourceforge.net/packages/make.htm"
exit 1
fi
fi

echo "[INFO] make found: $(which make)"
echo "[INFO] make version: $(make --version | head -1)"

SCRIPT_DIR="$(pwd)"

# Set a localized TEMP dir to avoid PyInstaller extraction permission errors
# beken_packager.exe is a PyInstaller bundle that needs to extract VCRUNTIME140.dll
LOCAL_TMP="$(cygpath -m "$SCRIPT_DIR/output/tmp")"
echo "[INFO] Setting local TEMP dir: $LOCAL_TMP"
mkdir -p "$SCRIPT_DIR/output/tmp"
export TEMP="$LOCAL_TMP"
export TMP="$LOCAL_TMP"
export TMPDIR="$LOCAL_TMP"
echo "[INFO] TEMP=$TEMP"
echo "[INFO] TMP=$TMP"
echo "[INFO] TMPDIR=$TMPDIR"

ACTION="$5"

# Navigate to the build system directory
cd "$SDK_DIR/platforms/BK7231N/BK7231N_os"

if [ "$ACTION" = "clean" ]; then
echo "[INFO] Running BK7231N clean from $(pwd)..."
make APP_BIN_NAME="$APP_NAME" USER_SW_VER="$(echo $APP_VERSION | cut -d'-' -f1)" APP_VERSION="$APP_VERSION" clean -C ./
echo "[INFO] Clean complete!"
exit 0
fi

echo "[INFO] Running BK7231N build from $(pwd)..."
bash build.sh "$APP_NAME" "$APP_VERSION" bk7231n "OBK_VARIANT=$OBK_VARIANT TARGET_PLATFORM=bk7231n OBK_DIR=../../../apps/$APP_NAME BERRY_SRCPATH=../../../apps/$APP_NAME/libraries/berry/src BERRY_MODULEPATH=../../../apps/$APP_NAME/src/berry/modules"

echo "[INFO] Build complete!"
Loading
Loading