make.sh: Add command to find dependencies

Message ID 20200513105259.28975-1-michael.tremer@ipfire.org
State Accepted
Commit ba137dd89827c4aaf5b7097c3794025cc0d3c1be
Headers
Series make.sh: Add command to find dependencies |

Commit Message

Michael Tremer May 13, 2020, 10:52 a.m. UTC
  Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 make.sh                 |  6 +++++-
 tools/find-dependencies | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100755 tools/find-dependencies
  

Patch

diff --git a/make.sh b/make.sh
index 78c4edc90..4acce807f 100755
--- a/make.sh
+++ b/make.sh
@@ -1993,8 +1993,12 @@  lang)
 update-contributors)
 	update_contributors
 	;;
+find-dependencies)
+	shift
+	exec "${BASEDIR}/tools/find-dependencies" "${BASEDIR}/build" "$@"
+	;;
 *)
-	echo "Usage: $0 {build|changelog|clean|gettoolchain|downloadsrc|shell|sync|toolchain|update-contributors}"
+	echo "Usage: $0 {build|changelog|clean|gettoolchain|downloadsrc|shell|sync|toolchain|update-contributors|find-dependencies}"
 	cat doc/make.sh-usage
 	;;
 esac
diff --git a/tools/find-dependencies b/tools/find-dependencies
new file mode 100755
index 000000000..25e6cddea
--- /dev/null
+++ b/tools/find-dependencies
@@ -0,0 +1,32 @@ 
+#!/bin/bash
+
+main() {
+	if [ $# -lt 2 ]; then
+		echo "${0}: Usage: PATH LIBRARY ..."
+		return 2
+	fi
+
+	local root="${1}"
+	shift
+
+	if [ ! -d "${root}" ]; then
+		echo "${0}: ${root}: No such file or directory"
+		return 1
+	fi
+
+	local libraries="$@"
+
+	# Build the regex filter
+	local filter="(${libraries[*]// /|})"
+
+	local file
+	for file in $(find "${root}" -xdev -type f -executable); do
+		if readelf -d "${file}" 2>/dev/null | grep -qE "NEEDED.*\[${filter}\]$"; then
+			echo "${file}"
+		fi
+	done
+
+	return 0
+}
+
+main "$@" || exit $?