194541Sru#!/bin/sh -e
294541Sru#
394541Sru# Copyright (c) 2002 Ruslan Ermilov, The FreeBSD Project
494541Sru# All rights reserved.
594541Sru#
694541Sru# Redistribution and use in source and binary forms, with or without
794541Sru# modification, are permitted provided that the following conditions
894541Sru# are met:
994541Sru# 1. Redistributions of source code must retain the above copyright
1094541Sru#    notice, this list of conditions and the following disclaimer.
1194541Sru# 2. Redistributions in binary form must reproduce the above copyright
1294541Sru#    notice, this list of conditions and the following disclaimer in the
1394541Sru#    documentation and/or other materials provided with the distribution.
1494541Sru#
1594541Sru# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1694541Sru# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1794541Sru# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1894541Sru# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1994541Sru# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2094541Sru# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2194541Sru# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2294541Sru# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2394541Sru# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2494541Sru# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2594541Sru# SUCH DAMAGE.
2694541Sru#
2794541Sru# $FreeBSD: stable/11/tools/make_libdeps.sh 326520 2017-12-04 09:53:03Z hselasky $
2894541Sru
29172400Sruexport PATH=/bin:/usr/bin
3094541Sru
31326520Shselaskyset -e
32326520Shselasky
33255454SdesLC_ALL=C			# make sort deterministic
3494541SruFS=': '				# internal field separator
3594541SruLIBDEPENDS=./_libdeps		# intermediate output file
36326520ShselaskyLIBDIRS=./_libdirs		# intermediate output file
3794541SruUSRSRC=${1:-/usr/src}		# source root
3894541SruLIBS="
3994541Sru	lib
4094541Sru	gnu/lib
4194541Sru	kerberos5/lib
4294541Sru	secure/lib
4394541Sru	usr.bin/lex/lib
44172400Sru	cddl/lib
45326520Shselasky	contrib/ofed
4694541Sru"				# where to scan for libraries
4794541Sru
4894541Sru
49326520Shselasky# convert -lfoo to foo
50326520Shselaskyconvert()
51326520Shselasky{
52326520Shselasky    sed -e "s/\-l//g" -e "s/pthread/thr/g" -e "s/ncurses.*/ncurses/g"
53326520Shselasky}
54326520Shselasky
55326520Shselasky# find library build directory given library name
56326520Shselaskyfindlibdir()
57326520Shselasky{
58326520Shselasky	while read NAME && read DIR
59326520Shselasky	do
60326520Shselasky		if [ "$NAME" = "$1" ]; then
61326520Shselasky			echo "$DIR"
62326520Shselasky			exit
63326520Shselasky		fi
64326520Shselasky	done
65326520Shselasky
66326520Shselasky	# Should not happen
67326520Shselasky	echo lib_not_found/lib$1
68326520Shselasky}
69326520Shselasky
70326520Shselasky# find library build directories given one or more library names
71326520Shselaskyresolvelibdirs()
72326520Shselasky{
73326520Shselasky	while read LIBNAME
74326520Shselasky	do
75326520Shselasky		cat $LIBDIRS | tr ' ' '\n' | findlibdir "$LIBNAME"
76326520Shselasky	done
77326520Shselasky}
78326520Shselasky
7994541Sru# Generate interdependencies between libraries.
8094541Sru#
8194541Srugenlibdepends()
8294541Sru{
8394541Sru	(
84326520Shselasky		# Reset file
85326520Shselasky		echo -n > $LIBDIRS
86326520Shselasky
87326520Shselasky		# First pass - generate list of directories
8894541Sru		cd ${USRSRC}
89326520Shselasky		find -s ${LIBS} -name Makefile |
9094541Sru		xargs grep -l 'bsd\.lib\.mk' |
9194541Sru		while read makefile; do
9294541Sru			libdir=$(dirname ${makefile})
93326520Shselasky			libname=$(
94326520Shselasky				cd ${libdir}
95326520Shselasky				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LIB
96326520Shselasky			)
97326520Shselasky			if [ "${libname}" ]; then
98326520Shselasky			    echo "${libname} ${libdir}" >> $LIBDIRS
99326520Shselasky			fi
100326520Shselasky		done
101326520Shselasky
102326520Shselasky		# Second pass - generate dependencies
103326520Shselasky		find -s ${LIBS} -name Makefile |
104326520Shselasky		xargs grep -l 'bsd\.lib\.mk' |
105326520Shselasky		while read makefile; do
106326520Shselasky			libdir=$(dirname ${makefile})
10794541Sru			deps=$(
10894541Sru				cd ${libdir}
109326520Shselasky				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LDADD
11094541Sru			)
11194541Sru			if [ "${deps}" ]; then
112326520Shselasky				echo ${libdir}"${FS}"$(echo ${deps} | tr ' ' '\n' | convert | resolvelibdirs)
11394541Sru			fi
11494541Sru		done
11594541Sru	)
11694541Sru}
11794541Sru
11894541Srumain()
11994541Sru{
12094541Sru	if [ ! -f ${LIBDEPENDS} ]; then
12194541Sru		genlibdepends >${LIBDEPENDS}
12294541Sru	fi
12394541Sru
12494541Sru	prebuild_libs=$(
125271278Sbdrewery		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} | tr ' ' '\n' |
126271278Sbdrewery		    sort -u
12794541Sru	)
12894541Sru	echo "Libraries with dependents:"
12994541Sru	echo
130271278Sbdrewery	echo ${prebuild_libs} | tr ' ' '\n'
13194541Sru	echo
13294541Sru
13394541Sru	echo "List of interdependencies:"
13494541Sru	echo
13594541Sru	for lib in ${prebuild_libs}; do
13694541Sru		grep "^${lib}${FS}" ${LIBDEPENDS} || true
13794541Sru	done |
13894541Sru	awk -F"${FS}" '{
13994541Sru		if ($2 in dependents)
14094541Sru			dependents[$2]=dependents[$2]" "$1
14194541Sru		else
14294541Sru			dependents[$2]=$1
14394541Sru	}
14494541Sru	END {
14594541Sru		for (lib in dependents)
14694541Sru			print dependents[lib]": " lib
14794541Sru	}' |
14894541Sru	sort
14994541Sru
15094541Sru	exit 0
15194541Sru}
15294541Sru
15394541Srumain
154