#!/bin/sh
set -e

CXX=${1:-g++}

AUTOPKGTEST_TMP=${AUTOPKGTEST_TMP:-$(mktemp -d)}

cd $AUTOPKGTEST_TMP

CXX_MAJOR=$($CXX --version | sed -rn '1s/.* ([0-9]+)\..*/\1/p')
NVCC_VERSION=$(nvcc --version | sed -rn 's/Cuda compilation tools, release ([0-9.]+),.*/\1/p')

res=0
for header in /usr/include/c++/$CXX_MAJOR/*
do
	test -f "$header" || continue
	header=$(basename $header)
	flags=
	ignore_fail=
	skip=
	case $header in
		complex.h)
			# cicc does not terminate
			[ "$NVCC_VERSION" = "12.4" ] && [ "$CXX_MAJOR" = "13" ] && skip="yes"
			;;
		coroutine)
			flags="-Xcompiler -fcoroutines"
			;;
	esac
	if [ "$skip" = "yes" ]; then
		echo "$header (skipped)"
		continue
	elif [ "$ignore_fail" = "yes" ]; then
		echo "$header (may fail)"
	else
		echo "$header"
	fi
	rc=0
	echo "#include <$header>" | timeout 900 nvcc -ccbin $CXX -x cu $flags -o $header.o -c - || rc=$?
	if [ "$rc" != 0 ]; then
		if [ "$ignore_fail" = "yes" ]; then
			echo "Ignored failure: $header"
		else
			echo "FAIL: $header"
			res=$rc
		fi
	fi
done

exit $res
