195c2daaada3faf66aec1cb93c82bba0de1d15d9
[openwrt/staging/dedeckeh.git] /
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
5 shared libgmp
6
7 This disables linking the >400 KB big libgmp and replace it with
8 the builtin mini-gmp which only increases size by ~30KB.
9
10 Signed-off-by: Steven Barth <cyrus@openwrt.org>
11 ---
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(-)
19
20 --- a/configure.ac
21 +++ b/configure.ac
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])
25
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)])], [],
30 + [with_libgmp=yes])
31 +AS_IF([test "x$with_libgmp" != xno], [
32 +AC_CHECK_LIB([gmp],[__gmpz_init], , AC_MSG_ERROR([No suitable version of libgmp found]))
33 +])
34 +AM_CONDITIONAL([BUILD_MINIGMP], [test "x$with_libgmp" == xno])
35 +
36 +
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])
39 +])
40
41 AC_ARG_WITH([cli], [AS_HELP_STRING([--without-cli],
42 [disable interactive CLI (libreadline support)])],
43 @@ -130,4 +140,5 @@ AC_OUTPUT
44 echo "
45 nft configuration:
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
52 @@ -2,7 +2,7 @@
53 #define NFTABLES_EXPRESSION_H
54
55 #include <stdbool.h>
56 -#include <gmp.h>
57 +#include <gmputil.h>
58 #include <linux/netfilter/nf_tables.h>
59
60 #include <nftables.h>
61 --- a/include/gmputil.h
62 +++ b/include/gmputil.h
63 @@ -1,7 +1,17 @@
64 #ifndef NFTABLES_GMPUTIL_H
65 #define NFTABLES_GMPUTIL_H
66
67 +#include <config.h>
68 +
69 +#ifdef HAVE_LIBGMP
70 #include <gmp.h>
71 +#else
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
76 +#endif
77 +
78 #include <asm/byteorder.h>
79
80 enum mpz_word_order {
81 --- a/include/utils.h
82 +++ b/include/utils.h
83 @@ -9,14 +9,14 @@
84 #include <unistd.h>
85 #include <assert.h>
86 #include <list.h>
87 -#include <gmp.h>
88 +#include <gmputil.h>
89
90 #define BITS_PER_BYTE 8
91
92 #ifdef DEBUG
93 #define pr_debug(fmt, arg...) gmp_printf(fmt, ##arg)
94 #else
95 -#define pr_debug(fmt, arg...) ({ if (false) gmp_printf(fmt, ##arg); 0; })
96 +#define pr_debug(fmt, arg...) ({ if (false) {}; 0; })
97 #endif
98
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
103 nft_SOURCES += cli.c
104 endif
105
106 +if BUILD_MINIGMP
107 +nft_SOURCES += mini-gmp.c
108 +endif
109 +
110 nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS}
111 --- a/src/gmputil.c
112 +++ b/src/gmputil.c
113 @@ -14,11 +14,9 @@
114 #include <stdio.h>
115 #include <unistd.h>
116 #include <string.h>
117 -#include <gmp.h>
118
119 #include <nftables.h>
120 #include <datatype.h>
121 -#include <gmputil.h>
122 #include <utils.h>
123
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);
127 }
128
129 +#ifndef HAVE_LIBGMP
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)
133 +{
134 + int n = 0;
135 + while (*f) {
136 + if (*f != '%') {
137 + if (fputc(*f, stdout) != *f)
138 + return -1;
139 +
140 + ++n;
141 + } else {
142 + unsigned long prec = 0;
143 + int base;
144 + size_t len;
145 + char *str;
146 + bool ok;
147 +
148 + if (*++f == '.')
149 + prec = strtoul(++f, (char**)&f, 10);
150 +
151 + if (*f++ != 'Z')
152 + return -1;
153 +
154 + if (*f == 'u')
155 + base = 10;
156 + else if (*f == 'x')
157 + base = 16;
158 + else
159 + return -1;
160 +
161 + len = mpz_sizeinbase(value, base);
162 + while (prec-- > len) {
163 + if (fputc('0', stdout) != '0')
164 + return -1;
165 +
166 + ++n;
167 + }
168 +
169 + str = mpz_get_str(NULL, base, value);
170 + ok = str && fwrite(str, 1, len, stdout) == len;
171 + free(str);
172 +
173 + if (!ok)
174 + return -1;
175 +
176 + n += len;
177 + }
178 + ++f;
179 + }
180 + return n;
181 +}
182 +#endif
183 +
184 static void *gmp_xrealloc(void *ptr, size_t old_size, size_t new_size)
185 {
186 return xrealloc(ptr, new_size);