Discussion:
conditionally include gnulib regex
(too old to reply)
Andrew L. Moore
2017-10-17 11:11:07 UTC
Permalink
Raw Message
Hi,
First of all, thank you for providing GNU gnulib,

I’m using the steps below to conditionally include gnulib regex
if either requested (i.e., by `configure —with-included-regex’)
or a system regex isn't found. The strategy is to hide
lib/regex.h until configure requests it. If there’s a better
way, I’d be interested to know and apologize if this
has been covered previously.

In configure.ac:

...
AC_MSG_CHECKING([whether included regex is requested])
AC_ARG_WITH([included-regex],
[AS_HELP_STRING([--with-included-regex],
[use GNU gnulib regex library included here])],[],
[with_included_regex=check])
AC_MSG_RESULT([$with_included_regex])
AS_IF([test ."$with_included_regex" != .'yes'],
[AC_CHECK_FUNCS([regcomp],[with_included_regex=no],
[AC_MSG_WARN([System regex not found, falling back to included version])
with_included_regex=yes])])
AM_CONDITIONAL([LIBADD_REGEX], [test ."$with_included_regex" = .'yes'])
AM_COND_IF([LIBADD_REGEX],
[AC_DEFINE([HAVE_REG_SYNTAX_T], [1],
[Define to 1 if regex.h defines `reg_syntax_t'.])],
[AC_CHECK_TYPES([reg_syntax_t],
[AC_DEFINE([HAVE_REG_SYNTAX_T], [1],
[Define to 1 if regex.h defines `reg_syntax_t'.])],[],
[[#include <sys/types.h>
#include <regex.h>]])])

gl_INIT
AM_CONDITIONAL([LDADD_LIBGNU],
[test ."$with_included_regex" = .'yes' || \
test ."$ac_cv_func_getopt_long_only" = .’yes’ || \
...])


Then lib/regex.h is renamed to lib/regex.h.in, and in lib/Makefile.am:

...
## begin gnulib module regex

if LIBADD_REGEX
BUILT_SOURCES += regex.h
DISTCLEANFILES += regex.h
AM_CPPFLAGS += -DRE_ENABLE_I18N=1 -D__USE_GNU=1
regex.h: regex.h.in
$(LN_S) $(top_srcdir)/lib/$@.in $@

endif LIBADD_REGEX

EXTRA_DIST = … regex.h.in …


Finally in src/Makefile.am:

...
if LDADD_LIBGNU
my_prog_LDADD += ../lib/libgnu.a
endif LDADD_LIBGNU

if LIBADD_REGEX
AM_CPPFLAGS += -DRE_ENABLE_I18N=1 -D__USE_GNU=1
endif


This appears to work well on GNU/Linux, BSD and Darwin,
with the caveat that before switching to an alternative regex,
it’s necessary to run `make distclean’.
-AM
Bruno Haible
2017-10-17 18:59:06 UTC
Permalink
Raw Message
Hi,
Post by Andrew L. Moore
The strategy is to hide
lib/regex.h until configure requests it. If there’s a better
way, I’d be interested to know and apologize if this
has been covered previously.
Technically, the way you use regex here is OK.

Nevertheless, here is why few package have an option like this:
The user of 'configure', i.e. the person who builds the package, usually
for a distro, when seeing this

$ ./configure --help
...
--with-included-regex use GNU gnulib regex library included here
...

will ask themselves: Should I use this option or not? What is the benefit
(added feature?) if I use it?

Many GNU packages therefore offer to the user of 'configure' a set of
features, with a clear meaning, and the configure script then makes decisions
(in this case, whether to include the system's <regex.h>). For example:

$ ./configure --help
...
--enable-posix enable strict POSIX compliance
--enable-gnu enable GNU extensions
--enable-embedded produce binaries of minimal code size
...

Then, you can implement a logic such as:
included_regex = (enable_posix && !system_has_posix_regex)
|| (enable_gnu && !system_has_gnu_regex)
|| (!enable_embedded && system_regex_is_buggy)

This is just a hypothetical example.
Post by Andrew L. Moore
AM_CPPFLAGS += -DRE_ENABLE_I18N=1 -D__USE_GNU=1
You shouldn't set __USE_GNU. That's a glibc-internal macro. If you want
GNU extensions to be defined in glibc, define _GNU_SOURCE to 1 before
the first include file. If you want platform specific functions to be
declared on all platforms, use the gnulib module 'extensions'.

Bruno

Loading...