1#! /bin/sh
2# autogen.sh
3# Copyright (C) 2003, 2014 g10 Code GmbH
4#
5# This file is free software; as a special exception the author gives
6# unlimited permission to copy and/or distribute it, with or without
7# modifications, as long as this notice is preserved.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
11# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12#
13# This is a generic script to create the configure script and handle cross
14# build environments.  It requires the presence of a autogen.rc file to
15# configure it for the respective package.  It is maintained as part of
16# GnuPG and source copied by other packages.
17#
18# Version: 2014-06-06
19
20configure_ac="configure.ac"
21
22cvtver () {
23  awk 'NR==1 {split($NF,A,".");X=1000000*A[1]+1000*A[2]+A[3];print X;exit 0}'
24}
25
26check_version () {
27    if [ $(( `("$1" --version || echo "0") | cvtver` >= $2 )) = 1 ]; then
28       return 0
29    fi
30    echo "**Error**: "\`$1\'" not installed or too old." >&2
31    echo '           Version '$3' or newer is required.' >&2
32    [ -n "$4" ] && echo '           Note that this is part of '\`$4\''.' >&2
33    DIE="yes"
34    return 1
35}
36
37fatal () {
38    echo "autogen.sh:" "$*" >&2
39    DIE=yes
40}
41
42info () {
43    if [ -z "${SILENT}" ]; then
44      echo "autogen.sh:" "$*" >&2
45    fi
46}
47
48die_p () {
49  if [ "$DIE" = "yes" ]; then
50    echo "autogen.sh: Stop." >&2
51    exit 1
52  fi
53}
54
55replace_sysroot () {
56    configure_opts=$(echo $configure_opts | sed "s#@SYSROOT@#${w32root}#g")
57    extraoptions=$(echo $extraoptions | sed "s#@SYSROOT@#${w32root}#g")
58}
59
60# Allow to override the default tool names
61AUTOCONF=${AUTOCONF_PREFIX}${AUTOCONF:-autoconf}${AUTOCONF_SUFFIX}
62AUTOHEADER=${AUTOCONF_PREFIX}${AUTOHEADER:-autoheader}${AUTOCONF_SUFFIX}
63
64AUTOMAKE=${AUTOMAKE_PREFIX}${AUTOMAKE:-automake}${AUTOMAKE_SUFFIX}
65ACLOCAL=${AUTOMAKE_PREFIX}${ACLOCAL:-aclocal}${AUTOMAKE_SUFFIX}
66
67GETTEXT=${GETTEXT_PREFIX}${GETTEXT:-gettext}${GETTEXT_SUFFIX}
68MSGMERGE=${GETTEXT_PREFIX}${MSGMERGE:-msgmerge}${GETTEXT_SUFFIX}
69
70DIE=no
71FORCE=
72SILENT=
73PRINT_HOST=no
74PRINT_BUILD=no
75tmp=$(dirname "$0")
76tsdir=$(cd "${tmp}"; pwd)
77version_parts=3
78
79if [ -n "${AUTOGEN_SH_SILENT}" ]; then
80  SILENT=" --silent"
81fi
82if test x"$1" = x"--help"; then
83  echo "usage: ./autogen.sh [--silent] [--force] [--build-TYPE] [ARGS]"
84  exit 0
85fi
86if test x"$1" = x"--silent"; then
87  SILENT=" --silent"
88  shift
89fi
90if test x"$1" = x"--force"; then
91  FORCE=" --force"
92  shift
93fi
94if test x"$1" = x"--print-host"; then
95  PRINT_HOST=yes
96  shift
97fi
98if test x"$1" = x"--print-build"; then
99  PRINT_BUILD=yes
100  shift
101fi
102
103
104# Reject unsafe characters in $HOME, $tsdir and cwd.  We consider spaces
105# as unsafe because it is too easy to get scripts wrong in this regard.
106am_lf='
107'
108case `pwd` in
109  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
110    fatal "unsafe working directory name" ;;
111esac
112case $tsdir in
113  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
114    fatal "unsafe source directory: \`$tsdir'" ;;
115esac
116case $HOME in
117  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
118    fatal "unsafe home directory: \`$HOME'" ;;
119esac
120die_p
121
122
123# List of variables sourced from autogen.rc.  The strings '@SYSROOT@' in
124# these variables are replaced by the actual system root.
125configure_opts=
126extraoptions=
127# List of optional variables sourced from autogen.rc and ~/.gnupg-autogen.rc
128w32_toolprefixes=
129w32_extraoptions=
130w32ce_toolprefixes=
131w32ce_extraoptions=
132w64_toolprefixes=
133w64_extraoptions=
134amd64_toolprefixes=
135# End list of optional variables sourced from ~/.gnupg-autogen.rc
136# What follows are variables which are sourced but default to
137# environment variables or lacking them hardcoded values.
138#w32root=
139#w32ce_root=
140#w64root=
141#amd64root=
142
143# Convenience option to use certain configure options for some hosts.
144myhost=""
145myhostsub=""
146case "$1" in
147    --find-version)
148        myhost="find-version"
149        SILENT=" --silent"
150        shift
151        ;;
152    --build-w32)
153        myhost="w32"
154        shift
155        ;;
156    --build-w32ce)
157        myhost="w32"
158        myhostsub="ce"
159        shift
160        ;;
161    --build-w64)
162        myhost="w32"
163        myhostsub="64"
164        shift
165        ;;
166    --build-amd64)
167        myhost="amd64"
168        shift
169        ;;
170    --build*)
171        fatal "**Error**: invalid build option $1"
172        shift
173        ;;
174    *)
175        ;;
176esac
177die_p
178
179
180# Source our configuration
181if [ -f "${tsdir}/autogen.rc" ]; then
182    . "${tsdir}/autogen.rc"
183fi
184
185# Source optional site specific configuration
186if [ -f "$HOME/.gnupg-autogen.rc" ]; then
187    info "sourcing extra definitions from $HOME/.gnupg-autogen.rc"
188    . "$HOME/.gnupg-autogen.rc"
189fi
190
191
192# **** FIND VERSION ****
193# This is a helper for the configure.ac M4 magic
194# Called
195#   ./autogen.sh --find-version PACKAGE MAJOR MINOR [MICRO]
196# returns a complete version string with automatic beta numbering.
197if [ "$myhost" = "find-version" ]; then
198    package="$1"
199    major="$2"
200    minor="$3"
201    micro="$4"
202
203    case "$version_parts" in
204      2)
205        matchstr1="$package-$major.[0-9]*"
206        matchstr2="$package-$major-base"
207        vers="$major.$minor"
208        ;;
209      *)
210        matchstr1="$package-$major.$minor.[0-9]*"
211        matchstr2="$package-$major.$minor-base"
212        vers="$major.$minor.$micro"
213        ;;
214    esac
215
216    beta=no
217    if [ -d .git ]; then
218      ingit=yes
219      tmp=$(git describe --match "${matchstr1}" --long 2>/dev/null)
220      if [ -n "$tmp" ]; then
221          tmp=$(echo "$tmp"|awk -F- '$3!=0 && $3 !~ /^beta/ {print"-beta"$3}')
222      else
223          tmp=$(git describe --match "${matchstr2}" --long 2>/dev/null \
224                | awk -F- '$4!=0{print"-beta"$4}')
225      fi
226      [ -n "$tmp" ] && beta=yes
227      rev=$(git rev-parse --short HEAD | tr -d '\n\r')
228      rvd=$((0x$(echo ${rev} | head -c 4)))
229    else
230      ingit=no
231      beta=yes
232      tmp="-unknown"
233      rev="0000000"
234      rvd="0"
235    fi
236
237    echo "$package-$vers$tmp:$beta:$ingit:$vers$tmp:$vers:$tmp:$rev:$rvd:"
238    exit 0
239fi
240# **** end FIND VERSION ****
241
242
243if [ ! -f "$tsdir/build-aux/config.guess" ]; then
244    fatal "$tsdir/build-aux/config.guess not found"
245    exit 1
246fi
247build=`$tsdir/build-aux/config.guess`
248if [ $PRINT_BUILD = yes ]; then
249    echo "$build"
250    exit 0
251fi
252
253
254
255# ******************
256#  W32 build script
257# ******************
258if [ "$myhost" = "w32" ]; then
259    case $myhostsub in
260        ce)
261          w32root="$w32ce_root"
262          [ -z "$w32root" ] && w32root="$HOME/w32ce_root"
263          toolprefixes="$w32ce_toolprefixes arm-mingw32ce"
264          extraoptions="$extraoptions $w32ce_extraoptions"
265          ;;
266        64)
267          w32root="$w64root"
268          [ -z "$w32root" ] && w32root="$HOME/w64root"
269          toolprefixes="$w64_toolprefixes x86_64-w64-mingw32"
270          extraoptions="$extraoptions $w64_extraoptions"
271          ;;
272        *)
273          [ -z "$w32root" ] && w32root="$HOME/w32root"
274          toolprefixes="$w32_toolprefixes i686-w64-mingw32 i586-mingw32msvc"
275          toolprefixes="$toolprefixes i386-mingw32msvc mingw32"
276          extraoptions="$extraoptions $w32_extraoptions"
277          ;;
278    esac
279    info "Using $w32root as standard install directory"
280    replace_sysroot
281
282    # Locate the cross compiler
283    crossbindir=
284    for host in $toolprefixes; do
285        if ${host}-gcc --version >/dev/null 2>&1 ; then
286            crossbindir=/usr/${host}/bin
287            conf_CC="CC=${host}-gcc"
288            break;
289        fi
290    done
291    if [ -z "$crossbindir" ]; then
292        fatal "cross compiler kit not installed"
293        if [ -z "$myhostsub" ]; then
294          info "Under Debian GNU/Linux, you may install it using"
295          info "  apt-get install mingw32 mingw32-runtime mingw32-binutils"
296        fi
297        die_p
298    fi
299    if [ $PRINT_HOST = yes ]; then
300        echo "$host"
301        exit 0
302    fi
303
304    if [ -f "$tsdir/config.log" ]; then
305        if ! head $tsdir/config.log | grep "$host" >/dev/null; then
306            fatal "Please run a 'make distclean' first"
307            die_p
308        fi
309    fi
310
311    $tsdir/configure --enable-maintainer-mode ${SILENT} \
312             --prefix=${w32root}  \
313             --host=${host} --build=${build} SYSROOT=${w32root} \
314             ${configure_opts} ${extraoptions} "$@"
315    rc=$?
316    exit $rc
317fi
318# ***** end W32 build script *******
319
320# ***** AMD64 cross build script *******
321# Used to cross-compile for AMD64 (for testing)
322if [ "$myhost" = "amd64" ]; then
323    [ -z "$amd64root" ] && amd64root="$HOME/amd64root"
324    info "Using $amd64root as standard install directory"
325    replace_sysroot
326
327    toolprefixes="$amd64_toolprefixes x86_64-linux-gnu amd64-linux-gnu"
328
329    # Locate the cross compiler
330    crossbindir=
331    for host in $toolprefixes ; do
332        if ${host}-gcc --version >/dev/null 2>&1 ; then
333            crossbindir=/usr/${host}/bin
334            conf_CC="CC=${host}-gcc"
335            break;
336        fi
337    done
338    if [ -z "$crossbindir" ]; then
339        echo "Cross compiler kit not installed" >&2
340        echo "Stop." >&2
341        exit 1
342    fi
343    if [ $PRINT_HOST = yes ]; then
344        echo "$host"
345        exit 0
346    fi
347
348    if [ -f "$tsdir/config.log" ]; then
349        if ! head $tsdir/config.log | grep "$host" >/dev/null; then
350            echo "Please run a 'make distclean' first" >&2
351            exit 1
352        fi
353    fi
354
355    $tsdir/configure --enable-maintainer-mode ${SILENT} \
356             --prefix=${amd64root}  \
357             --host=${host} --build=${build} \
358             ${configure_opts} ${extraoptions} "$@"
359    rc=$?
360    exit $rc
361fi
362# ***** end AMD64 cross build script *******
363
364
365# Grep the required versions from configure.ac
366autoconf_vers=`sed -n '/^AC_PREREQ(/ {
367s/^.*(\(.*\))/\1/p
368q
369}' ${configure_ac}`
370autoconf_vers_num=`echo "$autoconf_vers" | cvtver`
371
372automake_vers=`sed -n '/^min_automake_version=/ {
373s/^.*="\(.*\)"/\1/p
374q
375}' ${configure_ac}`
376automake_vers_num=`echo "$automake_vers" | cvtver`
377
378if [ -d "${tsdir}/po" ]; then
379  gettext_vers=`sed -n '/^AM_GNU_GETTEXT_VERSION(/ {
380s/^.*\[\(.*\)])/\1/p
381q
382}' ${configure_ac}`
383  gettext_vers_num=`echo "$gettext_vers" | cvtver`
384else
385  gettext_vers="n/a"
386fi
387
388if [ -z "$autoconf_vers" -o -z "$automake_vers" -o -z "$gettext_vers" ]
389then
390  echo "**Error**: version information not found in "\`${configure_ac}\'"." >&2
391  exit 1
392fi
393
394
395if check_version $AUTOCONF $autoconf_vers_num $autoconf_vers ; then
396    check_version $AUTOHEADER $autoconf_vers_num $autoconf_vers autoconf
397fi
398if check_version $AUTOMAKE $automake_vers_num $automake_vers; then
399  check_version $ACLOCAL $automake_vers_num $autoconf_vers automake
400fi
401if [ "$gettext_vers" != "n/a" ]; then
402  if check_version $GETTEXT $gettext_vers_num $gettext_vers; then
403    check_version $MSGMERGE $gettext_vers_num $gettext_vers gettext
404  fi
405fi
406
407if [ "$DIE" = "yes" ]; then
408    cat <<EOF
409
410Note that you may use alternative versions of the tools by setting
411the corresponding environment variables; see README.GIT for details.
412
413EOF
414    die_p
415fi
416
417# Check the git setup.
418if [ -d .git ]; then
419  CP="cp -a"
420  [ -z "${SILENT}" ] && CP="$CP -v"
421  if [ -f .git/hooks/pre-commit.sample -a ! -f .git/hooks/pre-commit ] ; then
422    [ -z "${SILENT}" ] && cat <<EOF
423*** Activating trailing whitespace git pre-commit hook. ***
424    For more information see this thread:
425      https://mail.gnome.org/archives/desktop-devel-list/2009-May/msg00084.html
426    To deactivate this pre-commit hook again move .git/hooks/pre-commit
427    and .git/hooks/pre-commit.sample out of the way.
428EOF
429      $CP .git/hooks/pre-commit.sample .git/hooks/pre-commit
430      chmod +x  .git/hooks/pre-commit
431  fi
432
433  if [ "$gettext_vers" != "n/a" ]; then
434    tmp=$(git config --get filter.cleanpo.clean)
435    if [ "$tmp" != \
436          "awk '/^\"POT-Creation-Date:/&&!s{s=1;next};!/^#: /{print}'" ]
437    then
438      info "*** Adding GIT filter.cleanpo.clean configuration."
439      git config --add filter.cleanpo.clean \
440        "awk '/^\"POT-Creation-Date:/&&!s{s=1;next};!/^#: /{print}'"
441    fi
442  fi
443  if [ -f build-aux/git-hooks/commit-msg -a ! -f .git/hooks/commit-msg ] ; then
444      [ -z "${SILENT}" ] && cat <<EOF
445*** Activating commit log message check hook. ***
446EOF
447      $CP build-aux/git-hooks/commit-msg .git/hooks/commit-msg
448      chmod +x  .git/hooks/commit-msg
449  fi
450fi
451
452aclocal_flags="-I m4"
453if [ -n "${extra_aclocal_flags}" ]; then
454  aclocal_flags="${aclocal_flags} ${extra_aclocal_flags}"
455fi
456if [ -n "${ACLOCAL_FLAGS}" ]; then
457  aclocal_flags="${aclocal_flags} ${ACLOCAL_FLAGS}"
458fi
459info "Running $ACLOCAL ${aclocal_flags} ..."
460$ACLOCAL ${aclocal_flags}
461info "Running autoheader..."
462$AUTOHEADER
463info "Running automake --gnu ..."
464$AUTOMAKE --gnu;
465info "Running autoconf${FORCE} ..."
466$AUTOCONF${FORCE}
467
468info "You may now run:${am_lf}  ${final_info}"
469