Skip to content

Commit 5728bc4

Browse files
Add automatic builds that contain examples on release
1 parent 9c875a0 commit 5728bc4

File tree

3 files changed

+211
-20
lines changed

3 files changed

+211
-20
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build firmware
1+
name: Build Firmware
22

33
on:
44
pull_request:
@@ -7,7 +7,6 @@ on:
77
push:
88
branches:
99
- features_for_launch
10-
workflow_dispatch:
1110

1211
jobs:
1312
build:
@@ -17,21 +16,5 @@ jobs:
1716
uses: actions/checkout@v4
1817
with:
1918
submodules: true
20-
- name: Install packages
21-
run: |
22-
sudo apt install cmake python3 build-essential gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
23-
- name: Build MPY Cross
24-
run: make -C micropython/mpy-cross
25-
- name: MicroPython submodules
26-
run: make -C micropython/ports/rp2 BOARD=SPARKFUN_XRP_CONTROLLER submodules
27-
- name: Set Pico SDK path
28-
run: echo "PICO_SDK_PATH=$GITHUB_WORKSPACE/micropython/lib/pico-sdk" >> "$GITHUB_ENV"
29-
- name: Build OpenCV
30-
run: make -C src/opencv PLATFORM=rp2350 --no-print-directory -j4
31-
- name: Build firmware
32-
run: make BOARD=SPARKFUN_XRP_CONTROLLER -j4
33-
- name: Upload UF2
34-
uses: actions/upload-artifact@v4
35-
with:
36-
name: firmware.uf2
37-
path: micropython/ports/rp2/build-SPARKFUN_XRP_CONTROLLER-LARGE_BINARY/firmware.uf2
19+
- name: Build Firmware
20+
run: source build.sh && build_micropython_opencv

.github/workflows/release.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build and Deploy Firmware Release
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-22.04
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v4
13+
with:
14+
submodules: true
15+
- name: Build Firmware
16+
run: source build.sh && build_micropython_opencv
17+
- name: Upload Release Assets
18+
uses: shogo82148/actions-upload-release-asset@v1
19+
with:
20+
asset_path: "micropython/ports/rp2/build-SPARKFUN_XRP_CONTROLLER-LARGE_BINARY/firmware.uf2"
21+
github_token: ${{ secrets.GITHUB_TOKEN }}
22+
upload_url: ${{ github.event.release.upload_url }}
23+

