ncurses 6.2 - patch 20210619
[ncurses.git] / test / listused.sh
1 #!/bin/sh
2 ##############################################################################
3 # Copyright 2020,2021 Thomas E. Dickey                                       #
4 # Copyright 2003-2006,2010 Free Software Foundation, Inc.                    #
5 #                                                                            #
6 # Permission is hereby granted, free of charge, to any person obtaining a    #
7 # copy of this software and associated documentation files (the "Software"), #
8 # to deal in the Software without restriction, including without limitation  #
9 # the rights to use, copy, modify, merge, publish, distribute, distribute    #
10 # with modifications, sublicense, and/or sell copies of the Software, and to #
11 # permit persons to whom the Software is furnished to do so, subject to the  #
12 # following conditions:                                                      #
13 #                                                                            #
14 # The above copyright notice and this permission notice shall be included in #
15 # all copies or substantial portions of the Software.                        #
16 #                                                                            #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20 # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23 # DEALINGS IN THE SOFTWARE.                                                  #
24 #                                                                            #
25 # Except as contained in this notice, the name(s) of the above copyright     #
26 # holders shall not be used in advertising or otherwise to promote the sale, #
27 # use or other dealings in this Software without prior written               #
28 # authorization.                                                             #
29 ##############################################################################
30 # $Id: listused.sh,v 1.10 2021/06/17 21:20:30 tom Exp $
31 # A very simple script to list all entrypoints that are used by either a test
32 # program, or within the libraries.  This relies on the output format of 'nm',
33 # and assumes that the libraries are configured with TRACE defined, and using
34 # these options:
35 #       --disable-macros
36 #       --enable-sp-funcs
37 #       --enable-widec
38 # Static libraries are used, to provide some filtering based on internal usage
39 # of the different symbols.
40
41 # keep the sorting independent of locale:
42 if test "${LANGUAGE+set}"    = set; then LANGUAGE=C;    export LANGUAGE;    fi
43 if test "${LANG+set}"        = set; then LANG=C;        export LANG;        fi
44 if test "${LC_ALL+set}"      = set; then LC_ALL=C;      export LC_ALL;      fi
45 if test "${LC_MESSAGES+set}" = set; then LC_MESSAGES=C; export LC_MESSAGES; fi
46 if test "${LC_CTYPE+set}"    = set; then LC_CTYPE=C;    export LC_CTYPE;    fi
47 if test "${LC_COLLATE+set}"  = set; then LC_COLLATE=C;  export LC_COLLATE;  fi
48
49 NM_OPTS=
50
51 if test ! -d ../objects ; then
52         echo "? need objects to run this script"
53         exit 1
54 elif test ! -d ../lib ; then
55         echo "? need libraries to run this script"
56         exit 1
57 fi
58
59 PROGS=
60 for name in `(echo "test:";sort modules; echo "progs:";sort ../progs/modules) |sed -e 's/[      ].*//' -e '/^[#@]/d'`
61 do
62         case $name in
63         *:)
64                 PROGS="$PROGS $name"
65                 ;;
66         *)
67                 NAME=../objects/${name}.o
68                 if test -f $NAME
69                 then
70                         PROGS="$PROGS $NAME"
71                 fi
72                 ;;
73         esac
74 done
75
76 # For each library -
77 for lib in ../lib/*.a
78 do
79         LIB=`basename $lib .a`
80         case $LIB in
81         *_*|*+*)
82                 continue
83                 ;;
84         esac
85
86         tmp=`echo $LIB|sed -e 's/w$//'`
87         echo
88         echo "${tmp}:"
89         echo $tmp |sed -e 's/./-/g'
90         # Construct a list of public externals provided by the library.
91         WANT=`nm $NM_OPTS $lib |\
92                 sed     -e 's/^[^ ]*//' \
93                         -e 's/^ *//' \
94                         -e '/^[ a-z] /d' \
95                         -e '/:$/d' \
96                         -e '/^$/d' \
97                         -e '/^U /d' \
98                         -e 's/^[A-Z] //' \
99                         -e '/^_/d' |\
100                 sort -u`
101         # List programs which use that external.
102         for name in $WANT
103         do
104                 HAVE=
105                 tags=
106                 last=
107                 for prog in $PROGS
108                 do
109                         case $prog in
110                         *:)
111                                 tags=$prog
112                                 ;;
113                         *)
114                                 TEST=`nm $NM_OPTS $prog |\
115                                         sed     -e 's/^[^ ]*//' \
116                                                 -e 's/^ *//' \
117                                                 -e '/^[ a-z] /d' \
118                                                 -e '/:$/d' \
119                                                 -e '/^$/d' \
120                                                 -e 's/^[A-Z] //' \
121                                                 -e '/^_/d' \
122                                                 -e 's/^'${name}'$/_/' \
123                                                 -e '/^[^_]/d'`
124                                 if test -n "$TEST"
125                                 then
126                                         have=`basename $prog .o`
127                                         if test -n "$HAVE"
128                                         then
129                                                 if test "$last" = "$tags"
130                                                 then
131                                                         HAVE="$HAVE $have"
132                                                 else
133                                                         HAVE="$HAVE $tags $have"
134                                                 fi
135                                         else
136                                                 HAVE="$tags $have"
137                                         fi
138                                         last="$tags"
139                                 fi
140                                 ;;
141                         esac
142                 done
143                 # if we did not find a program using it directly, see if it
144                 # is used within a library.
145                 if test -z "$HAVE"
146                 then
147                         for tmp in ../lib/*.a
148                         do
149                                 case $tmp in
150                                 *_*|*+*)
151                                         continue
152                                         ;;
153                                 esac
154                                 TEST=`nm $NM_OPTS $tmp |\
155                                         sed     -e 's/^[^ ]*//' \
156                                                 -e 's/^ *//' \
157                                                 -e '/^[ a-z] /d' \
158                                                 -e '/:$/d' \
159                                                 -e '/^$/d' \
160                                                 -e '/^[A-TV-Z] /d' \
161                                                 -e 's/^[A-Z] //' \
162                                                 -e '/^_/d' \
163                                                 -e 's/^'${name}'$/_/' \
164                                                 -e '/^[^_]/d'`
165                                 if test -n "$TEST"
166                                 then
167                                         tmp=`basename $tmp .a |sed -e 's/w$//'`
168                                         HAVE=`echo $tmp | sed -e 's/lib/lib: /'`
169                                         break
170                                 fi
171                         done
172                 fi
173                 test -z "$HAVE" && HAVE="-"
174                 lenn=`expr 39 - length $name`
175                 lenn=`expr $lenn / 8`
176                 tabs=
177                 while test $lenn != 0
178                 do
179                         tabs="${tabs}   "
180                         lenn=`expr $lenn - 1`
181                 done
182                 echo "${name}${tabs}${HAVE}"
183         done
184 done