Discussion:
tsearch: Fix compilation error on Android
Bruno Haible
2018-05-13 22:08:27 UTC
Permalink
Android 4.1 to 4.4 has the function tsearch() but not twalk(). Whereas gnulib
assumes that tsearch() and twalk() are implemented at the same time. This leads
to a compilation error:

In file included from test-tsearch.c:22:0:
test-tsearch.c:29:18: error: 'twalk' undeclared here (not in a function)
SIGNATURE_CHECK (twalk, void, (void const *,
^

This patch fixes it:


2018-05-13 Bruno Haible <***@clisp.org>

tsearch: Fix compilation error on Android.
* lib/search.in.h (twalk): Declare when HAVE_TWALK, not HAVE_TSEARCH,
is 0.
(GNULIB_defined_tsearch, GNULIB_defined_twalk): New macros.
* lib/tsearch.c (tsearch, tfind, tdelete): Define only if
GNULIB_defined_tsearch is true.
(twalk): Define only if GNULIB_defined_twalk is true.
* modules/tsearch (configure.ac): Compile tsearch.c also if HAVE_TWALK
is 0.
* m4/tsearch.m4 (gl_FUNC_TSEARCH): Set HAVE_TWALK.
* m4/search_h.m4 (gl_SEARCH_H_DEFAULTS): Initialize HAVE_TWALK.
* modules/search (Makefile.am): Substitute HAVE_TWALK.

diff --git a/lib/search.in.h b/lib/search.in.h
index ce0a0fd..47a4460 100644
--- a/lib/search.in.h
+++ b/lib/search.in.h
@@ -169,7 +169,7 @@ _GL_FUNCDECL_RPL (twalk, void,
_GL_CXXALIAS_RPL (twalk, void,
(const void *vroot, _gl_search_action_fn action));
# else
-# if !@HAVE_TSEARCH@
+# if !@HAVE_TWALK@
_GL_FUNCDECL_SYS (twalk, void,
(const void *vroot, _gl_search_action_fn action)
_GL_ARG_NONNULL ((2)));
@@ -179,6 +179,10 @@ _GL_CXXALIAS_SYS (twalk, void,
# endif
_GL_CXXALIASWARN (twalk);

+/* Flags used by tsearch.c. */
+# define GNULIB_defined_tsearch (@REPLACE_TSEARCH@ || !@HAVE_TSEARCH@)
+# define GNULIB_defined_twalk (@REPLACE_TSEARCH@ || !@HAVE_TWALK@)
+
#elif defined GNULIB_POSIXCHECK
# undef tsearch
# if HAVE_RAW_DECL_TSEARCH
diff --git a/lib/tsearch.c b/lib/tsearch.c
index bce1e9f..dabd760 100644
--- a/lib/tsearch.c
+++ b/lib/tsearch.c
@@ -174,6 +174,8 @@ check_tree (node root)

#endif

+#if GNULIB_defined_tsearch
+
/* Possibly "split" a node with two red successors, and/or fix up two red
edges in a row. ROOTP is a pointer to the lowest node we visited, PARENTP
and GPARENTP pointers to its parent/grandparent. P_R and GP_R contain the
@@ -612,6 +614,10 @@ __tdelete (const void *key, void **vrootp, __compar_fn_t compar)
weak_alias (__tdelete, tdelete)
#endif

+#endif /* GNULIB_defined_tsearch */
+
+
+#if GNULIB_defined_twalk

/* Walk the nodes of a tree.
ROOT is the root of the tree to be walked, ACTION the function to be
@@ -654,6 +660,8 @@ __twalk (const void *vroot, __action_fn_t action)
weak_alias (__twalk, twalk)
#endif

+#endif /* GNULIB_defined_twalk */
+

#ifdef _LIBC

diff --git a/m4/search_h.m4 b/m4/search_h.m4
index d2f7263..66fe464 100644
--- a/m4/search_h.m4
+++ b/m4/search_h.m4
@@ -1,4 +1,4 @@
-# search_h.m4 serial 9
+# search_h.m4 serial 10
dnl Copyright (C) 2007-2018 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -56,5 +56,6 @@ AC_DEFUN([gl_SEARCH_H_DEFAULTS],
GNULIB_TSEARCH=0; AC_SUBST([GNULIB_TSEARCH])
dnl Assume proper GNU behavior unless another module says otherwise.
HAVE_TSEARCH=1; AC_SUBST([HAVE_TSEARCH])
+ HAVE_TWALK=1; AC_SUBST([HAVE_TWALK])
REPLACE_TSEARCH=0; AC_SUBST([REPLACE_TSEARCH])
])
diff --git a/m4/tsearch.m4 b/m4/tsearch.m4
index 87d0d90..82350c9 100644
--- a/m4/tsearch.m4
+++ b/m4/tsearch.m4
@@ -1,4 +1,4 @@
-# tsearch.m4 serial 7
+# tsearch.m4 serial 8
dnl Copyright (C) 2006-2018 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -7,7 +7,7 @@ dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_FUNC_TSEARCH],
[
AC_REQUIRE([gl_SEARCH_H_DEFAULTS])
- AC_CHECK_FUNCS([tsearch])
+ AC_CHECK_FUNCS([tsearch twalk])
if test $ac_cv_func_tsearch = yes; then
dnl On OpenBSD 4.0, the return value of tdelete() is incorrect.
AC_REQUIRE([AC_PROG_CC])
@@ -53,6 +53,9 @@ main ()
else
HAVE_TSEARCH=0
fi
+ if test $ac_cv_func_twalk != yes; then
+ HAVE_TWALK=0
+ fi
])

# Prerequisites of lib/tsearch.c.
diff --git a/modules/search b/modules/search
index 70536ea..72dbe12 100644
--- a/modules/search
+++ b/modules/search
@@ -31,6 +31,7 @@ search.h: search.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H
-e 's|@''HAVE_TYPE_VISIT''@|$(HAVE_TYPE_VISIT)|g' \
-e 's/@''GNULIB_TSEARCH''@/$(GNULIB_TSEARCH)/g' \
-e 's|@''HAVE_TSEARCH''@|$(HAVE_TSEARCH)|g' \
+ -e 's|@''HAVE_TWALK''@|$(HAVE_TWALK)|g' \
-e 's|@''REPLACE_TSEARCH''@|$(REPLACE_TSEARCH)|g' \
-e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
-e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
diff --git a/modules/tsearch b/modules/tsearch
index ffb952b..4a2b5ed 100644
--- a/modules/tsearch
+++ b/modules/tsearch
@@ -10,7 +10,7 @@ search

configure.ac:
gl_FUNC_TSEARCH
-if test $HAVE_TSEARCH = 0 || test $REPLACE_TSEARCH = 1; then
+if test $HAVE_TSEARCH = 0 || test $HAVE_TWALK = 0 || test $REPLACE_TSEARCH = 1; then
AC_LIBOBJ([tsearch])
gl_PREREQ_TSEARCH
fi

Loading...