Skip to content

Commit

Permalink
tools: add build_firmware.sh providing common firmware build function…
Browse files Browse the repository at this point in the history
…ality

Currently its not possible to easily reuse the steps taken on the GitHub
CI to build the firmware, so lets factor out those common bits into new
build_firmware.sh script help which basically mimics the current
firmware build flow on the GitHub CI and can be as well reused for
example in local container based workflow.

Signed-off-by: Petr Štetiar <[email protected]>
  • Loading branch information
ynezz committed Dec 1, 2024
1 parent e345049 commit 42a0996
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ autogen/
*.project.mak
*.Makefile
build/
outputs/
build_dir/
artifact/
artifacts/
trashed_modified_files/
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN \
xz-utils

COPY requirements.txt /tmp/
COPY tools/build_firmware.sh /usr/bin/build_firmware.sh

RUN \
virtualenv /opt/venv \
Expand Down Expand Up @@ -81,8 +82,11 @@ ARG USER_GID=$USER_UID
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME

RUN mkdir -p /build_dir /outputs
RUN chown $USERNAME:$USERNAME \
/simplicity_sdk_*
/simplicity_sdk_* \
/build_dir \
/outputs

USER $USERNAME
WORKDIR /build
140 changes: 140 additions & 0 deletions tools/build_firmware.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash
set -euo pipefail

build_dir="build_dir"
output_dir="outputs"
work_dir="/build"
manifest_files=()
python_interpreter="/opt/venv/bin/python3"

# Show help message
show_help() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Build firmware images from manifest files"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " -m, --manifest FILE YAML manifest file describing the firmware to build"
echo " (can be specified multiple times)"
echo " -b, --build-dir DIR Directory for build files (default: ${build_dir})"
echo " -o, --output-dir DIR Directory for output files (default: ${output_dir})"
echo " -w, --work-dir DIR Working directory for build process (default: ${work_dir})"
echo " -p, --python PATH Python interpreter path (default: ${python_interpreter})"
exit 0
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
show_help
;;
-m | --manifest)
if [ -z "${2:-}" ]; then
echo "Error: --manifest requires a file argument"
exit 1
fi
manifest_files+=("$2")
shift
;;
-b | --build-dir)
if [ -z "${2:-}" ]; then
echo "Error: --build-dir requires a directory argument"
exit 1
fi
build_dir="$2"
shift
;;
-o | --output-dir)
if [ -z "${2:-}" ]; then
echo "Error: --output-dir requires a directory argument"
exit 1
fi
output_dir="$2"
shift
;;
-w | --work-dir)
if [ -z "${2:-}" ]; then
echo "Error: --work-dir requires a directory argument"
exit 1
fi
work_dir="$2"
shift
;;
-p | --python)
if [ -z "${2:-}" ]; then
echo "Error: --python requires a path argument"
exit 1
fi
python_interpreter="$2"
shift
;;
*)
echo "Error: Unknown argument: $1"
echo "Use -h or --help for usage information"
exit 1
;;
esac
shift
done

# Check that Python3 with ruamel.yaml is available
if ! "$python_interpreter" -c "import ruamel.yaml" &>/dev/null; then
echo "Error: Python3 with ruamel.yaml is not available at $python_interpreter"
echo "Install ruamel.yaml with 'pip install ruamel.yaml'"
exit 1
fi

# Check if any manifest files were provided
if [ ${#manifest_files[@]} -eq 0 ]; then
echo "Error: No manifest files provided"
echo "Use -h or --help for usage information"
exit 1
fi

git config --global --add safe.directory "$work_dir"

# Install SDK extensions
for sdk in /*_sdk_*; do
slc signature trust --sdk "$sdk"
ln -s "$(pwd)"/gecko_sdk_extensions "$sdk"/extension
for ext in "$sdk"/extension/*/; do
slc signature trust --sdk "$sdk" --extension-path "$ext"
done
done

# Build SDK arguments
sdk_args=()
for sdk_dir in /*_sdk*; do
sdk_args+=(--sdk "$sdk_dir")
done

# Build toolchain arguments
toolchain_args=()
for toolchain_dir in /opt/*arm-none-eabi*; do
toolchain_args+=(--toolchain "$toolchain_dir")
done

# Determine if we should keep the SLC daemon running
keep_daemon=""
if [ ${#manifest_files[@]} -gt 1 ]; then
keep_daemon="--keep-slc-daemon"
fi

# Build firmware
for manifest_file in "${manifest_files[@]}"; do
echo "Building firmware manifest $manifest_file, using build directory $build_dir and output directory $output_dir"

"$python_interpreter" tools/build_project.py \
"${sdk_args[@]}" \
"${toolchain_args[@]}" \
$keep_daemon \
--manifest "$manifest_file" \
--build-dir "$build_dir" \
--output-dir "$output_dir" \
--build-system makefile \
--output gbl \
--output hex \
--output out
done

0 comments on commit 42a0996

Please sign in to comment.