55# SPDX-License-Identifier: Apache-2.0
66
77# gap9-build_sdk.sh
8- # Helper script to clone, patch and build the GAP9 SDK. Intended to be
9- # invoked from the Makefile with environment variables set:
10- # GAP9_SDK_INSTALL_DIR (required)
11- # GAP9_SDK_COMMIT_HASH (optional, fallback provided)
12- # ROOT_DIR (optional, defaults to script dir)
8+ #
9+ # Clone, prepare, patch, and build a GAP9 SDK checkout in place.
10+ # This script is intended to be called from the project Makefile.
11+ #
12+ # Environment variables:
13+ # GAP9_SDK_INSTALL_DIR Required. Target directory for the SDK checkout/build.
14+ # GAP9_SDK_COMMIT_HASH Required. Commit to checkout.
15+ # GAP_SDK_URL Required. Git remote used for fetching the SDK.
16+ # ROOT_DIR Optional. Base directory for patch lookup.
17+ # Default: directory of this script.
1318
1419ROOT_DIR=" ${ROOT_DIR:- $(cd " $( dirname " $0 " ) " && pwd)} "
1520GAP9_SDK_INSTALL_DIR=" ${GAP9_SDK_INSTALL_DIR:? GAP9_SDK_INSTALL_DIR must be set} "
16- GAP9_SDK_COMMIT_HASH=" ${GAP9_SDK_COMMIT_HASH:- 897955d7ab326bd31684429eb16a2e485ab89afb } "
17- GAP_SDK_URL=" ${GAP_SDK_URL:- git @ iis-git.ee.ethz.ch : wiesep / gap9_sdk.git } "
21+ GAP9_SDK_COMMIT_HASH=" ${GAP9_SDK_COMMIT_HASH:? GAP9_SDK_COMMIT_HASH must be set } "
22+ GAP_SDK_URL=" ${GAP_SDK_URL:? GAP_SDK_URL must be set } "
1823
1924echo " Preparing GAP9 SDK in: ${GAP9_SDK_INSTALL_DIR} "
25+ mkdir -p " ${GAP9_SDK_INSTALL_DIR} "
26+ # Fail if CD fails, e.g. due to missing permissions or if the path is a file.
27+ cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
2028
21- if [ -d " ${GAP9_SDK_INSTALL_DIR} /.git" ]; then
22- echo " Directory ${GAP9_SDK_INSTALL_DIR} already exists and looks like a git repo. Updating remote URL and fetching latest changes..."
23- cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
24- git remote set-url origin " ${GAP_SDK_URL} " || true
29+ if [ ! -d " .git" ]; then
30+ # Support reusing a pre-created directory that is not yet a git repo.
31+ echo " Directory exists but .git folder is missing. Reinitializing git repository..."
32+ git init
33+ git remote add origin " ${GAP_SDK_URL} "
34+ git fetch --depth=1 origin ${GAP9_SDK_COMMIT_HASH}
35+ GIT_LFS_SKIP_SMUDGE=1 git reset --hard " ${GAP9_SDK_COMMIT_HASH} "
2536else
26- echo " Cloning GAP9 SDK..."
27- GIT_LFS_SKIP_SMUDGE=1 git clone " ${GAP_SDK_URL} " " ${GAP9_SDK_INSTALL_DIR} "
37+ # Keep local uncommitted changes out of the way before checkout.
38+ echo " Setting remote URL and fetching latest changes..."
39+ git remote set-url origin " ${GAP_SDK_URL} "
40+ git fetch --depth=1 origin ${GAP9_SDK_COMMIT_HASH}
41+ git add .
42+ git stash || true # Stash may fail if there are no changes, ignore that.
2843fi
2944
30- cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
3145echo " Checking out commit ${GAP9_SDK_COMMIT_HASH} (stash and fetch if necessary)"
32- git fetch --all --tags || true
33- git stash || true
3446GIT_LFS_SKIP_SMUDGE=1 git checkout " ${GAP9_SDK_COMMIT_HASH} "
3547git submodule update --init --recursive
48+ git lfs pull --include=" *.so"
3649
37- # Platform specific patch
50+ # Select platform patch by Debian arch first, then uname fallback.
3851ARCH=$( dpkg --print-architecture 2> /dev/null || true)
3952if [ -z " $ARCH " ]; then
4053 ARCH=$( uname -m)
@@ -45,14 +58,22 @@ arm64 | aarch64) PATCH=gap9-arm64.patch ;;
4558* ) PATCH= ;;
4659esac
4760
48- set -e # Enable strict error handling for the build process
49- if [ -n " $PATCH " ] && [ -f " ${ROOT_DIR} /${PATCH} " ]; then
50- echo " Applying platform patch: $PATCH "
51- git apply " ${ROOT_DIR} /${PATCH} "
61+ set -e # Fail fast for patch/build operations.
62+ if [ -n " $PATCH " ]; then
63+ if [ -f " ${ROOT_DIR} /${PATCH} " ]; then
64+ echo " Applying platform patch from ${ROOT_DIR} : $PATCH "
65+ git apply " ${ROOT_DIR} /${PATCH} "
66+ elif [ -f " ${ROOT_DIR} /Container/${PATCH} " ]; then
67+ echo " Applying platform patch from ${ROOT_DIR} /Container: $PATCH "
68+ git apply " ${ROOT_DIR} /Container/${PATCH} "
69+ else
70+ echo " No platform-specific patch to apply for architecture '$ARCH ' (looked for ${ROOT_DIR} /${PATCH} and ${ROOT_DIR} /Container/${PATCH} )"
71+ fi
5272else
53- echo " No platform-specific patch to apply for architecture '$ARCH ' (looked for ${ROOT_DIR} / ${PATCH} ) "
73+ echo " No platform-specific patch to apply for architecture '$ARCH '"
5474fi
55- set +e # Disable strict error handling to allow deactivation even if build fails
75+
76+ set +e # Relax mode so cleanup/deactivate still runs on non-critical failures.
5677
5778echo " Setting up Python virtual environment and installing dependencies"
5879python -m venv .gap9-venv
@@ -62,9 +83,9 @@ echo "Sourcing GAP9 SDK environment"
6283. configs/gap9_evk_audio.sh || true
6384
6485echo " Invoking make install_dependency cmake_sdk.build"
65- set -e # Enable strict error handling for the build process
86+ set -e # Build must stop immediately on errors.
6687make install_dependency cmake_sdk.build openocd.all autotiler.all
67- set +e # Disable strict error handling to allow deactivation even if build fails
88+ set +e # Allow deactivation even if previous step failed.
6889
6990deactivate
7091
0 commit comments