make_libdeps.sh revision 202969
11558Srgrimes#!/bin/sh -e
21558Srgrimes#
31558Srgrimes# Copyright (c) 2002 Ruslan Ermilov, The FreeBSD Project
41558Srgrimes# All rights reserved.
51558Srgrimes#
61558Srgrimes# Redistribution and use in source and binary forms, with or without
71558Srgrimes# modification, are permitted provided that the following conditions
81558Srgrimes# are met:
91558Srgrimes# 1. Redistributions of source code must retain the above copyright
101558Srgrimes#    notice, this list of conditions and the following disclaimer.
111558Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
121558Srgrimes#    notice, this list of conditions and the following disclaimer in the
131558Srgrimes#    documentation and/or other materials provided with the distribution.
141558Srgrimes#
151558Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161558Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171558Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181558Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191558Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201558Srgrimes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211558Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221558Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231558Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241558Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251558Srgrimes# SUCH DAMAGE.
261558Srgrimes#
271558Srgrimes# $FreeBSD: head/tools/make_libdeps.sh 202969 2010-01-25 14:17:36Z ru $
281558Srgrimes
291558Srgrimesexport PATH=/bin:/usr/bin
301558Srgrimes
311558SrgrimesFS=': '				# internal field separator
321558SrgrimesLIBDEPENDS=./_libdeps		# intermediate output file
331558SrgrimesUSRSRC=${1:-/usr/src}		# source root
341558SrgrimesLIBS="
351558Srgrimes	lib
361558Srgrimes	gnu/lib
371558Srgrimes	kerberos5/lib
3823247Swollman	secure/lib
391558Srgrimes	usr.bin/lex/lib
401558Srgrimes	cddl/lib
411558Srgrimes"				# where to scan for libraries
421558Srgrimes
431558Srgrimes# This sed(1) filter is used to convert -lfoo to path/to/libfoo.
4437671Scharnier#
451558SrgrimesSED_FILTER="
4637671Scharniersed -E
4723247Swollman    -e's; ;! ;g'
4842337Simp    -e's;$;!;'
491558Srgrimes    -e's;-lbsdxml!;lib/libexpat;g'
501558Srgrimes    -e's;-lpthread!;lib/libthr;g'
511558Srgrimes    -e's;-lm!;lib/msun;g'
521558Srgrimes    -e's;-l(ncurses|termcap)!;lib/ncurses/ncurses;g'
531558Srgrimes    -e's;-l(gcc)!;gnu/lib/lib\1;g'
5437671Scharnier    -e's;-lssp_nonshared!;gnu/lib/libssp/libssp_nonshared;g'
551558Srgrimes    -e's;-l(asn1|heimntlm|hx509|krb5|roken)!;kerberos5/lib/lib\1;g'
561558Srgrimes    -e's;-l(crypto|ssh|ssl)!;secure/lib/lib\1;g'
571558Srgrimes    -e's;-l([^!]+)!;lib/lib\1;g'
581558Srgrimes"
591558Srgrimes
601558Srgrimes# Generate interdependencies between libraries.
611558Srgrimes#
621558Srgrimesgenlibdepends()
631558Srgrimes{
641558Srgrimes	(
651558Srgrimes		cd ${USRSRC}
661558Srgrimes		find ${LIBS} -mindepth 1 -name Makefile |
671558Srgrimes		xargs grep -l 'bsd\.lib\.mk' |
681558Srgrimes		while read makefile; do
6923247Swollman			libdir=$(dirname ${makefile})
7023247Swollman			deps=$(
7123247Swollman				cd ${libdir}
7223247Swollman				make -m ${USRSRC}/share/mk -V LDADD
7323247Swollman			)
7427508Swollman			if [ "${deps}" ]; then
7523247Swollman				echo ${libdir}"${FS}"$(
7623247Swollman					echo ${deps} |
7723247Swollman					eval ${SED_FILTER}
7823247Swollman				)
7923247Swollman			fi
8023247Swollman		done
8123247Swollman	)
8223247Swollman}
8323247Swollman
841558Srgrimesmain()
851558Srgrimes{
8636378Sfenner	if [ ! -f ${LIBDEPENDS} ]; then
871558Srgrimes		genlibdepends >${LIBDEPENDS}
8823247Swollman	fi
891558Srgrimes
901558Srgrimes	prebuild_libs=$(
911558Srgrimes		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} |rs 0 1 |sort -u
921558Srgrimes	)
9323247Swollman	echo "Libraries with dependents:"
941558Srgrimes	echo
9536089Sjb	echo ${prebuild_libs} |
9636089Sjb	rs 0 1
9727533Sbde	echo
9827533Sbde
991558Srgrimes	echo "List of interdependencies:"
1001558Srgrimes	echo
1011558Srgrimes	for lib in ${prebuild_libs}; do
1021558Srgrimes		grep "^${lib}${FS}" ${LIBDEPENDS} || true
1031558Srgrimes	done |
1041558Srgrimes	awk -F"${FS}" '{
1051558Srgrimes		if ($2 in dependents)
1061558Srgrimes			dependents[$2]=dependents[$2]" "$1
1071558Srgrimes		else
1081558Srgrimes			dependents[$2]=$1
1091558Srgrimes	}
1101558Srgrimes	END {
1111558Srgrimes		for (lib in dependents)
1121558Srgrimes			print dependents[lib]": " lib
11320540Sfenner	}' |
11420540Sfenner	sort
11520540Sfenner
11620540Sfenner	exit 0
11720540Sfenner}
11820540Sfenner
11920540Sfennermain
12020540Sfenner