1#! /bin/sh
2# autogen.sh
3# Copyright (C) 2003, 2014, 2017, 2018 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: 2018-07-10
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)
77
78if [ -n "${AUTOGEN_SH_SILENT}" ]; then
79  SILENT=" --silent"
80fi
81if test x"$1" = x"--help"; then
82  echo "usage: ./autogen.sh [OPTIONS] [ARGS]"
83  echo "  Options:"
84  echo "    --silent       Silent operation"
85  echo "    --force        Pass --force to autoconf"
86  echo "    --find-version Helper for configure.ac"
87  echo "    --git-build    Run all commands to  build from a Git"
88  echo "    --print-host   Print only the host triplet"
89  echo "    --print-build  Print only the build platform triplet"
90  echo "    --build-TYPE   Configure to cross build for TYPE"
91  echo ""
92  echo "  ARGS are passed to configure in --build-TYPE mode."
93  echo "  Configuration for this script is expected in autogen.rc"
94  exit 0
95fi
96if test x"$1" = x"--silent"; then
97  SILENT=" --silent"
98  shift
99fi
100if test x"$1" = x"--force"; then
101  FORCE=" --force"
102  shift
103fi
104if test x"$1" = x"--print-host"; then
105  PRINT_HOST=yes
106  shift
107fi
108if test x"$1" = x"--print-build"; then
109  PRINT_BUILD=yes
110  shift
111fi
112
113
114# Reject unsafe characters in $HOME, $tsdir and cwd.  We consider spaces
115# as unsafe because it is too easy to get scripts wrong in this regard.
116am_lf='
117'
118case `pwd` in
119  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
120    fatal "unsafe working directory name" ;;
121esac
122case $tsdir in
123  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
124    fatal "unsafe source directory: \`$tsdir'" ;;
125esac
126case $HOME in
127  *[\;\\\"\#\$\&\'\`$am_lf\ \	]*)
128    fatal "unsafe home directory: \`$HOME'" ;;
129esac
130die_p
131
132
133# List of variables sourced from autogen.rc.  The strings '@SYSROOT@' in
134# these variables are replaced by the actual system root.
135configure_opts=
136extraoptions=
137# List of optional variables sourced from autogen.rc and ~/.gnupg-autogen.rc
138w32_toolprefixes=
139w32_extraoptions=
140w32ce_toolprefixes=
141w32ce_extraoptions=
142w64_toolprefixes=
143w64_extraoptions=
144amd64_toolprefixes=
145# End list of optional variables sourced from ~/.gnupg-autogen.rc
146# What follows are variables which are sourced but default to
147# environment variables or lacking them hardcoded values.
148#w32root=
149#w32ce_root=
150#w64root=
151#amd64root=
152
153# Convenience option to use certain configure options for some hosts.
154myhost=""
155myhostsub=""
156case "$1" in
157    --find-version)
158        myhost="find-version"
159        SILENT=" --silent"
160        shift
161        ;;
162    --git-build)
163        myhost="git-build"
164        shift
165        ;;
166    --build-w32)
167        myhost="w32"
168        shift
169        ;;
170    --build-w32ce)
171        myhost="w32"
172        myhostsub="ce"
173        shift
174        ;;
175    --build-w64)
176        myhost="w32"
177        myhostsub="64"
178        shift
179        ;;
180    --build-amd64)
181        myhost="amd64"
182        shift
183        ;;
184    --build*)
185        fatal "**Error**: invalid build option $1"
186        shift
187        ;;
188    *)
189        ;;
190esac
191die_p
192
193
194# **** GIT BUILD ****
195# This is a helper to build from git.
196if [ "$myhost" = "git-build" ]; then
197    tmp="$(pwd)"
198    cd "$tsdir" || fatal "error cd-ing to $tsdir"
199    ./autogen.sh || fatal "error running ./autogen.sh"
200    cd "$tmp"   || fatal "error cd-ing back to $tmp"
201    die_p
202    "$tsdir"/configure || fatal "error running $tsdir/configure"
203    die_p
204    make || fatal "error running make"
205    die_p
206    make check || fatal "error running male check"
207    die_p
208    exit 0
209fi
210# **** end GIT BUILD ****
211
212
213# Source our configuration
214if [ -f "${tsdir}/autogen.rc" ]; then
215    . "${tsdir}/autogen.rc"
216fi
217
218# Source optional site specific configuration
219if [ -f "$HOME/.gnupg-autogen.rc" ]; then
220    info "sourcing extra definitions from $HOME/.gnupg-autogen.rc"
221    . "$HOME/.gnupg-autogen.rc"
222fi
223
224
225# **** FIND VERSION ****
226# This is a helper for the configure.ac M4 magic
227# Called
228#   ./autogen.sh --find-version PACKAGE MAJOR MINOR [MICRO]
229# returns a complete version string with automatic beta numbering.
230if [ "$myhost" = "find-version" ]; then
231    package="$1"
232    major="$2"
233    minor="$3"
234    micro="$4"
235
236    if [ -z "$package" -o -z "$major" -o -z "$minor" ]; then
237      echo "usage: ./autogen.sh --find-version PACKAGE MAJOR MINOR [MICRO]" >&2
238      exit 1
239    fi
240
241    if [ -z "$micro" ]; then
242      matchstr1="$package-$major.[0-9]*"
243      matchstr2="$package-$major-base"
244      vers="$major.$minor"
245    else
246      matchstr1="$package-$major.$minor.[0-9]*"
247      matchstr2="$package-$major.$minor-base"
248      vers="$major.$minor.$micro"
249    fi
250
251    beta=no
252    if [ -e .git ]; then
253      ingit=yes
254      tmp=$(git describe --match "${matchstr1}" --long 2>/dev/null)
255      tmp=$(echo "$tmp" | sed s/^"$package"//)
256      if [ -n "$tmp" ]; then
257          tmp=$(echo "$tmp" | sed s/^"$package"//  \
258                | awk -F- '$3!=0 && $3 !~ /^beta/ {print"-beta"$3}')
259      else
260          tmp=$(git describe --match "${matchstr2}" --long 2>/dev/null \
261                | awk -F- '$4!=0{print"-beta"$4}')
262      fi
263      [ -n "$tmp" ] && beta=yes
264      rev=$(git rev-parse --short HEAD | tr -d '\n\r')
265      rvd=$((0x$(echo ${rev} | dd bs=1 count=4 2>/dev/null)))
266    else
267      ingit=no
268      beta=yes
269      tmp="-unknown"
270      rev="0000000"
271      rvd="0"
272    fi
273
274    echo "$package-$vers$tmp:$beta:$ingit:$vers$tmp:$vers:$tmp:$rev:$rvd:"
275    exit 0
276fi
277# **** end FIND VERSION ****
278
279
280if [ ! -f "$tsdir/build-aux/config.guess" ]; then
281    fatal "$tsdir/build-aux/config.guess not found"
282    exit 1
283fi
284build=`$tsdir/build-aux/config.guess`
285if [ $PRINT_BUILD = yes ]; then
286    echo "$build"
287    exit 0
288fi
289
290
291
292# ******************
293#  W32 build script
294# ******************
295if [ "$myhost" = "w32" ]; then
296    case $myhostsub in
297        ce)
298          w32root="$w32ce_root"
299          [ -z "$w32root" ] && w32root="$HOME/w32ce_root"
300          toolprefixes="$w32ce_toolprefixes arm-mingw32ce"
301          extraoptions="$extraoptions $w32ce_extraoptions"
302          ;;
303        64)
304          w32root="$w64root"
305          [ -z "$w32root" ] && w32root="$HOME/w64root"
306          toolprefixes="$w64_toolprefixes x86_64-w64-mingw32"
307          extraoptions="$extraoptions $w64_extraoptions"
308          ;;
309        *)
310          [ -z "$w32root" ] && w32root="$HOME/w32root"
311          toolprefixes="$w32_toolprefixes i686-w64-mingw32 i586-mingw32msvc"
312          toolprefixes="$toolprefixes i386-mingw32msvc mingw32"
313          extraoptions="$extraoptions $w32_extraoptions"
314          ;;
315    esac
316    info "Using $w32root as standard install directory"
317    replace_sysroot
318
319    # Locate the cross compiler
320    crossbindir=
321    for host in $toolprefixes; do
322        if ${host}-gcc --version >/dev/null 2>&1 ; then
323            crossbindir=/usr/${host}/bin
324            conf_CC="CC=${host}-gcc"
325            break;
326        fi
327    done
328    if [ -z "$crossbindir" ]; then
329        fatal "cross compiler kit not installed"
330        if [ -z "$myhostsub" ]; then
331          info "Under Debian GNU/Linux, you may install it using"
332          info "  apt-get install mingw32 mingw32-runtime mingw32-binutils"
333        fi
334        die_p
335    fi
336    if [ $PRINT_HOST = yes ]; then
337        echo "$host"
338        exit 0
339    fi
340
341    if [ -f "$tsdir/config.log" ]; then
342        if ! head $tsdir/config.log | grep "$host" >/dev/null; then
343            fatal "Please run a 'make distclean' first"
344            die_p
345        fi
346    fi
347
348    $tsdir/configure --enable-maintainer-mode ${SILENT} \
349             --prefix=${w32root}  \
350             --host=${host} --build=${build} SYSROOT=${w32root} \
351             PKG_CONFIG_LIBDIR=${w32root}/lib/pkgconfig \
352             ${configure_opts} ${extraoptions} "$@"
353    rc=$?
354    exit $rc
355fi
356# ***** end W32 build script *******
357
358# ***** AMD64 cross build script *******
359# Used to cross-compile for AMD64 (for testing)
360if [ "$myhost" = "amd64" ]; then
361    [ -z "$amd64root" ] && amd64root="$HOME/amd64root"
362    info "Using $amd64root as standard install directory"
363    replace_sysroot
364
365    toolprefixes="$amd64_toolprefixes x86_64-linux-gnu amd64-linux-gnu"
366
367    # Locate the cross compiler
368    crossbindir=
369    for host in $toolprefixes ; do
370        if ${host}-gcc --version >/dev/null 2>&1 ; then
371            crossbindir=/usr/${host}/bin
372            conf_CC="CC=${host}-gcc"
373            break;
374        fi
375    done
376    if [ -z "$crossbindir" ]; then
377        echo "Cross compiler kit not installed" >&2
378        echo "Stop." >&2
379        exit 1
380    fi
381    if [ $PRINT_HOST = yes ]; then
382        echo "$host"
383        exit 0
384    fi
385
386    if [ -f "$tsdir/config.log" ]; then
387        if ! head $tsdir/config.log | grep "$host" >/dev/null; then
388            echo "Please run a 'make distclean' first" >&2
389            exit 1
390        fi
391    fi
392
393    $tsdir/configure --enable-maintainer-mode ${SILENT} \
394             --prefix=${amd64root}  \
395             --host=${host} --build=${build} \
396             ${configure_opts} ${extraoptions} "$@"
397    rc=$?
398    exit $rc
399fi
400# ***** end AMD64 cross build script *******
401
402
403# Grep the required versions from configure.ac
404autoconf_vers=`sed -n '/^AC_PREREQ(/ {
405s/^.*(\(.*\))/\1/p
406q
407}' ${configure_ac}`
408autoconf_vers_num=`echo "$autoconf_vers" | cvtver`
409
410automake_vers=`sed -n '/^min_automake_version=/ {
411s/^.*="\(.*\)"/\1/p
412q
413}' ${configure_ac}`
414automake_vers_num=`echo "$automake_vers" | cvtver`
415
416if [ -d "${tsdir}/po" ]; then
417  gettext_vers=`sed -n '/^AM_GNU_GETTEXT_VERSION(/ {
418s/^.*\[\(.*\)])/\1/p
419q
420}' ${configure_ac}`
421  gettext_vers_num=`echo "$gettext_vers" | cvtver`
422else
423  gettext_vers="n/a"
424fi
425
426if [ -z "$autoconf_vers" -o -z "$automake_vers" -o -z "$gettext_vers" ]
427then
428  echo "**Error**: version information not found in "\`${configure_ac}\'"." >&2
429  exit 1
430fi
431
432
433if check_version $AUTOCONF $autoconf_vers_num $autoconf_vers ; then
434    check_version $AUTOHEADER $autoconf_vers_num $autoconf_vers autoconf
435fi
436if check_version $AUTOMAKE $automake_vers_num $automake_vers; then
437  check_version $ACLOCAL $automake_vers_num $autoconf_vers automake
438fi
439if [ "$gettext_vers" != "n/a" ]; then
440  if check_version $GETTEXT $gettext_vers_num $gettext_vers; then
441    check_version $MSGMERGE $gettext_vers_num $gettext_vers gettext
442  fi
443fi
444
445if [ "$DIE" = "yes" ]; then
446    cat <<EOF
447
448Note that you may use alternative versions of the tools by setting
449the corresponding environment variables; see README.GIT for details.
450
451EOF
452    die_p
453fi
454
455# Check the git setup.
456if [ -d .git ]; then
457  CP="cp -p"
458  # If we have a GNU cp we can add -v
459  if cp --version >/dev/null 2>/dev/null; then
460    [ -z "${SILENT}" ] && CP="$CP -v"
461  fi
462  if [ -f .git/hooks/pre-commit.sample -a ! -f .git/hooks/pre-commit ] ; then
463    [ -z "${SILENT}" ] && cat <<EOF
464*** Activating trailing whitespace git pre-commit hook. ***
465    For more information see this thread:
466      https://mail.gnome.org/archives/desktop-devel-list/2009-May/msg00084.html
467    To deactivate this pre-commit hook again move .git/hooks/pre-commit
468    and .git/hooks/pre-commit.sample out of the way.
469EOF
470      $CP .git/hooks/pre-commit.sample .git/hooks/pre-commit
471      chmod +x  .git/hooks/pre-commit
472  fi
473
474  if [ "$gettext_vers" != "n/a" ]; then
475    tmp=$(git config --get filter.cleanpo.clean)
476    if [ "$tmp" != \
477          "awk '/^\"POT-Creation-Date:/&&!s{s=1;next};!/^#: /{print}'" ]
478    then
479      info "*** Adding GIT filter.cleanpo.clean configuration."
480      git config --add filter.cleanpo.clean \
481        "awk '/^\"POT-Creation-Date:/&&!s{s=1;next};!/^#: /{print}'"
482    fi
483  fi
484  if [ -f build-aux/git-hooks/commit-msg -a ! -f .git/hooks/commit-msg ] ; then
485      [ -z "${SILENT}" ] && cat <<EOF
486*** Activating commit log message check hook. ***
487EOF
488      $CP build-aux/git-hooks/commit-msg .git/hooks/commit-msg
489      chmod +x  .git/hooks/commit-msg
490      if [ x"${display_name}" != x ]; then
491         git config format.subjectPrefix "PATCH ${display_name}"
492         git config sendemail.to "${patches_to}"
493      fi
494  fi
495fi
496
497aclocal_flags="-I m4"
498if [ -n "${extra_aclocal_flags}" ]; then
499  aclocal_flags="${aclocal_flags} ${extra_aclocal_flags}"
500fi
501if [ -n "${ACLOCAL_FLAGS}" ]; then
502  aclocal_flags="${aclocal_flags} ${ACLOCAL_FLAGS}"
503fi
504info "Running $ACLOCAL ${aclocal_flags} ..."
505$ACLOCAL ${aclocal_flags}
506info "Running autoheader..."
507$AUTOHEADER
508info "Running automake --gnu ..."
509$AUTOMAKE --gnu;
510info "Running autoconf${FORCE} ..."
511$AUTOCONF${FORCE}
512
513info "You may now run:${am_lf}  ${final_info}"
514