|
| 1 | +# Inlined from zephyrproject-rtos/action-zephyr-setup@v1 with caching for |
| 2 | +# west init / west update results. Cache key is derived from the west CLI |
| 3 | +# version and the contents of the west.yml manifest. |
| 4 | +# |
| 5 | +# Usage (drop-in replacement for action-zephyr-setup): |
| 6 | +# - uses: ./.github/actions/deps/ports/zephyr-cp/zephyr-setup |
| 7 | +# with: |
| 8 | +# app-path: zephyr-config |
| 9 | +# base-path: ports/zephyr-cp |
| 10 | +# toolchains: arm-zephyr-eabi |
| 11 | + |
| 12 | +name: Setup Zephyr project (cached) |
| 13 | +description: > |
| 14 | + Setup a Zephyr project using west, with caching of west init/update results. |
| 15 | +
|
| 16 | +inputs: |
| 17 | + app-path: |
| 18 | + description: Application code path (contains west.yml) |
| 19 | + required: true |
| 20 | + |
| 21 | + base-path: |
| 22 | + description: Application base path containing app-path |
| 23 | + required: false |
| 24 | + default: . |
| 25 | + |
| 26 | + manifest-file-name: |
| 27 | + description: Name of the west manifest file in app-path |
| 28 | + required: false |
| 29 | + default: west.yml |
| 30 | + |
| 31 | + toolchains: |
| 32 | + description: "Colon-separated list of toolchains to install" |
| 33 | + required: false |
| 34 | + default: arm-zephyr-eabi |
| 35 | + |
| 36 | + sdk-version: |
| 37 | + description: 'Zephyr SDK version or "auto"' |
| 38 | + required: false |
| 39 | + default: auto |
| 40 | + |
| 41 | + sdk-base: |
| 42 | + description: Base URL for Zephyr SDK downloads |
| 43 | + required: false |
| 44 | + default: https://github.com/zephyrproject-rtos/sdk-ng/releases/download |
| 45 | + |
| 46 | + west-version: |
| 47 | + description: West version to install (latest if empty) |
| 48 | + required: false |
| 49 | + default: "" |
| 50 | + |
| 51 | + enable-ccache: |
| 52 | + description: Enable ccache |
| 53 | + required: false |
| 54 | + default: "true" |
| 55 | + |
| 56 | + ccache-max-size: |
| 57 | + description: Max ccache size |
| 58 | + required: false |
| 59 | + default: 512MB |
| 60 | + |
| 61 | + ccache-cache-key: |
| 62 | + description: Extra string for ccache cache key |
| 63 | + required: false |
| 64 | + default: default |
| 65 | + |
| 66 | +runs: |
| 67 | + using: composite |
| 68 | + steps: |
| 69 | + # ── Install west & host tools ────────────────────────────────────── |
| 70 | + - name: Install dependencies |
| 71 | + shell: bash |
| 72 | + run: | |
| 73 | + if [ "${{ runner.os }}" = "Windows" ]; then |
| 74 | + python.exe -m pip install -U pip |
| 75 | + fi |
| 76 | +
|
| 77 | + pip3 install -U pip wheel |
| 78 | +
|
| 79 | + if [ -n "${{ inputs.west-version }}" ]; then |
| 80 | + pip3 install west==${{ inputs.west-version }} |
| 81 | + else |
| 82 | + pip3 install west |
| 83 | + fi |
| 84 | +
|
| 85 | + if [ "${{ runner.os }}" = "Linux" ]; then |
| 86 | + sudo rm -f /var/lib/man-db/auto-update |
| 87 | + sudo apt-get update -y |
| 88 | + sudo apt-get install -y ccache gperf |
| 89 | + if [ "${{ runner.arch }}" = "X64" ]; then |
| 90 | + sudo apt-get install -y libc6-dev-i386 g++-multilib |
| 91 | + fi |
| 92 | + elif [ "${{ runner.os }}" = "macOS" ]; then |
| 93 | + brew install ccache qemu dtc gperf coreutils |
| 94 | + elif [ "${{ runner.os }}" = "Windows" ]; then |
| 95 | + choco feature enable -n allowGlobalConfirmation |
| 96 | + choco install ninja wget gperf |
| 97 | + fi |
| 98 | +
|
| 99 | + # ── Cache west workspace (.west + fetched repos) ────────────────── |
| 100 | + - name: Get west version for cache key |
| 101 | + id: west-version |
| 102 | + shell: bash |
| 103 | + run: echo "version=$(west --version)" >> $GITHUB_OUTPUT |
| 104 | + |
| 105 | + - name: Cache west workspace |
| 106 | + id: cache-west |
| 107 | + uses: actions/cache@v4 |
| 108 | + with: |
| 109 | + path: | |
| 110 | + ${{ inputs.base-path }}/.west |
| 111 | + ${{ inputs.base-path }}/bootloader |
| 112 | + ${{ inputs.base-path }}/modules |
| 113 | + ${{ inputs.base-path }}/tools |
| 114 | + ${{ inputs.base-path }}/zephyr |
| 115 | + key: west-${{ runner.os }}-${{ steps.west-version.outputs.version }}-${{ hashFiles(format('{0}/{1}/{2}', inputs.base-path, inputs.app-path, inputs.manifest-file-name)) }} |
| 116 | + |
| 117 | + - name: Initialize west workspace |
| 118 | + if: steps.cache-west.outputs.cache-hit != 'true' |
| 119 | + working-directory: ${{ inputs.base-path }} |
| 120 | + shell: bash |
| 121 | + run: | |
| 122 | + west init -l ${{ inputs.app-path }} --mf ${{ inputs.manifest-file-name }} |
| 123 | +
|
| 124 | + - name: Update west modules |
| 125 | + if: steps.cache-west.outputs.cache-hit != 'true' |
| 126 | + working-directory: ${{ inputs.base-path }} |
| 127 | + shell: bash |
| 128 | + run: west update -o=--depth=1 -n |
| 129 | + |
| 130 | + # ── Environment / SDK version detection ─────────────────────────── |
| 131 | + - name: Environment setup |
| 132 | + working-directory: ${{ inputs.base-path }} |
| 133 | + id: env-setup |
| 134 | + shell: bash |
| 135 | + run: | |
| 136 | + runner="${{ runner.os }}-${{ runner.arch }}" |
| 137 | + if [ "$runner" = "Linux-X64" ]; then |
| 138 | + sdk_variant="linux-x86_64" |
| 139 | + elif [ "$runner" = "Linux-ARM64" ]; then |
| 140 | + sdk_variant="linux-aarch64" |
| 141 | + elif [ "$runner" = "macOS-X64" ]; then |
| 142 | + sdk_variant="macos-x86_64" |
| 143 | + elif [ "$runner" = "macOS-ARM64" ]; then |
| 144 | + sdk_variant="macos-aarch64" |
| 145 | + elif [ "$runner" = "Windows-X64" ]; then |
| 146 | + sdk_variant="windows-x86_64" |
| 147 | + else |
| 148 | + echo "Unsupported runner platform: $runner" |
| 149 | + fi |
| 150 | +
|
| 151 | + if [ "${{ runner.os }}" = "Linux" ]; then |
| 152 | + pip_cache_path="~/.cache/pip" |
| 153 | + ccache_path="$HOME/.cache/ccache" |
| 154 | + sdk_ext="tar.xz" |
| 155 | + setup_file="./setup.sh" |
| 156 | + setup_opt="-" |
| 157 | + elif [ "${{ runner.os }}" = "macOS" ]; then |
| 158 | + pip_cache_path="~/Library/Caches/pip" |
| 159 | + ccache_path="$HOME/Library/Caches/ccache" |
| 160 | + sdk_ext="tar.xz" |
| 161 | + setup_file="./setup.sh" |
| 162 | + setup_opt="-" |
| 163 | + elif [ "${{ runner.os }}" = "Windows" ]; then |
| 164 | + pip_cache_path="~/AppData/Local/pip/Cache" |
| 165 | + ccache_path="$HOME/AppData/Local/ccache/Cache" |
| 166 | + sdk_ext="7z" |
| 167 | + setup_file="./setup.cmd" |
| 168 | + setup_opt="//" |
| 169 | + fi |
| 170 | +
|
| 171 | + if [ "${{ inputs.sdk-version }}" = "auto" ]; then |
| 172 | + zephyr_path="zephyr" |
| 173 | + if west list -f '{abspath}' zephyr; then |
| 174 | + zephyr_path="$( west list -f '{abspath}' zephyr )" |
| 175 | + fi |
| 176 | +
|
| 177 | + if [ -f "${zephyr_path}/SDK_VERSION" ]; then |
| 178 | + echo "Reading SDK version from ${zephyr_path}/SDK_VERSION" |
| 179 | + sdk_version=$( cat ${zephyr_path}/SDK_VERSION ) |
| 180 | + else |
| 181 | + echo "Cannot find ${zephyr_path}/SDK_VERSION" |
| 182 | + exit 1 |
| 183 | + fi |
| 184 | + else |
| 185 | + sdk_version="${{ inputs.sdk-version }}" |
| 186 | + fi |
| 187 | +
|
| 188 | + sdk_file="zephyr-sdk-${sdk_version}_${sdk_variant}_minimal.${sdk_ext}" |
| 189 | + sdk_cache_key="$( sha1sum <<< ${sdk_file}-${{ github.event.repository.name }}-${{ inputs.toolchains }} | cut -d' ' -f1 )" |
| 190 | +
|
| 191 | + echo "SDK_VERSION=${sdk_version}" >> $GITHUB_ENV |
| 192 | + echo "SDK_FILE=${sdk_file}" >> $GITHUB_ENV |
| 193 | + echo "SDK_CACHE_KEY=${sdk_cache_key}" >> $GITHUB_ENV |
| 194 | + echo "PIP_CACHE_PATH=${pip_cache_path}" >> $GITHUB_ENV |
| 195 | + echo "CCACHE_TIMESTAMP=$(date +%s)" >> $GITHUB_ENV |
| 196 | + echo "CCACHE_DIR=${ccache_path}" >> $GITHUB_ENV |
| 197 | + echo "CCACHE_IGNOREOPTIONS=-specs=* --specs=*" >> $GITHUB_ENV |
| 198 | + echo "SETUP_FILE=${setup_file}" >> $GITHUB_ENV |
| 199 | + echo "SETUP_OPT=${setup_opt}" >> $GITHUB_ENV |
| 200 | +
|
| 201 | + # ── Python packages ─────────────────────────────────────────────── |
| 202 | + - name: Cache Python packages |
| 203 | + uses: actions/cache@v4 |
| 204 | + with: |
| 205 | + path: ${{ env.PIP_CACHE_PATH }} |
| 206 | + key: pip-${{ runner.os }}-${{ hashFiles(format('{0}/zephyr/scripts/requirements*.txt', inputs.base-path)) }} |
| 207 | + |
| 208 | + - name: Install Python packages |
| 209 | + if: runner.os != 'Windows' |
| 210 | + working-directory: ${{ inputs.base-path }} |
| 211 | + shell: bash |
| 212 | + run: | |
| 213 | + west packages pip --install --ignore-venv-check || pip3 install -r zephyr/scripts/requirements.txt |
| 214 | +
|
| 215 | + - name: Install Python packages (Windows) |
| 216 | + if: runner.os == 'Windows' |
| 217 | + working-directory: ${{ inputs.base-path }} |
| 218 | + shell: powershell |
| 219 | + run: | |
| 220 | + pip3 install @((west packages pip) -split ' '); if (!$?) { pip3 install -r zephyr/scripts/requirements.txt } |
| 221 | +
|
| 222 | + # ── Zephyr SDK ──────────────────────────────────────────────────── |
| 223 | + - name: Cache Zephyr SDK |
| 224 | + id: cache-toolchain |
| 225 | + uses: actions/cache@v4 |
| 226 | + with: |
| 227 | + path: ${{ inputs.base-path }}/zephyr-sdk |
| 228 | + key: ${{ env.SDK_CACHE_KEY }} |
| 229 | + |
| 230 | + - name: Download Zephyr SDK |
| 231 | + if: steps.cache-toolchain.outputs.cache-hit != 'true' |
| 232 | + working-directory: ${{ inputs.base-path }} |
| 233 | + shell: bash |
| 234 | + run: | |
| 235 | + wget --progress=dot:giga ${{ inputs.sdk-base }}/v${SDK_VERSION}/${SDK_FILE} |
| 236 | + rm -rf zephyr-sdk |
| 237 | + if [ "${{ runner.os }}" = "Windows" ]; then |
| 238 | + 7z x $SDK_FILE |
| 239 | + mv zephyr-sdk-${SDK_VERSION} zephyr-sdk |
| 240 | + else |
| 241 | + mkdir zephyr-sdk |
| 242 | + tar xvf $SDK_FILE -C zephyr-sdk --strip-components=1 |
| 243 | + fi |
| 244 | + rm -f $SDK_FILE |
| 245 | +
|
| 246 | + - name: Setup Zephyr SDK |
| 247 | + working-directory: ${{ inputs.base-path }}/zephyr-sdk |
| 248 | + shell: bash |
| 249 | + run: | |
| 250 | + IFS=":" |
| 251 | + TOOLCHAINS="${{ inputs.toolchains }}" |
| 252 | + for toolchain in $TOOLCHAINS; do |
| 253 | + ${SETUP_FILE} ${SETUP_OPT}t $toolchain |
| 254 | + done |
| 255 | + if [ ! -d sysroots ]; then |
| 256 | + ${SETUP_FILE} ${SETUP_OPT}h |
| 257 | + fi |
| 258 | + ${SETUP_FILE} ${SETUP_OPT}c |
| 259 | +
|
| 260 | + # ── ccache ──────────────────────────────────────────────────────── |
| 261 | + - name: Cache ccache data |
| 262 | + if: inputs.enable-ccache == 'true' && runner.os != 'Windows' |
| 263 | + uses: actions/cache@v4 |
| 264 | + with: |
| 265 | + path: ${{ env.CCACHE_DIR }} |
| 266 | + key: ccache-${{ inputs.ccache-cache-key }}-${{ env.CCACHE_TIMESTAMP }} |
| 267 | + restore-keys: ccache-${{ inputs.ccache-cache-key }}- |
| 268 | + |
| 269 | + - name: Set up ccache |
| 270 | + if: inputs.enable-ccache == 'true' && runner.os != 'Windows' |
| 271 | + shell: bash |
| 272 | + run: | |
| 273 | + mkdir -p ${{ env.CCACHE_DIR }} |
| 274 | + ccache -M ${{ inputs.ccache-max-size }} |
| 275 | + ccache -p |
| 276 | + ccache -z -s -vv |
0 commit comments