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+ cd " ${GAP9_SDK_INSTALL_DIR} "
2027
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
28+ if [ ! -d " .git" ]; then
29+ # Support reusing a pre-created directory that is not yet a git repo.
30+ echo " Directory exists but .git folder is missing. Reinitializing git repository..."
31+ git init
32+ git remote add origin " ${GAP_SDK_URL} "
33+ git fetch --depth=1 origin ${GAP9_SDK_COMMIT_HASH}
34+ GIT_LFS_SKIP_SMUDGE=1 git reset --hard " ${GAP9_SDK_COMMIT_HASH} "
2535else
26- if [ ! -d " ${GAP9_SDK_INSTALL_DIR} " ]; then
27- echo " Directory ${GAP9_SDK_INSTALL_DIR} does not exist. Cloning fresh..."
28- git clone " ${GAP_SDK_URL} " " ${GAP9_SDK_INSTALL_DIR} "
29- cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
30- else
31- echo " Directory exists but .git folder is missing. Reinitializing git repository..."
32- cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
33- git init
34- git remote add origin " ${GAP_SDK_URL} "
35- git fetch --all --tags || true
36- git reset --soft " ${GAP9_SDK_COMMIT_HASH} "
37- git add .
38- fi
36+ # Keep local uncommitted changes out of the way before checkout.
37+ echo " Setting remote URL and fetching latest changes..."
38+ git remote set-url origin " ${GAP_SDK_URL} "
39+ git fetch --depth=1 origin ${GAP9_SDK_COMMIT_HASH}
40+ git add .
41+ git stash
3942fi
4043
41- cd " ${GAP9_SDK_INSTALL_DIR} " || exit 1
4244echo " Checking out commit ${GAP9_SDK_COMMIT_HASH} (stash and fetch if necessary)"
43- git fetch --all --tags || true
44- git stash || true
4545GIT_LFS_SKIP_SMUDGE=1 git checkout " ${GAP9_SDK_COMMIT_HASH} "
4646git submodule update --init --recursive
47- git lfs pull --include=" *.so" || true
47+ git lfs pull --include=" *.so"
4848
49- # Platform specific patch
49+ # Select platform patch by Debian arch first, then uname fallback.
5050ARCH=$( dpkg --print-architecture 2> /dev/null || true)
5151if [ -z " $ARCH " ]; then
5252 ARCH=$( uname -m)
@@ -57,14 +57,22 @@ arm64 | aarch64) PATCH=gap9-arm64.patch ;;
5757* ) PATCH= ;;
5858esac
5959
60- set -e # Enable strict error handling for the build process
61- if [ -n " $PATCH " ] && [ -f " ${ROOT_DIR} /${PATCH} " ]; then
62- echo " Applying platform patch: $PATCH "
63- git apply " ${ROOT_DIR} /${PATCH} "
60+ set -e # Fail fast for patch/build operations.
61+ if [ -n " $PATCH " ]; then
62+ if [ -f " ${ROOT_DIR} /${PATCH} " ]; then
63+ echo " Applying platform patch from ${ROOT_DIR} : $PATCH "
64+ git apply " ${ROOT_DIR} /${PATCH} "
65+ elif [ -f " ${ROOT_DIR} /Container/${PATCH} " ]; then
66+ echo " Applying platform patch from ${ROOT_DIR} /Container: $PATCH "
67+ git apply " ${ROOT_DIR} /Container/${PATCH} "
68+ else
69+ echo " No platform-specific patch to apply for architecture '$ARCH ' (looked for ${ROOT_DIR} /${PATCH} and ${ROOT_DIR} /Container/${PATCH} )"
70+ fi
6471else
65- echo " No platform-specific patch to apply for architecture '$ARCH ' (looked for ${ROOT_DIR} / ${PATCH} ) "
72+ echo " No platform-specific patch to apply for architecture '$ARCH '"
6673fi
67- set +e # Disable strict error handling to allow deactivation even if build fails
74+
75+ set +e # Relax mode so cleanup/deactivate still runs on non-critical failures.
6876
6977echo " Setting up Python virtual environment and installing dependencies"
7078python -m venv .gap9-venv
@@ -74,9 +82,9 @@ echo "Sourcing GAP9 SDK environment"
7482. configs/gap9_evk_audio.sh || true
7583
7684echo " Invoking make install_dependency cmake_sdk.build"
77- set -e # Enable strict error handling for the build process
85+ set -e # Build must stop immediately on errors.
7886make install_dependency cmake_sdk.build openocd.all autotiler.all
79- set +e # Disable strict error handling to allow deactivation even if build fails
87+ set +e # Allow deactivation even if previous step failed.
8088
8189deactivate
8290
0 commit comments