Skip to content

Commit 1960fd2

Browse files
authored
feat: build.sh script (#19)
* feat: build.sh script * fix: codacy nitpick * nitpicks plus download progress * fix: nitpick * refactor: improve build.sh robustness with defensive checks, progress, and better extraction * fix: add toolchain integrity check to prevent partial extraction issues
1 parent e355ede commit 1960fd2

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ redirection.junk
66
kalman/test
77
.exe
88
gcc-arm*
9+
tmp/

build.sh

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/bin/sh
2+
3+
# Ensure the script runs from the repository root
4+
if [ ! -f "src/version.h" ]; then
5+
echo "Error: This script must be run from the repository root."
6+
exit 1
7+
fi
8+
9+
# --- Configuration ---
10+
# Expected directory name of the extracted toolchain
11+
TOOLCHAIN_DIR="gcc-arm-none-eabi-6-2017-q1-update"
12+
VERSION_FILE="src/version.h"
13+
BUILD_OUTPUT_FILE="output/F3.bin"
14+
15+
# --- 1. Conditional Download and Extraction ---
16+
17+
if [ ! -d "$TOOLCHAIN_DIR" ] || [ ! -f "$TOOLCHAIN_DIR/bin/arm-none-eabi-gcc" ]; then
18+
echo "Toolchain directory '$TOOLCHAIN_DIR' or required binary not found."
19+
20+
# 1a. Detect Operating System
21+
OS=""
22+
case $(uname) in
23+
"Linux" )
24+
OS="linux"
25+
;;
26+
"Darwin" )
27+
OS="mac"
28+
;;
29+
* )
30+
echo "Error: Unsupported operating system ($(uname)). Exiting."
31+
exit 1
32+
;;
33+
esac
34+
35+
echo "Detected OS: $OS. Starting download..."
36+
37+
# 1b. Construct Download URL
38+
DOWNLOAD_URL="https://developer.arm.com/-/media/Files/downloads/gnu-rm/6_1-2017q1/gcc-arm-none-eabi-6-2017-q1-update-${OS}.tar.bz2"
39+
40+
# 1c. Download (silent and follow redirects) and pipe directly to tar for extraction
41+
# 'xjf' is used: 'x' extract, 'j' for bzip2, 'f' for file/stream
42+
if ! curl -L -# "$DOWNLOAD_URL" | tar -xjf -; then
43+
echo "========================================================================="
44+
echo "ERROR: Failed to download or extract the ARM toolchain."
45+
echo "Please check if 'curl' and 'tar' are installed and the URL is still valid."
46+
echo "========================================================================="
47+
exit 1
48+
fi
49+
50+
echo "Download and extraction of $TOOLCHAIN_DIR completed successfully."
51+
else
52+
echo "Toolchain directory '$TOOLCHAIN_DIR' and required binary found. Skipping download and extraction."
53+
fi
54+
55+
56+
# --- 2. Build Steps ---
57+
58+
echo "Setting up PATH..."
59+
60+
# Export the new PATH variable, using 'pwd' to ensure we get the absolute path
61+
# to the toolchain's bin directory regardless of where the script is called from.
62+
TOOLCHAIN_BIN_PATH="$(pwd)/$TOOLCHAIN_DIR/bin"
63+
export PATH="$PATH:$TOOLCHAIN_BIN_PATH"
64+
65+
echo "Executing build script with: python make.py -C -T F3"
66+
67+
# Execute the python build script and check the result immediately
68+
if ! python make.py -C -T F3; then
69+
echo "=========================================================="
70+
echo "❌ Build failed. Check the output above for errors."
71+
echo "=========================================================="
72+
exit 1
73+
fi
74+
75+
echo "=========================================================="
76+
echo "✅ Build completed successfully."
77+
echo "=========================================================="
78+
echo ""
79+
80+
# --- 3. Post-Build: Version Extraction and Binary Copy ---
81+
82+
echo "--- Post-Build Processing ---"
83+
84+
# 3a. Check for version file existence
85+
if [ ! -f "$VERSION_FILE" ]; then
86+
echo "Error: Version file '$VERSION_FILE' not found. Cannot determine firmware version."
87+
exit 1
88+
fi
89+
90+
# 3b. Extract FIRMWARE_VERSION using grep and awk (highly portable)
91+
# Looks for the line, and prints the third field (the number).
92+
FIRMWARE_VERSION=$(grep '^[[:space:]]*#define[[:space:]]*FIRMWARE_VERSION' "$VERSION_FILE" | awk '{print $3}')
93+
94+
# Basic validation: ensure extracted version is a non-empty decimal number
95+
if [ -z "$FIRMWARE_VERSION" ] || ! expr "$FIRMWARE_VERSION" : '^[0-9]\+$' >/dev/null; then
96+
echo "Error: Could not extract a valid numerical FIRMWARE_VERSION from '$VERSION_FILE'."
97+
exit 1
98+
fi
99+
100+
echo "Extracted Firmware Version: $FIRMWARE_VERSION"
101+
102+
# 3c. Define the new destination filename, ensuring it goes into the output directory
103+
DEST_FILE="output/IMUF_${FIRMWARE_VERSION}.bin"
104+
105+
# 3d. Check if the built binary exists
106+
if [ ! -f "$BUILD_OUTPUT_FILE" ]; then
107+
echo "Error: Build output file '$BUILD_OUTPUT_FILE' not found. Cannot proceed with copying."
108+
exit 1
109+
fi
110+
111+
# 3e. Ensure output directory exists for the versioned binary
112+
if [ ! -d "output" ]; then
113+
mkdir -p output
114+
fi
115+
116+
# 3f. Copy the binary, preserving modification time, access time, and modes (-p)
117+
echo "Copying '$BUILD_OUTPUT_FILE' to '$DEST_FILE'..."
118+
if cp -p "$BUILD_OUTPUT_FILE" "$DEST_FILE"; then
119+
echo "=========================================================="
120+
echo "✅ Success: Binary copied to '$DEST_FILE'"
121+
echo "=========================================================="
122+
else
123+
echo "=========================================================="
124+
echo "❌ Error: Failed to copy the binary."
125+
echo "=========================================================="
126+
exit 1
127+
fi

0 commit comments

Comments
 (0)