#! /bin/sh
set -e

if [ "${AUTOPKGTEST_TMP}" = "" ]
then
	AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
	# shellcheck disable=SC2064
	trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi
cd "$AUTOPKGTEST_TMP"

ROCM_CLANG_PREFIX="/usr/lib/rocm/llvm/bin"

# Exercise clang's RocmInstallationDetector::detectHIPRuntime() path.
# With libamdhip64-dev installed, /usr/share/hip/version is present and
# clang should set HasHIPRuntime=true, which it prints under -v as
# "Found HIP installation: <path>, version <ver>".
#
# If this fails, either the install layout drifted or clang's HIP
# detection regressed (e.g. the previously-disabled
# 0003-rocm-compilersupport-force-hip-runtime-detection.patch was
# masking a real bug).

cat > probe.cpp << END
#include <hip/hip_runtime.h>
int main(void) { return 0; }
END

echo "$ ${ROCM_CLANG_PREFIX}/clang++ -v -x hip --offload-arch=gfx900 -c probe.cpp -o probe.o"
${ROCM_CLANG_PREFIX}/clang++ -v -x hip --offload-arch=gfx900 -c probe.cpp -o probe.o 2>verbose.log

echo "--- HIP-detection diagnostics ---"
grep -E "Found HIP installation|HIP " verbose.log || true
echo "---------------------------------"

grep -q "Found HIP installation:" verbose.log
