xref: /aosp_15_r20/external/coreboot/util/scripts/testsoc (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1#!/bin/bash
2set -e -o pipefail
3
4PROGNAME="$(basename "${0}")"
5VERSION="1.00"
6
7ABUILD="./util/abuild/abuild"
8OUTPUT="coreboot-builds"
9MAINBOARDS=()
10UNSORTED=()
11CPUS=$(nproc || echo "4")
12NO_CROS=0
13
14# Extra arguments to pass to abuild
15ABUILD_ARGS=""
16
17# Text STYLE variables
18BOLD="\033[1m"
19RED='\033[38;5;9m'
20GREEN='\033[38;5;2m'
21NO_COLOR='\033[0m'
22
23usage() {
24	cat <<EOF
25The ${PROGNAME} script helps select boards to run test builds on.  It searches
26through all of the mainboard Kconfig files for specified identifiers and then
27runs abuild on the mainboards it finds.
28
29  Usage: ${PROGNAME} [options]
30
31Options:
32 -a | --abuild "<text>"  Specify options to pass to abuild
33 -C | --cpus <num>       Specify number of CPUs to use (currently ${CPUS})
34 -K | --kconfig <CONFIG> Search for Kconfig option
35 -n | --no_cros	         Don't run chromeos builds
36 -h | --help             Print usage and exit
37 -D | --debug            Print debug information.  Use -DD to show all commands
38 -V | --version          Print the version and exit
39      --nocolor          Don't print color codes
40EOF
41}
42
43_echo_color() {
44	local color="$1"
45	local text="$2"
46	local newline="${3:-0}"
47	if [[ ${newline} == "0" ]]; then
48		printf "${color}%s${NO_COLOR}" "${text}"
49	else
50		printf "${color}%s${NO_COLOR}\n" "${text}"
51	fi
52}
53
54_echo_error() {
55	_echo_color "${RED}" "$*" 1 >&2
56}
57
58show_version() {
59	echo
60	_echo_color "${BOLD}${GREEN}" "${PROGNAME} version ${VERSION}"
61	echo
62}
63
64get_real_dir() (
65	cd -- "$1" >/dev/null 2>&1 || exit 1
66	pwd -P
67)
68
69get_args() {
70	local mblist
71	local mainboards=()
72
73	if ! args="$(getopt -l version,help,debug,nocolor,kconfig:,cpus:,no_cros,abuild: -o a:C:K:nDhV -- "$@")"; then
74		usage
75		exit 1
76	fi
77
78	eval set -- "${args}"
79
80	while true; do
81		case "$1" in
82		-a | --abuild)
83			shift
84			ABUILD_ARGS=$1
85			;;
86		-C | --cpus)
87			shift
88			CPUS=$1
89			;;
90		-K | --kconfig)
91			shift
92			mblist=$(grep -r "$1" src/mainboard | grep Kconfig | sed 's|src/mainboard/||;s|/Kconfig.*||')
93			printf "Adding mainboard for %s\n%s\n" "$1" "${mblist}"
94			echo
95			mapfile -t mainboards <<<"$mblist"
96			UNSORTED+=(${mainboards[@]})
97			;;
98		-n | no_cros)
99			NO_CROS=1
100			;;
101		-D | --debug)
102			if [ -n "${VERBOSE}" ]; then
103				set -x
104			else
105				VERBOSE="V=1"
106			fi
107			;;
108		-h | --help)
109			usage
110			exit 0
111			;;
112		--nocolor)
113			BOLD=""
114			RED=""
115			GREEN=""
116			NO_COLOR=""
117			;;
118		-V | --version) exit 0 ;;
119		--)
120			shift
121			break
122			;;
123		*)
124			_echo_error "Unknown argument '$1'"
125			usage
126			exit 1
127			;;
128		esac
129		shift
130	done
131
132	if [[ -n $1 ]]; then
133		_echo_error "Unknown command '$1'"
134		echo
135		usage
136		exit 1
137	fi
138}
139
140main() {
141	show_version
142	get_args "$@"
143
144	if [[ ! -f "MAINTAINERS" ]]; then
145		echo "${PWD}"
146		_echo_error "Error: This doesn't look like the coreboot directory."
147		exit 1
148	fi
149
150	# Sort and dedupe list
151	mapfile -t MAINBOARDS <<<"$(printf "%s\n" "${UNSORTED[@]}" | sort -u)"
152
153	if [[ "${#MAINBOARDS[@]}" != "0" ]]; then
154		echo "Using ${CPUS} CPUs to build ${#MAINBOARDS[@]} boards:"
155		printf "%s\n" "${MAINBOARDS[@]}"
156		echo
157	else
158		_echo_error "Error: No mainboards found/specified."
159		exit 1
160	fi
161
162	for board in ${MAINBOARDS[*]}; do
163		rm -rf "./${OUTPUT}"
164
165		# Non-CrOS build
166		if ! "${ABUILD}" --exitcode --cpus ${CPUS} --target "${board}" ${ABUILD_ARGS}; then
167			_echo_error "Error: Non-cros build of ${board} failed."
168			exit 1
169		fi
170
171		# CrOS build
172		if [[ ${NO_CROS} -eq 0 ]]; then
173			rm -rf "./${OUTPUT}"
174			if ! "${ABUILD}" --exitcode --cpus ${CPUS} --target "${board}" --chromeos ${ABUILD_ARGS}; then
175				_echo_error "Error: CrOS build of ${board} failed."
176				exit 1
177			fi
178		fi
179
180	done
181
182	echo
183	echo "Successfully built all boards:"
184	printf "%s\n" "${MAINBOARDS[@]}"
185
186}
187
188main "$@"
189