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/10/tools/make_libdeps.sh 326521 2017-12-04 09:54:03Z hselasky $
2894541Sru
29172400Sruexport PATH=/bin:/usr/bin
3094541Sru
31326521Shselaskyset -e
32326521Shselasky
33255454SdesLC_ALL=C			# make sort deterministic
3494541SruFS=': '				# internal field separator
3594541SruLIBDEPENDS=./_libdeps		# intermediate output file
36326521ShselaskyLIBDIRS=./_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
45326521Shselasky	contrib/ofed
4694541Sru"				# where to scan for libraries
4794541Sru
4894541Sru
49326521Shselasky# convert -lfoo to foo
50326521Shselaskyconvert()
51326521Shselasky{
52326521Shselasky    sed -e "s/\-l//g" -e "s/pthread/thr/g" -e "s/ncurses.*/ncurses/g"
53326521Shselasky}
54326521Shselasky
55326521Shselasky# find library build directory given library name
56326521Shselaskyfindlibdir()
57326521Shselasky{
58326521Shselasky	while read NAME && read DIR
59326521Shselasky	do
60326521Shselasky		if [ "$NAME" = "$1" ]; then
61326521Shselasky			echo "$DIR"
62326521Shselasky			exit
63326521Shselasky		fi
64326521Shselasky	done
65326521Shselasky
66326521Shselasky	# Should not happen
67326521Shselasky	echo lib_not_found/lib$1
68326521Shselasky}
69326521Shselasky
70326521Shselasky# find library build directories given one or more library names
71326521Shselaskyresolvelibdirs()
72326521Shselasky{
73326521Shselasky	while read LIBNAME
74326521Shselasky	do
75326521Shselasky		cat $LIBDIRS | tr ' ' '\n' | findlibdir "$LIBNAME"
76326521Shselasky	done
77326521Shselasky}
78326521Shselasky
7994541Sru# Generate interdependencies between libraries.
8094541Sru#
8194541Srugenlibdepends()
8294541Sru{
8394541Sru	(
84326521Shselasky		# Reset file
85326521Shselasky		echo -n > $LIBDIRS
86326521Shselasky
87326521Shselasky		# First pass - generate list of directories
8894541Sru		cd ${USRSRC}
89326521Shselasky		find -s ${LIBS} -name Makefile |
9094541Sru		xargs grep -l 'bsd\.lib\.mk' |
9194541Sru		while read makefile; do
9294541Sru			libdir=$(dirname ${makefile})
93326521Shselasky			libname=$(
94326521Shselasky				cd ${libdir}
95326521Shselasky				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LIB
96326521Shselasky			)
97326521Shselasky			if [ "${libname}" ]; then
98326521Shselasky			    echo "${libname} ${libdir}" >> $LIBDIRS
99326521Shselasky			fi
100326521Shselasky		done
101326521Shselasky
102326521Shselasky		# Second pass - generate dependencies
103326521Shselasky		find -s ${LIBS} -name Makefile |
104326521Shselasky		xargs grep -l 'bsd\.lib\.mk' |
105326521Shselasky		while read makefile; do
106326521Shselasky			libdir=$(dirname ${makefile})
10794541Sru			deps=$(
10894541Sru				cd ${libdir}
109326521Shselasky				make -m ${USRSRC}/share/mk WITH_OFED=YES -V LDADD
11094541Sru			)
11194541Sru			if [ "${deps}" ]; then
112326521Shselasky				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=$(
12594541Sru		awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} |rs 0 1 |sort -u
12694541Sru	)
12794541Sru	echo "Libraries with dependents:"
12894541Sru	echo
12994541Sru	echo ${prebuild_libs} |
13094541Sru	rs 0 1
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