#!/bin/bash
#
# Kernel API Tester by Pawel Rozanski
# kat <kernelbuilddir> <outfile> <test1> [ .... <testn> ]
#
# Tries to run gcc on test files & generate output file which contains selected
# definitions from test files
#
cd kat
if [ ! -d "$1" ]; then
  echo "There is no kernel build dir $1"
  exit 1
fi
if [ -z "${CC}" ]; then CC=gcc; fi
echo -n "KAT will test if C compiler works..."
echo "main(){}" | ${CC} -x c - -o /dev/null
if [ $? -ne 0 ]; then
  echo "${CC}" failed
  echo "Please set CC to working compiler or set it to empty (for default gcc)"
  exit 1
fi
echo "ok"
OUT=../${2}
echo "/* Generated by Kernel API Tester */" > ${OUT}
shift 2
for i in $@; do
  echo -n "Checking ${i}... "
  if [ "$V" ]; then
    make TESTNAME="../${i%%.c}.o"
  else
    make TESTNAME="../${i%%.c}.o" > /dev/null 2> /dev/null
  fi
  if [ $? -eq 0 ]; then
    echo "true"
    sed '/\/\/T/!d;s/^\/\/T//' ../${i} >> ${OUT}
  else
    echo "false"
    sed '/\/\/F/!d;s/^\/\/F//' ../${i} >> ${OUT}
  fi
done
