xref: /freebsd/contrib/libfido2/.actions/build-bsd (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1#!/bin/sh -eux
2
3# Copyright (c) 2022 Yubico AB. All rights reserved.
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file.
6# SPDX-License-Identifier: BSD-2-Clause
7
8BASE_URL="https://builds.sr.ht"
9MANIFEST="$(mktemp)"
10LOGFILE="$(mktemp)"
11trap '[ -f "${LOGFILE}" ] && cat -- "${LOGFILE}"' EXIT
12
13# construct the sourcehut build manifest
14cat > "${MANIFEST}" <<- EOF
15image: ${IMAGE}
16packages:
17  - cmake
18  - llvm
19  - pcsc-lite
20EOF
21
22case "${IMAGE}" in
23	freebsd*)
24cat >> "${MANIFEST}" <<- EOF
25  - libcbor
26  - pkgconf
27EOF
28	;;
29esac
30
31cat >> "${MANIFEST}" <<- EOF
32sources:
33  - ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}#$(git rev-parse HEAD)
34tasks:
35  - build: |
36      if [ "\$(uname)" = "OpenBSD" ]; then
37        SUDO="doas -u root"
38      else
39        SUDO=sudo
40      fi
41      SCAN="/usr/local/bin/scan-build --use-cc=/usr/bin/cc --status-bugs"
42      cd libfido2
43      for T in Debug Release; do
44        mkdir build-\$T
45        (cd build-\$T && \${SCAN} cmake -DCMAKE_BUILD_TYPE=\$T ..)
46        \${SCAN} make -j"\$(sysctl -n hw.ncpu)" -C build-\$T
47        make -C build-\$T regress
48        \${SUDO} make -C build-\$T install
49      done
50EOF
51
52q() {
53	curl \
54		--silent \
55		--oauth2-bearer "${SOURCEHUT_TOKEN}" \
56		--header "Content-Type: application/json" \
57		--data @- -- \
58		"${BASE_URL}/query" \
59	| tee -a -- "${LOGFILE}"
60}
61
62submit_job() {
63	local manifest="$1"
64	jq \
65		--compact-output --null-input \
66		'{ query: $body, variables: { var: $var } }' \
67		--arg body 'mutation($var: String!) { submit(manifest: $var) { id } }' \
68		--rawfile var "${manifest}" \
69	| q \
70	| jq --exit-status --raw-output '.data.submit.id'
71}
72
73job_status() {
74	local id="$1"
75	jq \
76		--compact-output --null-input \
77		'{ query: $body, variables: { var: $var } }' \
78		--arg body 'query($var: Int!) { job(id: $var) { status } }' \
79		--argjson var "${id}" \
80	| q \
81	| jq --exit-status --raw-output '.data.job.status'
82}
83
84JOB_ID="$(submit_job "${MANIFEST}")" || exit 1
85[ -z "${JOB_ID}" ] && exit 1
86echo "Job '${JOB_ID}' running at ${BASE_URL}/~yubico-libfido2/job/${JOB_ID}"
87
88while true; do
89	JOB_STATUS="$(job_status "${JOB_ID}")" || exit 1
90	case "${JOB_STATUS}" in
91		SUCCESS) exit 0;;
92		FAILED) exit 1;;
93		PENDING|QUEUED|RUNNING) ;;
94		*) exit 1;;
95	esac
96	sleep 60
97done
98