|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +export PLATFORMIO_ESP32_PATH="$HOME/.platformio/packages/framework-arduinoespressif32" |
| 4 | +PLATFORMIO_ESP32_URL="https://github.com/platformio/platform-espressif32.git" |
| 5 | + |
| 6 | +TOOLCHAIN_VERSION="12.2.0+20230208" |
| 7 | +ESPTOOLPY_VERSION="~1.40501.0" |
| 8 | +ESPRESSIF_ORGANIZATION_NAME="espressif" |
| 9 | +SDKCONFIG_DIR="$PLATFORMIO_ESP32_PATH/tools/esp32-arduino-libs" |
| 10 | +SCRIPTS_DIR="./.github/scripts" |
| 11 | +COUNT_SKETCHES="${SCRIPTS_DIR}/sketch_utils.sh count" |
| 12 | +CHECK_REQUIREMENTS="${SCRIPTS_DIR}/sketch_utils.sh check_requirements" |
| 13 | + |
| 14 | +echo "Installing Python Wheel ..." |
| 15 | +pip install wheel > /dev/null 2>&1 |
| 16 | + |
| 17 | +echo "Installing PlatformIO ..." |
| 18 | +pip install -U https://github.com/platformio/platformio/archive/master.zip > /dev/null 2>&1 |
| 19 | + |
| 20 | +echo "Installing Platform ESP32 ..." |
| 21 | +python -m platformio platform install $PLATFORMIO_ESP32_URL > /dev/null 2>&1 |
| 22 | + |
| 23 | +echo "Replacing the package versions ..." |
| 24 | +replace_script="import json; import os;" |
| 25 | +replace_script+="fp=open(os.path.expanduser('~/.platformio/platforms/espressif32/platform.json'), 'r+');" |
| 26 | +replace_script+="data=json.load(fp);" |
| 27 | +# Use framework sources from the repository |
| 28 | +replace_script+="data['packages']['framework-arduinoespressif32']['version'] = '*';" |
| 29 | +replace_script+="del data['packages']['framework-arduinoespressif32']['owner'];" |
| 30 | +# Use toolchain packages from the "espressif" organization |
| 31 | +replace_script+="data['packages']['toolchain-xtensa-esp32']['owner']='$ESPRESSIF_ORGANIZATION_NAME';" |
| 32 | +replace_script+="data['packages']['toolchain-xtensa-esp32s2']['owner']='$ESPRESSIF_ORGANIZATION_NAME';" |
| 33 | +replace_script+="data['packages']['toolchain-riscv32-esp']['owner']='$ESPRESSIF_ORGANIZATION_NAME';" |
| 34 | +# Update versions to use the upstream |
| 35 | +replace_script+="data['packages']['toolchain-xtensa-esp32']['version']='$TOOLCHAIN_VERSION';" |
| 36 | +replace_script+="data['packages']['toolchain-xtensa-esp32s2']['version']='$TOOLCHAIN_VERSION';" |
| 37 | +replace_script+="data['packages']['toolchain-xtensa-esp32s3']['version']='$TOOLCHAIN_VERSION';" |
| 38 | +replace_script+="data['packages']['toolchain-riscv32-esp']['version']='$TOOLCHAIN_VERSION';" |
| 39 | +# Add new "framework-arduinoespressif32-libs" package |
| 40 | +# Read "package_esp32_index.template.json" to extract a url to a zip package for "esp32-arduino-libs" |
| 41 | +replace_script+="fpackage=open(os.path.join('package', 'package_esp32_index.template.json'), 'r+');" |
| 42 | +replace_script+="package_data=json.load(fpackage);" |
| 43 | +replace_script+="fpackage.close();" |
| 44 | +replace_script+="libs_package_archive_url=next(next(system['url'] for system in tool['systems'] if system['host'] == 'x86_64-pc-linux-gnu') for tool in package_data['packages'][0]['tools'] if tool['name'] == 'esp32-arduino-libs');" |
| 45 | +replace_script+="data['packages'].update({'framework-arduinoespressif32-libs':{'type':'framework','optional':False,'version':libs_package_archive_url}});" |
| 46 | +replace_script+="data['packages']['toolchain-xtensa-esp32'].update({'optional':False});" |
| 47 | +# esptool.py may require an upstream version (for now platformio is the owner) |
| 48 | +replace_script+="data['packages']['tool-esptoolpy']['version']='$ESPTOOLPY_VERSION';" |
| 49 | +# Save results |
| 50 | +replace_script+="fp.seek(0);fp.truncate();json.dump(data, fp, indent=2);fp.close()" |
| 51 | +python -c "$replace_script" |
| 52 | + |
| 53 | +if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then |
| 54 | + echo "Linking Core..." |
| 55 | + ln -s $GITHUB_WORKSPACE "$PLATFORMIO_ESP32_PATH" |
| 56 | +else |
| 57 | + echo "Cloning Core Repository ..." |
| 58 | + git clone --recursive https://github.com/espressif/arduino-esp32.git "$PLATFORMIO_ESP32_PATH" > /dev/null 2>&1 |
| 59 | +fi |
| 60 | + |
| 61 | +echo "PlatformIO for ESP32 has been installed" |
| 62 | +echo "" |
| 63 | + |
| 64 | +function build_pio_sketch(){ # build_pio_sketch <board> <options> <path-to-ino> |
| 65 | + if [ "$#" -lt 3 ]; then |
| 66 | + echo "ERROR: Illegal number of parameters" |
| 67 | + echo "USAGE: build_pio_sketch <board> <options> <path-to-ino>" |
| 68 | + return 1 |
| 69 | + fi |
| 70 | + |
| 71 | + local board="$1" |
| 72 | + local options="$2" |
| 73 | + local sketch="$3" |
| 74 | + local sketch_dir=$(dirname "$sketch") |
| 75 | + echo "" |
| 76 | + echo "Compiling '"$(basename "$sketch")"' ..." |
| 77 | + python -m platformio ci --board "$board" "$sketch_dir" --project-option="$options" |
| 78 | +} |
| 79 | + |
| 80 | +function build_pio_sketches(){ # build_pio_sketches <board> <options> <examples-path> <chunk> <total-chunks> |
| 81 | + if [ "$#" -lt 3 ]; then |
| 82 | + echo "ERROR: Illegal number of parameters" |
| 83 | + echo "USAGE: build_pio_sketches <board> <options> <examples-path> [<chunk> <total-chunks>]" |
| 84 | + return 1 |
| 85 | + fi |
| 86 | + |
| 87 | + local board=$1 |
| 88 | + local options="$2" |
| 89 | + local examples=$3 |
| 90 | + local chunk_idex=$4 |
| 91 | + local chunks_num=$5 |
| 92 | + |
| 93 | + if [ "$#" -lt 5 ]; then |
| 94 | + chunk_idex="0" |
| 95 | + chunks_num="1" |
| 96 | + fi |
| 97 | + |
| 98 | + if [ "$chunks_num" -le 0 ]; then |
| 99 | + echo "ERROR: Chunks count must be positive number" |
| 100 | + return 1 |
| 101 | + fi |
| 102 | + if [ "$chunk_idex" -ge "$chunks_num" ]; then |
| 103 | + echo "ERROR: Chunk index must be less than chunks count" |
| 104 | + return 1 |
| 105 | + fi |
| 106 | + |
| 107 | + set +e |
| 108 | + ${COUNT_SKETCHES} "$examples" "esp32" |
| 109 | + local sketchcount=$? |
| 110 | + set -e |
| 111 | + local sketches=$(cat sketches.txt) |
| 112 | + rm -rf sketches.txt |
| 113 | + |
| 114 | + local chunk_size=$(( $sketchcount / $chunks_num )) |
| 115 | + local all_chunks=$(( $chunks_num * $chunk_size )) |
| 116 | + if [ "$all_chunks" -lt "$sketchcount" ]; then |
| 117 | + chunk_size=$(( $chunk_size + 1 )) |
| 118 | + fi |
| 119 | + |
| 120 | + local start_index=$(( $chunk_idex * $chunk_size )) |
| 121 | + if [ "$sketchcount" -le "$start_index" ]; then |
| 122 | + echo "Skipping job" |
| 123 | + return 0 |
| 124 | + fi |
| 125 | + |
| 126 | + local end_index=$(( $(( $chunk_idex + 1 )) * $chunk_size )) |
| 127 | + if [ "$end_index" -gt "$sketchcount" ]; then |
| 128 | + end_index=$sketchcount |
| 129 | + fi |
| 130 | + |
| 131 | + local start_num=$(( $start_index + 1 )) |
| 132 | + echo "Found $sketchcount Sketches"; |
| 133 | + echo "Chunk Count : $chunks_num" |
| 134 | + echo "Chunk Size : $chunk_size" |
| 135 | + echo "Start Sketch: $start_num" |
| 136 | + echo "End Sketch : $end_index" |
| 137 | + |
| 138 | + local sketchnum=0 |
| 139 | + for sketch in $sketches; do |
| 140 | + local sketchdir=$(dirname $sketch) |
| 141 | + local sketchdirname=$(basename $sketchdir) |
| 142 | + local sketchname=$(basename $sketch) |
| 143 | + if [[ "$sketchdirname.ino" != "$sketchname" ]]; then |
| 144 | + continue |
| 145 | + elif [ -f $sketchdir/ci.json ]; then |
| 146 | + # If the target is listed as false, skip the sketch. Otherwise, include it. |
| 147 | + is_target=$(jq -r '.targets[esp32]' $sketchdir/ci.json) |
| 148 | + if [[ "$is_target" == "false" ]]; then |
| 149 | + continue |
| 150 | + fi |
| 151 | + |
| 152 | + local has_requirements=$(${CHECK_REQUIREMENTS} $sketchdir "$SDKCONFIG_DIR/esp32/sdkconfig") |
| 153 | + if [ "$has_requirements" == "0" ]; then |
| 154 | + continue |
| 155 | + fi |
| 156 | + fi |
| 157 | + |
| 158 | + sketchnum=$(($sketchnum + 1)) |
| 159 | + if [ "$sketchnum" -le "$start_index" ] \ |
| 160 | + || [ "$sketchnum" -gt "$end_index" ]; then |
| 161 | + continue |
| 162 | + fi |
| 163 | + build_pio_sketch "$board" "$options" "$sketch" |
| 164 | + local result=$? |
| 165 | + if [ $result -ne 0 ]; then |
| 166 | + return $result |
| 167 | + fi |
| 168 | + done |
| 169 | + return 0 |
| 170 | +} |
0 commit comments