set -e
cd $(dirname $(readlink -f "$0"))
tensorflow_version=1.3.0
valgrind_log=valgrind.log
truncate --size=0 "$valgrind_log"
export TF_NEED_JEMALLOC=0
export TF_RUST_BUILD_FROM_SRC=true
rm -rf tensorflow-sys/target
cargo +nightly build --features=nightly -p tensorflow-sys -vvv
export LD_LIBRARY_PATH="$(echo "$PWD"/target/debug/build/tensorflow-sys-*/out/lib-v$tensorflow_version)"
echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
for example in addition regression expressions; do
cargo +nightly build --features='nightly tensorflow_unstable' --example="$example"
valgrind --leak-check=full target/debug/examples/"$example" >> "$valgrind_log" 2>&1
done
rel_log=$(readlink -f "$PWD"/"$valgrind_log")
if grep -i 'error summary' < "$valgrind_log" > /dev/null; then
echo "Error running valgrind. See $rel_log"
fi
lost_bytes=$(awk '/(definitely|indirectly) lost:/{sum+=gensub(",","","g",$4)}END{print sum}' < "$valgrind_log")
echo "Lost bytes: $lost_bytes"
echo "For details, see $rel_log"