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