make_libdeps.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # $FreeBSD$
  28. export PATH=/bin:/usr/bin
  29. set -e
  30. LC_ALL=C # make sort deterministic
  31. FS=': ' # internal field separator
  32. LIBDEPENDS=./_libdeps # intermediate output file
  33. LIBDIRS=./_libdirs # intermediate output file
  34. USRSRC=${1:-/usr/src} # source root
  35. LIBS="
  36. lib
  37. gnu/lib
  38. kerberos5/lib
  39. secure/lib
  40. usr.bin/lex/lib
  41. cddl/lib
  42. contrib/ofed
  43. " # where to scan for libraries
  44. # convert -lfoo to foo
  45. convert()
  46. {
  47. sed -e "s/\-l//g" -e "s/pthread/thr/g" -e "s/ncurses.*/ncurses/g"
  48. }
  49. # find library build directory given library name
  50. findlibdir()
  51. {
  52. while read NAME && read DIR
  53. do
  54. if [ "$NAME" = "$1" ]; then
  55. echo "$DIR"
  56. exit
  57. fi
  58. done
  59. # Should not happen
  60. echo lib_not_found/lib$1
  61. }
  62. # find library build directories given one or more library names
  63. resolvelibdirs()
  64. {
  65. while read LIBNAME
  66. do
  67. cat $LIBDIRS | tr ' ' '\n' | findlibdir "$LIBNAME"
  68. done
  69. }
  70. # Generate interdependencies between libraries.
  71. #
  72. genlibdepends()
  73. {
  74. (
  75. # Reset file
  76. echo -n > $LIBDIRS
  77. # First pass - generate list of directories
  78. cd ${USRSRC}
  79. find -s ${LIBS} -name Makefile |
  80. xargs grep -l 'bsd\.lib\.mk' |
  81. while read makefile; do
  82. libdir=$(dirname ${makefile})
  83. libname=$(
  84. cd ${libdir}
  85. make -m ${USRSRC}/share/mk WITH_OFED=YES -V LIB
  86. )
  87. if [ "${libname}" ]; then
  88. echo "${libname} ${libdir}" >> $LIBDIRS
  89. fi
  90. done
  91. # Second pass - generate dependencies
  92. find -s ${LIBS} -name Makefile |
  93. xargs grep -l 'bsd\.lib\.mk' |
  94. while read makefile; do
  95. libdir=$(dirname ${makefile})
  96. deps=$(
  97. cd ${libdir}
  98. make -m ${USRSRC}/share/mk WITH_OFED=YES -V LDADD
  99. )
  100. if [ "${deps}" ]; then
  101. echo ${libdir}"${FS}"$(echo ${deps} | tr ' ' '\n' | convert | resolvelibdirs)
  102. fi
  103. done
  104. )
  105. }
  106. main()
  107. {
  108. if [ ! -f ${LIBDEPENDS} ]; then
  109. genlibdepends >${LIBDEPENDS}
  110. fi
  111. prebuild_libs=$(
  112. awk -F"${FS}" '{ print $2 }' ${LIBDEPENDS} | tr ' ' '\n' |
  113. sort -u
  114. )
  115. echo "Libraries with dependents:"
  116. echo
  117. echo ${prebuild_libs} | tr ' ' '\n'
  118. echo
  119. echo "List of interdependencies:"
  120. echo
  121. for lib in ${prebuild_libs}; do
  122. grep "^${lib}${FS}" ${LIBDEPENDS} || true
  123. done |
  124. awk -F"${FS}" '{
  125. if ($2 in dependents)
  126. dependents[$2]=dependents[$2]" "$1
  127. else
  128. dependents[$2]=$1
  129. }
  130. END {
  131. for (lib in dependents)
  132. print dependents[lib]": " lib
  133. }' |
  134. sort
  135. exit 0
  136. }
  137. main