2018-07-24 00:06:04 +02:00
#!/usr/bin/env bash
2016-08-23 05:59:02 +02:00
if [ $# -eq 0 ] ; then
# With no args, read stdin to scrape compiler output.
2018-04-03 22:23:57 +02:00
# shellcheck disable=SC2046
set -- $( while read -r LINE; do
2016-08-23 05:59:02 +02:00
case " $LINE " in
*undefined\ reference\ to*)
2019-12-13 21:26:15 +01:00
# file.cc:(.text+0x10): undefined reference to `foo()'
2016-08-23 05:59:02 +02:00
LINE = ${ LINE #*undefined reference to \` }
2018-04-03 22:23:57 +02:00
echo " ${ LINE % \' * } "
2016-08-23 05:59:02 +02:00
; ;
2019-12-13 21:26:15 +01:00
*undefined\ symbol:*)
# ld: error: undefined symbol: foo()
echo " ${ LINE #*undefined symbol : } "
; ;
2016-08-23 05:59:02 +02:00
*)
continue
; ;
2020-03-10 17:54:09 +01:00
esac ; done | LC_ALL = C sort -u)
2016-08-23 05:59:02 +02:00
fi
for SYMBOL; do
2017-12-15 11:14:54 +01:00
# If there are multiple declarations, pick first (eg. common/memleak.h
# has notleak_ as a declaration, and then an inline).
2018-04-03 22:23:57 +02:00
WHERE = $( grep -nH " ^[a-zA-Z0-9_ (),]* [*]* $SYMBOL ( " ./*/*.h | head -n1)
2019-09-06 06:41:05 +02:00
if [ x" $WHERE " = x ] ; then
2017-10-29 11:59:04 +01:00
echo " /* Could not find declaration for $SYMBOL */ "
continue
2016-08-23 05:59:02 +02:00
fi
2017-12-10 13:41:10 +01:00
2016-08-23 05:59:02 +02:00
FILE = ${ WHERE %% : * }
FILE_AND_LINE = ${ WHERE % : * }
LINE = ${ FILE_AND_LINE #* : }
2018-04-03 22:23:57 +02:00
END = $( tail -n " + ${ LINE } " < " $FILE " | grep -n ';$' ) ;
2016-08-23 05:59:02 +02:00
NUM = ${ END %% : * }
2019-09-06 06:41:05 +02:00
if grep -q " $SYMBOL .*mock empty " " $FILE " ; then
STUB = "{ }"
else
# \n on RHS is a GNU extension, and we want to work on FreeBSD
# shellcheck disable=SC1004
STUB = ' \
{ fprintf( stderr, " ' $SYMBOL ' called!\\n " ) ; abort( ) ; } '
fi
echo " /* Generated stub for $SYMBOL */ "
2020-01-31 09:33:40 +01:00
tail -n " + ${ LINE } " < " $FILE " | head -n " $NUM " | sed 's/^extern *//' | sed 's/PRINTF_FMT([^)]*)//' | sed 's/NON_NULL_ARGS([^)]*)//' | sed 's/NO_NULL_ARGS//g' | sed 's/NORETURN//g' | sed 's/LAST_ARG_NULL//g' | sed 's/WARN_UNUSED_RESULT//g' | sed 's/,/ UNNEEDED,/g' | sed 's/\([a-z0-9A-Z*_]* [a-z0-9A-Z*_]*\));/\1 UNNEEDED);/' | sed " s/;\$/ $STUB / " | sed 's/[[:space:]]*$//'
2016-08-23 05:59:02 +02:00
done