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