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