make_libdeps.sh revision 255454
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: head/tools/make_libdeps.sh 255454 2013-09-10 18:34:38Z des $
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'
51247779Sbapt    -e's;-lbsdyml!;lib/libyaml;g'
52202969Sru    -e's;-lpthread!;lib/libthr;g'
5394541Sru    -e's;-lm!;lib/msun;g'
54202969Sru    -e's;-l(ncurses|termcap)!;lib/ncurses/ncurses;g'
55202969Sru    -e's;-l(gcc)!;gnu/lib/lib\1;g'
56202969Sru    -e's;-lssp_nonshared!;gnu/lib/libssp/libssp_nonshared;g'
57233294Sstas    -e's;-l(asn1|hdb|kdc|heimbase|heimntlm|heimsqlite|hx509|krb5|roken|wind)!;kerberos5/lib/lib\1;g'
58128184Sru    -e's;-l(crypto|ssh|ssl)!;secure/lib/lib\1;g'
5994541Sru    -e's;-l([^!]+)!;lib/lib\1;g'
6094541Sru"
6194541Sru
6294541Sru# Generate interdependencies between libraries.
6394541Sru#
6494541Srugenlibdepends()
6594541Sru{
6694541Sru	(
6794541Sru		cd ${USRSRC}
68255454Sdes		find -s ${LIBS} -mindepth 1 -name Makefile |
6994541Sru		xargs grep -l 'bsd\.lib\.mk' |
7094541Sru		while read makefile; do
7194541Sru			libdir=$(dirname ${makefile})
7294541Sru			deps=$(
7394541Sru				cd ${libdir}
74164581Sdelphij				make -m ${USRSRC}/share/mk -V LDADD
7594541Sru			)
7694541Sru			if [ "${deps}" ]; then
7794541Sru				echo ${libdir}"${FS}"$(
7894541Sru					echo ${deps} |
7994541Sru					eval ${SED_FILTER}
8094541Sru				)
8194541Sru			fi
8294541Sru		done
8394541Sru	)
8494541Sru}
8594541Sru
8694541Srumain()
8794541Sru{
8894541Sru	if [ ! -f ${LIBDEPENDS} ]; then
8994541Sru		genlibdepends >${LIBDEPENDS}
9094541Sru	fi
9194541Sru
9294541Sru	prebuild_libs=$(
9394541Sru		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} |rs 0 1 |sort -u
9494541Sru	)
9594541Sru	echo "Libraries with dependents:"
9694541Sru	echo
9794541Sru	echo ${prebuild_libs} |
9894541Sru	rs 0 1
9994541Sru	echo
10094541Sru
10194541Sru	echo "List of interdependencies:"
10294541Sru	echo
10394541Sru	for lib in ${prebuild_libs}; do
10494541Sru		grep "^${lib}${FS}" ${LIBDEPENDS} || true
10594541Sru	done |
10694541Sru	awk -F"${FS}" '{
10794541Sru		if ($2 in dependents)
10894541Sru			dependents[$2]=dependents[$2]" "$1
10994541Sru		else
11094541Sru			dependents[$2]=$1
11194541Sru	}
11294541Sru	END {
11394541Sru		for (lib in dependents)
11494541Sru			print dependents[lib]": " lib
11594541Sru	}' |
11694541Sru	sort
11794541Sru
11894541Sru	exit 0
11994541Sru}
12094541Sru
12194541Srumain
122