0fbeb5b0f5e517a4b7bba3d373d26e282c295f7f
[openwrt/openwrt.git] / .github / workflows / build.yml
1 name: Build sub target
2
3 on:
4 workflow_call:
5 secrets:
6 coverity_api_token:
7 inputs:
8 container_name:
9 type: string
10 default: tools
11 target:
12 required: true
13 type: string
14 subtarget:
15 required: true
16 type: string
17 testing:
18 type: boolean
19 build_toolchain:
20 type: boolean
21 include_feeds:
22 type: boolean
23 build_full:
24 type: boolean
25 build_kernel:
26 type: boolean
27 build_all_modules:
28 type: boolean
29 build_all_kmods:
30 type: boolean
31 build_all_boards:
32 type: boolean
33 use_openwrt_container:
34 type: boolean
35 default: true
36 coverity_project_name:
37 type: string
38 default: OpenWrt
39 coverity_check_packages:
40 type: string
41 coverity_compiler_template_list:
42 type: string
43 default: >-
44 arm-openwrt-linux-gcc
45 coverity_force_compile_packages:
46 type: string
47 default: >-
48 curl
49 libnl
50 mbedtls
51 wolfssl
52 openssl
53 build_external_toolchain:
54 type: boolean
55 upload_external_toolchain:
56 type: boolean
57 use_ccache_cache:
58 type: boolean
59 default: true
60 ccache_type:
61 type: string
62 default: kernel
63
64 permissions:
65 contents: read
66
67 jobs:
68 setup_build:
69 name: Setup build ${{ inputs.target }}/${{ inputs.subtarget }}
70 runs-on: ubuntu-latest
71 outputs:
72 owner_lc: ${{ steps.lower_owner.outputs.owner_lc }}
73 container_tag: ${{ steps.determine_tools_container.outputs.container_tag }}
74 container_name: ${{ steps.determine_tools_container.outputs.container_name }}
75
76 steps:
77 - name: Checkout
78 uses: actions/checkout@v3
79
80 - name: Set lower case owner name
81 id: lower_owner
82 run: |
83 OWNER_LC=$(echo "${{ github.repository_owner }}" \
84 | tr '[:upper:]' '[:lower:]')
85
86 if [ ${{ inputs.use_openwrt_container }} == "true" ]; then
87 OWNER_LC=openwrt
88 fi
89
90 echo "owner_lc=$OWNER_LC" >> $GITHUB_OUTPUT
91
92 # Per branch tools container tag
93 # By default stick to latest
94 # For official test targetting openwrt stable branch
95 # Get the branch or parse the tag and push dedicated tools containers
96 # For local test to use the correct container for stable release testing
97 # you need to use for the branch name a prefix of openwrt-[0-9][0-9].[0-9][0-9]-
98 - name: Determine tools container tag
99 id: determine_tools_container
100 run: |
101 CONTAINER_NAME=${{ inputs.container_name }}
102 CONTAINER_TAG=latest
103 if [ -n "${{ github.base_ref }}" ]; then
104 if echo "${{ github.base_ref }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]$'; then
105 CONTAINER_TAG="${{ github.base_ref }}"
106 fi
107 elif [ ${{ github.ref_type }} == "branch" ]; then
108 if echo "${{ github.ref_name }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]$'; then
109 CONTAINER_TAG=${{ github.ref_name }}
110 elif echo "${{ github.ref_name }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]-'; then
111 CONTAINER_TAG="$(echo ${{ github.ref_name }} | sed 's/^\(openwrt-[0-9][0-9]\.[0-9][0-9]\)-.*/\1/')"
112 fi
113 elif [ ${{ github.ref_type }} == "tag" ]; then
114 if echo "${{ github.ref_name }}" | grep -q -E '^v[0-9][0-9]\.[0-9][0-9]\..+'; then
115 CONTAINER_TAG=openwrt-"$(echo ${{ github.ref_name }} | sed 's/^v\([0-9][0-9]\.[0-9][0-9]\)\..\+/\1/')"
116 fi
117 fi
118
119 if [ "$CONTAINER_NAME" = "toolchain" ]; then
120 GHCR_TOKEN=$(echo ${{ secrets.GITHUB_TOKEN }} | base64)
121 GHCR_HEADER="Authorization: Bearer ${GHCR_TOKEN}"
122 GHCR_MANIFEST_LINK=https://ghcr.io/v2/${{ steps.lower_owner.outputs.owner_lc }}/${{ inputs.container_name }}/manifests/${{ inputs.target }}-${{ inputs.subtarget }}-"$CONTAINER_TAG"
123 # Check if container exist
124 if [ $(curl -s -o /dev/null -w "%{http_code}" -H "$GHCR_HEADER" -I "$GHCR_MANIFEST_LINK") = 200 ]; then
125 CONTAINER_TAG=${{ inputs.target }}-${{ inputs.subtarget }}-"$CONTAINER_TAG"
126 else
127 CONTAINER_NAME=tools
128 fi
129 fi
130
131 echo "Tools container to use $CONTAINER_NAME:$CONTAINER_TAG"
132 echo "container_tag=$CONTAINER_TAG" >> $GITHUB_OUTPUT
133 echo "container_name=$CONTAINER_NAME" >> $GITHUB_OUTPUT
134
135 build:
136 name: Build ${{ inputs.target }}/${{ inputs.subtarget }}
137 needs: setup_build
138 runs-on: ubuntu-latest
139
140 container: ghcr.io/${{ needs.setup_build.outputs.owner_lc }}/${{ needs.setup_build.outputs.container_name }}:${{ needs.setup_build.outputs.container_tag }}
141
142 permissions:
143 contents: read
144 packages: read
145 actions: write
146
147 steps:
148 - name: Checkout master directory
149 uses: actions/checkout@v3
150 with:
151 path: openwrt
152
153 - name: Checkout packages feed
154 if: inputs.include_feeds == true
155 uses: actions/checkout@v3
156 with:
157 repository: openwrt/packages
158 path: openwrt/feeds/packages
159
160 - name: Checkout luci feed
161 if: inputs.include_feeds == true
162 uses: actions/checkout@v3
163 with:
164 repository: openwrt/luci
165 path: openwrt/feeds/luci
166
167 - name: Checkout routing feed
168 if: inputs.include_feeds == true
169 uses: actions/checkout@v3
170 with:
171 repository: openwrt/routing
172 path: openwrt/feeds/routing
173
174 - name: Checkout telephony feed
175 if: inputs.include_feeds == true
176 uses: actions/checkout@v3
177 with:
178 repository: openwrt/telephony
179 path: openwrt/feeds/telephony
180
181 - name: Parse toolchain file
182 if: inputs.build_toolchain == false
183 id: parse-toolchain
184 working-directory: openwrt
185 run: |
186 if [ -d /external-toolchain/ ]; then
187 echo "toolchain-type=external_container" >> $GITHUB_OUTPUT
188 exit 0
189 fi
190
191 TOOLCHAIN_PATH=snapshots
192
193 if [ -n "${{ github.base_ref }}" ]; then
194 if echo "${{ github.base_ref }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]$'; then
195 major_ver="$(echo ${{ github.base_ref }} | sed 's/^openwrt-/v/')"
196 fi
197 elif [ "${{ github.ref_type }}" = "branch" ]; then
198 if echo "${{ github.ref_name }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]$'; then
199 major_ver="$(echo ${{ github.ref_name }} | sed 's/^openwrt-/v/')"
200 elif echo "${{ github.ref_name }}" | grep -q -E '^openwrt-[0-9][0-9]\.[0-9][0-9]-'; then
201 major_ver="$(echo ${{ github.ref_name }} | sed 's/^openwrt-\([0-9][0-9]\.[0-9][0-9]\)-.*/v\1/')"
202 fi
203 elif [ "${{ github.ref_type }}" = "tag" ]; then
204 if echo "${{ github.ref_name }}" | grep -q -E '^v[0-9][0-9]\.[0-9][0-9]\..+'; then
205 major_ver="$(echo ${{ github.ref_name }} | sed 's/^\(v[0-9][0-9]\.[0-9][0-9]\)\..\+/\1/')"
206 fi
207 fi
208
209 if [ -n "$major_ver" ]; then
210 git fetch --tags -f
211 latest_tag="$(git tag --sort=-creatordate -l $major_ver* | head -n1)"
212 if [ -n "$latest_tag" ]; then
213 TOOLCHAIN_PATH=releases/$(echo $latest_tag | sed 's/^v//')
214 fi
215 fi
216
217 SUMS_FILE="https://downloads.cdn.openwrt.org/$TOOLCHAIN_PATH/targets/${{ inputs.target }}/${{ inputs.subtarget }}/sha256sums"
218 if curl $SUMS_FILE | grep -q ".*openwrt-toolchain.*tar.xz"; then
219 TOOLCHAIN_STRING="$( curl $SUMS_FILE | grep ".*openwrt-toolchain.*tar.xz")"
220 TOOLCHAIN_FILE=$(echo "$TOOLCHAIN_STRING" | sed -n -e 's/.*\(openwrt-toolchain.*\).tar.xz/\1/p')
221
222 echo "toolchain-type=external_toolchain" >> $GITHUB_OUTPUT
223 elif curl $SUMS_FILE | grep -q ".*openwrt-sdk.*tar.xz"; then
224 TOOLCHAIN_STRING="$( curl $SUMS_FILE | grep ".*openwrt-sdk.*tar.xz")"
225 TOOLCHAIN_FILE=$(echo "$TOOLCHAIN_STRING" | sed -n -e 's/.*\(openwrt-sdk.*\).tar.xz/\1/p')
226
227 echo "toolchain-type=external_sdk" >> $GITHUB_OUTPUT
228 else
229 echo "toolchain-type=internal" >> $GITHUB_OUTPUT
230 fi
231
232 echo "TOOLCHAIN_FILE=$TOOLCHAIN_FILE" >> "$GITHUB_ENV"
233 echo "TOOLCHAIN_PATH=$TOOLCHAIN_PATH" >> "$GITHUB_ENV"
234
235 - name: Fix permission
236 run: |
237 chown -R buildbot:buildbot openwrt
238
239 - name: Prepare prebuilt tools
240 shell: su buildbot -c "sh -e {0}"
241 working-directory: openwrt
242 run: |
243 mkdir -p staging_dir build_dir
244 ln -s /prebuilt_tools/staging_dir/host staging_dir/host
245 ln -s /prebuilt_tools/build_dir/host build_dir/host
246
247 ./scripts/ext-tools.sh --refresh
248
249 - name: Update & Install feeds
250 if: inputs.include_feeds == true
251 shell: su buildbot -c "sh -e {0}"
252 working-directory: openwrt
253 run: |
254 ./scripts/feeds update -a
255 ./scripts/feeds install -a
256
257 - name: Restore ccache cache
258 id: restore-ccache-cache
259 if: inputs.use_ccache_cache == true
260 uses: actions/cache/restore@v3
261 with:
262 path: openwrt/.ccache
263 key: ccache-${{ inputs.ccache_type }}-${{ inputs.target }}/${{ inputs.subtarget }}-${{ hashFiles('openwrt/include/kernel-**') }}
264 restore-keys: |
265 ccache-${{ inputs.ccache_type }}-${{ inputs.target }}/${{ inputs.subtarget }}-
266
267 - name: Download external toolchain/sdk
268 if: inputs.build_toolchain == false && steps.parse-toolchain.outputs.toolchain-type != 'internal' && steps.parse-toolchain.outputs.toolchain-type != 'external_container'
269 shell: su buildbot -c "sh -e {0}"
270 working-directory: openwrt
271 run: |
272 wget -O - https://downloads.cdn.openwrt.org/${{ env.TOOLCHAIN_PATH }}/targets/${{ inputs.target }}/${{ inputs.subtarget }}/${{ env.TOOLCHAIN_FILE }}.tar.xz \
273 | tar --xz -xf -
274
275 - name: Configure testing kernel
276 if: inputs.testing == true
277 shell: su buildbot -c "sh -e {0}"
278 working-directory: openwrt
279 run: |
280 echo CONFIG_TESTING_KERNEL=y >> .config
281
282 - name: Configure all kernel modules
283 if: inputs.build_all_kmods == true
284 shell: su buildbot -c "sh -e {0}"
285 working-directory: openwrt
286 run: |
287 echo CONFIG_ALL_KMODS=y >> .config
288
289 - name: Configure all modules
290 if: inputs.build_all_modules == true
291 shell: su buildbot -c "sh -e {0}"
292 working-directory: openwrt
293 run: |
294 echo CONFIG_ALL=y >> .config
295
296 - name: Configure all boards
297 if: inputs.build_all_boards == true
298 shell: su buildbot -c "sh -e {0}"
299 working-directory: openwrt
300 run: |
301 echo CONFIG_TARGET_MULTI_PROFILE=y >> .config
302 echo CONFIG_TARGET_PER_DEVICE_ROOTFS=y >> .config
303 echo CONFIG_TARGET_ALL_PROFILES=y >> .config
304
305 - name: Configure ccache
306 if: inputs.use_ccache_cache == true
307 shell: su buildbot -c "sh -e {0}"
308 working-directory: openwrt
309 run: |
310 echo CONFIG_CCACHE=y >> .config
311
312 - name: Configure external toolchain in container
313 if: inputs.build_toolchain == false && steps.parse-toolchain.outputs.toolchain-type == 'external_container'
314 shell: su buildbot -c "sh -e {0}"
315 working-directory: openwrt
316 run: |
317 echo CONFIG_DEVEL=y >> .config
318 echo CONFIG_AUTOREMOVE=y >> .config
319
320 ./scripts/ext-toolchain.sh \
321 --toolchain /external-toolchain/$(ls /external-toolchain/ | grep openwrt-toolchain)/toolchain-* \
322 --overwrite-config \
323 --config ${{ inputs.target }}/${{ inputs.subtarget }}
324
325 - name: Configure external toolchain
326 if: inputs.build_toolchain == false && steps.parse-toolchain.outputs.toolchain-type == 'external_toolchain'
327 shell: su buildbot -c "sh -e {0}"
328 working-directory: openwrt
329 run: |
330 echo CONFIG_DEVEL=y >> .config
331 echo CONFIG_AUTOREMOVE=y >> .config
332
333 ./scripts/ext-toolchain.sh \
334 --toolchain ${{ env.TOOLCHAIN_FILE }}/toolchain-* \
335 --overwrite-config \
336 --config ${{ inputs.target }}/${{ inputs.subtarget }}
337
338 - name: Adapt external sdk to external toolchain format
339 if: inputs.build_toolchain == false && steps.parse-toolchain.outputs.toolchain-type == 'external_sdk'
340 shell: su buildbot -c "sh -e {0}"
341 working-directory: openwrt
342 run: |
343 TOOLCHAIN_DIR=${{ env.TOOLCHAIN_FILE }}/staging_dir/$(ls ${{ env.TOOLCHAIN_FILE }}/staging_dir | grep toolchain)
344 TOOLCHAIN_BIN=$TOOLCHAIN_DIR/bin
345 OPENWRT_DIR=$(pwd)
346
347 # Find target name from toolchain info.mk
348 GNU_TARGET_NAME=$(cat $TOOLCHAIN_DIR/info.mk | grep TARGET_CROSS | sed 's/^TARGET_CROSS=\(.*\)-$/\1/')
349
350 cd $TOOLCHAIN_BIN
351
352 # Revert sdk wrapper scripts applied to all the bins
353 for app in $(find . -name "*.bin"); do
354 TARGET_APP=$(echo $app | sed 's/\.\/\.\(.*\)\.bin/\1/')
355 rm $TARGET_APP
356 mv .$TARGET_APP.bin $TARGET_APP
357 done
358
359 # Setup the wrapper script in the sdk toolchain dir simulating an external toolchain build
360 cp $OPENWRT_DIR/target/toolchain/files/wrapper.sh $GNU_TARGET_NAME-wrapper.sh
361 for app in cc gcc g++ c++ cpp ld as ; do
362 [ -f $GNU_TARGET_NAME-$app ] && mv $GNU_TARGET_NAME-$app $GNU_TARGET_NAME-$app.bin
363 ln -sf $GNU_TARGET_NAME-wrapper.sh $GNU_TARGET_NAME-$app
364 done
365
366 - name: Configure external toolchain with sdk
367 if: inputs.build_toolchain == false && steps.parse-toolchain.outputs.toolchain-type == 'external_sdk'
368 shell: su buildbot -c "sh -e {0}"
369 working-directory: openwrt
370 run: |
371 echo CONFIG_DEVEL=y >> .config
372 echo CONFIG_AUTOREMOVE=y >> .config
373
374 ./scripts/ext-toolchain.sh \
375 --toolchain ${{ env.TOOLCHAIN_FILE }}/staging_dir/toolchain-* \
376 --overwrite-config \
377 --config ${{ inputs.target }}/${{ inputs.subtarget }}
378
379 - name: Configure internal toolchain
380 if: inputs.build_toolchain == true || steps.parse-toolchain.outputs.toolchain-type == 'internal'
381 shell: su buildbot -c "sh -e {0}"
382 working-directory: openwrt
383 run: |
384 echo CONFIG_DEVEL=y >> .config
385 echo CONFIG_AUTOREMOVE=y >> .config
386
387 echo "CONFIG_TARGET_${{ inputs.target }}=y" >> .config
388 echo "CONFIG_TARGET_${{ inputs.target }}_${{ inputs.subtarget }}=y" >> .config
389
390 make defconfig
391
392 - name: Show configuration
393 shell: su buildbot -c "sh -e {0}"
394 working-directory: openwrt
395 run: ./scripts/diffconfig.sh
396
397 - name: Build tools
398 shell: su buildbot -c "sh -e {0}"
399 working-directory: openwrt
400 run: make tools/install -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
401
402 - name: Build toolchain
403 shell: su buildbot -c "sh -e {0}"
404 working-directory: openwrt
405 run: make toolchain/install -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
406
407 - name: Build Kernel
408 if: inputs.build_kernel == true
409 shell: su buildbot -c "sh -e {0}"
410 working-directory: openwrt
411 run: make target/compile -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
412
413 - name: Build Kernel Kmods
414 if: inputs.build_kernel == true
415 shell: su buildbot -c "sh -e {0}"
416 working-directory: openwrt
417 run: make package/linux/compile -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
418
419 - name: Build everything
420 if: inputs.build_full == true
421 shell: su buildbot -c "sh -e {0}"
422 working-directory: openwrt
423 run: make -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
424
425 - name: Build external toolchain
426 if: inputs.build_external_toolchain == true
427 shell: su buildbot -c "sh -e {0}"
428 working-directory: openwrt
429 run: make target/toolchain/compile -j$(nproc) BUILD_LOG=1 || ret=$? .github/workflows/scripts/show_build_failures.sh
430
431 - name: Coverity prepare toolchain
432 if: inputs.coverity_check_packages != ''
433 shell: su buildbot -c "sh -e {0}"
434 working-directory: openwrt
435 run: |
436 wget -q https://scan.coverity.com/download/linux64 --post-data "token=${{ secrets.coverity_api_token }}&project=${{ inputs.coverity_project_name }}" -O coverity.tar.gz
437 wget -q https://scan.coverity.com/download/linux64 --post-data "token=${{ secrets.coverity_api_token }}&project=${{ inputs.coverity_project_name }}&md5=1" -O coverity.tar.gz.md5
438 echo ' coverity.tar.gz' >> coverity.tar.gz.md5
439 md5sum -c coverity.tar.gz.md5
440
441 mkdir cov-analysis-linux64
442 tar xzf coverity.tar.gz --strip 1 -C cov-analysis-linux64
443 export PATH=$(pwd)/cov-analysis-linux64/bin:$PATH
444
445 for template in ${{ inputs.coverity_compiler_template_list }}; do
446 cov-configure --template --comptype gcc --compiler "$template"
447 done
448
449 - name: Clean and recompile packages with Coverity toolchain
450 if: inputs.coverity_check_packages != ''
451 shell: su buildbot -c "bash {0}"
452 working-directory: openwrt
453 run: |
454 set -o pipefail -o errexit
455
456 coverity_check_packages=(${{ inputs.coverity_check_packages }})
457 printf -v clean_packages "package/%s/clean " "${coverity_check_packages[@]}"
458 make -j$(nproc) BUILD_LOG=1 $clean_packages || ret=$? .github/workflows/scripts/show_build_failures.sh
459
460 coverity_force_compile_packages=(${{ inputs.coverity_force_compile_packages }})
461 printf -v force_compile_packages "package/%s/compile " "${coverity_force_compile_packages[@]}"
462 make -j$(nproc) BUILD_LOG=1 $force_compile_packages || ret=$? .github/workflows/scripts/show_build_failures.sh
463
464 printf -v compile_packages "package/%s/compile " "${coverity_check_packages[@]}"
465 export PATH=$(pwd)/cov-analysis-linux64/bin:$PATH
466 cov-build --dir cov-int make -j $(nproc) BUILD_LOG=1 $compile_packages || ret=$? .github/workflows/scripts/show_build_failures.sh
467
468 - name: Upload build to Coverity for analysis
469 if: inputs.coverity_check_packages != ''
470 shell: su buildbot -c "sh -e {0}"
471 working-directory: openwrt
472 run: |
473 tar czf cov-int.tar.gz ./cov-int
474 curl \
475 --form token="${{ secrets.coverity_api_token }}" \
476 --form email="contact@openwrt.org" \
477 --form file=@cov-int.tar.gz \
478 --form version="${{ github.ref_name }}-${{ github.sha }}" \
479 --form description="OpenWrt ${{ github.ref_name }}-${{ github.sha }}" \
480 "https://scan.coverity.com/builds?project=${{ inputs.coverity_project_name }}"
481
482 - name: Upload logs
483 if: failure()
484 uses: actions/upload-artifact@v3
485 with:
486 name: ${{ inputs.target }}-${{ inputs.subtarget }}-logs
487 path: "openwrt/logs"
488
489 - name: Delete already present ccache cache
490 if: steps.restore-ccache-cache.outputs.cache-hit == 'true' && inputs.use_ccache_cache == true
491 uses: octokit/request-action@v2.x
492 with:
493 route: DELETE /repos/{repository}/actions/caches?key={key}
494 env:
495 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
496 INPUT_REPOSITORY: ${{ github.repository }}
497 INPUT_KEY: ${{ steps.restore-ccache-cache.outputs.cache-primary-key }}
498
499 - name: Save ccache cache
500 if: inputs.use_ccache_cache == true
501 uses: actions/cache/save@v3
502 with:
503 path: openwrt/.ccache
504 key: ${{ steps.restore-ccache-cache.outputs.cache-primary-key }}
505
506 - name: Find external toolchain name
507 id: get-toolchain-name
508 if: inputs.upload_external_toolchain == true
509 working-directory: openwrt
510 run: |
511 TOOLCHAIN_NAME=$(ls bin/targets/${{inputs.target }}/${{ inputs.subtarget }} | grep toolchain)
512 echo "toolchain-name=$TOOLCHAIN_NAME" >> $GITHUB_OUTPUT
513
514 - name: Upload prebuilt toolchain
515 if: inputs.upload_external_toolchain == true
516 uses: actions/upload-artifact@v3
517 with:
518 name: ${{ inputs.target }}-${{ inputs.subtarget }}-external-toolchain
519 path: openwrt/bin/targets/${{ inputs.target }}/${{ inputs.subtarget }}/${{ steps.get-toolchain-name.outputs.toolchain-name }}
520 retention-days: 1