#!/bin/bash -efu
# SPDX-License-Identifier: GPL-2.0-only
#
# Generate defines based on kernel having some symbols declared.
# Tests should work without linking, because kernel may not be
# completely compiled (only prepared).
#
# Copyright (C) 2019-2021 <abc@openwall.com>
#

export LANG=C LC_ALL=C LC_MESSAGES=C LC_CTYPE=C
fatal() {
  echo "Error: $*" >&2
  exit 1
}

eval $(grep ^KDIR Makefile | tr -d ' ')
[ "$KDIR" ] || fatal "KDIR is not found"

WD=cc-test-build
mkdir -p $WD
cd ./$WD || fatal "cannot cd to $WD"

# args: HAVE_SUMBOL symbol include
kbuild_test_compile() {
  local cmd

  cat > test.c
  echo obj-m = test.o > Makefile
  cmd="make -s -B -C $KDIR M=$PWD modules"
  echo "$cmd" > log
  if $cmd >> log 2>&1; then
    echo " declared" >&2
    [ "$2" ] && echo "// $2 is declared ${3:+in <$3>}"
    echo "#define HAVE_$1"
    echo
  else
    echo " undeclared" >&2
    echo "#undef HAVE_$1"
    echo "// ${2:-symbol} is undeclared${3:+ in <$3>}."
    echo "// Compile:"
    sed  "s/^/\/\/   /" test.c
    echo "// Output:"
    sed  "s/^/\/\/   /" log
    echo
    if ! egrep -q \
	  -e 'has no member named' \
	  -e 'undeclared' \
	  -e 'storage size of .* isn.t known' \
	  -e 'No such file or directory' \
	  -e 'incompatible types when initializing' \
	  -e 'initializer element is not constant' \
	  -e 'dereferencing pointer to incomplete type' \
	  log; then
      echo "Error: unexpected error from compiler" >&2
      cat log >&2
      echo >&2
      exit 3
    fi
  fi
}

# Test that symbol is defined (will catch functions mostly).
kbuild_test_symbol() {
  echo -n "Test function $* " >&2
  kbuild_test_compile ${1^^} $1 ${2-} <<-EOF
	#include <linux/module.h>
	${3:-${2:+#include <$2>}}
	MODULE_LICENSE("GPL");
	void *test = $1;
	EOF
}

# Test that symbol is defined (functions and globals).
kbuild_test_ref() {
  echo -n "Test symbol $* " >&2
  kbuild_test_compile ${1^^}_REF $1 ${2-} <<-EOF
	#include <linux/module.h>
	${2:+#include <$2>}
	MODULE_LICENSE("GPL");
	void *test = &$1;
	EOF
}
# Test that struct is defined.
kbuild_test_struct() {
  echo -n "Test struct $* " >&2
  kbuild_test_compile ${1^^} "struct $1" ${2-} <<-EOF
	#include <linux/module.h>
	${2:+#include <$2>}
	MODULE_LICENSE("GPL");
	struct $1 test;
	EOF
}

# Test that struct have member
kbuild_test_member() {
  echo -n "Test member $* " >&2
  structname=${1%.*}
  member=${1#*.}
  def=${1^^}
  def=${def//./_}
  kbuild_test_compile $def "struct $1" ${2-} <<-EOF
	#include <linux/module.h>
	${2:+#include <$2>}
	MODULE_LICENSE("GPL");
	typeof(((struct $structname*)0)->$member) test;
	EOF
}
echo "// Autogenerated for $KDIR"
echo

# helpers introduced in 613dbd95723aee7abd16860745691b6c7bda20dc
kbuild_test_symbol xt_family linux/netfilter_ipv4/ip_tables.h
kbuild_test_struct timeval linux/ktime.h
# 97a32539b9568 proc: convert everything to "struct proc_ops"
# d56c0d45f0e27 proc: decouple proc from VFS with "struct proc_ops"
kbuild_test_struct proc_ops linux/proc_fs.h
# No since v5.1, but present in CentOS-8's 4.18.0-227
kbuild_test_symbol synchronize_sched linux/rcupdate.h
# No since 5, but present in include/net/netfilter/br_netfilter.h >= linux v4.2 and < 5 (non-backports)
kbuild_test_symbol nf_bridge_info_get linux/netfilter_bridge.h
# Stumbled on 5.9
kbuild_test_struct vlan_dev_priv linux/if_vlan.h
# Kernel version check broken by centos8
kbuild_test_symbol put_unaligned_be24 '???/unaligned.h' '#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,12,0)
#include <linux/unaligned.h>
#else
#include <asm/unaligned.h>
#endif'
# totalram_pages changed from atomic to inline function.
kbuild_test_symbol totalram_pages linux/mm.h
kbuild_test_ref    totalram_pages linux/mm.h
# b86c0e6429da ("netfilter: ecache: prepare for event notifier merge")
kbuild_test_member nf_ct_event_notifier.ct_event net/netfilter/nf_conntrack_ecache.h
# 6.4: 0199849acd07 ("sysctl: remove register_sysctl_paths()")
kbuild_test_symbol register_sysctl_paths linux/sysctl.h

echo "// End of compat_def.h"

cd $OLDPWD
rm -rf $WD

# debug output for Travis
if [ -z "${PWD/*travis*}" ]; then
  cat compat_def.h >&2
fi
