split out version functions; add libtool_2_2 check
[platform/upstream/gst-common.git] / gst-autogen.sh
1 # a silly hack that generates autoregen.sh but it's handy
2 # Remove the old autoregen.sh first to create a new file,
3 # as the current one may be being read by the shell executing
4 # this script.
5 if [ -f "autoregen.sh" ]; then
6   rm autoregen.sh
7 fi
8 echo "#!/bin/sh" > autoregen.sh
9 echo "./autogen.sh $@ \$@" >> autoregen.sh
10 chmod +x autoregen.sh
11
12 # helper functions for autogen.sh
13
14 debug ()
15 # print out a debug message if DEBUG is a defined variable
16 {
17   if test ! -z "$DEBUG"
18   then
19     echo "DEBUG: $1"
20   fi
21 }
22
23 version_get ()
24 # based on the command's version output, set variables
25 # _MAJOR, _MINOR, _MICRO, _VERSION, using the given prefix as variable prefix
26 #
27 # arg 1: command binary name
28 # arg 2: (uppercased) variable name prefix
29 {
30   COMMAND=$1
31   VARPREFIX=$2
32
33   # strip everything that's not a digit, then use cut to get the first field
34   pkg_version=`$COMMAND --version|head -n 1|sed 's/^.*)[^0-9]*//'|cut -d' ' -f1`
35   debug "pkg_version $pkg_version"
36   # remove any non-digit characters from the version numbers to permit numeric
37   # comparison
38   pkg_major=`echo $pkg_version | cut -d. -f1 | sed s/[a-zA-Z\-].*//g`
39   pkg_minor=`echo $pkg_version | cut -d. -f2 | sed s/[a-zA-Z\-].*//g`
40   pkg_micro=`echo $pkg_version | cut -d. -f3 | sed s/[a-zA-Z\-].*//g`
41   test -z "$pkg_major" && pkg_major=0
42   test -z "$pkg_minor" && pkg_minor=0
43   test -z "$pkg_micro" && pkg_micro=0
44   debug "found major $pkg_major minor $pkg_minor micro $pkg_micro"
45   eval `echo ${VARPREFIX}`_MAJOR=$pkg_major
46   eval `echo ${VARPREFIX}`_MINOR=$pkg_minor
47   eval `echo ${VARPREFIX}`_MICRO=$pkg_micro
48   eval `echo ${VARPREFIX}`_VERSION=$pkg_version
49 }
50
51 version_compare ()
52 # arg1: VARPREFIX
53 # arg2: MAJOR
54 # arg3: MINOR
55 # arg4: MICRO
56 {
57   VARPREFIX=$1
58   MAJOR=$2
59   MINOR=$3
60   MICRO=$4
61
62   pkg_major=$(eval echo $`echo ${VARPREFIX}`_MAJOR);
63   pkg_minor=$(eval echo $`echo ${VARPREFIX}`_MINOR);
64   pkg_micro=$(eval echo $`echo ${VARPREFIX}`_MICRO);
65
66   #start checking the version
67   debug "version check"
68
69     # reset check
70     WRONG=
71
72     if [ ! "$pkg_major" -gt "$MAJOR" ]; then
73       debug "major: $pkg_major <= $MAJOR"
74       if [ "$pkg_major" -lt "$MAJOR" ]; then
75         debug "major: $pkg_major < $MAJOR"
76         WRONG=1
77       elif [ ! "$pkg_minor" -gt "$MINOR" ]; then
78         debug "minor: $pkg_minor <= $MINOR"
79         if [ "$pkg_minor" -lt "$MINOR" ]; then
80           debug "minor: $pkg_minor < $MINOR"
81           WRONG=1
82         elif [ "$pkg_micro" -lt "$MICRO" ]; then
83           debug "micro: $pkg_micro < $MICRO"
84           WRONG=1
85         fi
86       fi
87     fi
88     if test ! -z "$WRONG"; then
89       return 1
90     fi
91     return 0
92 }
93
94
95 version_check ()
96 # check the version of a package
97 # first argument : package name (executable)
98 # second argument : optional path where to look for it instead
99 # third argument : source download url
100 # rest of arguments : major, minor, micro version
101 # all consecutive ones : suggestions for binaries to use
102 # (if not specified in second argument)
103 {
104   PACKAGE=$1
105   PKG_PATH=$2
106   URL=$3
107   MAJOR=$4
108   MINOR=$5
109   MICRO=$6
110
111   # for backwards compatibility, we let PKG_PATH=PACKAGE when PKG_PATH null
112   if test -z "$PKG_PATH"; then PKG_PATH=$PACKAGE; fi
113   debug "major $MAJOR minor $MINOR micro $MICRO"
114   VERSION=$MAJOR
115   if test ! -z "$MINOR"; then VERSION=$VERSION.$MINOR; else MINOR=0; fi
116   if test ! -z "$MICRO"; then VERSION=$VERSION.$MICRO; else MICRO=0; fi
117
118   debug "major $MAJOR minor $MINOR micro $MICRO"
119
120   for SUGGESTION in $PKG_PATH; do
121     COMMAND="$SUGGESTION"
122
123     # don't check if asked not to
124     test -z "$NOCHECK" && {
125       echo -n "  checking for $COMMAND >= $VERSION ... "
126     } || {
127       # we set a var with the same name as the package, but stripped of
128       # unwanted chars
129       VAR=`echo $PACKAGE | sed 's/-//g'`
130       debug "setting $VAR"
131       eval $VAR="$COMMAND"
132       return 0
133     }
134
135     which $COMMAND > /dev/null 2>&1
136     if test $? -eq 1;
137     then 
138       debug "$COMMAND not found"
139       continue
140     fi
141
142     VARPREFIX=`echo $COMMAND | sed 's/-//g' | tr [:lower:] [:upper:]`
143     version_get $COMMAND $VARPREFIX
144
145     version_compare $VARPREFIX $MAJOR $MINOR $MICRO
146     if test $? -ne 0; then
147       echo "found $pkg_version, not ok !"
148       continue
149     else
150       echo "found $pkg_version, ok."
151       # we set a var with the same name as the package, but stripped of
152       # unwanted chars
153       VAR=`echo $PACKAGE | sed 's/-//g'`
154       debug "setting $VAR"
155       eval $VAR="$COMMAND"
156       return 0
157     fi
158   done
159
160   echo "$PACKAGE not found !"
161   echo "You must have $PACKAGE installed to compile $package."
162   echo "Download the appropriate package for your distribution,"
163   echo "or get the source tarball at $URL"
164   return 1;
165 }
166
167 aclocal_check ()
168 {
169   # normally aclocal is part of automake
170   # so we expect it to be in the same place as automake
171   # so if a different automake is supplied, we need to adapt as well
172   # so how's about replacing automake with aclocal in the set var,
173   # and saving that in $aclocal ?
174   # note, this will fail if the actual automake isn't called automake*
175   # or if part of the path before it contains it
176   if [ -z "$automake" ]; then
177     echo "Error: no automake variable set !"
178     return 1
179   else
180     aclocal=`echo $automake | sed s/automake/aclocal/`
181     debug "aclocal: $aclocal"
182     if [ "$aclocal" != "aclocal" ];
183     then
184       CONFIGURE_DEF_OPT="$CONFIGURE_DEF_OPT --with-aclocal=$aclocal"
185     fi
186     if [ ! -x `which $aclocal` ]; then
187       echo "Error: cannot execute $aclocal !"
188       return 1
189     fi
190   fi
191 }
192
193 autoheader_check ()
194 {
195   # same here - autoheader is part of autoconf
196   # use the same voodoo
197   if [ -z "$autoconf" ]; then
198     echo "Error: no autoconf variable set !"
199     return 1
200   else
201     autoheader=`echo $autoconf | sed s/autoconf/autoheader/`
202     debug "autoheader: $autoheader"
203     if [ "$autoheader" != "autoheader" ];
204     then
205       CONFIGURE_DEF_OPT="$CONFIGURE_DEF_OPT --with-autoheader=$autoheader"
206     fi
207     if [ ! -x `which $autoheader` ]; then
208       echo "Error: cannot execute $autoheader !"
209       return 1
210     fi
211   fi
212
213 }
214
215 autoconf_2_52d_check ()
216 {
217   # autoconf 2.52d has a weird issue involving a yes:no error
218   # so don't allow it's use
219   test -z "$NOCHECK" && {
220     ac_version=`$autoconf --version|head -n 1|sed 's/^[a-zA-Z\.\ ()]*//;s/ .*$//'`
221     if test "$ac_version" = "2.52d"; then
222       echo "autoconf 2.52d has an issue with our current build."
223       echo "We don't know who's to blame however.  So until we do, get a"
224       echo "regular version.  RPM's of a working version are on the gstreamer site."
225       exit 1
226     fi
227   }
228   return 0
229 }
230 libtool_2_2_gettext_check ()
231 {
232   # libtool 2.2 needs autopoint 0.17 or higher
233   if test $LIBTOOLIZE_MAJOR -ge 2 && test $LIBTOOLIZE_MINOR -ge 2
234   then
235     if test $AUTOPOINT_MAJOR -eq 0 && test $AUTOPOINT_MINOR -le 17
236     then
237       echo "libtool 2.2 requires autopoint 0.17 or higher"
238       return 1
239     fi
240   fi
241   return 0
242 }
243
244
245 die_check ()
246 {
247   # call with $DIE
248   # if set to 1, we need to print something helpful then die
249   DIE=$1
250   if test "x$DIE" = "x1";
251   then
252     echo
253     echo "- Please get the right tools before proceeding."
254     echo "- Alternatively, if you're sure we're wrong, run with --nocheck."
255     exit 1
256   fi
257 }
258
259 autogen_options ()
260 {
261   if test "x$1" = "x"; then
262     return 0
263   fi
264
265   while test "x$1" != "x" ; do
266     optarg=`expr "x$1" : 'x[^=]*=\(.*\)'`
267     case "$1" in
268       --noconfigure)
269           NOCONFIGURE=defined
270           AUTOGEN_EXT_OPT="$AUTOGEN_EXT_OPT --noconfigure"
271           echo "+ configure run disabled"
272           shift
273           ;;
274       --nocheck)
275           AUTOGEN_EXT_OPT="$AUTOGEN_EXT_OPT --nocheck"
276           NOCHECK=defined
277           echo "+ autotools version check disabled"
278           shift
279           ;;
280       -d|--debug)
281           DEBUG=defined
282           AUTOGEN_EXT_OPT="$AUTOGEN_EXT_OPT --debug"
283           echo "+ debug output enabled"
284           shift
285           ;;
286       -h|--help)
287           echo "autogen.sh (autogen options) -- (configure options)"
288           echo "autogen.sh help options: "
289           echo " --noconfigure            don't run the configure script"
290           echo " --nocheck                don't do version checks"
291           echo " --debug                  debug the autogen process"
292           echo
293           echo " --with-autoconf PATH     use autoconf in PATH"
294           echo " --with-automake PATH     use automake in PATH"
295           echo
296           echo "Any argument either not in the above list or after a '--' will be "
297           echo "passed to ./configure."
298           exit 1
299           ;;
300       --with-automake=*)
301           AUTOMAKE=$optarg
302           echo "+ using alternate automake in $optarg"
303           CONFIGURE_DEF_OPT="$CONFIGURE_DEF_OPT --with-automake=$AUTOMAKE"
304           shift
305           ;;
306       --with-autoconf=*)
307           AUTOCONF=$optarg
308           echo "+ using alternate autoconf in $optarg"
309           CONFIGURE_DEF_OPT="$CONFIGURE_DEF_OPT --with-autoconf=$AUTOCONF"
310           shift
311           ;;
312       --) shift ; break ;;
313       *)
314           echo "+ passing argument $1 to configure"
315           CONFIGURE_EXT_OPT="$CONFIGURE_EXT_OPT $1"
316           shift
317           ;;
318     esac
319   done
320
321   for arg do CONFIGURE_EXT_OPT="$CONFIGURE_EXT_OPT $arg"; done
322   if test ! -z "$CONFIGURE_EXT_OPT"
323   then
324     echo "+ options passed to configure: $CONFIGURE_EXT_OPT"
325   fi
326 }
327
328 toplevel_check ()
329 {
330   srcfile=$1
331   test -f $srcfile || {
332         echo "You must run this script in the top-level $package directory"
333         exit 1
334   }
335 }
336
337 tool_run ()
338 {
339   tool=$1
340   options=$2
341   run_if_fail=$3
342   echo "+ running $tool $options..."
343   $tool $options || {
344     echo
345     echo $tool failed
346     eval $run_if_fail
347     exit 1
348   }
349 }
350
351 install_git_hooks ()
352 {
353   if test -d .git; then
354     # install pre-commit hook for doing clean commits
355     for hook in pre-commit; do
356       if test ! \( -x .git/hooks/$hook -a -L .git/hooks/$hook \); then
357         echo "+ Installing git $hook hook"
358         rm -f .git/hooks/$hook
359         ln -s ../../common/hooks/$hook.hook .git/hooks/$hook || {
360           # if we couldn't create a symbolic link, try doing a plain cp
361           if cp common/hooks/pre-commit.hook .git/hooks/pre-commit; then
362             chmod +x .git/hooks/pre-commit;
363           else
364             echo "********** Couldn't install git $hook hook **********";
365           fi
366         }
367       fi
368     done
369   fi
370 }