3a882a5bb6811fc8dbd3470271fb008f1c1f653b
[gnulib.git] / m4 / gettext.m4
1 # Macro to add for using GNU gettext.
2 # Ulrich Drepper <drepper@cygnus.com>, 1995.
3 #
4 # This file can be copied and used freely without restrictions.  It can
5 # be used in projects which are not available under the GNU General Public
6 # License or the GNU Library General Public License but which still want
7 # to provide support for the GNU gettext functionality.
8 # Please note that the actual code of the GNU gettext library is covered
9 # by the GNU Library General Public License, and the rest of the GNU
10 # gettext package package is covered by the GNU General Public License.
11 # They are *not* in the public domain.
12
13 # serial 10
14
15 dnl Usage: AM_GNU_GETTEXT([TOOLSYMBOL], [NEEDSYMBOL], [LIBDIR]).
16 dnl If TOOLSYMBOL is specified and is 'use-libtool', then a libtool library
17 dnl    $(top_builddir)/intl/libintl.la will be created (shared and/or static,
18 dnl    depending on --{enable,disable}-{shared,static} and on the presence of
19 dnl    AM-DISABLE-SHARED). Otherwise, a static library
20 dnl    $(top_builddir)/intl/libintl.a will be created.
21 dnl If NEEDSYMBOL is specified and is 'need-ngettext', then GNU gettext
22 dnl    implementations (in libc or libintl) without the ngettext() function
23 dnl    will be ignored.
24 dnl LIBDIR is used to find the intl libraries.  If empty,
25 dnl    the value `$(top_builddir)/intl/' is used.
26 dnl
27 dnl The result of the configuration is one of three cases:
28 dnl 1) GNU gettext, as included in the intl subdirectory, will be compiled
29 dnl    and used.
30 dnl    Catalog format: GNU --> install in $(datadir)
31 dnl    Catalog extension: .mo after installation, .gmo in source tree
32 dnl 2) GNU gettext has been found in the system's C library.
33 dnl    Catalog format: GNU --> install in $(datadir)
34 dnl    Catalog extension: .mo after installation, .gmo in source tree
35 dnl 3) No internationalization, always use English msgid.
36 dnl    Catalog format: none
37 dnl    Catalog extension: none
38 dnl The use of .gmo is historical (it was needed to avoid overwriting the
39 dnl GNU format catalogs when building on a platform with an X/Open gettext),
40 dnl but we keep it in order not to force irrelevant filename changes on the
41 dnl maintainers.
42 dnl
43 AC_DEFUN([AM_GNU_GETTEXT],
44   [AC_REQUIRE([AC_PROG_MAKE_SET])dnl
45    AC_REQUIRE([AC_PROG_CC])dnl
46    AC_REQUIRE([AC_CANONICAL_HOST])dnl
47    AC_REQUIRE([AC_PROG_RANLIB])dnl
48    AC_REQUIRE([AC_ISC_POSIX])dnl
49    AC_REQUIRE([AC_HEADER_STDC])dnl
50    AC_REQUIRE([AC_C_CONST])dnl
51    AC_REQUIRE([AC_C_INLINE])dnl
52    AC_REQUIRE([AC_TYPE_OFF_T])dnl
53    AC_REQUIRE([AC_TYPE_SIZE_T])dnl
54    AC_REQUIRE([AC_FUNC_ALLOCA])dnl
55    AC_REQUIRE([AC_FUNC_MMAP])dnl
56    AC_REQUIRE([jm_GLIBC21])dnl
57
58    AC_CHECK_HEADERS([argz.h limits.h locale.h nl_types.h malloc.h stddef.h \
59 stdlib.h string.h unistd.h sys/param.h])
60    AC_CHECK_FUNCS([feof_unlocked fgets_unlocked getcwd getegid geteuid \
61 getgid getuid mempcpy munmap putenv setenv setlocale stpcpy strchr strcasecmp \
62 strdup strtoul tsearch __argz_count __argz_stringify __argz_next])
63
64    AM_ICONV
65    AM_LANGINFO_CODESET
66    AM_LC_MESSAGES
67    AM_WITH_NLS([$1],[$2],[$3])
68
69    dnl If the AC_CONFIG_AUX_DIR macro for autoconf is used we possibly
70    dnl find the mkinstalldirs script in another subdir but $(top_srcdir).
71    dnl Try to locate is.
72    MKINSTALLDIRS=
73    if test -n "$ac_aux_dir"; then
74      MKINSTALLDIRS="$ac_aux_dir/mkinstalldirs"
75    fi
76    if test -z "$MKINSTALLDIRS"; then
77      MKINSTALLDIRS="\$(top_srcdir)/mkinstalldirs"
78    fi
79    AC_SUBST(MKINSTALLDIRS)
80
81    dnl Enable libtool support if the surrounding package wishes it.
82    INTL_LIBTOOL_SUFFIX_PREFIX=ifelse([$1], use-libtool, [l], [])
83    AC_SUBST(INTL_LIBTOOL_SUFFIX_PREFIX)
84   ])
85
86 dnl Usage: Just like AM_GNU_GETTEXT, which see.
87 AC_DEFUN([AM_WITH_NLS],
88   [AC_MSG_CHECKING([whether NLS is requested])
89     dnl Default is enabled NLS
90     AC_ARG_ENABLE(nls,
91       [  --disable-nls           do not use Native Language Support],
92       USE_NLS=$enableval, USE_NLS=yes)
93     AC_MSG_RESULT($USE_NLS)
94     AC_SUBST(USE_NLS)
95
96     BUILD_INCLUDED_LIBINTL=no
97     USE_INCLUDED_LIBINTL=no
98     INTLLIBS=
99
100     dnl If we use NLS figure out what method
101     if test "$USE_NLS" = "yes"; then
102       AC_DEFINE(ENABLE_NLS, 1,
103         [Define to 1 if translation of program messages to the user's native language
104    is requested.])
105       AC_MSG_CHECKING([whether included gettext is requested])
106       AC_ARG_WITH(included-gettext,
107         [  --with-included-gettext use the GNU gettext library included here],
108         nls_cv_force_use_gnu_gettext=$withval,
109         nls_cv_force_use_gnu_gettext=no)
110       AC_MSG_RESULT($nls_cv_force_use_gnu_gettext)
111
112       gt_use_preinstalled_gnugettext=no
113       nls_cv_use_gnu_gettext="$nls_cv_force_use_gnu_gettext"
114       if test "$nls_cv_force_use_gnu_gettext" != "yes"; then
115         dnl User does not insist on using GNU NLS library.  Figure out what
116         dnl to use.  If GNU gettext is available we use this.  Else we have
117         dnl to fall back to GNU NLS library.
118
119         dnl Add a version number to the cache macros.
120         define(gt_cv_func_gnugettext_libc, [gt_cv_func_gnugettext]ifelse([$2], need-ngettext, 2, 1)[_libc])
121         define(gt_cv_func_gnugettext_libintl, [gt_cv_func_gnugettext]ifelse([$2], need-ngettext, 2, 1)[_libintl])
122
123         AC_CHECK_HEADER(libintl.h,
124           [AC_CACHE_CHECK([for GNU gettext in libc], gt_cv_func_gnugettext_libc,
125             [AC_TRY_LINK([#include <libintl.h>
126 extern int _nl_msg_cat_cntr;],
127                [bindtextdomain ("", "");
128 return (int) gettext ("")]ifelse([$2], need-ngettext, [ + (int) ngettext ("", "", 0)], [])[ + _nl_msg_cat_cntr],
129                gt_cv_func_gnugettext_libc=yes,
130                gt_cv_func_gnugettext_libc=no)])
131
132            if test "$gt_cv_func_gnugettext_libc" != "yes"; then
133              AC_CACHE_CHECK([for GNU gettext in libintl],
134                gt_cv_func_gnugettext_libintl,
135                [gt_save_LIBS="$LIBS"
136                 LIBS="$LIBS -lintl $LIBICONV"
137                 AC_TRY_LINK([#include <libintl.h>
138 extern int _nl_msg_cat_cntr;],
139                   [bindtextdomain ("", "");
140 return (int) gettext ("")]ifelse([$2], need-ngettext, [ + (int) ngettext ("", "", 0)], [])[ + _nl_msg_cat_cntr],
141                   gt_cv_func_gnugettext_libintl=yes,
142                   gt_cv_func_gnugettext_libintl=no)
143                 LIBS="$gt_save_LIBS"])
144            fi
145
146            dnl If an already present or preinstalled GNU gettext() is found,
147            dnl use it.  But if this macro is used in GNU gettext, and GNU
148            dnl gettext is already preinstalled in libintl, we update this
149            dnl libintl.  (Cf. the install rule in intl/Makefile.in.)
150            if test "$gt_cv_func_gnugettext_libc" = "yes" \
151               || { test "$gt_cv_func_gnugettext_libintl" = "yes" \
152                    && test "$PACKAGE" != gettext; }; then
153              gt_use_preinstalled_gnugettext=yes
154            fi
155         ])
156
157         if test "$gt_use_preinstalled_gnugettext" != "yes"; then
158           dnl GNU gettext is not found in the C library.
159           dnl Fall back on included GNU gettext library.
160           nls_cv_use_gnu_gettext=yes
161         fi
162       fi
163
164       if test "$gt_use_preinstalled_gnugettext" = "yes"; then
165         if test "$gt_cv_func_gnugettext_libintl" = "yes"; then
166           dnl If iconv() is in a separate libiconv library, then anyone
167           dnl linking with libintl{.a,.so} also needs to link with libiconv.
168           INTLLIBS="-lintl $LIBICONV"
169         fi
170
171         dnl For backward compatibility. Some packages may be using this.
172         AC_DEFINE(HAVE_GETTEXT, 1,
173           [Define if the GNU gettext() function is already present or preinstalled.])
174
175         gt_save_LIBS="$LIBS"
176         LIBS="$LIBS $INTLLIBS"
177         AC_CHECK_FUNCS(dcgettext)
178         LIBS="$gt_save_LIBS"
179
180       fi
181
182       if test "$nls_cv_use_gnu_gettext" = "yes"; then
183         dnl Mark actions used to generate GNU NLS library.
184         INTLOBJS="\$(GETTOBJS)"
185         BUILD_INCLUDED_LIBINTL=yes
186         USE_INCLUDED_LIBINTL=yes
187         INTLLIBS="ifelse([$3],[],\$(top_builddir)/intl,[$3])/libintl.ifelse([$1], use-libtool, [l], [])a $LIBICONV"
188         LIBS=`echo " $LIBS " | sed -e 's/ -lintl / /' -e 's/^ //' -e 's/ $//'`
189       fi
190
191       if test "$gt_use_preinstalled_gnugettext" = "yes" \
192          || test "$nls_cv_use_gnu_gettext" = "yes"; then
193         dnl Mark actions to use GNU gettext tools.
194         CATOBJEXT=.gmo
195
196         dnl Search for GNU msgfmt in the PATH.
197         AM_PATH_PROG_WITH_TEST(MSGFMT, msgfmt,
198           [$ac_dir/$ac_word --statistics /dev/null >/dev/null 2>&1], :)
199         AC_PATH_PROG(GMSGFMT, gmsgfmt, $MSGFMT)
200
201         dnl Search for GNU xgettext in the PATH.
202         AM_PATH_PROG_WITH_TEST(XGETTEXT, xgettext,
203           [$ac_dir/$ac_word --omit-header /dev/null >/dev/null 2>&1], :)
204
205         dnl This could go away some day; the PATH_PROG_WITH_TEST already does it.
206         dnl Test whether we really found GNU msgfmt.
207         if test "$GMSGFMT" != ":"; then
208           dnl If it is no GNU msgfmt we define it as : so that the
209           dnl Makefiles still can work.
210           if $GMSGFMT --statistics /dev/null >/dev/null 2>&1; then
211             : ;
212           else
213             AC_MSG_RESULT(
214               [found msgfmt program is not GNU msgfmt; ignore it])
215             GMSGFMT=":"
216           fi
217         fi
218
219         dnl This could go away some day; the PATH_PROG_WITH_TEST already does it.
220         dnl Test whether we really found GNU xgettext.
221         if test "$XGETTEXT" != ":"; then
222           dnl If it is no GNU xgettext we define it as : so that the
223           dnl Makefiles still can work.
224           if $XGETTEXT --omit-header /dev/null >/dev/null 2>&1; then
225             : ;
226           else
227             AC_MSG_RESULT(
228               [found xgettext program is not GNU xgettext; ignore it])
229             XGETTEXT=":"
230           fi
231         fi
232       fi
233
234       dnl We need to process the po/ directory.
235       POSUB=po
236     fi
237     AC_OUTPUT_COMMANDS(
238      [for ac_file in $CONFIG_FILES; do
239         # Support "outfile[:infile[:infile...]]"
240         case "$ac_file" in
241           *:*) ac_file=`echo "$ac_file"|sed 's%:.*%%'` ;;
242         esac
243         # PO directories have a Makefile.in generated from Makefile.in.in.
244         case "$ac_file" in */Makefile.in)
245           # Adjust a relative srcdir.
246           ac_dir=`echo "$ac_file"|sed 's%/[^/][^/]*$%%'`
247           ac_dir_suffix="/`echo "$ac_dir"|sed 's%^\./%%'`"
248           ac_dots=`echo "$ac_dir_suffix"|sed 's%/[^/]*%../%g'`
249           # In autoconf-2.13 it is called $ac_given_srcdir.
250           # In autoconf-2.50 it is called $srcdir.
251           test -n "$ac_given_srcdir" || ac_given_srcdir="$srcdir"
252           case "$ac_given_srcdir" in
253             .)  top_srcdir=`echo $ac_dots|sed 's%/$%%'` ;;
254             /*) top_srcdir="$ac_given_srcdir" ;;
255             *)  top_srcdir="$ac_dots$ac_given_srcdir" ;;
256           esac
257           if test -f "$ac_given_srcdir/$ac_dir/POTFILES.in"; then
258             rm -f "$ac_dir/POTFILES"
259             test -n "$as_me" && echo "$as_me: creating $ac_dir/POTFILES" || echo "creating $ac_dir/POTFILES"
260             sed -e "/^#/d" -e "/^[      ]*\$/d" -e "s,.*,     $top_srcdir/& \\\\," -e "\$s/\(.*\) \\\\/\1/" < "$ac_given_srcdir/$ac_dir/POTFILES.in" > "$ac_dir/POTFILES"
261             # ALL_LINGUAS, GMOFILES, POFILES depend on $ac_dir but don't
262             # depend on user-specified configuration parameters.
263             if test -f "$ac_given_srcdir/$ac_dir/LINGUAS"; then
264               # The LINGUAS file contains the set of available languages.
265               if test -n "$ALL_LINGUAS"; then
266                 test -n "$as_me" && echo "$as_me: setting ALL_LINGUAS in configure.in is obsolete" || echo "setting ALL_LINGUAS in configure.in is obsolete"
267               fi
268               ALL_LINGUAS_=`sed -e "/^#/d" "$ac_given_srcdir/$ac_dir/LINGUAS"`
269               # Hide the ALL_LINGUAS assigment from automake.
270               eval 'ALL_LINGUAS''=$ALL_LINGUAS_'
271             fi
272             GMOFILES=
273             POFILES=
274             for lang in $ALL_LINGUAS; do
275               GMOFILES="$GMOFILES $lang.gmo"
276               POFILES="$POFILES $lang.po"
277             done
278             # CATALOGS depends on both $ac_dir and the user's LINGUAS
279             # environment variable.
280             INST_LINGUAS=
281             if test -n "$ALL_LINGUAS"; then
282               for presentlang in $ALL_LINGUAS; do
283                 useit=no
284                 for desiredlang in ${LINGUAS-$ALL_LINGUAS}; do
285                   # Use the presentlang catalog if desiredlang is
286                   #   a. equal to presentlang, or
287                   #   b. a variant of presentlang (because in this case,
288                   #      presentlang can be used as a fallback for messages
289                   #      which are not translated in the desiredlang catalog).
290                   case "$desiredlang" in
291                     "$presentlang"*) useit=yes;;
292                   esac
293                 done
294                 if test $useit = yes; then
295                   INST_LINGUAS="$INST_LINGUAS $presentlang"
296                 fi
297               done
298             fi
299             CATALOGS=
300             if test -n "$INST_LINGUAS"; then
301               for lang in $INST_LINGUAS; do
302                 CATALOGS="$CATALOGS $lang.gmo"
303               done
304             fi
305             test -n "$as_me" && echo "$as_me: creating $ac_dir/Makefile" || echo "creating $ac_dir/Makefile"
306             sed -e "/POTFILES =/r $ac_dir/POTFILES" -e "s|@GMOFILES@|$GMOFILES|g" -e "s|@POFILES@|$POFILES|g" -e "s|@CATALOGS@|$CATALOGS|g" "$ac_dir/Makefile.in" > "$ac_dir/Makefile"
307           fi
308           ;;
309         esac
310       done],
311      [# Capture the value of obsolete $ALL_LINGUAS because we need it to
312       # compute GMOFILES, POFILES, CATALOGS. But hide it from automake.
313       eval 'ALL_LINGUAS''="$ALL_LINGUAS"'
314       # Capture the value of $LINGUAS because we need it to compute CATALOGS.
315       LINGUAS="$LINGUAS"
316      ])
317
318
319     dnl If this is used in GNU gettext we have to set BUILD_INCLUDED_LIBINTL
320     dnl to 'yes' because some of the testsuite requires it.
321     if test "$PACKAGE" = gettext; then
322       BUILD_INCLUDED_LIBINTL=yes
323     fi
324
325     dnl intl/plural.c is generated from intl/plural.y. It requires bison,
326     dnl because plural.y uses bison specific features. It requires at least
327     dnl bison-1.26 because earlier versions generate a plural.c that doesn't
328     dnl compile.
329     dnl bison is only needed for the maintainer (who touches plural.y). But in
330     dnl order to avoid separate Makefiles or --enable-maintainer-mode, we put
331     dnl the rule in general Makefile. Now, some people carelessly touch the
332     dnl files or have a broken "make" program, hence the plural.c rule will
333     dnl sometimes fire. To avoid an error, defines BISON to ":" if it is not
334     dnl present or too old.
335     AC_CHECK_PROGS([INTLBISON], [bison])
336     if test -z "$INTLBISON"; then
337       ac_verc_fail=yes
338     else
339       dnl Found it, now check the version.
340       AC_MSG_CHECKING([version of bison])
341 changequote(<<,>>)dnl
342       ac_prog_version=`$INTLBISON --version 2>&1 | sed -n 's/^.*GNU Bison .* \([0-9]*\.[0-9.]*\).*$/\1/p'`
343       case $ac_prog_version in
344         '') ac_prog_version="v. ?.??, bad"; ac_verc_fail=yes;;
345         1.2[6-9]* | 1.[3-9][0-9]* | [2-9].*)
346 changequote([,])dnl
347            ac_prog_version="$ac_prog_version, ok"; ac_verc_fail=no;;
348         *) ac_prog_version="$ac_prog_version, bad"; ac_verc_fail=yes;;
349       esac
350       AC_MSG_RESULT([$ac_prog_version])
351     fi
352     if test $ac_verc_fail = yes; then
353       INTLBISON=:
354     fi
355
356     dnl Make all variables we use known to autoconf.
357     AC_SUBST(BUILD_INCLUDED_LIBINTL)
358     AC_SUBST(USE_INCLUDED_LIBINTL)
359     AC_SUBST(CATOBJEXT)
360     AC_SUBST(INTLLIBS)
361     AC_SUBST(INTLOBJS)
362     AC_SUBST(POSUB)
363
364     dnl For backward compatibility. Some configure.ins may be using this.
365     nls_cv_header_intl=
366     nls_cv_header_libgt=
367
368     dnl For backward compatibility. Some Makefiles may be using this.
369     DATADIRNAME=share
370     AC_SUBST(DATADIRNAME)
371
372     dnl For backward compatibility. Some Makefiles may be using this.
373     INSTOBJEXT=.mo
374     AC_SUBST(INSTOBJEXT)
375
376     dnl For backward compatibility. Some Makefiles may be using this.
377     GENCAT=gencat
378     AC_SUBST(GENCAT)
379   ])