1 From d73f1b630848fb7d90f51938e3c75a42ad947c26 Mon Sep 17 00:00:00 2001
2 From: Steven Barth <cyrus@openwrt.org>
3 Date: Mon, 15 Dec 2014 14:26:34 +0100
4 Subject: [PATCH 5/5] build: add --without-libgmp switch to disable use of
7 This disables linking the >400 KB big libgmp and replace it with
8 the builtin mini-gmp which only increases size by ~30KB.
10 Signed-off-by: Steven Barth <cyrus@openwrt.org>
12 configure.ac | 17 +++++++++++++---
13 include/expression.h | 2 +-
14 include/gmputil.h | 10 +++++++++
15 include/utils.h | 4 ++--
16 src/Makefile.am | 4 ++++
17 src/gmputil.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++--
18 6 files changed, 86 insertions(+), 8 deletions(-)
22 @@ -73,8 +73,18 @@ AM_CONDITIONAL([BUILD_PDF], [test "$DBLA
23 PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3])
24 PKG_CHECK_MODULES([LIBNFTNL], [libnftnl >= 1.0.2])
26 -AC_CHECK_LIB([gmp], [__gmpz_init], ,
27 - AC_MSG_ERROR([No suitable version of libgmp found]))
28 +AC_ARG_WITH([libgmp], [AS_HELP_STRING([--without-libgmp],
29 + [Disable libgmp support (use builtin mini-gmp)])], [],
31 +AS_IF([test "x$with_libgmp" != xno], [
32 +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found]))
34 +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno])
37 +AS_IF([test "x$with_libgmp" != xyes -a "x$CONFIG_DEBUG" = xy], [
38 +AC_MSG_ERROR([--without-libgmp MUST be used with --disable-debug])
41 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
42 [disable interactive CLI (libreadline support)])],
43 @@ -130,4 +140,5 @@ AC_OUTPUT
46 cli support: ${with_cli}
47 - enable debugging: ${with_debug}"
48 + enable debugging: ${with_debug}
49 + use shared libgmp: ${with_libgmp}"
50 --- a/include/expression.h
51 +++ b/include/expression.h
53 #define NFTABLES_EXPRESSION_H
58 #include <linux/netfilter/nf_tables.h>
61 --- a/include/gmputil.h
62 +++ b/include/gmputil.h
64 #ifndef NFTABLES_GMPUTIL_H
65 #define NFTABLES_GMPUTIL_H
72 +#include <mini-gmp.h>
73 +/* mini-gmp doesn't come with gmp_printf, so we use our own minimal variant */
74 +extern int mpz_printf(const char *format, const mpz_t value);
75 +#define gmp_printf mpz_printf
78 #include <asm/byteorder.h>
90 #define BITS_PER_BYTE 8
93 #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)
95 -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; })
96 +#define pr_debug(fmt, arg...) ({ if (false) {}; 0; })
99 #define __fmtstring(x, y) __attribute__((format(printf, x, y)))
100 --- a/src/Makefile.am
101 +++ b/src/Makefile.am
102 @@ -51,4 +51,8 @@ if BUILD_CLI
107 +nft_SOURCES += mini-gmp.c
110 nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS}
119 #include <nftables.h>
120 #include <datatype.h>
121 -#include <gmputil.h>
124 void mpz_bitmask(mpz_t rop, unsigned int width)
125 @@ -148,6 +146,61 @@ void mpz_switch_byteorder(mpz_t rop, uns
126 mpz_import_data(rop, data, BYTEORDER_HOST_ENDIAN, len);
130 +/* mini-gmp doesn't have a gmp_printf so we use our own minimal
131 + * variant here which is able to format a single mpz_t */
132 +int mpz_printf(const char *f, const mpz_t value)
137 + if (fputc(*f, stdout) != *f)
142 + unsigned long prec = 0;
149 + prec = strtoul(++f, (char**)&f, 10);
156 + else if (*f == 'x')
161 + len = mpz_sizeinbase(value, base);
162 + while (prec-- > len) {
163 + if (fputc('0', stdout) != '0')
169 + str = mpz_get_str(NULL, base, value);
170 + ok = str && fwrite(str, 1, len, stdout) == len;
184 static void *gmp_xrealloc(void *ptr, size_t old_size, size_t new_size)
186 return xrealloc(ptr, new_size);