2007-05-05 Wade Berrier <wberrier@novell.com>
[mono.git] / scripts / mono-find-requires.in
1 #!/bin/bash
2 #
3 # mono-find-requires
4 #
5 # Authors:
6 #       Ben Maurer (bmaurer@ximian.com)
7 #
8 # (C) 2005 Novell (http://www.novell.com)
9 #
10
11 IFS=$'\n'
12 filelist=($(grep -Ev '/usr/doc/|/usr/share/doc/'))
13 monolist=($(printf "%s\n" "${filelist[@]}" | egrep "\\.(exe|dll)\$"))
14
15 # parse .config files to find which native libraries to depend on 
16 #  (target attribute must have double quotes for this to work, ie: target="file" )
17 # Add /etc/mono/config ?
18 configlist=($(printf "%s\n" "${filelist[@]}" | egrep "\\.config\$"))
19
20 # Set the prefix, unless it is overriden (used when building mono rpms)
21 : ${prefix=@prefix@}
22
23 libdir=$prefix/@reloc_libdir@
24 bindir=$prefix/bin
25
26 # Bail out if monodis or libmono is missing
27 if [ ! -x $bindir/monodis ] || [ ! -f $libdir/libmono.so ] ; then
28         echo "monodis missing or unusable, exiting..." 1>&2
29         exit 1
30 fi
31
32 # special case for 64bit archs
33 if test "x@reloc_libdir@" = "xlib64" ; then
34         libext="()(64bit)"
35 else
36         libext=""
37 fi
38
39 # set LD_LIBRARY_PATH to ensure that libmono.so is found
40 export LD_LIBRARY_PATH=$libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
41
42 REQUIRES=$(
43         for i in "${monolist[@]}"; do
44                 ($bindir/monodis --assemblyref $i | awk '
45                         BEGIN { START=0; LIBNAME=""; VERSION=""; }
46                         (START==0) && /^[0-9]+: Version=/ {
47                                 START=1;
48                                 sub(/Version=/, "", $2);
49                                 VERSION=$2
50                         }
51
52                         (START==1) && /^\tName=/ {
53                                 sub(/Name=/, "", $1);
54                                 LIBNAME=$1
55                                 # Allow rpm deps to be resolved for 1.0 profile version
56                                 if (VERSION=="1.0.3300.0")
57                                         OP=">="
58                                 else
59                                         OP="="
60                                 print "mono(" LIBNAME ") " OP " " VERSION
61                                 START=0
62                         }
63                     ') 2> /dev/null
64         done
65         # Parse the xml .config files to see what native binaries we call into
66         # TODO: also check monodis --moduleref
67         for i in "${configlist[@]}"; do
68                 awk 'match($_, /<dllmap .*target=.*/) {
69                         ignore=0
70                         req=""
71                         split($_, toks, "\"")
72                         for(i=1; i <= length(toks); i++) {
73                                 if(toks[i] ~ /target=/) {
74                                         req=toks[i+1]
75                                 }
76                                 if(toks[i] ~ /os=/) {
77                                         negate=0
78                                         found=0
79
80                                         attr=toks[i+1]
81                                         if(attr ~ /^!/) {
82                                                 attr=substr(attr, 2, length(attr)-1)
83                                                 negate=1
84                                         }
85
86                                         split(attr, os_targets, ",")
87                                         for(j=1; j <= length(os_targets); j++) {
88                                                 if(os_targets[j] == "linux") {
89                                                         found=1
90                                                 }
91                                         }
92
93                                         if(negate) {
94                                                 found=!found
95                                         }
96                                         if (!found) {
97                                                 ignore=1
98                                         } 
99                                 }
100                         }
101                         if(!ignore) {
102                                 system("rpm -q --whatprovides --queryformat \"%{NAME}\n\" ""\""req"'$libext'""\"")
103                         }
104                 } ' $i 2>/dev/null
105         done
106 )
107
108 PROVIDES=$(
109         for i in "${monolist[@]}"; do
110                 ($bindir/monodis --assembly $i | awk '
111                         BEGIN { LIBNAME=""; VERSION=""; }
112                         /^Version:/ { VERSION=$2 }
113                         /^Name:/    { LIBNAME=$2 }
114                         END {
115                                 if (VERSION && LIBNAME)
116                                         print "mono(" LIBNAME ") = " VERSION
117                         }
118                     ') 2>/dev/null
119         done
120 )
121 #
122 # This is a little magic trick to get all REQUIRES that are not
123 # in PROVIDES. While RPM functions correctly when such deps exist,
124 # they make the metadata a bit bloated.
125 #
126
127 # Filter out dups from both lists
128 REQUIRES=$(echo "$REQUIRES" | sort | uniq)
129 PROVIDES=$(echo "$PROVIDES" | sort | uniq)
130
131 #
132 # Get a list of elements that exist in exactly one of PROVIDES or REQUIRES
133 #
134 UNIQ=$(echo "$PROVIDES
135 $REQUIRES" | sort | uniq -u)
136
137 #
138 # Of those, only choose the ones that are in REQUIRES
139 #
140 echo "$UNIQ
141 $REQUIRES" | sort | uniq -d