
echo
echo '=============================================================================='
echo "DEFS=$DEFS"
echo "DEFS2=$DEFS2"

if test `uname` = FreeBSD ; then
    LIBS='-ldl -lpthread -lexecinfo'
else
    LIBS='-ldl -lpthread'
fi

if test x$1 \!= x ; then
    only=$1
fi

cat ../dfwmalloc.c | sed -e '/Header:/,/End header./!D' > dfwmalloc.h
echo >> dfwmalloc.h
echo '#define STATICFN          static' >> dfwmalloc.h
echo '#define STATICFNINLINE    static' >> dfwmalloc.h
echo '#define STATICFNNOINLINE  static' >> dfwmalloc.h
grep -A2 '^[#]ifndef MIN_ALIGN' ../dfwmalloc.c >> dfwmalloc.h

OPTS='-O0 -g3 -fno-builtin'
WARN='-Wall -Wextra  -Wno-unused-function'
INC='--include dfwmalloc.h --include stdio.h --include assert.h --include errno.h --include string.h'

cat ../dfwmalloc.c | sed -e '/struct cb_page_info [{]$/,/^[}].*struct cb_page_info/!D' > cb_page_info.h
cat ../dfwmalloc.c | grep '^typedef .* cb_int_t;$' > cb_int_t.h
cat ../dfwmalloc.c | sed -e '/@cb_size_t_to_ulong@start@/,/@cb_size_t_to_ulong@end@/!D' > cb_size_t_to_ulong.h

echo '#undef NO_BLOCK_ORDERING' > vecheap.h
cat ../dfwmalloc.c | sed -e '/@INLINE5START@/,/@INLINE5END@/!D' >> vecheap.h

rm -f dfwmalloc.o

if test x$PRECOMPILE = xtrue ; then
    echo "precompiling static data"
    PRECOMPDEF='-DUSE_PRECOMPILED_STATIC_DATA'
    gcc -O0 $WARN $DEFS "$DEFS2" -DPRECOMPILE_STATIC_DATA -DWANT_PRINTFSIMPLE -o staticdata ../dfwmalloc.c $LIBS || exit 1
    ./staticdata > staticdata.c || exit 1
    cat staticdata.c | sed -e 's/[}][,][ ][{]/},@    {/g' -e 's/  *$//' | tr '@' '\n' > staticdata-view.c
    echo "...done"
else
    echo "NOT precompiling static data, just generating for viewing"
    gcc -O0 $WARN $DEFS "$DEFS2" -DPRECOMPILE_STATIC_DATA -DWANT_PRINTFSIMPLE -o staticdata ../dfwmalloc.c $LIBS || exit 1
    ./staticdata > staticdata.c || exit 1
    cat staticdata.c | sed -e 's/[}][,][ ][{]/},@    {/g' -e 's/  *$//' | tr '@' '\n' > staticdata-view.c
    echo "...done"
    PRECOMPDEF=''
fi

echo 'Building dfwmalloc.o...'

rm -f ../staticdata.c
gcc -c -o dfwmalloc.o -I. ../dfwmalloc.c $WARN -O0 -g3 \
-DWITH_UNITTESTS \
$PRECOMPDEF \
$DEFS \
"$DEFS2" \
\
|| exit 1

echo 'done'

TESTS='
test-lfv.c
test-arena.c
test-basic.c
test-frag.c
test-vecheap.c
test-vss.c
test-cbsizetolong.c
test-extend.c
test-memalign.c
test-thrash.c
test-threads.c
test-truncate.c
test-ones.c
test-cb.c
test-calloc.c
test-heaplimits.c
'

ulimit -c 100000000

echo "bt" > gdb-cmds
echo "q" >> gdb-cmds

if test x$CHECK_EFFICIENCY \!= x ; then
    DEFS="-DCHECK_EFFICIENCY=$CHECK_EFFICIENCY $DEFS"
fi

for i in $TESTS ; do
    test x \!= x$only && test x$i \!= x$only && continue
    echo $i | grep -qs '^[#]' && continue
    echo "gcc $OPTS $WARN $INC $DEFS $DEFS2 -DUSE_TLS  -o test main.c $i dfwmalloc.o $LIBS"
    gcc $OPTS $WARN $INC $DEFS "$DEFS2" -DUSE_TLS -I.  -o test main.c $i dfwmalloc.o $LIBS || exit 1
    rm -f core
    ./test || {
        test -f core && gdb -q -x gdb-cmds ./test ./core ;
        echo "Error: $i" ; 
        exit 1 ;
    }
    echo "=====>  $i: Success"
done

echo
echo 'All tests passed'


