1#!/bin/sh
2#
3# Copyright (c) 2002-2020
4#         The Xfce development team. All rights reserved.
5#
6# Written for Xfce by Benedikt Meurer <benny@xfce.org>.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21#
22# xdt-autogen - Part of the Xfce developer tools.
23#
24
25VERSION="@VERSION@"
26
27XDT_AUTOGEN_VERSION_MAJOR="@VERSION_MAJOR@"
28XDT_AUTOGEN_VERSION_MINOR="@VERSION_MINOR@"
29XDT_AUTOGEN_VERSION_MICRO="@VERSION_MICRO@"
30XDT_AUTOGEN_VERSION_REVISION="@VERSION_REVISION@"
31
32prefix="@prefix@"
33datarootdir="@datarootdir@"
34m4macrodir="${datarootdir}/aclocal"
35
36##
37## to properly handle whitespaces in filenames,
38## and more generally special characters different from '\1'
39##
40
41default_IFS=$IFS
42special_IFS=$(printf '\1')
43
44##
45## a few portability tests
46##
47
48if test -z "$EGREP"; then
49  if type egrep >/dev/null 2>&1; then
50    EGREP=egrep
51  else
52    EGREP="grep -E"
53  fi
54fi
55
56##
57## figures out any subdirs that should be configured as a part
58## of recursive configure.
59##
60parse_configure_subdirs()
61{
62  test -f "$1" && sed -n -e 's|\\[\\nt]| |g' \
63                         -e 's|.*AC_CONFIG_SUBDIRS(\[\{0,1\}\([[:alnum:]_ @/-]\{1,\}\).*|\1|p' \
64                      "$1"
65}
66
67##
68## Helper function to look up configure.{in,ac} files recursively.
69##
70lookup_configure_ac_files()
71{
72  configure_ac_file=""
73
74  if test -f "$1/configure.ac"; then
75    configure_ac_file="$1/configure.ac";
76  elif test -f "$1/configure.in"; then
77    configure_ac_file="$1/configure.in";
78  else
79    cat >&2 <<EOF
80xdt-autogen: Directory "$1" does not look like a package
81             directory, neither configure.ac nor configure.in is
82             present.
83EOF
84    exit 1
85  fi
86
87  test -n "$configure_ac_file" && printf "%s$special_IFS" "$configure_ac_file"
88
89  subdirs=$(parse_configure_subdirs "${configure_ac_file}")
90  IFS=$special_IFS
91  for subdir in $subdirs; do
92    IFS=$default_IFS
93    lookup_configure_ac_files "$1/$subdir";
94  done
95}
96
97##
98## Helper function to look up configure.{in,ac}.in files recursively.
99##
100lookup_configure_ac_in_files()
101{
102  configure_ac_in_file=""
103
104  if test -f "$1/configure.ac.in"; then
105    configure_ac_in_file="$1/configure.ac.in";
106  elif test -f "$1/configure.in.in"; then
107    configure_ac_in_file="$1/configure.in.in";
108  fi
109
110  test -n "$configure_ac_in_file" && printf "%s$special_IFS" "$configure_ac_in_file"
111
112  subdirs=$(parse_configure_subdirs "${configure_ac_in_file}")
113  IFS=$special_IFS
114  for subdir in $subdirs; do
115    IFS=$default_IFS
116    lookup_configure_ac_in_files "$1/$subdir";
117  done
118}
119
120
121
122##
123## check command-line args
124##
125if test "$1" = "--version" -o "$1" = "-V"; then
126  echo "$(basename "$0") $VERSION"
127  exit 0
128fi
129
130##
131## see if the caller is requesting a minimum version
132##
133do_version_check() {
134  test -z "$XDT_AUTOGEN_REQUIRED_VERSION" && return 0
135
136  major=$(echo "$XDT_AUTOGEN_REQUIRED_VERSION" | cut -d. -f1)
137  test -n "$major" || return 1
138  test "$major" -le "$XDT_AUTOGEN_VERSION_MAJOR" || return 1
139  test "$XDT_AUTOGEN_VERSION_MAJOR" -gt "$major" && return 0
140
141  minor=$(echo "$XDT_AUTOGEN_REQUIRED_VERSION" | cut -d. -f2)
142  test -n "$minor" || return 1
143  test "$minor" -le "$XDT_AUTOGEN_VERSION_MINOR" || return 1
144  test "$XDT_AUTOGEN_VERSION_MINOR" -gt "$minor" && return 0
145
146  micro=$(echo "$XDT_AUTOGEN_REQUIRED_VERSION" | cut -d. -f3)
147  test -n "$micro" || return 1
148  test "$micro" -le "$XDT_AUTOGEN_VERSION_MICRO" || return 1
149  test "$XDT_AUTOGEN_VERSION_MICRO" -gt "$micro" && return 0
150
151  return 0
152}
153
154if ! do_version_check; then
155          cat >&2 <<EOF
156xdt-autogen: This version of xdt-autogen ($VERSION) is too old.
157             Version $XDT_AUTOGEN_REQUIRED_VERSION or greater is required.
158EOF
159
160  exit 1
161fi
162
163##
164## Determine XDG data dirs
165##
166test -z "${XDG_DATA_HOME}" && XDG_DATA_HOME="${HOME}/.local/share"
167test -z "${XDG_DATA_DIRS}" && XDG_DATA_DIRS="/usr/local/share:/usr/share"
168test -d "@datadir@" && XDG_DATA_DIRS="@datadir@:${XDG_DATA_DIRS}"
169XDG_DATA_DIRS="${XDG_DATA_HOME}:${XDG_DATA_DIRS}"
170export XDG_DATA_DIRS XDG_DATA_HOME
171
172
173MASTER_DIR=$PWD; test -z "${MASTER_DIR}" && MASTER_DIR="."
174
175##
176## First we do some substitutions to generate configure.{ac,in} if necessary
177##
178CONFIGURE_AC_IN_FILES=$(lookup_configure_ac_in_files "$MASTER_DIR")
179IFS=$special_IFS
180for configure_ac_in_file in $CONFIGURE_AC_IN_FILES; do
181  IFS=$default_IFS
182  configure_ac_file=${configure_ac_in_file%.in}
183
184  # first generate a revision id
185  if test -d .git; then
186    revision=$(git rev-parse --short HEAD)
187  fi
188
189  if test -z "$revision"; then
190    revision="UNKNOWN"
191  fi
192
193  # and do the substitution
194  # We don't need @LINGUAS@ list anymore because it is generated from xdt-i18n.m4
195  tmp=$(basename "${configure_ac_in_file}")
196  cat >"$configure_ac_file" <<EOF
197dnl
198dnl This file was autogenerated from "${tmp}".
199dnl Edit that file instead!
200dnl
201
202EOF
203  sed -e "s/@REVISION@/${revision}/g" \
204      -e "s/@LINGUAS@//g" \
205      "$configure_ac_in_file" >> "$configure_ac_file"
206
207done
208
209
210##
211## Search for the configure.{ac,in} files
212##
213CONFIGURE_AC_FILES=$(lookup_configure_ac_files "$MASTER_DIR")
214
215
216##
217## Check for a suitable make
218##
219if test -z "${MAKE}"; then
220  if type gmake >/dev/null 2>/dev/null; then
221    MAKE="gmake"
222  elif type make >/dev/null 2>/dev/null; then
223    MAKE="make"
224  else
225    cat >&2 <<EOF
226xdt-autogen: You must have "make" installed on your system.
227EOF
228    exit 1
229  fi
230  export MAKE
231fi
232
233
234##
235## cleanup autogenerated files
236##
237if test "$1" = "clean"; then
238  IFS=$special_IFS
239  for configure_ac_file in $CONFIGURE_AC_FILES; do
240    IFS=$default_IFS
241    directory=$(dirname "${configure_ac_file}")
242
243    echo "Running ${MAKE} distclean in ${directory}..."
244    ( cd "${directory}" && ${MAKE} distclean ) >/dev/null 2>&1
245
246    echo "Cleaning generated files in ${directory}..."
247
248    # determine the output files used in this package directory
249    output_files=$(
250      sed -n -e 's|\\[\\nt]| |g' \
251             -e 's|.*AC_OUTPUT(\[\{0,1\}\([[:alnum:]_@/\. -]\{1,\}\).*|\1|p' \
252          "${configure_ac_file}"
253    )
254    # we are in the repository here: these filenames don't contain whitespaces
255    # nor special characters: no need to change the IFS
256    for output_file in $output_files; do
257      if test "$(basename "$output_file")" = "Makefile"; then
258        rm -f "${directory}/${output_file}.in"
259        rm -f "${directory}/${output_file}.in.in"
260      fi
261      rm -f "${directory}/${output_file}"
262    done
263
264    (
265      cd "${directory}" && {
266        rm -f config.* configure configure.lineno aclocal.m4
267        rm -f compile depcomp ltmain.sh missing install-sh
268        rm -f po/Makefile.in.in po/stamp-it
269        rm -f stamp-h1 ./*.spec
270        rm -f mkinstalldirs libtool
271        rm -rf autom4te.cache m4 gtk-doc.m4
272        rm -f intltool-* gtk-doc.make
273        rm -f test-driver
274
275        if test -f po/POTFILES.in; then
276          rm -f po/POTFILES
277        fi
278        if test -f configure.ac.in -a -f configure.ac; then
279          rm -f configure.ac
280        elif test -f configure.in.in -a -f configure.in; then
281          rm -f configure.in
282        fi
283      }
284    )
285
286    # determine translations used in this package directory
287#    translations=$(
288#      sed -n -e 's|\\[\\nt]| |g' \
289#             -e 's|.*XDT_I18N(\[\{0,1\}\([a-zA-Z_@ ]\{1,\}\).*|\1|p') \
290#          "${configure_ac_file}"
291#    )
292#    for translation in $translations; do
293#      rm -f "${directory}/po/${translation}.gmo"
294#    done
295    rm -f "${directory}"/po/*.gmo;
296  done
297
298  exit 0
299fi
300
301
302##
303## Check for autoconf
304##
305if test -z "${XDT_PROG_AUTOCONF}"; then
306  test -z "${AUTOCONF_VERSION}" && i=autoconf || i=autoconf-${AUTOCONF_VERSION}
307  ${i} --version </dev/null >/dev/null 2>&1 && XDT_PROG_AUTOCONF=${i}
308fi
309
310test -z "${XDT_PROG_AUTOCONF}" && {
311  cat >&2 <<EOF
312xdt-autogen: You must have "autoconf" installed on your system.
313             Download the appropriate package for your distribution,
314             or get the source tarball at ftp://ftp.gnu.org/pub/gnu/.
315EOF
316  exit 1
317}
318
319
320##
321## Check for intltoolize
322##
323test -z "${XDT_PROG_INTLTOOLIZE}" && XDT_PROG_INTLTOOLIZE="intltoolize"
324IFS=$special_IFS
325for configure_ac_file in $CONFIGURE_AC_FILES; do
326  IFS=$default_IFS
327  if $EGREP -q "^(AC|IT)_PROG_INTLTOOL" "${configure_ac_file}"; then
328    ${XDT_PROG_INTLTOOLIZE} --version </dev/null >/dev/null 2>&1 || {
329      cat >&2 <<EOF
330xdt-autogen: You must have "intltool" installed on your system.
331             You can download the source tarball from
332             ftp://ftp.gnome.org/pub/GNOME/.
333EOF
334      exit 1
335    }
336    break
337  fi
338done
339
340IFS=$special_IFS
341for configure_ac_file in $CONFIGURE_AC_FILES; do
342  IFS=$default_IFS
343  if grep -q "^AC_PROG_INTLTOOL" "${configure_ac_file}"; then
344  cat >&2 <<EOF
345xdt-autogen: It is recommended to use IT_PROG_INTLTOOL([0.35.0])
346             in your configure.ac file and remove AC_PROG_INTLTOOL
347
348             See https://bugzilla.xfce.org/show_bug.cgi?id=8930 for
349             more information.
350
351EOF
352  fi
353done
354
355
356##
357## Check for libtoolize
358##
359if test -z "${XDT_PROG_LIBTOOLIZE}"; then
360  if type glibtoolize >/dev/null 2>&1; then
361    XDT_PROG_LIBTOOLIZE="glibtoolize"
362  elif type libtoolize >/dev/null 2>&1; then
363    XDT_PROG_LIBTOOLIZE="libtoolize"
364  fi
365fi
366
367IFS=$special_IFS
368for configure_ac_file in $CONFIGURE_AC_FILES; do
369  IFS=$default_IFS
370  runlibtoolize=0
371  if grep -q "^AC_PROG_LIBTOOL" "${configure_ac_file}"; then
372  cat >&2 <<EOF
373xdt-autogen: It is recommended to use LT_PREREQ([@VERSION_LIBTOOL@]) and
374             LT_INIT([disable-static]) in your configure.ac
375             file and remove AC_PROG_LIBTOOL and AC_DISABLE_STATIC.
376
377             See https://bugzilla.xfce.org/show_bug.cgi?id=6920 for
378             more information.
379
380EOF
381    runlibtoolize=1
382  fi
383
384  if grep -q "^LT_PREREQ" "${configure_ac_file}"; then
385    runlibtoolize=1
386  fi
387
388  if test "$runlibtoolize" -eq 1; then
389    ${XDT_PROG_LIBTOOLIZE} --version </dev/null >/dev/null 2>&1 || {
390      cat >&2 <<EOF
391xdt-autogen: You must have "libtool" installed on your system.
392             Download the appropriate package for your distribution,
393             or get the source tarball at ftp://ftp.gnu.org/pub/gnu/.
394EOF
395      exit 1
396    }
397    break
398  fi
399done
400
401
402##
403## Check for glib-gettextize
404##
405test -z "${XDT_PROG_GLIB_GETTEXTIZE}" && XDT_PROG_GLIB_GETTEXTIZE="glib-gettextize"
406IFS=$special_IFS
407for configure_ac_file in $CONFIGURE_AC_FILES; do
408  IFS=$default_IFS
409  directory=$(dirname "${configure_ac_file}")
410  if test -d "${directory}/po"; then
411    ${XDT_PROG_GLIB_GETTEXTIZE} --version </dev/null >/dev/null 2>&1 || {
412      cat >&2 <<EOF
413xdt-autogen: You must have "glib2" installed. You can get if from
414             ftp://ftp.gtk.org/pub/gtk/.
415EOF
416      exit 1
417    }
418    break
419  fi
420done
421
422
423##
424## Check for gtkdocize
425##
426test -z "${XDT_PROG_GTKDOCIZE}" && XDT_PROG_GTKDOCIZE="gtkdocize"
427IFS=$special_IFS
428for configure_ac_file in $CONFIGURE_AC_FILES; do
429  IFS=$default_IFS
430  if grep -q "^GTK_DOC_CHECK" "${configure_ac_file}"; then
431    ${XDT_PROG_GTKDOCIZE} --version </dev/null >/dev/null 2>&1 || {
432      cat >&2 <<EOF
433xdt-autogen: You must have "gtk-doc" installed. You can get if from
434             http://www.gtk.org/gtk-doc/.
435EOF
436      exit 1
437    }
438    break
439  fi
440done
441
442
443##
444## Check for aclocal
445##
446if test -z "${XDT_PROG_ACLOCAL}"; then
447  test -z "${ACLOCAL_VERSION}" && i=aclocal || i=aclocal-${ACLOCAL_VERSION}
448  ${i} --version </dev/null >/dev/null 2>&1 && XDT_PROG_ACLOCAL=${i}
449fi
450
451test -z "${XDT_PROG_ACLOCAL}" && {
452  cat >&2 <<EOF
453xdt-autogen: You must have "automake" installed (which includes the
454             "aclocal" tool). You can get the source tarball at
455             ftp://ftp.gnu.org/pub/gnu/.
456EOF
457  exit 1
458}
459
460
461##
462## Check for autoheader
463##
464test -z "${XDT_PROG_AUTOHEADER}" &&
465IFS=$special_IFS
466for configure_ac_file in $CONFIGURE_AC_FILES; do
467  IFS=$default_IFS
468  if $EGREP -q "^A(M|C)_CONFIG_HEADER" "${configure_ac_file}"; then
469    test -z "${AUTOHEADER_VERSION}" && i=autoheader \
470                                    || i=autoheader-${AUTOHEADER_VERSION}
471    ${i} --version </dev/null >/dev/null 2>&1 && XDT_PROG_AUTOHEADER=${i}
472
473    test -z "${XDT_PROG_AUTOHEADER}" && {
474      cat >&2 <<EOF
475xdt-autogen: You must have "autoconf" installed (which includes the
476             "autoheader" tool). You can get the source tarball at
477             ftp://ftp.gnu.org/pub/gnu/.
478EOF
479      exit 1
480    }
481    break
482  fi
483done
484
485
486##
487## Check for automake
488##
489if test -z "${XDT_PROG_AUTOMAKE}"; then
490  test -z "${AUTOMAKE_VERSION}" && i=automake || i=automake-${AUTOMAKE_VERSION}
491  ${i} --version </dev/null >/dev/null 2>&1 && XDT_PROG_AUTOMAKE=${i}
492fi
493
494test -z "${XDT_PROG_AUTOMAKE}" && {
495  cat >&2 <<EOF
496xdt-autogen: You must have "automake" installed on your system.
497             You can get the source tarball at
498             ftp://ftp.gnu.org/pub/gnu/.
499EOF
500  exit 1
501}
502
503
504##
505## Check for configure flags
506##
507test -z "${XDT_CONFIGURE_FLAGS}" && XDT_CONFIGURE_FLAGS="--enable-maintainer-mode"
508CONFIGURE_FLAGS="${XDT_CONFIGURE_FLAGS} $*"
509
510
511##
512## Do the real work(TM)
513##
514IFS=$special_IFS
515for configure_ac_file in ${CONFIGURE_AC_FILES}; do
516  IFS=$default_IFS
517  # figure out the package dir path
518  source_dir=$(dirname "${configure_ac_file}")
519  echo "Preparing package directory ${source_dir}..."
520
521  # set aclocal flags
522  ACLOCAL_FLAGS="${ACLOCAL_FLAGS} ${XDT_ACLOCAL_FLAGS} -I ${m4macrodir}"
523  if test -d "${source_dir}/m4macros"; then
524    ACLOCAL_DIR_1="${source_dir}/m4macros";
525  fi
526  if test -d "${source_dir}/m4"; then
527    ACLOCAL_DIR_2="${source_dir}/m4";
528  fi
529
530  if test -d "${source_dir}/po"; then
531    if test ! -f "${source_dir}/aclocal.m4"; then
532      echo "Creating ${source_dir}/aclocal.m4..." \
533      && echo "dnl Auto-generated by xdt-autogen" > "${source_dir}/aclocal.m4" \
534      || exit 1
535    fi
536    echo "Running ${XDT_PROG_GLIB_GETTEXTIZE} --force --copy..." \
537    && ${XDT_PROG_GLIB_GETTEXTIZE} --force --copy \
538    || exit 1
539    if test -f "${source_dir}/aclocal.m4" -a ! -w "${source_dir}/aclocal.m4"; then
540      echo "Making ${source_dir}/aclocal.m4 writable..." \
541      && chmod u+w "${source_dir}/aclocal.m4" \
542      || exit 1
543    fi
544  fi
545
546  if $EGREP -q "^(AC|IT)_PROG_INTLTOOL" "${configure_ac_file}"; then
547    ( echo "Running ${XDT_PROG_INTLTOOLIZE} --automake --copy --force" \
548      && cd "${source_dir}" \
549      && ${XDT_PROG_INTLTOOLIZE} --automake --copy --force ) || exit 1
550  fi
551
552  # patch the po/Makefile.in.in to take into account the setting of
553  # XGETTEXT_ARGS properly
554  if test -f "${source_dir}/po/Makefile.in.in"; then
555    if ! grep -q '^XGETTEXT_ARGS[ ]*=[ ]*@XGETTEXT_ARGS@$' \
556                 "${source_dir}/po/Makefile.in.in"
557    then
558      echo "Patching file 'po/Makefile.in.in'"
559      if sed \
560        -e 's/^\(XGETTEXT[ ]*=[ ]*@XGETTEXT@\)[ ]*$/\1 $(XGETTEXT_ARGS)/' \
561        -e 's/^\(MSGMERGE[ ]*=\)[ ]*\(INTLTOOL_EXTRACT=\)/\1 XGETTEXT_ARGS="$(XGETTEXT_ARGS)" \2/' \
562        -e 's/^\(GENPOT[ ]*=\)[ ]*\(INTLTOOL_EXTRACT=\)/\1 XGETTEXT_ARGS="$(XGETTEXT_ARGS)" \2/' \
563        -e "/^XGETTEXT = @XGETTEXT@/{
564i\\
565XGETTEXT_ARGS = @XGETTEXT_ARGS@
566}" "${source_dir}/po/Makefile.in.in" > "${source_dir}/po/Makefile.in.in.tmp"
567      then
568        mv -f "${source_dir}/po/Makefile.in.in.tmp" \
569          "${source_dir}/po/Makefile.in.in" || exit 1
570      fi
571    fi
572  fi
573
574  if grep -q -e "^AC_PROG_LIBTOOL" -e "^LT_PREREQ" "${configure_ac_file}"; then
575    ( echo "Running ${XDT_PROG_LIBTOOLIZE} --force --copy..." \
576      && cd "${source_dir}" \
577      && ${XDT_PROG_LIBTOOLIZE} --force --copy ) || exit 1
578  fi
579
580  if grep -q "^GTK_DOC_CHECK" "${configure_ac_file}"; then
581    ( echo "Running ${XDT_PROG_GTKDOCIZE} --copy..." \
582      && cd "${source_dir}" \
583      && ${XDT_PROG_GTKDOCIZE} --copy ) || exit 1
584  fi
585
586  ( echo -n "Running ${XDT_PROG_ACLOCAL}" ${ACLOCAL_FLAGS} \
587            ${ACLOCAL_DIR_1:+-I "${ACLOCAL_DIR_1}"} \
588            ${ACLOCAL_DIR_2:+-I "${ACLOCAL_DIR_2}"} \
589    && echo '...' \
590    && cd "${source_dir}" \
591    && ${XDT_PROG_ACLOCAL} ${ACLOCAL_FLAGS} \
592         ${ACLOCAL_DIR_1:+-I "${ACLOCAL_DIR_1}"} \
593         ${ACLOCAL_DIR_2:+-I "${ACLOCAL_DIR_2}"} ) || exit 1
594
595  if $EGREP -q "^A(M|C)_CONFIG_HEADER" "${configure_ac_file}"; then
596    ( echo "Running ${XDT_PROG_AUTOHEADER}..." \
597      && cd "${source_dir}" \
598      && ${XDT_PROG_AUTOHEADER} ) || exit 1
599  fi
600
601  ( echo "Running ${XDT_PROG_AUTOMAKE} --force-missing --add-missing --copy --gnu..." \
602    && cd "${source_dir}" \
603    && ${XDT_PROG_AUTOMAKE} --force-missing --add-missing --copy --gnu ) || exit 1
604
605  ( echo "Running ${XDT_PROG_AUTOCONF}..." \
606    && cd "${source_dir}" \
607    && ${XDT_PROG_AUTOCONF} ) || exit 1
608
609  echo
610done
611
612
613##
614## Run configure
615##
616if test -z "${NOCONFIGURE}"; then
617  ( echo "Running ${MASTER_DIR}/configure ${CONFIGURE_FLAGS}..." \
618    && cd "${MASTER_DIR}" \
619    && ./configure ${CONFIGURE_FLAGS} \
620    && echo "Now type \"make\" to compile." ) || exit 1
621else
622  echo "Skipping configure process."
623fi
624
625
626# vi:set ts=2 sw=2 et ai:
627