Adjust script detection rules to work with file >= 5.07 too (RhBug:712251)
[platform/upstream/rpm.git] / autodeps / linux.req
1 #!/bin/bash
2
3 #
4 # Auto-generate requirements for executables (both ELF and a.out) and library
5 # sonames, script interpreters, and perl modules.
6 #
7
8 ulimit -c 0
9
10 #
11 # --- Set needed to 0 for traditional find-requires behavior.
12 needed=1
13 if [ X"$1" = Xldd ]; then
14     needed=0
15 elif [ X"$1" = Xobjdump ]; then
16     needed=1
17 fi
18
19 #
20 # --- Grab the file manifest and classify files.
21 #filelist=`sed "s/['\"]/\\\&/g"`
22 filelist=`sed "s/[]['\"*?{}]/\\\\\&/g"`
23 exelist=`echo $filelist | xargs -r file | \
24         grep -Ev ":.* (commands|script)[, ]" | \
25         grep ":.*executable" | cut -d: -f1`
26 scriptlist=`echo $filelist | xargs -r file | \
27         grep -E ":.* (commands|script)[, ]" | cut -d: -f1`
28 liblist=`echo $filelist | xargs -r file | \
29         grep ":.*shared object" | cut -d : -f1`
30
31 interplist=
32 perllist=
33 pythonlist=
34 tcllist=
35
36 #
37 # --- Alpha does not mark 64bit dependencies
38 case `uname -m` in
39   alpha*)       mark64="" ;;
40   *)            mark64="()(64bit)" ;;
41 esac
42
43 if [ "$needed" -eq 0 ]; then
44 #
45 # --- Executable dependency sonames.
46   for f in $exelist; do
47     [ -r $f -a -x $f ] || continue
48     lib64=`if file -L $f 2>/dev/null | \
49         grep "ELF 64-bit" >/dev/null; then echo "$mark64"; fi`
50     ldd $f | awk '/=>/ {
51         if ($1 !~ /libNoVersion.so/ && $1 !~ /4[um]lib.so/ && $1 !~ /libredhat-kernel.so/) {
52             gsub(/'\''"/,"\\&",$1);
53             printf "%s'$lib64'\n", $1
54         }
55     }'
56   done | xargs -r -n 1 basename | sort -u
57
58 #
59 # --- Library dependency sonames.
60   for f in $liblist; do
61     [ -r $f ] || continue
62     lib64=`if file -L $f 2>/dev/null | \
63         grep "ELF 64-bit" >/dev/null; then echo "$mark64"; fi`
64     ldd $f | awk '/=>/ {
65         if ($1 !~ /libNoVersion.so/ && $1 !~ /4[um]lib.so/ && $1 !~ /libredhat-kernel.so/) {
66             gsub(/'\''"/,"\\&",$1);
67             printf "%s'$lib64'\n", $1
68         }
69     }'
70   done | xargs -r -n 1 basename | sort -u
71 fi
72
73 #
74 # --- Script interpreters.
75 for f in $scriptlist; do
76     [ -r $f -a -x $f ] || continue
77     interp=`head -n 1 $f | sed -e 's/^\#\![     ]*//' | cut -d" " -f1`
78     interplist="$interplist $interp"
79     case $interp in
80     */perl)     perllist="$perllist $f" ;;
81     esac
82 done
83 [ -n "$interplist" ] && { echo "$interplist" | tr '[:blank:]' \\n | sort -u ; }
84
85 #
86 # --- Add perl module files to perllist.
87 for f in $filelist; do
88     [ -r $f -a "${f%.pm}" != "${f}" ] && perllist="$perllist $f"
89 done
90
91 #
92 # --- Weak symbol versions (from glibc).
93 [ -n "$mark64" ] && mark64="(64bit)"
94 for f in $liblist $exelist ; do
95     [ -r $f ] || continue
96     lib64=`if file -L $f 2>/dev/null | \
97         grep "ELF 64-bit" >/dev/null; then echo "$mark64"; fi`
98     objdump -p $f | awk 'BEGIN { START=0; LIBNAME=""; needed='$needed'; }
99         /^$/ { START=0; }
100         /^Dynamic Section:$/ { START=1; }
101         (START==1) && /NEEDED/ {
102             if (needed) {
103                 if ("'$lib64'" != "") {
104                     sub(/$/, "()'$lib64'", $2) ;
105                 }
106                 print $2 ;
107             }
108         }
109         (START==2) && /^[A-Za-z]/ { START=3; }
110         /^Version References:$/ { START=2; }
111         (START==2) && /required from/ {
112             sub(/:/, "", $3);
113             LIBNAME=$3;
114         }
115         (START==2) && (LIBNAME!="") && ($4!="") && (($4~/^GLIBC_*/) || ($4~/^GCC_*/)) {
116             print LIBNAME "(" $4 ")'$lib64'";
117         }
118     '
119 done | sort -u
120
121 #
122 # --- Perl modules.
123 [ -x /usr/lib/rpm/perl.req -a -n "$perllist" ] && \
124     echo $perllist | tr '[:blank:]' \\n | /usr/lib/rpm/perl.req | sort -u
125
126 #
127 # --- Python modules.
128 [ -x /usr/lib/rpm/python.req -a -n "$pythonlist" ] && \
129     echo $pythonlist | tr '[:blank:]' \\n | /usr/lib/rpm/python.req | sort -u
130
131 #
132 # --- Tcl modules.
133 [ -x /usr/lib/rpm/tcl.req -a -n "$tcllist" ] && \
134     echo $tcllist | tr '[:blank:]' \\n | /usr/lib/rpm/tcl.req | sort -u
135
136 exit 0