#!/usr/bin/env bash

# Pipe the output of make when stubsyms.cpp is empty to this script:
#
# sgx-experiments/enclave$ make 2>&1 | ../enclave/gen-stubsyms.sh

workdir=`pwd`/../enclave
o=$workdir/stubsyms.cpp

grep "undefined reference to" | tr -d "'"'`' | grep -o '[^ ]*$' | sort | uniq >$workdir/stubsyms.txt

cat <<EOF >$o
// Generated by gen-stubsyms.sh

extern "C" {

extern void debug_print(const char *str);
extern void abort();

#define STUB(x) void x() { debug_print(#x); abort(); }

EOF

cat $workdir/stubsyms.txt | while read; do
    echo "STUB($REPLY)" >>$o
done

echo "}  // extern C" >>$o