Browse Source

Added support for detection of 'assert()' and replacement if 'assert()' is not available

Evgeny Grin (Karlson2k) 8 years ago
parent
commit
279a9ad2ab
3 changed files with 99 additions and 3 deletions
  1. 49 2
      configure.ac
  2. 1 1
      src/microhttpd/Makefile.am
  3. 49 0
      src/microhttpd/mhd_assert.h

+ 49 - 2
configure.ac

@@ -1417,8 +1417,6 @@ AC_LINK_IFELSE(
 
 AM_CONDITIONAL([HAVE_FORK_WAITPID], [test "x$mhd_have_fork_waitpid" = "xyes"])
 
-MHD_LIB_LDFLAGS="$MHD_LIB_LDFLAGS -export-dynamic -no-undefined"
-
 # gcov compilation
 AC_MSG_CHECKING(whether to compile with support for code coverage analysis)
 AC_ARG_ENABLE([coverage],
@@ -1432,6 +1430,54 @@ AM_CONDITIONAL([USE_COVERAGE], [test "x$use_gcov" = "xyes"])
 AX_COUNT_CPUS
 AC_SUBST([CPU_COUNT])
 
+AC_MSG_CHECKING([[whether to enable debug asserts]])
+AC_ARG_ENABLE([[asserts]],
+              AS_HELP_STRING([[--enable-asserts]],
+                             [enable test build with debug asserts]),
+              [], [[enable_asserts='no']])
+AS_CASE([[$enable_asserts]], [[yes]], [[:]], [[no]], [[:]], [[enable_asserts='no']])
+AC_MSG_RESULT([[$enable_asserts]])
+
+AS_VAR_IF([[enable_asserts]], [["yes"]],
+  [
+   AC_DEFINE([[_DEBUG]], [[1]], [Define to use debug asserts.])
+   [mhd_assert_test_prg="#include <assert.h>
+   int pos_val(void) {return 5;}
+   int neg_val(void) {return -5;}
+   int main(void)
+   { int pos_var = pos_val(), neg_var = neg_val();
+     assert(neg_var > pos_var); /* Must trigger assert. */
+     (void)pos_var; (void)neg_var;
+     return 0; }
+   "]
+   AC_CACHE_CHECK([[whether system assert() is available]], [mhd_cv_sys_assert_avail],
+     [
+      AC_LINK_IFELSE([AC_LANG_SOURCE([[$mhd_assert_test_prg]])],
+                     [[mhd_cv_sys_assert_avail='yes']],
+                     [[mhd_cv_sys_assert_avail='no']])
+     ]
+   )
+   AS_VAR_IF([[mhd_cv_sys_assert_avail]], [["yes"]],
+     [
+      AC_CACHE_CHECK([[whether system assert() is usable]], [mhd_cv_sys_assert_use],
+        [
+         AC_RUN_IFELSE([AC_LANG_SOURCE([[$mhd_assert_test_prg]])],
+                       [[mhd_cv_sys_assert_use='no']],
+                       [[mhd_cv_sys_assert_use='yes']],
+                       [[mhd_cv_sys_assert_use='assuming yes']])
+        ]
+      )
+      AS_VAR_IF([[mhd_cv_sys_assert_use]], [["no"]], [],
+        [AC_DEFINE([[HAVE_ASSERT]], [[1]], [Define if you have usable assert() and assert.h])])
+     ]
+   )
+   AS_UNSET([mhd_assert_test_prg])
+  ],
+  [AC_DEFINE([[NDEBUG]], [[1]], [Define to disable usage of debug asserts.])]
+)
+
+MHD_LIB_LDFLAGS="$MHD_LIB_LDFLAGS -export-dynamic -no-undefined"
+
 AC_SUBST(MHD_LIB_CPPFLAGS)
 AC_SUBST(MHD_LIB_CFLAGS)
 AC_SUBST(MHD_LIB_LDFLAGS)
@@ -1485,6 +1531,7 @@ AC_MSG_NOTICE([libmicrohttpd ${PACKAGE_VERSION} Configuration Summary:
   Target directory:  ${prefix}
   Shutdown of listening socket
   trigger select:    ${mhd_cv_host_shtdwn_trgr_select}
+  Use debug asserts: ${enable_asserts}
   Messages:          ${enable_messages}
   Basic auth.:       ${enable_bauth}
   Digest auth.:      ${enable_dauth}

+ 1 - 1
src/microhttpd/Makefile.am

@@ -63,7 +63,7 @@ libmicrohttpd_la_SOURCES = \
   sysfdsetsize.c sysfdsetsize.h \
   mhd_str.c mhd_str.h \
   mhd_threads.c mhd_threads.h \
-  mhd_locks.h \
+  mhd_locks.h mhd_assert.h \
   mhd_sockets.c mhd_sockets.h \
   mhd_itc.c mhd_itc.h mhd_itc_types.h \
   mhd_compat.c mhd_compat.h \

+ 49 - 0
src/microhttpd/mhd_assert.h

@@ -0,0 +1,49 @@
+/*
+  This file is part of libmicrohttpd
+  Copyright (C) 2017 Karlson2k (Evgeny Grin)
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library.
+  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file microhttpd/mhd_assert.h
+ * @brief  macros for mhd_assert()
+ * @author Karlson2k (Evgeny Grin)
+ */
+
+#ifndef MHD_ASSERT_H
+#define MHD_ASSERT_H 1
+
+#include "mhd_options.h"
+#ifdef NDEBUG
+#  define mhd_assert(ignore) ((void)0)
+#else  /* _DEBUG */
+#  ifdef HAVE_ASSERT
+#    include <assert.h>
+#    define mhd_assert(CHK) assert(CHK)
+#  else  /* ! HAVE_ASSERT */
+#    include <stdio.h>
+#    include <stdlib.h>
+#    define mhd_assert(CHK) \
+       do { \
+           if (!(CHK)) { \
+             fprintf(stderr, "%s:%u Assertion failed: %s\nProgram aborted.\n", \
+                     __FILE__, (unsigned)__LINE__, #CHK); \
+             fflush(stderr); abort(); } \
+          } while(0)
+#  endif /* ! HAVE_ASSERT */
+#endif /* _DEBUG */
+
+#endif /* ! MHD_ASSERT_H */