Change jm_ to gl_ in AC_DEFINE'd names. Update all uses.
[gnulib.git] / 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-17 18:30:23 $'
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 -h "$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 sed_extract_prog=':[    ]*$/ {
235   :a
236     n
237     s/^Description:[    ]*$//
238     s/^Files:[  ]*$//
239     s/^Depends-on:[     ]*$//
240     s/^configure\.ac:[  ]*$//
241     s/^Makefile\.am:[   ]*$//
242     s/^Include:[        ]*$//
243     s/^Maintainer:[     ]*$//
244     tb
245     p
246     ba
247   :b
248 }'
249
250 # func_get_description module
251 func_get_description ()
252 {
253   sed -n -e "/^Description$sed_extract_prog" < "$gnulib_dir/modules/$1"
254 }
255
256 # func_get_filelist module
257 func_get_filelist ()
258 {
259   sed -n -e "/^Files$sed_extract_prog" < "$gnulib_dir/modules/$1"
260   #echo m4/onceonly.m4
261   echo m4/onceonly_2_57.m4
262 }
263
264 # func_get_dependencies module
265 func_get_dependencies ()
266 {
267   sed -n -e "/^Depends-on$sed_extract_prog" < "$gnulib_dir/modules/$1"
268 }
269
270 # func_get_autoconf_snippet module
271 func_get_autoconf_snippet ()
272 {
273   sed -n -e "/^configure\.ac$sed_extract_prog" < "$gnulib_dir/modules/$1"
274 }
275
276 # func_get_automake_snippet module
277 func_get_automake_snippet ()
278 {
279   sed -n -e "/^Makefile\.am$sed_extract_prog" < "$gnulib_dir/modules/$1"
280 }
281
282 # func_get_include_directive module
283 func_get_include_directive ()
284 {
285   sed -n -e "/^Include$sed_extract_prog" < "$gnulib_dir/modules/$1" | \
286   sed -e 's/^\(["<]\)/#include \1/'
287 }
288
289 # func_get_maintainer module
290 func_get_maintainer ()
291 {
292   sed -n -e "/^Maintainer$sed_extract_prog" < "$gnulib_dir/modules/$1"
293 }
294
295 # func_create_testdir testdir modules
296 func_create_testdir ()
297 {
298   testdir="$1"
299   modules="$2"
300   modules=`for m in $modules; do echo $m; done | sort | uniq`
301
302   # Determine final module list.
303   while true; do
304     xmodules=
305     for module in $modules; do
306       func_verify_module
307       if test -n "$module"; then
308         # Duplicate dependenies are harmless, but Jim wants a warning.
309         duplicated_deps=`func_get_dependencies $module | sort | uniq -d`
310         if test -n "$duplicated_deps"; then
311           echo "warning: module $module has duplicated dependencies: "`echo $duplicated_deps` 1>&2
312         fi
313         xmodules="$xmodules $module "`func_get_dependencies $module`
314       fi
315     done
316     xmodules=`for m in $xmodules; do echo $m; done | sort | uniq`
317     if test "$xmodules" = "$modules"; then
318       break
319     fi
320     modules="$xmodules"
321   done
322   echo "Module list with included dependencies:"
323   echo "$modules" | sed -e 's/^/  /'
324
325   # Determine final file list.
326   files=
327   for module in $modules; do
328     func_verify_module
329     if test -n "$module"; then
330       files="$files "`func_get_filelist $module`
331     fi
332   done
333   files=`for f in $files; do echo $f; done | sort | uniq`
334   echo "File list:"
335   echo "$files" | sed -e 's/^/  /'
336
337   # Create directories.
338   for d in `echo "$files" | sed -n -e 's,^\(.*\)/[^/]*,\1,p'`; do
339     if test "$d" != config; then
340       mkdir -p "$testdir/$d"
341     fi
342   done
343
344   # Copy files.
345   for f in $files; do
346     case "$f" in
347       config/*) g=`echo "$f" | sed -e 's,^config/,,'` ;;
348       *) g="$f" ;;
349     esac
350     ln "$gnulib_dir/$f" "$testdir/$g" 2>/dev/null ||
351     cp -p "$gnulib_dir/$f" "$testdir/$g"
352   done
353
354   # Create lib/Makefile.am.
355   mkdir -p "$testdir/lib"
356   (echo "## Process this file with automake to produce Makefile.in."
357    echo
358    echo "AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies"
359    echo
360    echo "noinst_LIBRARIES = $libname.a"
361    echo
362    echo "$libname"'_a_SOURCES ='
363    echo "$libname"'_a_LIBADD = @LIBOBJS@'
364    echo '#'"$libname"'_la_LIBADD = @LTLIBOBJS@'
365    echo "EXTRA_DIST ="
366    echo "BUILT_SOURCES ="
367    echo "SUFFIXES ="
368    echo "MOSTLYCLEANFILES ="
369    echo "CLEANFILES ="
370    echo "DISTCLEANFILES ="
371    echo "MAINTAINERCLEANFILES ="
372    for module in $modules; do
373      func_verify_module
374      if test -n "$module"; then
375        func_get_automake_snippet "$module" | sed -e "s,lib_SOURCES,$libname"'_a_SOURCES,g' -e "s,lib_OBJECTS,$libname"'_a_OBJECTS,g'
376        if test "$module" = 'alloca'; then
377          echo "$libname"'_a_LIBADD += @ALLOCA@'
378          echo '#'"$libname"'_la_LIBADD += @LTALLOCA@'
379        fi
380      fi
381    done
382   ) > "$testdir/lib/Makefile.am"
383
384   # Create m4/Makefile.am.
385   mkdir -p "$testdir/m4"
386   (echo "## Process this file with automake to produce Makefile.in."
387    echo
388    echo "EXTRA_DIST ="
389    for f in $files; do
390      case "$f" in
391        m4/* )
392          echo "EXTRA_DIST += "`echo "$f" | sed -e 's,^m4/,,'` ;;
393      esac
394    done
395   ) > "$testdir/m4/Makefile.am"
396
397   subdirs="lib m4"
398
399   if test -f "$testdir"/m4/gettext.m4; then
400     # Avoid stupid error message from automake:
401     # "AM_GNU_GETTEXT used but `po' not in SUBDIRS"
402     mkdir -p "$testdir/po"
403     (echo "## Process this file with automake to produce Makefile.in."
404     ) > "$testdir/po/Makefile.am"
405     subdirs="$subdirs po"
406   fi
407
408   # Create Makefile.am.
409   (echo "## Process this file with automake to produce Makefile.in."
410    echo
411    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
412    echo
413    echo "SUBDIRS = $subdirs"
414    echo
415    echo "ACLOCAL_AMFLAGS = -I m4"
416   ) > "$testdir/Makefile.am"
417
418   # Create configure.ac.
419   (echo "# Process this file with autoconf to produce a configure script."
420    echo "AC_INIT(dummy,0)"
421    echo "AM_INIT_AUTOMAKE"
422    echo
423    echo "AM_CONFIG_HEADER(config.h)"
424    echo
425    echo "AC_PROG_CC"
426    echo "AC_PROG_INSTALL"
427    echo "AC_PROG_MAKE_SET"
428    echo "AC_PROG_RANLIB"
429    echo
430    if grep AC_GNU_SOURCE "$testdir"/m4/*.m4 > /dev/null; then
431      echo "AC_GNU_SOURCE"
432      echo
433    fi
434    if grep gl_USE_SYSTEM_EXTENSIONS "$testdir"/m4/*.m4 > /dev/null; then
435      echo "gl_USE_SYSTEM_EXTENSIONS"
436      echo
437    fi
438    for module in $modules; do
439      func_verify_module
440      if test -n "$module"; then
441        func_get_autoconf_snippet "$module"
442      fi
443    done
444    echo
445    makefiles="Makefile"
446    for d in $subdirs; do
447      makefiles="$makefiles $d/Makefile"
448    done
449    echo "AC_OUTPUT([$makefiles])"
450   ) > "$testdir/configure.ac"
451
452   # Create autogenerated files.
453   (cd "$testdir"
454    echo "executing ${ACLOCAL} -I m4"
455    ${ACLOCAL} -I m4
456    echo "executing ${AUTOHEADER}"
457    ${AUTOHEADER}
458    echo "executing ${AUTOCONF}"
459    ${AUTOCONF}
460    echo "executing ${AUTOMAKE} --add-missing --copy"
461    ${AUTOMAKE} --add-missing --copy
462   )
463   if grep '^BUILT_SOURCES *+=' "$testdir/lib/Makefile.am" > /dev/null; then
464     (cd "$testdir"
465      ./configure
466        cd lib
467        built_sources=`grep '^BUILT_SOURCES *=' Makefile.in | sed -e 's/^BUILT_SOURCES *=//'`
468        if test -n "$built_sources"; then
469          make $built_sources
470        fi
471        cd ..
472      make distclean
473     )
474   fi
475 }
476
477 # func_create_megatestdir megatestdir allmodules
478 func_create_megatestdir ()
479 {
480   megatestdir="$1"
481   allmodules="$2"
482   if test -z "$allmodules"; then
483     allmodules=`func_all_modules`
484   fi
485
486   megasubdirs=
487   # First, all modules one by one.
488   for onemodule in $allmodules; do
489     func_create_testdir "$megatestdir/$onemodule" $onemodule
490     megasubdirs="${megasubdirs}$onemodule "
491   done
492   # Then, all modules all together.
493   # Except fnmatch-posix, which conflicts with fnmatch-gnu. FIXME.
494   allmodules=`for m in $allmodules; do if test $m != fnmatch-posix; then echo $m; fi; done`
495   func_create_testdir "$megatestdir/ALL" "$allmodules"
496   megasubdirs="${megasubdirs}ALL"
497
498   # Create Makefile.am.
499   (echo "## Process this file with automake to produce Makefile.in."
500    echo
501    echo "AUTOMAKE_OPTIONS = 1.5 foreign"
502    echo
503    echo "SUBDIRS = $megasubdirs"
504   ) > "$megatestdir/Makefile.am"
505
506   # Create configure.ac.
507   (echo "# Process this file with autoconf to produce a configure script."
508    echo "AC_INIT(dummy,0)"
509    echo "AM_INIT_AUTOMAKE"
510    echo
511    echo "AC_PROG_MAKE_SET"
512    echo
513    echo "AC_CONFIG_SUBDIRS([$megasubdirs])"
514    echo "AC_OUTPUT([Makefile])"
515   ) > "$megatestdir/configure.ac"
516
517   # Create autogenerated files.
518   (cd "$megatestdir"
519    echo "executing ${ACLOCAL}"
520    ${ACLOCAL}
521    echo "executing ${AUTOCONF}"
522    ${AUTOCONF}
523    echo "executing ${AUTOMAKE} --add-missing --copy Makefile"
524    ${AUTOMAKE} --add-missing --copy Makefile
525   )
526 }
527
528 case $mode in
529   "" )
530     func_fatal_error "no mode specified" ;;
531
532   list )
533     func_all_modules
534     ;;
535
536   import )
537     func_fatal_error "NYI" ;;
538
539   create-testdir )
540     if test -z "$destdir"; then
541       func_fatal_error "please specify --dir option"
542     fi
543     mkdir "$destdir"
544     test -d "$destdir" \
545       || func_fatal_error "could not create destination directory"
546     func_create_testdir "$destdir" "$*"
547     ;;
548
549   create-megatestdir )
550     if test -z "$destdir"; then
551       func_fatal_error "please specify --dir option"
552     fi
553     mkdir "$destdir" || func_fatal_error "could not create destination directory"
554     func_create_megatestdir "$destdir" "$*"
555     ;;
556
557   test )
558     test -n "$destdir" || destdir=testdir$$
559     mkdir "$destdir" || func_fatal_error "could not create destination directory"
560     func_create_testdir "$destdir" "$*"
561     cd "$destdir"
562       mkdir build
563       cd build
564         ../configure
565         make
566         make check
567         make distclean
568         remaining=`find . -type f -print`
569         if test -n "$remaining"; then
570           echo "Remaining files:" $remaining 1>&2
571           echo "gnulib-tool: *** Stop." 1>&2
572           exit 1
573         fi
574       cd ..
575     cd ..
576     rm -rf "$destdir"
577     ;;
578
579   megatest )
580     test -n "$destdir" || destdir=testdir$$
581     mkdir "$destdir" || func_fatal_error "could not create destination directory"
582     func_create_megatestdir "$destdir" "$*"
583     cd "$destdir"
584       mkdir build
585       cd build
586         ../configure
587         make
588         make check
589         make distclean
590         remaining=`find . -type f -print`
591         if test -n "$remaining"; then
592           echo "Remaining files:" $remaining 1>&2
593           echo "gnulib-tool: *** Stop." 1>&2
594           exit 1
595         fi
596       cd ..
597     cd ..
598     rm -rf "$destdir"
599     ;;
600
601   extract-description )
602     for module
603     do
604       func_verify_module
605       if test -n "$module"; then
606         func_get_description "$module"
607       fi
608     done
609     ;;
610
611   extract-filelist )
612     for module
613     do
614       func_verify_module
615       if test -n "$module"; then
616         func_get_filelist "$module"
617       fi
618     done
619     ;;
620
621   extract-dependencies )
622     for module
623     do
624       func_verify_module
625       if test -n "$module"; then
626         func_get_dependencies "$module"
627       fi
628     done
629     ;;
630
631   extract-autoconf-snippet )
632     for module
633     do
634       func_verify_module
635       if test -n "$module"; then
636         func_get_autoconf_snippet "$module"
637       fi
638     done
639     ;;
640
641   extract-automake-snippet )
642     for module
643     do
644       func_verify_module
645       if test -n "$module"; then
646         func_get_automake_snippet "$module"
647       fi
648     done
649     ;;
650
651   extract-include-directive )
652     for module
653     do
654       func_verify_module
655       if test -n "$module"; then
656         func_get_include_directive "$module"
657       fi
658     done
659     ;;
660
661   extract-maintainer )
662     for module
663     do
664       func_verify_module
665       if test -n "$module"; then
666         func_get_maintainer "$module"
667       fi
668     done
669     ;;
670
671   * )
672     func_fatal_error "unknown operation mode --$mode" ;;
673 esac
674
675 exit 0