git: f3790c0170d8 - main - sanity: warn about unnused LIB_DEPENDS entries

From: Tobias C. Berner <tcberner_at_FreeBSD.org>
Date: Tue, 12 Sep 2023 18:32:39 UTC
The branch main has been updated by tcberner:

URL: https://cgit.FreeBSD.org/ports/commit/?id=f3790c0170d85995b0fa58c6c50cef03088bf5ec

commit f3790c0170d85995b0fa58c6c50cef03088bf5ec
Author:     Tobias C. Berner <tcberner@FreeBSD.org>
AuthorDate: 2023-09-12 15:21:03 +0000
Commit:     Tobias C. Berner <tcberner@FreeBSD.org>
CommitDate: 2023-09-12 18:27:47 +0000

    sanity: warn about unnused LIB_DEPENDS entries
    
    `stage-qa` already warns about missing dependencies. However, it does not warn
    about possibly unneeded ones.
    
    This change tries to address this, by simply walking the list of linked against
    shared libraries and then matching the entries of LIB_DEPENDS against them.
    
    Note: this may lead to false positives -- as always, user your brain,
    and don't rely on output of static tools alone.
    
    Possible output might look like:
    
    [...]
    ====> Running Q/A tests (stage-qa)
    Warning: you might not need LIB_DEPENDS on libqgpgme.so
    Warning: you might not need LIB_DEPENDS on libintl.so
    Warning: you might not need LIB_DEPENDS on libKF5IconThemes.so
    Warning: you might not need LIB_DEPENDS on libqca-qt5.so
    Warning: you might not need LIB_DEPENDS on libQt5Test.so
    [...]
    
    Note, that in this case all are false positives.
    
    Differential Revision: https://reviews.freebsd.org/D27304
---
 Mk/Scripts/qa.sh | 11 ++++++++++-
 Mk/bsd.port.mk   |  1 +
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/Mk/Scripts/qa.sh b/Mk/Scripts/qa.sh
index bdbff58cdb23..63fa9f6f370e 100644
--- a/Mk/Scripts/qa.sh
+++ b/Mk/Scripts/qa.sh
@@ -649,7 +649,7 @@ proxydeps_suggest_uses() {
 }
 
 proxydeps() {
-	local file dep_file dep_file_pkg already rc
+	local file dep_file dep_file_pkg already rc dep_lib_file dep_lib_files
 
 	rc=0
 
@@ -697,6 +697,8 @@ proxydeps() {
 				rc=1
 			fi
 			already="${already} ${dep_file}"
+			dep_lib_file=$(basename ${dep_file})
+			dep_lib_files="${dep_lib_files} ${dep_lib_file%%.so*}.so"
 		done <<-EOT
 		$(env LD_LIBMAP_DISABLE=1 ldd -a "${STAGEDIR}${file}" | \
 			awk '
@@ -712,6 +714,13 @@ proxydeps() {
 		sed -e 's/^\.//')
 	EOT
 
+	# Check whether all files in LIB_DPEENDS are actually linked against
+	for _library in ${WANTED_LIBRARIES} ; do
+		if ! listcontains ${_library} "${dep_lib_files}" ; then
+			warn "you might not need LIB_DEPENDS on ${_library}"
+		fi
+	done
+
 	[ -z "${PROXYDEPS_FATAL}" ] && return 0
 
 	return ${rc}
diff --git a/Mk/bsd.port.mk b/Mk/bsd.port.mk
index b11c99fc0476..4679774d9640 100644
--- a/Mk/bsd.port.mk
+++ b/Mk/bsd.port.mk
@@ -1650,6 +1650,7 @@ QA_ENV+=		STAGEDIR=${STAGEDIR} \
 				PKGORIGIN=${PKGORIGIN} \
 				LIB_RUN_DEPENDS='${_LIB_RUN_DEPENDS:C,[^:]*:([^:]*):?.*,\1,}' \
 				UNIFIED_DEPENDS=${_UNIFIED_DEPENDS:C,([^:]*:[^:]*):?.*,\1,:O:u:Q} \
+				WANTED_LIBRARIES='${LIB_DEPENDS:C,([^:]*):([^:]*):?.*,\1,}' \
 				PKGBASE=${PKGBASE} \
 				LICENSE="${LICENSE}" \
 				LICENSE_PERMS="${_LICENSE_PERMS}" \