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