svn path=/trunk/mcs/; revision=104772
[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 #       Wade Berrier (wberrier@novell.com)
8 #
9 # (C) 2008 Novell (http://www.novell.com)
10 #
11
12 IFS=$'\n'
13 filelist=($(grep -Ev '/usr/doc/|/usr/share/doc/'))
14 monolist=($(printf "%s\n" "${filelist[@]}" | egrep "\\.(exe|dll)\$"))
15
16 # parse .config files to find which native libraries to depend on 
17 #  (target attribute must have double quotes for this to work, ie: target="file" )
18 # Add /etc/mono/config ?
19 configlist=($(printf "%s\n" "${filelist[@]}" | egrep "\\.config\$"))
20
21 # Set the prefix, unless it is overriden (used when building mono rpms)
22 : ${prefix=@prefix@}
23
24 # Can override .config scanning if specified
25 : ${IGNORE_CONFIG_SCAN=0}
26
27 libdir=$prefix/@reloc_libdir@
28 bindir=$prefix/bin
29
30 # Bail out if monodis or libmono is missing
31 if [ ! -x $bindir/monodis ] || [ ! -f $libdir/libmono.so ] ; then
32         echo "monodis missing or unusable, exiting..." 1>&2
33         exit 1
34 fi
35
36 # special case for 64bit archs
37 if test "x@reloc_libdir@" = "xlib64" ; then
38         libext="()(64bit)"
39 else
40         # (note, this works on ppc64 since we only have 32bit mono)
41         libext=""
42 fi
43
44 # Exceptions:
45 case `uname -m` in
46         # ia64 doesn't use lib64 for 'libdir' (sles 9 rpm used to provide both... no longer)
47         ia64)   libext="()(64bit)" ;;
48 esac
49
50 # set LD_LIBRARY_PATH to ensure that libmono.so is found
51 export LD_LIBRARY_PATH=$libdir${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
52
53 REQUIRES=$(
54         for i in "${monolist[@]}"; do
55                 ($bindir/monodis --assemblyref $i | awk '
56                         BEGIN { START=0; LIBNAME=""; VERSION=""; }
57                         (START==0) && /^[0-9]+: Version=/ {
58                                 START=1;
59                                 sub(/Version=/, "", $2);
60                                 VERSION=$2
61                         }
62
63                         (START==1) && /^\tName=/ {
64                                 sub(/Name=/, "", $1);
65                                 LIBNAME=$1
66                                 # Allow rpm deps to be resolved for 1.0 profile version
67                                 if (VERSION=="1.0.3300.0")
68                                         OP=">="
69                                 else
70                                         OP="="
71                                 print "mono(" LIBNAME ") " OP " " VERSION
72                                 START=0
73                         }
74                     ') 2> /dev/null
75         done
76 )
77
78 if [ $IGNORE_CONFIG_SCAN -eq 0 ] ; then
79
80 rpm_config_REQUIRES=$(
81         # Parse the xml .config files to see what native binaries we call into
82         # TODO: also check monodis --moduleref
83         for i in "${configlist[@]}"; do
84                 awk 'match($_, /<dllmap .*target=.*/) {
85                         ignore=0
86                         req=""
87                         split($_, toks, "\"")
88                         toks_size=0
89                         for(tok in toks) { toks_size++ }
90                         for(i=1; i <= toks_size; i++) {
91                                 if(toks[i] ~ /target=/) {
92                                         req=toks[i+1]
93                                 }
94                                 if(toks[i] ~ /os=/) {
95                                         negate=0
96                                         found=0
97
98                                         attr=toks[i+1]
99                                         if(attr ~ /^!/) {
100                                                 attr=substr(attr, 2, length(attr)-1)
101                                                 negate=1
102                                         }
103
104                                         split(attr, os_targets, ",")
105                                         os_targets_size=0
106                                         for(os_target in os_targets) { os_targets_size++ }
107                                         for(j=1; j <= os_targets_size; j++) {
108                                                 if(os_targets[j] == "linux") {
109                                                         found=1
110                                                 }
111                                         }
112
113                                         if(negate) {
114                                                 found=!found
115                                         }
116                                         if (!found) {
117                                                 ignore=1
118                                         } 
119                                 }
120                         }
121                         if(!ignore) {
122                                 print req"'$libext'"
123                         }
124                 } ' $i 2>/dev/null
125         done
126 )
127
128 # Resolve provides to packages, warning on missing to stderr
129 config_REQUIRES=$(
130         first=1 # avoid an empty line if no .config reqs are found
131         for i in ${rpm_config_REQUIRES[@]} ; do
132                 out=$(rpm -q --whatprovides --queryformat "%{NAME}\n" $i)
133                 if [ $? -eq 0 ] ; then
134                         if [ $first -eq 1 ] ; then
135                                echo ""
136                                first=0
137                         fi
138                         echo $out
139                 else
140                         # echo to stderr
141                         echo "mono-find-requires: Warning, could not find package that provides: $i" >&2
142                 fi
143         done
144 )
145
146 fi
147
148 # Note about above:
149 #  Use to do: system("rpm -q --whatprovides --queryformat \"%{NAME}\n\" ""\""req"'$libext'""\"")
150 #  rpmlint prefers to have lib names instead of package names.  There was a reason I was using package names but it slips me now...
151 #  Ah... now I remember... it's for noarch packs.  The noarch packages can be built on either 32 or 64 bit... so we have to depend
152 #   on the package name instead.
153
154 PROVIDES=$(
155         for i in "${monolist[@]}"; do
156                 ($bindir/monodis --assembly $i | awk '
157                         BEGIN { LIBNAME=""; VERSION=""; }
158                         /^Version:/ { VERSION=$2 }
159                         /^Name:/    { LIBNAME=$2 }
160                         END {
161                                 if (VERSION && LIBNAME)
162                                         print "mono(" LIBNAME ") = " VERSION
163                         }
164                     ') 2>/dev/null
165         done
166 )
167 #
168 # This is a little magic trick to get all REQUIRES that are not
169 # in PROVIDES. While RPM functions correctly when such deps exist,
170 # they make the metadata a bit bloated.
171 #
172 # TODO: make this use the mono-find-provides script, to share code
173
174 # Filter out dups from both lists
175 REQUIRES=$(echo "$REQUIRES $config_REQUIRES"  | sort | uniq)
176 PROVIDES=$(echo "$PROVIDES" | sort | uniq)
177
178 #
179 # Get a list of elements that exist in exactly one of PROVIDES or REQUIRES
180 #
181 UNIQ=$(echo "$PROVIDES
182 $REQUIRES" | sort | uniq -u)
183
184 #
185 # Of those, only choose the ones that are in REQUIRES
186 #
187 echo "$UNIQ
188 $REQUIRES" | sort | uniq -d