#!/bin/sh
# Fail the build unless our QtWidgets and QtWaylandClient are drop-in
# replacements for Debian's:
#  1. Qt was configured with exactly the same features (the generated
#     *-config.h / *-config_p.h headers are identical to Debian's), so the
#     private class layouts that KWin, QtQuickWidgets, qt6-wayland and
#     others rely on are the same.
#  2. Every symbol Debian's library exports is also exported by ours
#     (ours may export a few more; the patches add helper methods).
#
# Usage: check-qt-abi BUILDDIR [SYSTEM_INCLUDE_DIR SYSTEM_LIB_DIR]
set -eu

build=${1:?usage: check-qt-abi BUILDDIR}
multiarch=$(dpkg-architecture -qDEB_HOST_MULTIARCH)
sysinc=${2:-/usr/include/$multiarch/qt6}
syslib=${3:-/usr/lib/$multiarch}
fail=0

echo "check-qt-abi: comparing feature headers with $sysinc"
for h in qtcore-config.h qtcore-config_p.h \
         qtgui-config.h qtgui-config_p.h \
         qtwidgets-config.h qtwidgets-config_p.h \
         qtwaylandglobal-config.h qtwaylandglobal-config_p.h \
         qtwaylandclient-config.h qtwaylandclient-config_p.h; do
    # The copies under $build/include are one-line forwarding stubs.
    ours=$(find "$build" -path "$build/include" -prune -o -name "$h" -print | head -n 1)
    theirs=$(find "$sysinc" -name "$h" | head -n 1)
    if [ -z "$ours" ] && [ -z "$theirs" ]; then
        continue
    fi
    if [ -z "$ours" ] || [ -z "$theirs" ]; then
        echo "check-qt-abi: $h exists on one side only (ours: ${ours:-none}, Debian: ${theirs:-none})"
        fail=1
        continue
    fi
    if ! diff -u "$theirs" "$ours"; then
        echo "check-qt-abi: $h differs from Debian's (diff above: - Debian, + ours)"
        fail=1
    fi
done

exported() {
    nm -D --defined-only --with-symbol-versions "$1" | awk '{ print $NF }' | sort -u
}

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
for lib in libQt6Widgets.so.6 libQt6WaylandClient.so.6; do
    ours=$build/lib/$multiarch/$lib
    theirs=$syslib/$lib
    if [ ! -e "$ours" ] || [ ! -e "$theirs" ]; then
        echo "check-qt-abi: $lib missing (ours: $ours, Debian: $theirs)"
        fail=1
        continue
    fi
    soname_ours=$(objdump -p "$ours" | awk '$1 == "SONAME" { print $2 }')
    soname_theirs=$(objdump -p "$theirs" | awk '$1 == "SONAME" { print $2 }')
    if [ "$soname_ours" != "$soname_theirs" ]; then
        echo "check-qt-abi: $lib SONAME $soname_ours, Debian has $soname_theirs"
        fail=1
    fi
    exported "$ours" > "$tmp/ours"
    exported "$theirs" > "$tmp/theirs"
    comm -23 "$tmp/theirs" "$tmp/ours" > "$tmp/missing"
    comm -13 "$tmp/theirs" "$tmp/ours" > "$tmp/extra"
    if [ -s "$tmp/missing" ]; then
        echo "check-qt-abi: $lib lacks $(wc -l < "$tmp/missing") symbol(s) that Debian's exports:"
        c++filt < "$tmp/missing" | sed 's/^/    /'
        fail=1
    fi
    echo "check-qt-abi: $lib: $(wc -l < "$tmp/theirs") symbols as in Debian, $(wc -l < "$tmp/extra") extra"
    c++filt < "$tmp/extra" | sed 's/^/    + /'
done

if [ "$fail" -ne 0 ]; then
    echo "check-qt-abi: FAILED - these libraries are not drop-in replacements for Debian's"
    exit 1
fi
echo "check-qt-abi: OK"