build.sh

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
if which nproc > /dev/null; then
2+
MAKEOPTS="-j$(nproc)"
3+
else
4+
MAKEOPTS="-j$(sysctl -n hw.ncpu)"
5+
fi
6+
7+
# TODO: Could also make these opts into the build_micropython_opencv function if we care...
8+
FROZEN_MODULES_DIR="$(dirname "$0")/frozen_modules"
9+
FROZEN_EXAMPLES_ARCHIVE_SCRIPT="frozen_examples.py"
10+
FROZEN_EXAMPLES_UNPACKED_DIR="micropython-opencv-examples"
11+
12+
# Uses freezefs to create a frozen filesystem archive for the provided directory.
13+
# See https://github.com/bixb922/freezefs for more details on freezefs
14+
# Options:
15+
# $1: The directory to freeze
16+
# $2: The name that you want the frozen directory to have once unpacked on the board
17+
# $3: The output file name for the frozen archive .py file
18+
function create_frozen_fs {
19+
local DIR_TO_FREEZE=$1
20+
local DIR_NAME_ON_BOARD=$2
21+
local OUTPUT_FILE=$3
22+
23+
echo "Creating frozen filesystem for directory: $DIR_TO_FREEZE"
24+
echo "The frozen directory will be named: $DIR_NAME_ON_BOARD"
25+
echo "The output file will be: $OUTPUT_FILE"
26+
27+
cp -r $DIR_TO_FREEZE $DIR_NAME_ON_BOARD
28+
29+
python -m freezefs $DIR_NAME_ON_BOARD $OUTPUT_FILE
30+
}
31+
32+
# Options:
33+
# $1: The directory to add to the manifest
34+
# $2: The port (e.g. rp2)
35+
# $3: The board (e.g. SPARKFUN_XRP_CONTROLLER)
36+
# $4: The mpconfigboard file name (e.g. mpconfigboard.cmake or mpconfigboard.m) Default: mpconfigboard.cmake
37+
function add_to_manifest {
38+
local DIR=$1
39+
local PORT=$2
40+
local BOARD=$3
41+
local MPCONFIG_FILE="${4:-mpconfigboard.cmake}"
42+
43+
# Add the directory to the manifest file
44+
echo "Adding $DIR to the manifest for $PORT on $BOARD using $MPCONFIG_FILE"
45+
local BOARD_DIR="micropython/ports/${PORT}/boards/${BOARD}"
46+
47+
# Create manifest.py if it doesn't exist
48+
if [ ! -f ${BOARD_DIR}/manifest.py ]; then
49+
echo "include(\"\$(PORT_DIR)/boards/manifest.py\")" > ${BOARD_DIR}/manifest.py
50+
51+
# also add the necessary frozen manifest line to mpconfigboard.cmake: set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)
52+
# We will use the optional MPCONFIG_FILE argument to determine if we should add this line
53+
54+
if [ -n "$MPCONFIG_FILE" ]; then
55+
if [[ $MPCONFIG_FILE == *.mk ]]; then
56+
# e.g. for TEENSY which uses mpconfigboard.mk instead of mpconfigboard.cmake
57+
echo "Adding frozen manifest line to mpconfigboard.mk for $BOARD"
58+
printf "\nFROZEN_MANIFEST ?= \$(BOARD_DIR)/manifest.py" >> ${BOARD_DIR}/$MPCONFIG_FILE
59+
elif [[ $MPCONFIG_FILE == *.cmake ]]; then
60+
echo "Adding frozen manifest line to mpconfigboard.cmake for $BOARD"
61+
printf "\nset(MICROPY_FROZEN_MANIFEST \"\${MICROPY_BOARD_DIR}/manifest.py\")" >> ${BOARD_DIR}/$MPCONFIG_FILE
62+
fi
63+
fi
64+
fi
65+
66+
# Add the freeze line to the manifest.py for the board
67+
echo "Adding freeze line to manifest.py for $BOARD"
68+
printf "\nfreeze(\"${DIR}\")" >> ${BOARD_DIR}/manifest.py
69+
70+
# Helpful for debugging during the build process, but can be removed if we'd rather not see this output...
71+
echo "Manifest.py for $BOARD:"
72+
cat ${BOARD_DIR}/manifest.py
73+
}
74+
75+
# Adds the frozen data filesystem to the boot.py file for the given port
76+
# Options:
77+
# $1: Port name
78+
# $2: Frozen data file path
79+
# $3: Copy Source: If copying imported frozen data to a mutable location, this is the directory name of the source (optional)
80+
# $4: Copy Destination: If copying imported frozen data to a mutable location, this is the directory name of the destination (optional)
81+
# $5: Add destination to sys.path? If true, the destination directory will be added to sys.path in _boot.py (optional)
82+
# NOTE: By providing the source and destination, the frozen data filesystem will be copied to a mutable location on the board
83+
# If they are not provided, the frozen data filesystem will still be accessible, but will be read-only.
84+
function add_frozen_data_to_boot_for_port {
85+
local TARGET_PORT_NAME=$1
86+
local FROZEN_DATA_FILE=$2
87+
local SOURCE_DIR=$3
88+
local DESTINATION_DIR=$4
89+
local ADD_TO_SYSPATH=${5:-false}
90+
91+
# Remove the ".py" extension from the frozen data file
92+
local FROZEN_DATA_BASENAME=$(basename $FROZEN_DATA_FILE .py)
93+
94+
# Check if the _boot.py file exists in the port's modules directory and error out if it does not
95+
if [ ! -f micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py ]; then
96+
echo "Error: _boot.py file not found in ports/${TARGET_PORT_NAME}/modules/"
97+
exit 1
98+
fi
99+
100+
# Add the frozen data filesystem to the _boot.py file
101+
local BOOT_FILE="micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py"
102+
103+
echo "Adding frozen data filesystem to ${BOOT_FILE}"
104+
echo "import ${FROZEN_DATA_BASENAME}" >> ${BOOT_FILE}
105+
106+
# Now, copy the unpacked frozen data filesystem to a mutable location if the source and destination are provided
107+
if [ -n "$SOURCE_DIR" ] && [ -n "$DESTINATION_DIR" ]; then
108+
echo "Copying frozen data from ${SOURCE_DIR} to ${DESTINATION_DIR} in _boot.py"
109+
local BOOT_FILE="micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py"
110+
echo "import os" >> ${BOOT_FILE}
111+
# Simple recursive function to copy the directory tree (since i.e. shutil.copytree is not available on MicroPython)
112+
echo "def copytree(src, dst):" >> ${BOOT_FILE}
113+
echo " try:" >> ${BOOT_FILE}
114+
echo " os.mkdir(dst)" >> ${BOOT_FILE}
115+
echo " except OSError:" >> ${BOOT_FILE}
116+
echo " pass" >> ${BOOT_FILE}
117+
echo " for entry in os.ilistdir(src):" >> ${BOOT_FILE}
118+
echo " fname, typecode, _, _ = entry" >> ${BOOT_FILE}
119+
echo " src_path = src + '/' + fname" >> ${BOOT_FILE}
120+
echo " dst_path = dst + '/' + fname" >> ${BOOT_FILE}
121+
echo " if typecode == 0x4000:" >> ${BOOT_FILE} # typecode == 0x4000 means directory
122+
echo " copytree(src_path, dst_path)" >> ${BOOT_FILE}
123+
echo " else:" >> ${BOOT_FILE}
124+
echo " with open(src_path, 'rb') as fsrc:" >> ${BOOT_FILE}
125+
echo " with open(dst_path, 'wb') as fdst:" >> ${BOOT_FILE}
126+
echo " fdst.write(fsrc.read())" >> ${BOOT_FILE}
127+
echo "copytree('${SOURCE_DIR}', '${DESTINATION_DIR}')" >> ${BOOT_FILE}
128+
fi
129+
130+
# If the ADD_TO_SYSPATH flag is true, add the destination directory to sys.path
131+
if [ "$ADD_TO_SYSPATH" = true ]; then
132+
echo "Adding ${DESTINATION_DIR} to sys.path in _boot.py"
133+
echo "import sys" >> ${BOOT_FILE}
134+
echo "sys.path.append('/${DESTINATION_DIR}')" >> ${BOOT_FILE}
135+
fi
136+
137+
# Helpful for debugging during the build process, but can be removed if we'd rather not see this output...
138+
echo "Content of _boot.py after adding frozen data filesystem:"
139+
cat micropython/ports/${TARGET_PORT_NAME}/modules/_boot.py
140+
}
141+
142+
# Installs necessary dependencies and builds OpenCV and the firmware
143+
# Also freezes the examples directory in a filesystem archive on the board
144+
function build_micropython_opencv {
145+
# Install necessary packages (Could move into an install_dependencies.sh if we want this to be more explicit/modular)
146+
sudo apt-get update
147+
sudo apt install cmake python3 build-essential gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
148+
# Install necessary python packages (could also move this to a requirements.txt file)
149+
pip install freezefs
150+
151+
# Create a directory for frozen modules, we can add arbitrary .py files to this directory in the future.
152+
# For now it will just contain the archived examples script.
153+
mkdir "$FROZEN_MODULES_DIR"
154+
155+
# Create our frozen filesystem archive for the examples directory
156+
# Note the "." to make the read-only version of the examples directory hidden in IDEs like Thonny
157+
create_frozen_fs "examples" ".$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_MODULES_DIR/$FROZEN_EXAMPLES_ARCHIVE_SCRIPT"
158+
159+
# Add necessary content to the manifest file to freeze the modules in the provided directory
160+
add_to_manifest "$FROZEN_MODULES_DIR" "rp2" "SPARKFUN_XRP_CONTROLLER" "mpconfigvariant_LARGE_BINARY.cmake"
161+
162+
# Add necessary content to the boot.py file to unpack the frozen data filesystem on boot
163+
# Provide the source and destination directories to copy the frozen data filesystem to a mutable (and non-hidden) location
164+
# Provide "true" as the last argument to add the destination directory to sys.path (since our examples directory contains modules that we want to be importable...)
165+
add_frozen_data_to_boot_for_port "rp2" "$FROZEN_EXAMPLES_ARCHIVE_SCRIPT" ".$FROZEN_EXAMPLES_UNPACKED_DIR" "$FROZEN_EXAMPLES_UNPACKED_DIR" true
166+
167+
# Set Pico SDK path to $GITHUB_WORKSPACE/micropython/lib/pico-sdk if $GITHUB_WORKSPACE is set, otherwise use the current directory
168+
if [ -n "$GITHUB_WORKSPACE" ]; then
169+
export PICO_SDK_PATH="$GITHUB_WORKSPACE/micropython/lib/pico-sdk"
170+
else
171+
export PICO_SDK_PATH=$(dirname "$0")/micropython/lib/pico-sdk
172+
fi
173+
174+
# Build MPY Cross compiler
175+
make -C micropython/mpy-cross
176+
177+
# Update necessary MicroPython submodules
178+
make -C micropython/ports/rp2 BOARD=SPARKFUN_XRP_CONTROLLER submodules
179+
180+
# Build OpenCV
181+
make -C src/opencv PLATFORM=rp2350 --no-print-directory ${MAKEOPTS}
182+
183+
# Build firmware
184+
make BOARD=SPARKFUN_XRP_CONTROLLER ${MAKEOPTS}
185+
}

0 commit comments

Comments
 (0)