TODO: Note future addition of STA_REF_DBG as Kconfig option
[wave300.git] / support / mtlkfront.sh
blobb339e449c04ce1dec3252548da27fd0d1e49336d
1 #!/bin/sh
3 #Basic Script configuration
4 mtlkfront_do_output_processing=y
5 print_when_checking=n
6 print_when_preprocessing=y
7 trace_commands=n
8 do_logger_preprocessing=y
10 is_source_file()
12 test "x`echo $1 | grep '\.\(c\|cpp\)$'`" != x
15 logger_preprocessing_enabled()
17 test "x$do_logger_preprocessing" = "xy"
20 check_mtlk_inc_source_file()
22 awk "/^[[:blank:]]*\#include[[:blank:]]+[\"\<]mtlkinc\.h[\"\>]/ { exit 0; } \
23 /^[[:blank:]]*\#include/ { print NR; exit 1; }" < $1 &&
24 return 0
26 return 1
29 check_mtlk_inc_header_file()
31 awk "/^[[:blank:]]*\#include[[:blank:]]+[\"\<]mtlkinc\.h[\"\>]/ { print NR; exit 1; }" < $1 &&
32 return 0
34 return 1
37 check_mtlk_inc()
39 if is_source_file $1
40 then
41 LINE_NO=`check_mtlk_inc_source_file $1`
42 else
43 LINE_NO=`check_mtlk_inc_header_file $1`
46 if [ "$LINE_NO" != "" ] ;
47 then
48 echo $1\($LINE_NO\) : error: wrong mtlkinc.h inclusion
49 return 1
52 return 0
55 #Output-postprocessing part
56 #Performs output preprocessing and symlinks parsing to hide rebasing
57 if test x"$mtlkfront_do_output_processing" = x"y"; then
58 FILTER_CMD=do_filtering
59 else
60 FILTER_CMD=cat
63 do_filtering () {
65 perl -e '
66 use strict;
68 my $builddir=path_to_regexp($ARGV[0]);
69 my $srcdir=$ARGV[1];
71 while (<STDIN>)
73 my $line=$_;
74 my @file_names = ();
76 #Get all substrings that look like file names from the string
77 if(@file_names = ($line =~ /[\w\d\.\/\-]+\/+/g))
79 foreach my $file_name (@file_names)
81 my $full_name=trim(`([ -e $file_name ] && readlink -f $file_name) || echo $file_name`);
82 my $file_name_regexp = path_to_regexp($file_name);
83 $line =~ s/$file_name_regexp/$full_name\//;
87 $line =~ s/$builddir/$srcdir/g;
88 #Remove multiple consequent slashes
89 $line =~ s/([a-zA-Z0-9_\.\-])\/+([a-zA-Z0-9_\.\-])/\1\/\2/g;
90 print "$line";
93 sub trim($)
95 my $string = shift;
96 $string =~ s/^\s+//;
97 $string =~ s/[\s\r]+$//;
98 return $string;
101 sub path_to_regexp($)
103 my $path = shift;
104 $path =~ s/([\:\\\/\.\,\-])/\\$1/g;
105 return $path;
106 }' "`readlink -f $abs_builddir`/" "`readlink -f $abs_top_srcdir`/"
109 run_filtered () {
111 { $* 2>$abs_builddir/.$$.stderr; echo $?>$abs_builddir/.$$.pipe.result; } | $FILTER_CMD
112 $FILTER_CMD < $abs_builddir/.$$.stderr 1>&2
114 RESULT_VAL=`cat $abs_builddir/.$$.pipe.result`
116 rm -f $abs_builddir/.$$.pipe.result $abs_builddir/.$$.stderr
117 return $RESULT_VAL
121 run_stderr_filtered () {
123 { $* 2>$abs_builddir/.$$.stderr; echo $?>$abs_builddir/.$$.pipe.result; }
124 $FILTER_CMD < $abs_builddir/.$$.stderr 1>&2
126 RESULT_VAL=`cat $abs_builddir/.$$.pipe.result`
128 rm -f $abs_builddir/.$$.pipe.result $abs_builddir/.$$.stderr
129 return $RESULT_VAL
133 #Logger-related part
134 #Performs static code checks and logprep preprocessing
135 if test "x$logger_origin_name" = x; then
136 logger_origin_name=UNKNOWN_ORIGIN;
139 if test "x$logger_binary_name" = x; then
140 logger_binary_name=UNKNOWN_ELF;
143 if test "x$logger_origin_id" = x; then
144 logger_origin_id=255;
147 # We use heuristics telling that file name
148 # is the last parameter of the compile command
149 FILE_NAME=`awk '{print $NF;exit;}'<<END
153 #If file is .C of .CPP and compilation do preprocessing with logprep
154 if logger_preprocessing_enabled && \
155 is_source_file $FILE_NAME && \
156 test "x`echo $* | grep -v -w '[-]E'`" != x; then
158 # Build dependencies list
159 MKDEP_CMD=`echo $* | \
160 sed -e's|-M[TF][[:space:]][[:space:]]*[^[:space:]][^[:space:]]*||g' \
161 -e's|\(-Wp,\)*-M[DP][[:space:]][[:space:]]*||g' \
162 -e's|\(-Wp,\)*-M[DP]\,[^[:space:]]*||g' \
163 -e"s|-o[[:space:]][[:space:]]*[^[:space:]][^[:space:]]*| -D__MTLK_DEPENDENCY_GENERATION_PATH -MM -MT $FILE_NAME|g"`
165 # Create logmacros.[ch] and loggroups.h because source code depends on them
166 for file in $abs_builddir/logmacros.h $abs_builddir/logmacros.c $abs_builddir/loggroups.h; do
167 test -e $file || touch $file
168 done
170 TOP_SRCDIR=`readlink -f $abs_top_srcdir`
171 RAW_DEPS="`run_stderr_filtered $MKDEP_CMD`" || exit 10
172 DEPS=`echo $RAW_DEPS | \
173 awk '{ for (i = 1; i <= NF; i++) print $i }' | \
174 grep -v ':$' | \
175 grep -v '\\.mod\\.c$' | \
176 sed 's/\\\\//' | \
177 grep -v 'logmacros\\.[hc]' | \
178 xargs -n 1 -r readlink -f | \
179 grep $TOP_SRCDIR`
181 # DEPS already contains name of file being compiled, so we just need
182 # to run logprep for each file in list
183 for file in $DEPS; do
184 TIMESTAMP_FILE=$abs_builddir/`echo $file | sed -e"s|$TOP_SRCDIR|./.logprep_ts/|g"`.logprep_ts
185 if test ! -e $TIMESTAMP_FILE -o $file -nt $TIMESTAMP_FILE; then
186 (test x$print_when_checking = xy) && ( (test -e $TIMESTAMP_FILE && echo " CK [M] $file") || echo " CK $file" )
187 check_mtlk_inc $file || exit $?
188 (test x$print_when_preprocessing = xy) && ( (test -e $TIMESTAMP_FILE && echo " LP [M] $file") || echo " LP $file" )
189 logpreprocessed_file=$abs_builddir/`basename $file`_$$.lgprpd
190 perl $abs_tools_srcdir/rtlogger/logprep/logprep.pl gcc \
191 $logger_origin_id $logger_origin_name \
192 $abs_builddir/loggroups.h $abs_builddir/logmacros.h \
193 $abs_builddir/logmacros.c $abs_builddir/$logger_origin_name.scd $logger_binary_name \
194 < $file > $logpreprocessed_file || { echo Logprep error while processing file $file && exit 1; }
195 #Do not rebuild all sources because of loggger database changes
196 touch -m -t 198001010000.00 $abs_builddir/loggroups.h $abs_builddir/logmacros.c $abs_builddir/logmacros.h
197 #Do rebuild logmacro_database.o because it could be new macros were added
198 rm -f $abs_builddir/logmacro_database.o
200 if diff -q $file $logpreprocessed_file >/dev/null 2>&1; then
201 rm -f $logpreprocessed_file
202 else
203 mv $logpreprocessed_file $file
205 mkdir -p `dirname $TIMESTAMP_FILE` && touch $TIMESTAMP_FILE
207 done
211 (test x$trace_commands = xy) && (echo "MTLKFRONT invoked for: $*" 1>&2)
213 #Here we put logger origin ID into the marker supplied by Makefile.am.common
214 PREPROCESSED_COMMAND=`echo $* | sed -e"s|LOG_LOCAL_OID=MTLKFRONT_WILL_FILL_IT|LOG_LOCAL_OID=$logger_origin_id|g"`
216 (test x$trace_commands = xy) && (echo "MTLKFRONT will invoke: $PREPROCESSED_COMMAND" 1>&2)
218 run_filtered $PREPROCESSED_COMMAND