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$
2894541Sru
29172400Sruexport PATH=/bin:/usr/bin
3094541Sru
31255454SdesLC_ALL=C			# make sort deterministic
3294541SruFS=': '				# internal field separator
3394541SruLIBDEPENDS=./_libdeps		# intermediate output file
3494541SruUSRSRC=${1:-/usr/src}		# source root
3594541SruLIBS="
3694541Sru	lib
3794541Sru	gnu/lib
3894541Sru	kerberos5/lib
3994541Sru	secure/lib
4094541Sru	usr.bin/lex/lib
41172400Sru	cddl/lib
4294541Sru"				# where to scan for libraries
4394541Sru
4494541Sru# This sed(1) filter is used to convert -lfoo to path/to/libfoo.
4594541Sru#
4694541SruSED_FILTER="
4794541Srused -E
4894541Sru    -e's; ;! ;g'
4994541Sru    -e's;$;!;'
50115122Sru    -e's;-lbsdxml!;lib/libexpat;g'
51202969Sru    -e's;-lpthread!;lib/libthr;g'
5294541Sru    -e's;-lm!;lib/msun;g'
53202969Sru    -e's;-l(ncurses|termcap)!;lib/ncurses/ncurses;g'
54202969Sru    -e's;-l(gcc)!;gnu/lib/lib\1;g'
55202969Sru    -e's;-lssp_nonshared!;gnu/lib/libssp/libssp_nonshared;g'
56233294Sstas    -e's;-l(asn1|hdb|kdc|heimbase|heimntlm|heimsqlite|hx509|krb5|roken|wind)!;kerberos5/lib/lib\1;g'
57128184Sru    -e's;-l(crypto|ssh|ssl)!;secure/lib/lib\1;g'
5894541Sru    -e's;-l([^!]+)!;lib/lib\1;g'
5994541Sru"
6094541Sru
6194541Sru# Generate interdependencies between libraries.
6294541Sru#
6394541Srugenlibdepends()
6494541Sru{
6594541Sru	(
6694541Sru		cd ${USRSRC}
67255454Sdes		find -s ${LIBS} -mindepth 1 -name Makefile |
6894541Sru		xargs grep -l 'bsd\.lib\.mk' |
6994541Sru		while read makefile; do
7094541Sru			libdir=$(dirname ${makefile})
7194541Sru			deps=$(
7294541Sru				cd ${libdir}
73164581Sdelphij				make -m ${USRSRC}/share/mk -V LDADD
7494541Sru			)
7594541Sru			if [ "${deps}" ]; then
7694541Sru				echo ${libdir}"${FS}"$(
7794541Sru					echo ${deps} |
7894541Sru					eval ${SED_FILTER}
7994541Sru				)
8094541Sru			fi
8194541Sru		done
8294541Sru	)
8394541Sru}
8494541Sru
8594541Srumain()
8694541Sru{
8794541Sru	if [ ! -f ${LIBDEPENDS} ]; then
8894541Sru		genlibdepends >${LIBDEPENDS}
8994541Sru	fi
9094541Sru
9194541Sru	prebuild_libs=$(
9294541Sru		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} |rs 0 1 |sort -u
9394541Sru	)
9494541Sru	echo "Libraries with dependents:"
9594541Sru	echo
9694541Sru	echo ${prebuild_libs} |
9794541Sru	rs 0 1
9894541Sru	echo
9994541Sru
10094541Sru	echo "List of interdependencies:"
10194541Sru	echo
10294541Sru	for lib in ${prebuild_libs}; do
10394541Sru		grep "^${lib}${FS}" ${LIBDEPENDS} || true
10494541Sru	done |
10594541Sru	awk -F"${FS}" '{
10694541Sru		if ($2 in dependents)
10794541Sru			dependents[$2]=dependents[$2]" "$1
10894541Sru		else
10994541Sru			dependents[$2]=$1
11094541Sru	}
11194541Sru	END {
11294541Sru		for (lib in dependents)
11394541Sru			print dependents[lib]": " lib
11494541Sru	}' |
11594541Sru	sort
11694541Sru
11794541Sru	exit 0
11894541Sru}
11994541Sru
12094541Srumain
121