Add a warning about duplicate dependencies.
[pspp] / gnulib-tool
1 #! /bin/sh
2 #
3 # Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #
19
20 # This program is meant for authors or maintainers which want to import
21 # modules from gnulib into their packages.
22
23 progname=$0
24 package=gnulib
25 cvsdatestamp='$Date: 2003-09-16 14:53:35 $'
26 last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'`
27 version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'`
28
29 # You can set AUTOCONFPATH to empty if autoconf 2.57 is already in your PATH.
30 AUTOCONFPATH=
31 case $USER in
32   bruno )
33     AUTOCONFBINDIR=/packages/gnu-inst-autoconf/2.57/bin
34     AUTOCONFPATH="eval env PATH=${AUTOCONFBINDIR}:\$PATH "
35     ;;
36 esac
37
38 AUTOCONF="${AUTOCONFPATH}autoconf"
39 AUTOHEADER="${AUTOCONFPATH}autoheader"
40 AUTOMAKE="${AUTOCONFPATH}automake-1.7"
41 ACLOCAL="aclocal-1.7"
42
43 # func_usage
44 # outputs to stdout the --help usage message.
45 func_usage ()
46 {
47   echo "\
48 Usage: gnulib-tool --list
49        gnulib-tool --import module1 ... moduleN
50        gnulib-tool --create-testdir --dir=directory module1 ... moduleN
51        gnulib-tool --create-megatestdir --dir=directory [module1 ... moduleN]
52        gnulib-tool --test --dir=directory module1 ... moduleN
53        gnulib-tool --megatest --dir=directory [module1 ... moduleN]
54        gnulib-tool --extract-description module
55        gnulib-tool --extract-filelist module
56        gnulib-tool --extract-dependencies module
57        gnulib-tool --extract-autoconf-snippet module
58        gnulib-tool --extract-automake-snippet module
59        gnulib-tool --extract-include-directive module
60        gnulib-tool --extract-maintainer module
61
62 Operation modes:
63       --list                print the available module names
64       --import              import the given modules into the current package
65       --create-testdir      create a scratch package with the given modules
66       --create-megatestdir  create a mega scratch package with the given modules
67                             one by one and all together
68       --test                test the combination of the given modules
69                             (recommended to use CC=\"gcc -Wall\" here)
70       --megatest            test the given modules one by one and all together
71                             (recommended to use CC=\"gcc -Wall\" here)
72       --extract-description        extract the description
73       --extract-filelist           extract the list of files
74       --extract-dependencies       extract the dependencies
75       --extract-autoconf-snippet   extract the snippet for configure.ac
76       --extract-automake-snippet   extract the snippet for lib/Makefile.am
77       --extract-include-directive  extract the #include directive
78       --extract-maintainer         report the maintainer(s) inside gnulib
79
80 Options:
81       --dir=DIRECTORY       specify the target directory
82       --lib=LIBRARY         specify the library name
83       --no-changelog        don't update or create ChangeLog files
84
85 Report bugs to <bug-gnulib@gnu.org>."
86 }
87
88 # func_version
89 # outputs to stdout the --version message.
90 func_version ()
91 {
92   echo "$progname (GNU $package) $version"
93   echo "Copyright (C) 2002 Free Software Foundation, Inc.
94 This is free software; see the source for copying conditions.  There is NO
95 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
96   echo "Written by" "Bruno Haible"
97 }
98
99 # func_fatal_error message
100 # outputs to stderr a fatal error message, and terminates the program.
101 func_fatal_error ()
102 {
103   echo "gnulib-tool: *** $1" 1>&2
104   echo "gnulib-tool: *** Stop." 1>&2
105   exit 1
106 }
107
108 # Command-line option processing.
109 # Removes the OPTIONS from the arguments. Sets the variables:
110 # - mode            list or import or create-testdir or create-megatestdir
111 # - destdir         from --dir
112 # - libname         from --lib
113 # - do_changelog    false if --no-changelog was given, : otherwise
114 {
115   mode=
116   destdir=
117   libname=libfoo
118   do_changelog=:
119
120   while test $# -gt 0; do
121     case "$1" in
122       --list | --lis )
123         mode=list
124         shift ;;
125       --import | --impor | --impo | --imp | --im | --i )
126         mode=import
127         shift ;;
128       --create-testdir | --create-testdi | --create-testd | --create-test | --create-tes | --create-te | --create-t )
129         mode=create-testdir
130         shift ;;
131       --create-megatestdir | --create-megatestdi | --create-megatestd | --create-megatest | --create-megates | --create-megate | --create-megat | --create-mega | --create-meg | --create-me | --create-m )
132         mode=create-megatestdir
133         shift ;;
134       --test | --tes | --te | --t )
135         mode=test
136         shift ;;
137       --megatest | --megates | --megate | --megat | --mega | --meg | --me | --m )
138         mode=megatest
139         shift ;;
140       --extract-* )
141         mode=`echo "X$1" | sed -e 's/^X--//'`
142         shift ;;
143       --dir )
144         shift
145         if test $# = 0; then
146           func_fatal_error "missing argument for --dir"
147         fi
148         destdir=$1
149         shift ;;
150       --dir=* )
151         destdir=`echo "X$1" | sed -e 's/^X--dir=//'`
152         shift ;;
153       --lib )
154         shift
155         if test $# = 0; then
156           func_fatal_error "missing argument for --lib"
157         fi
158         libname=$1
159         shift ;;
160       --lib=* )
161         libname=`echo "X$1" | sed -e 's/^X--lib=//'`
162         shift ;;
163       --no-changelog | --no-changelo | --no-changel | --no-change | --no-chang | --no-chan | --no-cha | --no-ch | --no-c )
164         do_changelog=false
165         shift ;;
166       --help | --hel | --he | --h )
167         func_usage
168         exit 0 ;;
169       --version | --versio | --versi | --vers | --ver | --ve | --v )
170         func_version
171         exit 0 ;;
172       -- )
173         # Stop option prcessing
174         shift
175         break ;;
176       -* )
177         echo "gnulib-tool: unknown option $1" 1>&2
178         echo "Try 'gnulib-tool --help' for more information." 1>&2
179         exit 1 ;;
180       * )
181         break ;;
182     esac
183   done
184 }
185
186 case "$0" in
187   /*) self_abspathname="$0" ;;
188   */*) self_abspathname=`pwd`/"$0" ;;
189   *) for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`; do
190        if test -x "$d/$0" && test ! -d "$d/$0"; then
191          self_abspathname="$d/$0"
192          break
193        fi
194      done
195      if test -z "$self_abspathname"; then
196        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
197      fi
198      ;;
199 esac
200 while test -L "$self_abspathname"; do
201   # Resolve symbolic link.
202   sedexpr1='s, -> ,#%%#,'
203   sedexpr2='s,^.*#%%#\(.*\)$,\1,p'
204   linkval=`LC_ALL=C ls -l "$self_abspathname" | sed -e "$sedexpr1" | sed -n -e "$sedexpr2"`
205   test -n "$linkval" || break
206   case "$linkval" in
207     /* ) self_abspathname="$linkval" ;;
208     * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
209   esac
210 done
211 gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
212
213 # func_all_modules
214 func_all_modules ()
215 {
216   (cd "$gnulib_dir/modules" && ls -1) \
217       | sed -e '/^CVS$/d' -e '/^ChangeLog$/d' -e '/^TEMPLATE$/d' -e '/~$/d' \
218       | sort
219 }
220
221 # func_verify_module
222 # verifies a module name
223 func_verify_module ()
224 {
225   if test ! -f "$gnulib_dir/modules/$module" \
226      || test "CVS" = "$module" \
227      || test "ChangeLog" = "$module" \
228      || test "TEMPLATE" = "$module"; then
229     echo "gnulib-tool: module $module doesn't exist" 1>&2
230     module=
231   fi
232 }
233
234 tags_regexp='\(Description\|Files\|Depends-on\|configure\.ac\|Makefile\.am\|Include\|Maintainer\)'
235 sed_extract_prog=':[    ]*$/ {
236   :a
237     n
238     s/^'"$tags_regexp"':[       ]*$//
239     tb
240     p
241     ba
242   :b
243 }'
244
245 # func_get_description module
246 func_get_description ()
247 {
248   sed -n -e "/^Description$sed_extract_prog" < "$gnulib_dir/modules/$1"
249 }
250
251 # func_get_filelist module
252 func_get_filelist ()
253 {
254   sed -n -e "/^Files$sed_extract_prog" < "$gnulib_dir/modules/$1"
255   #echo m4/onceonly.m4
256   echo m4/onceonly_2_57.m4
257 }
258
259 # func_get_dependencies module
260 func_get_dependencies ()
261 {
262   sed -n -e "/^Depends-on$sed_extract_prog" < "$gnulib_dir/modules/$1"
263 }
264
265 # func_get_autoconf_snippet module
266 func_get_autoconf_snippet ()
267 {
268   sed -n -e "/^configure\.ac$sed_extract_prog" < "$gnulib_dir/modules/$1"
269 }
270
271 # func_get_automake_snippet module
272 func_get_automake_snippet ()
273 {
274   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$gnulib_dir/modules/$1"
275 }
276
277 # func_get_include_directive module
278 func_get_include_directive ()
279 {
280   sed -n -e "/^Include$sed_extract_prog" < "$gnulib_dir/modules/$1" | \
281   sed -e 's/^\(["<]\)/#include \1/'
282 }
283
284 # func_get_maintainer module
285 func_get_maintainer ()
286 {
287   sed -n -e "/^Maintainer$sed_extract_prog" < "$gnulib_dir/modules/$1"
288 }
289
290 # func_create_testdir testdir modules
291 func_create_testdir ()
292 {
293   testdir="$1"
294   modules="$2"
295   modules=`for m in $modules; do echo $m; done | sort | uniq`
296
297   # Determine final module list.
298   while true; do
299     xmodules=
300     for module in $modules; do
301       func_verify_module
302       if test -n "$module"; then
303         # Duplicate dependenies are harmless, but Jim wants a warning.
304         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
305         if test -n "$duplicated_deps"; then
306           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
307         fi
308         xmodules="$xmodules $module "`func_get_dependencies $module`
309       fi
310     done
311     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
312     if test "$xmodules" = "$modules"; then
313       break
314     fi
315     modules="$xmodules"
316   done
317   echo "Module list with included dependencies:"
318   echo "$modules" | sed -e 's/^/  /'
319
320   # Determine final file list.
321   files=
322   for module in $modules; do
323     func_verify_module
324     if test -n "$module"; then
325       files="$files "`func_get_filelist $module`
326     fi
327   done
328   files=`for f in $files; do echo $f; done | sort | uniq`
329   echo "File list:"
330   echo "$files" | sed -e 's/^/  /'
331
332   # Create directories.
333   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
334     if test "$d" != config; then
335       mkdir -p "$testdir/$d"
336     fi
337   done
338
339   # Copy files.
340   for f in $files; do
341     case "$f" in
342       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
343       *) g="$f" ;;
344     esac
345     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
346     cp -p "$gnulib_dir/$f" "$testdir/$g"
347   done
348
349   # Create lib/Makefile.am.
350   mkdir -p "$testdir/lib"
351   (echo "## Process this file with automake to produce Makefile.in."
352    echo
353    echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
354    echo
355    echo "noinst_LIBRARIES = $libname.a"
356    echo
357    echo "$libname"'_a_SOURCES ='
358    echo "$libname"'_a_LIBADD = @LIBOBJS@'
359    echo '#'"$libname"'_la_LIBADD = @LTLIBOBJS@'
360    echo "EXTRA_DIST ="
361    echo "BUILT_SOURCES ="
362    echo "SUFFIXES ="
363    echo "MOSTLYCLEANFILES ="
364    echo "CLEANFILES ="
365    echo "DISTCLEANFILES ="
366    echo "MAINTAINERCLEANFILES ="
367    for module in $modules; do
368      func_verify_module
369      if test -n "$module"; then
370        func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,$libname"'_a_SOURCES,g' -e "s,lib_OBJECTS,$libname"'_a_OBJECTS,g'
371        if test "$module" = 'alloca'; then
372          echo "$libname"'_a_LIBADD += @ALLOCA@'
373          echo '#'"$libname"'_la_LIBADD += @LTALLOCA@'
374        fi
375      fi
376    done
377   ) > "$testdir/lib/Makefile.am"
378
379   # Create m4/Makefile.am.
380   mkdir -p "$testdir/m4"
381   (echo "## Process this file with automake to produce Makefile.in."
382    echo
383    echo "EXTRA_DIST ="
384    for f in $files; do
385      case "$f" in
386        m4/* )
387          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
388      esac
389    done
390   ) > "$testdir/m4/Makefile.am"
391
392   subdirs="lib m4"
393
394   if test -f "$testdir"/m4/gettext.m4; then
395     # Avoid stupid error message from automake:
396     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
397     mkdir -p "$testdir/po"
398     (echo "## Process this file with automake to produce Makefile.in."
399     ) > "$testdir/po/Makefile.am"
400     subdirs="$subdirs po"
401   fi
402
403   # Create Makefile.am.
404   (echo "## Process this file with automake to produce Makefile.in."
405    echo
406    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
407    echo
408    echo "SUBDIRS = $subdirs"
409    echo
410    echo "ACLOCAL_AMFLAGS = -I m4"
411   ) > "$testdir/Makefile.am"
412
413   # Create configure.ac.
414   (echo "# Process this file with autoconf to produce a configure script."
415    echo "AC_INIT(dummy,0)"
416    echo "AM_INIT_AUTOMAKE"
417    echo
418    echo "AM_CONFIG_HEADER(config.h)"
419    echo
420    echo "AC_PROG_CC"
421    echo "AC_PROG_INSTALL"
422    echo "AC_PROG_MAKE_SET"
423    echo "AC_PROG_RANLIB"
424    echo
425    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
426      echo "AC_GNU_SOURCE"
427      echo
428    fi
429    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
430      echo "gl_USE_SYSTEM_EXTENSIONS"
431      echo
432    fi
433    for module in $modules; do
434      func_verify_module
435      if test -n "$module"; then
436        func_get_autoconf_snippet "$module"
437      fi
438    done
439    echo
440    makefiles="Makefile"
441    for d in $subdirs; do
442      makefiles="$makefiles $d/Makefile"
443    done
444    echo "AC_OUTPUT([$makefiles])"
445   ) > "$testdir/configure.ac"
446
447   # Create autogenerated files.
448   (cd "$testdir"
449    echo "executing ${ACLOCAL} -I m4"
450    ${ACLOCAL} -I m4
451    echo "executing ${AUTOHEADER}"
452    ${AUTOHEADER}
453    echo "executing ${AUTOCONF}"
454    ${AUTOCONF}
455    echo "executing ${AUTOMAKE} --add-missing --copy"
456    ${AUTOMAKE} --add-missing --copy
457   )
458   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
459     (cd "$testdir"
460      ./configure
461        cd lib
462        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
463        if test -n "$built_sources"; then
464          make $built_sources
465        fi
466        cd ..
467      make distclean
468     )
469   fi
470 }
471
472 # func_create_megatestdir megatestdir allmodules
473 func_create_megatestdir ()
474 {
475   megatestdir="$1"
476   allmodules="$2"
477   if test -z "$allmodules"; then
478     allmodules=`func_all_modules`
479   fi
480
481   megasubdirs=
482   # First, all modules one by one.
483   for onemodule in $allmodules; do
484     func_create_testdir "$megatestdir/$onemodule" $onemodule
485     megasubdirs="${megasubdirs}$onemodule "
486   done
487   # Then, all modules all together.
488   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
489   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
490   func_create_testdir "$megatestdir/ALL" "$allmodules"
491   megasubdirs="${megasubdirs}ALL"
492
493   # Create Makefile.am.
494   (echo "## Process this file with automake to produce Makefile.in."
495    echo
496    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
497    echo
498    echo "SUBDIRS = $megasubdirs"
499   ) > "$megatestdir/Makefile.am"
500
501   # Create configure.ac.
502   (echo "# Process this file with autoconf to produce a configure script."
503    echo "AC_INIT(dummy,0)"
504    echo "AM_INIT_AUTOMAKE"
505    echo
506    echo "AC_PROG_MAKE_SET"
507    echo
508    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
509    echo "AC_OUTPUT([Makefile])"
510   ) > "$megatestdir/configure.ac"
511
512   # Create autogenerated files.
513   (cd "$megatestdir"
514    echo "executing ${ACLOCAL}"
515    ${ACLOCAL}
516    echo "executing ${AUTOCONF}"
517    ${AUTOCONF}
518    echo "executing ${AUTOMAKE} --add-missing --copy Makefile"
519    ${AUTOMAKE} --add-missing --copy Makefile
520   )
521 }
522
523 case $mode in
524   "" )
525     func_fatal_error "no mode specified" ;;
526
527   list )
528     func_all_modules
529     ;;
530
531   import )
532     func_fatal_error "NYI" ;;
533
534   create-testdir )
535     if test -z "$destdir"; then
536       func_fatal_error "please specify --dir option"
537     fi
538     mkdir "$destdir"
539     test -d "$destdir" \
540       || func_fatal_error "could not create destination directory"
541     func_create_testdir "$destdir" "$*"
542     ;;
543
544   create-megatestdir )
545     if test -z "$destdir"; then
546       func_fatal_error "please specify --dir option"
547     fi
548     mkdir "$destdir" || func_fatal_error "could not create destination directory"
549     func_create_megatestdir "$destdir" "$*"
550     ;;
551
552   test )
553     test -n "$destdir" || destdir=testdir$$
554     mkdir "$destdir" || func_fatal_error "could not create destination directory"
555     func_create_testdir "$destdir" "$*"
556     cd "$destdir"
557       mkdir build
558       cd build
559         ../configure
560         make
561         make check
562         make distclean
563         remaining=`find . -type f -print`
564         if test -n "$remaining"; then
565           echo "Remaining files:" $remaining 1>&2
566           echo "gnulib-tool: *** Stop." 1>&2
567           exit 1
568         fi
569       cd ..
570     cd ..
571     rm -rf "$destdir"
572     ;;
573
574   megatest )
575     test -n "$destdir" || destdir=testdir$$
576     mkdir "$destdir" || func_fatal_error "could not create destination directory"
577     func_create_megatestdir "$destdir" "$*"
578     cd "$destdir"
579       mkdir build
580       cd build
581         ../configure
582         make
583         make check
584         make distclean
585         remaining=`find . -type f -print`
586         if test -n "$remaining"; then
587           echo "Remaining files:" $remaining 1>&2
588           echo "gnulib-tool: *** Stop." 1>&2
589           exit 1
590         fi
591       cd ..
592     cd ..
593     rm -rf "$destdir"
594     ;;
595
596   extract-description )
597     for module
598     do
599       func_verify_module
600       if test -n "$module"; then
601         func_get_description "$module"
602       fi
603     done
604     ;;
605
606   extract-filelist )
607     for module
608     do
609       func_verify_module
610       if test -n "$module"; then
611         func_get_filelist "$module"
612       fi
613     done
614     ;;
615
616   extract-dependencies )
617     for module
618     do
619       func_verify_module
620       if test -n "$module"; then
621         func_get_dependencies "$module"
622       fi
623     done
624     ;;
625
626   extract-autoconf-snippet )
627     for module
628     do
629       func_verify_module
630       if test -n "$module"; then
631         func_get_autoconf_snippet "$module"
632       fi
633     done
634     ;;
635
636   extract-automake-snippet )
637     for module
638     do
639       func_verify_module
640       if test -n "$module"; then
641         func_get_automake_snippet "$module"
642       fi
643     done
644     ;;
645
646   extract-include-directive )
647     for module
648     do
649       func_verify_module
650       if test -n "$module"; then
651         func_get_include_directive "$module"
652       fi
653     done
654     ;;
655
656   extract-maintainer )
657     for module
658     do
659       func_verify_module
660       if test -n "$module"; then
661         func_get_maintainer "$module"
662       fi
663     done
664     ;;
665
666   * )
667     func_fatal_error "unknown operation mode --$mode" ;;
668 esac
669
670 exit 0