#!/bin/sh
# DF-4826 — build-membership check: is sys/dev/drm/amd/amdgpu/amdgpu_acp.c
# ever compiled into the DragonFly kernel or the amdgpu module?
#
# Four independent, deterministic checks against the source tree, plus one
# binary-artifact check (run inside the DFly guest) against the shipped
# /boot/kernel/amdgpu.ko. Every check must point the same way for the
# dead-code verdict: the TU is NOT in the build.
#
# Usage:  ./check_dead.sh [path-to-sys-tree]        (host part)
#         (guest part) run_root 'sh /tmp/guest_acp_check.sh'

SYS="${1:-/home/maxx/dfbsd/dfbsd/sys}"

echo "== [1] amdgpu Makefile SRCS membership =="
# NOTE: 'grep amdgpu_acp' matches 'amdgpu_acpi.c' as a substring; require the
# exact TU name with the .c suffix and a word boundary.
hits=$(grep -n 'amdgpu_acp\.c' "$SYS/dev/drm/amd/amdgpu/Makefile" || true)
echo "  amdgpu_acp.c in Makefile SRCS: ${hits:-<none>}"
grep -n 'amdgpu_acpi\.c' "$SYS/dev/drm/amd/amdgpu/Makefile" | sed 's/^/  (positive control, different file) /'

echo "== [2] required shim headers exist? =="
for h in linux/pm_domain.h sound/designware_i2s.h sound/pcm.h; do
    if [ -f "$SYS/dev/drm/include/$h" ]; then
        echo "  $h: PRESENT"
    else
        echo "  $h: ABSENT  (fatal include error: amdgpu_acp.c:26-30)"
    fi
done
echo "  platform_device shim struct: $(sed -n '36,37p' "$SYS/dev/drm/include/linux/platform_device.h" | tr '\n' ' ')"
echo "  mfd/core.h defines: $(grep -c 'struct\|mfd_add\|void' "$SYS/dev/drm/include/linux/mfd/core.h") substantive lines (include-guard only)"

echo "== [3] CONFIG_DRM_AMD_ACP ever defined? =="
defs=$(grep -rn 'define CONFIG_DRM_AMD_ACP\|CONFIG_DRM_AMD_ACP=' "$SYS" 2>/dev/null || true)
echo "  definitions: ${defs:-<none>}"
echo "  references (all #ifdef / #if defined consumers):"
grep -rn 'CONFIG_DRM_AMD_ACP' "$SYS" 2>/dev/null | grep -v 'define CONFIG' | sed 's/^/    /'

echo "== [4] any mfd/genpd API symbol anywhere else in sys/ (shim presence)? =="
for sym in mfd_add_hotplug_devices mfd_add_devices mfd_remove_devices pm_genpd_init pm_genpd_add_device bus_find_device_by_name; do
    n=$(grep -rl "$sym" "$SYS" --include='*.c' --include='*.h' 2>/dev/null | grep -v 'amd/amdgpu/amdgpu_acp.c' | wc -l | tr -d ' ')
    echo "  $sym defined outside amdgpu_acp.c: $n files"
done

echo "== [5] shipped binary artifact (guest): /boot/kernel/amdgpu.ko =="
echo "  (run inside guest; results recorded in run.log)"
echo
echo "VERDICT INPUTS: [1] no SRCS entry, [2] >=1 fatal missing include, [3] no definition,"
echo "[4] zero shim files, [5] zero ACP symbols in shipped amdgpu.ko  => dead translation unit."
