build-sys: no need to install native-messages.h.
[profile/ivi/speech-recognition.git] / build-aux / gen-debug-table
1 #!/bin/bash
2
3 #
4 # Generate the necessary information for resolving line-number information
5 # to function names in C files that reference a certain symbol (by default
6 # mrp_debug). This is used to convert file:line information to function
7 # names while _listing_ debug sites.
8 #
9
10
11 error () {
12     echo "error: $*" 1>&2
13 }
14
15 warning () {
16     echo "warning: $*" 1>&2
17 }
18
19 info () {
20     echo "$*" 1>&2
21 }
22
23 usage () {
24     info "usage: $0 [-s <symbol>] [-q] -o <output> <inputs>"
25     exit ${1:-1}
26 }
27
28 emit () {
29     local _opts
30
31     if [ "$1" = "-n" ]; then
32         _opts="-n"
33         shift
34     else
35         _opts=""
36     fi
37     if [ "$1" = "-e" ]; then
38         _opts="$_opts -e"
39         shift
40     fi
41
42     echo $_opts "$*" >> $OUTPUT
43 }
44
45 emit_preamble() {
46     emit "#include <stdlib.h>"
47     emit "#include <murphy/common/debug.h>"
48     emit
49 }
50
51 emit_postamble() {
52     emit "#include <murphy/common/debug-auto-register.c>"
53 }
54
55 emit_function_info() {
56     prev=""
57     cnt=0
58     while read func line file; do
59         if [ "$prev" != "$file" ]; then
60             if [ $cnt -gt 0 ]; then
61                 emit "    { .line = 0, .func = NULL }"
62                 emit "};"
63                 emit "static mrp_debug_file_t file_$(($cnt - 1)) = {"
64                 emit "    .file = \"$prev\","
65                 emit "    .info = info_$(($cnt - 1))"
66                 emit "};"
67                 emit ""
68             fi
69             emit "/* $file */"
70             emit "static mrp_debug_info_t info_$cnt[] = {"
71             cnt=$(($cnt + 1))
72             prev=$file
73         fi
74         emit "    { .line = $line, .func = \"$func\" },"
75     done
76     if [ $cnt -gt 0 ]; then
77         emit "    { .line = 0, .func = NULL }"
78         emit "};"
79         emit "static mrp_debug_file_t file_$(($cnt - 1)) = {"
80         emit "    .file = \"$prev\","
81         emit "    .info = info_$(($cnt - 1))"
82         emit "};"
83         emit ""
84     fi
85     emit "/* table of all files */"
86     emit "static mrp_debug_file_t *debug_files[] = {"
87     i=0
88     while [ $i -lt $cnt ]; do
89         emit "    &file_$i,"
90         i=$(($i + 1))
91     done
92     emit "    NULL"
93     emit "};"
94     emit ""
95 }
96
97 emit_no_function_info() {
98     emit "/* No ctags found, could not generate debug info... */"
99     emit "static mrp_debug_file_t *debug_files[] = {"
100     emit "    NULL"
101     emit "};"
102     emit ""
103 }
104
105 # set up defaults
106 SYMBOL="mrp_debug\("                  # symbol to look for
107 SOURCE=""                             # default to all file containing $SYMBOL
108 OUTPUT=""                             # no default output, must be specified
109
110 # parse command line
111 while [ -n "${1#-}" ]; do
112     case $1 in
113         -o|--output)
114             if [ -z "$OUTPUT" ]; then
115                 shift
116                 OUTPUT="$1"
117             else
118                 error "Multiple output files requested."
119                 usage
120             fi
121             ;;
122         -s|--symbol)
123             shift;
124            SYMBOL="$1"
125             ;;
126         -v|--verbose)
127             VERBOSE="yes"
128             ;;
129         -h|--help)
130             usage 0
131             ;;
132         -*)
133             error "Unknown option '$1'."
134             usage
135             ;;
136         *)
137             SOURCE="$SOURCE $1"
138             ;;
139     esac
140     shift
141 done
142
143 # check that we've got everything mandatory
144 if [ -z "$OUTPUT" ]; then
145     error "No output file specified (use the -o option)."
146     usage
147 fi
148 if [ -z "$SYMBOL" ]; then
149     SYMBOL="mrp_debug\("
150 fi
151 if [ -z "$SOURCE" ]; then
152     [ -n "VERBOSE" ] && info "Scanning all C files containing $SYMBOL..."
153     SOURCE="`find . -name \*.c -exec grep -Hn $SYMBOL\( {} \; | \
154                   cut -d ':' -f 1 | sort -u`"
155 fi
156
157 [ -n "$VERBOSE" ] && info "Generating debug function info file $OUTPUT..."
158 rm -f $OUTPUT
159 touch $OUTPUT
160
161 # generate the output
162 emit_preamble
163
164 which ctags >& /dev/null
165 if [ "$?" = "0" ]; then
166     ctags -x --c-kinds=f $SOURCE | tr -s '\t' ' ' | \
167         cut -d ' '  -f 1,3,4 | sort -k 3,3 -k 2,2n | emit_function_info
168 else
169     warning "No ctags found, generating empty debug tables..."
170     emit_no_function_info
171 fi
172
173 emit_postamble