xref: /spdk/scripts/bpf/gen_enums.sh (revision da231290b273ae555f25c4eb12a7f34efee0d29a)
1#!/usr/bin/env bash
2set -e
3
4rootdir=$(git rev-parse --show-toplevel)
5
6_print_enums() {
7	local enum_type=$1 enum_string=$2 enum_prefix=$3 enum output
8
9	output=$(< "$rootdir/$(git -C "$rootdir" grep -G -l "$enum_string" -- lib module)")
10
11	# Isolate the enum block
12	output=${output#*$enum_string$'\n'} output=${output%%$'\n'\};*}
13	# Fold it onto an array
14	IFS="," read -ra output <<< "${output//[[:space:]]/}"
15	# Drop the assignments
16	output=("${output[@]/=*/}")
17
18	for enum in "${!output[@]}"; do
19		if [[ ${output[enum]} != "$enum_prefix"* ]]; then
20			printf 'enum name %s does not start with expected prefix %s\n' "${output[enum]}" "$enum_prefix"
21			return 1
22		fi >&2
23		printf '  @%s[%d] = "%s";\n' "$enum_type" "$enum" "${output[enum]#$enum_prefix}"
24	done
25}
26
27print_enums() {
28	for state in "${!state_enums[@]}"; do
29		_print_enums "$state" "${state_enums["$state"]}" "${state_prefix["$state"]}"
30	done
31}
32
33print_clear() { printf '  clear(@%s);\n' "${!state_enums[@]}"; }
34
35declare -A state_enums=() state_prefix=()
36
37state_enums["target"]="enum nvmf_tgt_state {"
38state_enums["subsystem"]="enum spdk_nvmf_subsystem_state {"
39state_prefix["target"]=NVMF_TGT_
40state_prefix["subsystem"]=SPDK_NVMF_SUBSYSTEM_
41
42enums=$(print_enums)
43clear=$(print_clear)
44
45# Add an empty line before "BEGIN {" to avoid it being commented out
46# when there is annotation at the end of bpftrace script
47cat <<- ENUM
48
49	BEGIN {
50		$enums
51	}
52	END {
53		$clear
54	}
55ENUM
56