#!/bin/bash -fu
# 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_SYMBOL symbol [include] [success] [failure]
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 " ${4-declared}" >&2
    [ "$2" ] && echo "// $2 ${4-is declared}${3:+ in <$3>}"
    echo "#define HAVE_$1"
    echo
    return 0
  else
    echo " ${5-undeclared}" >&2
    echo "#undef HAVE_$1"
    echo "// ${2:-symbol} ${5-is undeclared}${3:+ in <$3>}. 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
    return 1
  fi
}

# Test that symbol is defined (will catch functions mostly).
kbuild_test_symbol() {
  echo -n "Test function $1 $2" >&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
}

# Test that a header is available/exist
kbuild_test_header() {
  echo -n "Test header $*" >&2
  structname=${1%.*}
  member=${1#*.}
  def=${1^^}
  def=${def##*/}
  def=${def//./_}
  kbuild_test_compile $def "header $1" "" "exists" "doesn't exist" <<-EOF
	#include <linux/module.h>
	#include <$1>
	MODULE_LICENSE("GPL");
	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
# Fails on 3.10.0-957.10.1.el7.x86_64
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
# If we have strscpy, we can use that (more optimal compared to strlcpy).
kbuild_test_symbol sized_strscpy linux/string.h
# Do we have get_random_u32_below
kbuild_test_symbol get_random_u32_below linux/random.h
# Do we have get_random_u32
kbuild_test_symbol get_random_u32 linux/random.h

# prandom functions moved from random.h to prandom.h recentish.
# We use these for fallback for the above only.
if kbuild_test_header linux/prandom.h; then
  prand_h=linux/prandom.h
else
  prand_h=linux/random.h
fi
kbuild_test_symbol prandom_u32 $prand_h
kbuild_test_symbol prandom_u32_max $prand_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
