From d73f1b630848fb7d90f51938e3c75a42ad947c26 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Mon, 15 Dec 2014 14:26:34 +0100 Subject: [PATCH 5/5] build: add --without-libgmp switch to disable use of shared libgmp This disables linking the >400 KB big libgmp and replace it with the builtin mini-gmp which only increases size by ~30KB. Signed-off-by: Steven Barth --- configure.ac | 17 +++++++++++++--- include/expression.h | 2 +- include/gmputil.h | 10 +++++++++ include/utils.h | 4 ++-- src/Makefile.am | 4 ++++ src/gmputil.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 6 files changed, 86 insertions(+), 8 deletions(-) --- a/configure.ac +++ b/configure.ac @@ -73,8 +73,18 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3]) PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2]) -AC_CHECK_LIB([gmp], [__gmpz_init], , - AC_MSG_ERROR([No suitable version of libgmp found])) +AC_ARG_WITH([libgmp], [AS_HELP_STRING([--without-libgmp], + [Disable libgmp support (use builtin mini-gmp)])], [], + [with_libgmp=yes]) +AS_IF([test "x$with_libgmp" != xno], [ +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found])) +]) +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno]) + + +AS_IF([test "x$with_libgmp" != xyes -a "x$CONFIG_DEBUG" = xy], [ +AC_MSG_ERROR([--without-libgmp MUST be used with --disable-debug]) +]) AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli], [disable interactive CLI (libreadline support)])], @@ -130,4 +140,5 @@ AC_OUTPUT echo " nft configuration: cli support: ${with_cli} - enable debugging: ${with_debug}" + enable debugging: ${with_debug} + use shared libgmp: ${with_libgmp}" --- a/include/expression.h +++ b/include/expression.h @@ -2,7 +2,7 @@ #define NFTABLES_EXPRESSION_H #include -#include +#include #include #include --- a/include/gmputil.h +++ b/include/gmputil.h @@ -1,7 +1,17 @@ #ifndef NFTABLES_GMPUTIL_H #define NFTABLES_GMPUTIL_H +#include + +#ifdef HAVE_LIBGMP #include +#else +#include +/* mini-gmp doesn't come with gmp_printf, so we use our own minimal variant */ +extern int mpz_printf(const char *format, const mpz_t value); +#define gmp_printf mpz_printf +#endif + #include enum mpz_word_order { --- a/include/utils.h +++ b/include/utils.h @@ -9,14 +9,14 @@ #include #include #include -#include +#include #define BITS_PER_BYTE 8 #ifdef DEBUG #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg) #else -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; }) +#define pr_debug(fmt, arg...) ({ if (false) {}; 0; }) #endif #define __fmtstring(x, y) __attribute__((format(printf, x, y))) --- a/src/Makefile.am +++ b/src/Makefile.am @@ -51,4 +51,8 @@ if BUILD_CLI nft_SOURCES += cli.c endif +if BUILD_MINIGMP +nft_SOURCES += mini-gmp.c +endif + nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS} --- a/src/gmputil.c +++ b/src/gmputil.c @@ -14,11 +14,9 @@ #include #include #include -#include #include #include -#include #include void mpz_bitmask(mpz_t rop, unsigned int width) @@ -148,6 +146,61 @@ void mpz_switch_byteorder(mpz_t rop, uns mpz_import_data(rop, data, BYTEORDER_HOST_ENDIAN, len); } +#ifndef HAVE_LIBGMP +/* mini-gmp doesn't have a gmp_printf so we use our own minimal + * variant here which is able to format a single mpz_t */ +int mpz_printf(const char *f, const mpz_t value) +{ + int n = 0; + while (*f) { + if (*f != '%') { + if (fputc(*f, stdout) != *f) + return -1; + + ++n; + } else { + unsigned long prec = 0; + int base; + size_t len; + char *str; + bool ok; + + if (*++f == '.') + prec = strtoul(++f, (char**)&f, 10); + + if (*f++ != 'Z') + return -1; + + if (*f == 'u') + base = 10; + else if (*f == 'x') + base = 16; + else + return -1; + + len = mpz_sizeinbase(value, base); + while (prec-- > len) { + if (fputc('0', stdout) != '0') + return -1; + + ++n; + } + + str = mpz_get_str(NULL, base, value); + ok = str && fwrite(str, 1, len, stdout) == len; + free(str); + + if (!ok) + return -1; + + n += len; + } + ++f; + } + return n; +} +#endif + static void *gmp_xrealloc(void *ptr, size_t old_size, size_t new_size) { return xrealloc(ptr, new_size);