|
| 1 | +if which nproc > /dev/null; then |
| 2 | + MAKEOPTS="-j$(nproc)" |
| 3 | +else |
| 4 | + MAKEOPTS="-j$(sysctl -n hw.ncpu)" |
| 5 | +fi |
| 6 | + |
| 7 | +# Downloads the latest Qwiic release from the SparkFun Qwiic_Py repository and extracts the contents to the given directory |
| 8 | +# Options: |
| 9 | + # $1: Output directory |
| 10 | +function download_qwiic_release { |
| 11 | + local LATEST_RELEASE=`gh release -R sparkfun/Qwiic_Py list --json name,isLatest --jq '.[] | select(.isLatest)|.name'` |
| 12 | + curl -sL -o pylibs.zip https://github.com/sparkfun/Qwiic_Py/releases/latest/download/qwiic-py-py-${LATEST_RELEASE}.zip |
| 13 | + unzip pylibs.zip -d . |
| 14 | + |
| 15 | + mkdir -p $1 |
| 16 | + cp -r qwiic-py-py-${LATEST_RELEASE}/lib/* $1 |
| 17 | +} |
| 18 | + |
| 19 | +# Builds all SparkFun boards for the given port |
| 20 | +# Options: |
| 21 | + # $1: Port name |
| 22 | +function build_for_port { |
| 23 | + local TARGET_PORT_NAME=$1 |
| 24 | + local SPARKFUN_PREFIX="SPARKFUN_" |
| 25 | + local SPARKFUN_BOARD_PREFIX="ports/${TARGET_PORT_NAME}/boards/${SPARKFUN_PREFIX}*" |
| 26 | + |
| 27 | + for board in $SPARKFUN_BOARD_PREFIX; do |
| 28 | + BOARD_TO_BUILD=${SPARKFUN_PREFIX}${board#$SPARKFUN_BOARD_PREFIX} |
| 29 | + make ${MAKEOPTS} -C ports/${TARGET_PORT_NAME} BOARD=$BOARD_TO_BUILD clean |
| 30 | + make ${MAKEOPTS} -C ports/${TARGET_PORT_NAME} BOARD=$BOARD_TO_BUILD submodules |
| 31 | + make ${MAKEOPTS} -C ports/${TARGET_PORT_NAME} BOARD=$BOARD_TO_BUILD |
| 32 | + done |
| 33 | +} |
| 34 | + |
| 35 | +# Builds all SparkFun RP2 boards (might break into a build_for_port function that we pass the port to later if ESP32 takes the exact same build coms) |
| 36 | +# Options: |
| 37 | + # $1: Whether to build the cross compiler |
| 38 | +function build_all_sparkfun_boards_rp2 { |
| 39 | + if $1; then |
| 40 | + make ${MAKEOPTS} -C mpy-cross |
| 41 | + fi |
| 42 | + |
| 43 | + build_for_port "rp2" |
| 44 | +} |
| 45 | + |
| 46 | +# Builds all SparkFun ESP32 boards |
| 47 | +# Options: |
| 48 | + # $1: Whether to build the cross compiler |
| 49 | +function build_all_sparkfun_boards_esp32 { |
| 50 | + source esp-idf/export.sh |
| 51 | + |
| 52 | + if $1; then |
| 53 | + make ${MAKEOPTS} -C mpy-cross |
| 54 | + fi |
| 55 | + |
| 56 | + build_for_port "esp32" |
| 57 | +} |
| 58 | + |
| 59 | +# Copies all files with the given prefix from the SparkFun build directories to the output directory |
| 60 | +# Options: |
| 61 | + # $1: Output directory |
| 62 | + # $2: Port directory |
| 63 | + # $3: Build prefix |
| 64 | + # $4: File basename |
| 65 | + # $5: Extension |
| 66 | + # $6: Prefix to put on output files |
| 67 | +function copy_all_for_prefix { |
| 68 | + local OUT_DIRECTORY=$1 |
| 69 | + local PORT_DIRECTORY=$2 # The directory where the ports are located (e.g. ports/rp2) |
| 70 | + local BUILD_PREFIX=$3 # The prefix of the SparkFun build directories (e.g. build-SPARKFUN_) |
| 71 | + local FILE_BASENAME=$4 # the target base name of the file to copy from each SparkFun build directory (e.g. firmware) |
| 72 | + local EXTENSION=$5 # The extension of the file to copy (e.g. uf2) |
| 73 | + local OUTPUT_PREFIX=$6 # The prefix to put on the output files (e.g. MICROPYTHON_ or MINIMAL_MICROPYTHON_) |
| 74 | + |
| 75 | + mkdir -p ${OUT_DIRECTORY} |
| 76 | + |
| 77 | + for file in $(find ${PORT_DIRECTORY} -wholename "*${BUILD_PREFIX}*/*${FILE_BASENAME}.${EXTENSION}"); do |
| 78 | + echo "Moving $file to ${OUT_DIRECTORY}" |
| 79 | + mv $file ${OUT_DIRECTORY}/${OUTPUT_PREFIX}$(echo $file | sed -n "s/.*${BUILD_PREFIX}\([^/]*\)\/${FILE_BASENAME}.${EXTENSION}/\1/p").${EXTENSION} |
| 80 | + done |
| 81 | +} |
| 82 | + |
| 83 | +# The esp32 has 3 different binaries that we need to put into a directory and then zip and then move to the output directory |
| 84 | +# Options: |
| 85 | + # $1: Output directory |
| 86 | + # $2: Port directory |
| 87 | + # $3: Build prefix |
| 88 | + # $4: Prefix to put on output files |
| 89 | +# We need to copy |
| 90 | +function copy_all_for_prefix_esp32 { |
| 91 | + local OUT_DIRECTORY=$1 |
| 92 | + local PORT_DIRECTORY=$2 # The directory where the ports are located (e.g. ports/esp32) |
| 93 | + local BUILD_PREFIX=$3 # The prefix of the SparkFun build directories (e.g. build-SPARKFUN_) |
| 94 | + local OUTPUT_PREFIX=$4 # The prefix to put on the output files (e.g. MICROPYTHON_ or MINIMAL_MICROPYTHON_) |
| 95 | + |
| 96 | + mkdir -p ${OUT_DIRECTORY} |
| 97 | + |
| 98 | + for board in $(find ${PORT_DIRECTORY} -type d -name "${BUILD_PREFIX}*"); do |
| 99 | + BOARD_NAME=$(echo $board | sed -n "s/.*${BUILD_PREFIX}\([^/]*\)/\1/p") |
| 100 | + echo "Copying binaries for $BOARD_NAME" |
| 101 | + ZIP_DIR=${OUTPUT_PREFIX}${BOARD_NAME} |
| 102 | + mkdir -p ${ZIP_DIR} |
| 103 | + cp ${board}/micropython.bin ${ZIP_DIR}/micropython.bin |
| 104 | + cp ${board}/bootloader/bootloader.bin ${ZIP_DIR}/bootloader.bin |
| 105 | + cp ${board}/partition_table/partition-table.bin ${ZIP_DIR}/partition-table.bin |
| 106 | + |
| 107 | + echo "Zipping binaries for $BOARD_NAME" |
| 108 | + zip -r ${ZIP_DIR}.zip ${ZIP_DIR} |
| 109 | + |
| 110 | + echo "Moving zip file for $BOARD_NAME to ${OUT_DIRECTORY}" |
| 111 | + mv ${ZIP_DIR}.zip ${OUT_DIRECTORY} |
| 112 | + done |
| 113 | +} |
| 114 | + |
| 115 | +# Adds the line freeze("<DIRECTORY_WHERE_WE_DOWNLOADED_QWIIC_STUFF>") to the manifest.py for each board |
| 116 | +# Options: |
| 117 | + # $1: Qwiic directory |
| 118 | + # $2: BOARD directory |
| 119 | + # $3: Board prefix |
| 120 | +function add_qwiic_manifest { |
| 121 | + local QWIIC_DIRECTORY=$1 # The directory where the Qwiic drivers are located to be frozen |
| 122 | + local BOARD_DIRECTORY=$2 # The directory where the boards are located (e.g. ports/rp2/boards) |
| 123 | + local BOARD_PREFIX=$3 # The prefix of the SparkFun board directories (e.g. SPARKFUN_) |
| 124 | + |
| 125 | + for board in $(find ${BOARD_DIRECTORY} -type d -name "${BOARD_PREFIX}*"); do |
| 126 | + if [ ! -f ${board}/manifest.py ]; then |
| 127 | + echo "Creating manifest.py for $board" |
| 128 | + echo "include(\"${BOARD_DIRECTORY}/manifest.py\")" > ${board}/manifest.py |
| 129 | + fi |
| 130 | + |
| 131 | + echo "Adding freeze line to manifest.py for $board" |
| 132 | + printf "\nfreeze(\"${QWIIC_DIRECTORY}\")" >> ${board}/manifest.py |
| 133 | + |
| 134 | + echo "Manifest.py for $board:" |
| 135 | + cat ${board}/manifest.py |
| 136 | + done |
| 137 | +} |
| 138 | + |
| 139 | +# Assumes that MAKEOPTS environment variable is set |
| 140 | +# This is designed to be the user-facing function that will build all SparkFun boards |
| 141 | +# Options: |
| 142 | + # -p: Output file prefix |
| 143 | + # -o: Output directory |
| 144 | + # -q: Qwiic directory |
| 145 | +function build_sparkfun { |
| 146 | + local OUTPUT_FILE_PREFIX="MICROPYTHON_" |
| 147 | + local OUTPUT_DIRECTORY="sparkfun_release" |
| 148 | + local QWIIC_DIRECTORY="qwiic_lib" |
| 149 | + |
| 150 | + while getopts "p:o:q:" opt; do |
| 151 | + case ${opt} in |
| 152 | + p ) |
| 153 | + OUTPUT_FILE_PREFIX=$OPTARG |
| 154 | + ;; |
| 155 | + o ) |
| 156 | + OUTPUT_DIRECTORY=$OPTARG |
| 157 | + ;; |
| 158 | + q ) |
| 159 | + QWIIC_DIRECTORY=$OPTARG |
| 160 | + ;; |
| 161 | + esac |
| 162 | + done |
| 163 | + |
| 164 | + echo "OUTPUT_DIRECTORY: ${OUTPUT_DIRECTORY}" |
| 165 | + echo "Performing minimal SparkFun build for ESP32 and RP2" |
| 166 | + |
| 167 | + # Perform minimal build for ESP32 |
| 168 | + build_all_sparkfun_boards_esp32 true |
| 169 | + |
| 170 | + # Perform minimal build for RP2 |
| 171 | + build_all_sparkfun_boards_rp2 false |
| 172 | + |
| 173 | + # Copy all esp32 binary files to the output directory |
| 174 | + copy_all_for_prefix_esp32 ${OUTPUT_DIRECTORY} "ports/esp32" "build-SPARKFUN_" "MINIMAL_${OUTPUT_FILE_PREFIX}" |
| 175 | + |
| 176 | + # Copy all rp2 binary files to the output directory |
| 177 | + copy_all_for_prefix ${OUTPUT_DIRECTORY} "ports/rp2" "build-SPARKFUN_" "firmware" "uf2" "MINIMAL_${OUTPUT_FILE_PREFIX}" |
| 178 | + |
| 179 | + echo "Downloading Qwiic library and adding to manifest.py for SparkFun boards" |
| 180 | + # Perform Qwiic download |
| 181 | + download_qwiic_release ${QWIIC_DIRECTORY} |
| 182 | + |
| 183 | + # This is an ugly way to pass the qwiic path. Should make it cleaner than a relative path... |
| 184 | + # Add the downloaded Qwiic drivers to the manifest.py for each esp32 board |
| 185 | + add_qwiic_manifest "../../../../${QWIIC_DIRECTORY}" "ports/esp32/boards" "SPARKFUN_" |
| 186 | + |
| 187 | + # Add the downloaded Qwiic drivers to the manifest.py for each rp2 board |
| 188 | + add_qwiic_manifest "../../../../${QWIIC_DIRECTORY}" "ports/rp2/boards" "SPARKFUN_" |
| 189 | + |
| 190 | + echo "Performing full SparkFun build for ESP32 and RP2" |
| 191 | + |
| 192 | + # Perform Qwiic Build for ESP32 |
| 193 | + build_all_sparkfun_boards_esp32 false |
| 194 | + |
| 195 | + # Perform Qwiic Build for RP2 |
| 196 | + build_all_sparkfun_boards_rp2 false |
| 197 | + |
| 198 | + # Copy all esp32 binary files to the output directory |
| 199 | + copy_all_for_prefix_esp32 ${OUTPUT_DIRECTORY} "ports/esp32" "build-SPARKFUN_" ${OUTPUT_FILE_PREFIX} |
| 200 | + |
| 201 | + # Copy all rp2 binary files to the output directory |
| 202 | + copy_all_for_prefix ${OUTPUT_DIRECTORY} "ports/rp2" "build-SPARKFUN_" "firmware" "uf2" ${OUTPUT_FILE_PREFIX} |
| 203 | +} |
0 commit comments