xref: /freebsd/tools/make_libdeps.sh (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1#!/bin/sh -e
2#
3# Copyright (c) 2002 Ruslan Ermilov, The FreeBSD Project
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28export PATH=/bin:/usr/bin
29
30set -e
31
32LC_ALL=C			# make sort deterministic
33FS=': '				# internal field separator
34LIBDEPENDS=./_libdeps		# intermediate output file
35LIBDIRS=./_libdirs		# intermediate output file
36USRSRC=${1:-/usr/src}		# source root
37LIBS="
38	lib
39	gnu/lib
40	kerberos5/lib
41	secure/lib
42	usr.bin/lex/lib
43	cddl/lib
44	contrib/ofed
45"				# where to scan for libraries
46
47
48# convert -lfoo to foo
49convert()
50{
51    sed -e "s/\-l//g" -e "s/pthread/thr/g" -e "s/ncurses.*/ncurses/g"
52}
53
54# find library build directory given library name
55findlibdir()
56{
57	while read NAME && read DIR
58	do
59		if [ "$NAME" = "$1" ]; then
60			echo "$DIR"
61			exit
62		fi
63	done
64
65	# Should not happen
66	echo lib_not_found/lib$1
67}
68
69# find library build directories given one or more library names
70resolvelibdirs()
71{
72	while read LIBNAME
73	do
74		cat $LIBDIRS | tr ' ' '\n' | findlibdir "$LIBNAME"
75	done
76}
77
78# Generate interdependencies between libraries.
79#
80genlibdepends()
81{
82	(
83		# Reset file
84		echo -n > $LIBDIRS
85
86		# First pass - generate list of directories
87		cd ${USRSRC}
88		find -s ${LIBS} -name Makefile |
89		xargs grep -l 'bsd\.lib\.mk' |
90		while read makefile; do
91			libdir=$(dirname ${makefile})
92			libname=$(
93				cd ${libdir}
94				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LIB
95			)
96			if [ "${libname}" ]; then
97			    echo "${libname} ${libdir}" >> $LIBDIRS
98			fi
99		done
100
101		# Second pass - generate dependencies
102		find -s ${LIBS} -name Makefile |
103		xargs grep -l 'bsd\.lib\.mk' |
104		while read makefile; do
105			libdir=$(dirname ${makefile})
106			deps=$(
107				cd ${libdir}
108				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LDADD
109			)
110			if [ "${deps}" ]; then
111				echo ${libdir}"${FS}"$(echo ${deps} | tr ' ' '\n' | convert | resolvelibdirs)
112			fi
113		done
114	)
115}
116
117main()
118{
119	if [ ! -f ${LIBDEPENDS} ]; then
120		genlibdepends >${LIBDEPENDS}
121	fi
122
123	prebuild_libs=$(
124		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} | tr ' ' '\n' |
125		    sort -u
126	)
127	echo "Libraries with dependents:"
128	echo
129	echo ${prebuild_libs} | tr ' ' '\n'
130	echo
131
132	echo "List of interdependencies:"
133	echo
134	for lib in ${prebuild_libs}; do
135		grep "^${lib}${FS}" ${LIBDEPENDS} || true
136	done |
137	awk -F"${FS}" '{
138		if ($2 in dependents)
139			dependents[$2]=dependents[$2]" "$1
140		else
141			dependents[$2]=$1
142	}
143	END {
144		for (lib in dependents)
145			print dependents[lib]": " lib
146	}' |
147	sort
148
149	exit 0
150}
151
152main
153