summaryrefslogtreecommitdiffstats
path: root/configure
blob: 928048b3d8c0a8f63645a6eefbff6b2216060b4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# This is not an autoconf generated configure

INCLUDE="$PWD/include"
PREFIX="/usr"
LIBDIR="\${prefix}/lib"
COLOR="never"

# Output file which is input to Makefile
CONFIG=config.mk

# Make a temp directory in build tree.
TMPDIR=$(mktemp -d config.XXXXXX)
trap 'status=$?; rm -rf $TMPDIR; exit $status' EXIT HUP INT QUIT TERM

check_toolchain()
{
    : ${PKG_CONFIG:=pkg-config}
    : ${AR=ar}
    : ${CC=gcc}
    : ${YACC=bison}
    echo "PKG_CONFIG:=${PKG_CONFIG}" >>$CONFIG
    echo "AR:=${AR}" >>$CONFIG
    echo "CC:=${CC}" >>$CONFIG
    echo "YACC:=${YACC}" >>$CONFIG
}

check_xtables()
{
	if ! ${PKG_CONFIG} xtables --exists; then
		echo "TC_CONFIG_NO_XT:=y" >>$CONFIG
	fi
}

check_xt()
{
    #check if we have xtables from iptables >= 1.4.5.
    cat >$TMPDIR/ipttest.c <<EOF
#include <xtables.h>
#include <linux/netfilter.h>
static struct xtables_globals test_globals = {
	.option_offset = 0,
	.program_name = "tc-ipt",
	.program_version = XTABLES_VERSION,
	.orig_opts = NULL,
	.opts = NULL,
	.exit_err = NULL,
};

int main(int argc, char **argv)
{
	xtables_init_all(&test_globals, NFPROTO_IPV4);
	return 0;
}
EOF

    if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL \
	$(${PKG_CONFIG} xtables --cflags --libs) -ldl >/dev/null 2>&1; then
	echo "TC_CONFIG_XT:=y" >>$CONFIG
	echo "using xtables"
    fi
    rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
}

check_xt_old()
{
    # bail if previous XT checks has already succeeded.
    grep -q TC_CONFIG_XT $CONFIG && return

    #check if we don't need our internal header ..
    cat >$TMPDIR/ipttest.c <<EOF
#include <xtables.h>
char *lib_dir;
unsigned int global_option_offset = 0;
const char *program_version = XTABLES_VERSION;
const char *program_name = "tc-ipt";
struct afinfo afinfo = {
	.libprefix      = "libxt_",
};

void exit_error(enum exittype status, const char *msg, ...)
{
}

int main(int argc, char **argv) {

	return 0;
}

EOF

    if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1; then
	echo "TC_CONFIG_XT_OLD:=y" >>$CONFIG
	echo "using old xtables (no need for xt-internal.h)"
    fi
    rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
}

check_xt_old_internal_h()
{
    # bail if previous XT checks has already succeeded.
    grep -q TC_CONFIG_XT $CONFIG && return

    #check if we need our own internal.h
    cat >$TMPDIR/ipttest.c <<EOF
#include <xtables.h>
#include "xt-internal.h"
char *lib_dir;
unsigned int global_option_offset = 0;
const char *program_version = XTABLES_VERSION;
const char *program_name = "tc-ipt";
struct afinfo afinfo = {
	.libprefix      = "libxt_",
};

void exit_error(enum exittype status, const char *msg, ...)
{
}

int main(int argc, char **argv) {

	return 0;
}

EOF
	if $CC -I$INCLUDE $IPTC -o $TMPDIR/ipttest $TMPDIR/ipttest.c $IPTL -ldl >/dev/null 2>&1; then
	    echo "using old xtables with xt-internal.h"
	    echo "TC_CONFIG_XT_OLD_H:=y" >>$CONFIG
	fi
	rm -f $TMPDIR/ipttest.c $TMPDIR/ipttest
}

check_lib_dir()
{
	LIBDIR=$(echo $LIBDIR | sed "s|\${prefix}|$PREFIX|")

	echo -n "lib directory: "
	echo "$LIBDIR"
	echo "LIBDIR:=$LIBDIR" >> $CONFIG
}

check_setns()
{
    cat >$TMPDIR/setnstest.c <<EOF
#define _GNU_SOURCE
#include <sched.h>
int main(int argc, char **argv)
{
	(void)setns(0,0);
	return 0;
}
EOF
    if $CC -I$INCLUDE -o $TMPDIR/setnstest $TMPDIR/setnstest.c >/dev/null 2>&1; then
	echo "IP_CONFIG_SETNS:=y" >>$CONFIG
	echo "yes"
	echo "CFLAGS += -DHAVE_SETNS" >>$CONFIG
    else
	echo "no"
    fi
    rm -f $TMPDIR/setnstest.c $TMPDIR/setnstest
}

check_name_to_handle_at()
{
    cat >$TMPDIR/name_to_handle_at_test.c <<EOF
#define _GNU_SOURCE
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
	struct file_handle *fhp;
	int mount_id, flags, dirfd;
	char *pathname;
	name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags);
	return 0;
}
EOF
    if $CC -I$INCLUDE -o $TMPDIR/name_to_handle_at_test $TMPDIR/name_to_handle_at_test.c >/dev/null 2>&1; then
        echo "yes"
        echo "CFLAGS += -DHAVE_HANDLE_AT" >>$CONFIG
    else
        echo "no"
    fi
    rm -f $TMPDIR/name_to_handle_at_test.c $TMPDIR/name_to_handle_at_test
}

check_ipset()
{
    cat >$TMPDIR/ipsettest.c <<EOF
#include <linux/netfilter/ipset/ip_set.h>
#ifndef IP_SET_INVALID
#define IPSET_DIM_MAX 3
typedef unsigned short ip_set_id_t;
#endif
#include <linux/netfilter/xt_set.h>

struct xt_set_info info;
#if IPSET_PROTOCOL == 6 || IPSET_PROTOCOL == 7
int main(void)
{
	return IPSET_MAXNAMELEN;
}
#else
#error unknown ipset version
#endif
EOF

    if $CC -I$INCLUDE -o $TMPDIR/ipsettest $TMPDIR/ipsettest.c >/dev/null 2>&1; then
	echo "TC_CONFIG_IPSET:=y" >>$CONFIG
	echo "yes"
    else
	echo "no"
    fi
    rm -f $TMPDIR/ipsettest.c $TMPDIR/ipsettest
}

check_elf()
{
    if ${PKG_CONFIG} libelf --exists; then
	echo "HAVE_ELF:=y" >>$CONFIG
	echo "yes"

	echo 'CFLAGS += -DHAVE_ELF' "$(${PKG_CONFIG} libelf --cflags)" >> $CONFIG
	echo 'LDLIBS += ' "$(${PKG_CONFIG} libelf --libs)" >>$CONFIG
    else
	echo "no"
    fi
}

have_libbpf_basic()
{
    cat >$TMPDIR/libbpf_test.c <<EOF
#include <bpf/libbpf.h>
int main(int argc, char **argv) {
    bpf_program__set_autoload(NULL, false);
    bpf_map__ifindex(NULL);
    bpf_map__set_pin_path(NULL, NULL);
    bpf_object__open_file(NULL, NULL);
    return 0;
}
EOF

    $CC -o $TMPDIR/libbpf_test $TMPDIR/libbpf_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1
    local ret=$?

    rm -f $TMPDIR/libbpf_test.c $TMPDIR/libbpf_test
    return $ret
}

have_libbpf_sec_name()
{
    cat >$TMPDIR/libbpf_sec_test.c <<EOF
#include <bpf/libbpf.h>
int main(int argc, char **argv) {
    void *ptr;
    bpf_program__section_name(NULL);
    return 0;
}
EOF

    $CC -o $TMPDIR/libbpf_sec_test $TMPDIR/libbpf_sec_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1
    local ret=$?

    rm -f $TMPDIR/libbpf_sec_test.c $TMPDIR/libbpf_sec_test
    return $ret
}

check_force_libbpf_on()
{
    # if set LIBBPF_FORCE=on but no libbpf support, just exist the config
    # process to make sure we don't build without libbpf.
    if [ "$LIBBPF_FORCE" = on ]; then
        echo "	LIBBPF_FORCE=on set, but couldn't find a usable libbpf"
        exit 1
    fi
}

check_libbpf()
{
    # if set LIBBPF_FORCE=off, disable libbpf entirely
    if [ "$LIBBPF_FORCE" = off ]; then
        echo "no"
        return
    fi

    if ! ${PKG_CONFIG} libbpf --exists && [ -z "$LIBBPF_DIR" ] ; then
        echo "no"
        check_force_libbpf_on
        return
    fi

    if [ $(uname -m) = x86_64 ]; then
        local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib64"
    else
        local LIBBPF_LIBDIR="${LIBBPF_DIR}/usr/lib"
    fi

    if [ -n "$LIBBPF_DIR" ]; then
        LIBBPF_CFLAGS="-I${LIBBPF_DIR}/usr/include"
        LIBBPF_LDLIBS="${LIBBPF_LIBDIR}/libbpf.a -lz -lelf"
        LIBBPF_VERSION=$(PKG_CONFIG_LIBDIR=${LIBBPF_LIBDIR}/pkgconfig ${PKG_CONFIG} libbpf --modversion)
    else
        LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags)
        LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs)
        LIBBPF_VERSION=$(${PKG_CONFIG} libbpf --modversion)
    fi

    if ! have_libbpf_basic; then
        echo "no"
        echo "	libbpf version $LIBBPF_VERSION is too low, please update it to at least 0.1.0"
        check_force_libbpf_on
        return
    else
        echo "HAVE_LIBBPF:=y" >> $CONFIG
        echo 'CFLAGS += -DHAVE_LIBBPF ' $LIBBPF_CFLAGS >> $CONFIG
        echo "CFLAGS += -DLIBBPF_VERSION=\\\"$LIBBPF_VERSION\\\"" >> $CONFIG
        echo 'LDLIBS += ' $LIBBPF_LDLIBS >> $CONFIG

        if [ -z "$LIBBPF_DIR" ]; then
            echo "CFLAGS += -DLIBBPF_DYNAMIC" >> $CONFIG
        fi
    fi

    # bpf_program__title() is deprecated since libbpf 0.2.0, use
    # bpf_program__section_name() instead if we support
    if have_libbpf_sec_name; then
        echo "HAVE_LIBBPF_SECTION_NAME:=y" >> $CONFIG
        echo 'CFLAGS += -DHAVE_LIBBPF_SECTION_NAME ' >> $CONFIG
    fi

    echo "yes"
    echo "	libbpf version $LIBBPF_VERSION"
}

check_selinux()
# SELinux is a compile time option in the ss utility
{
	if ${PKG_CONFIG} libselinux --exists; then
		echo "HAVE_SELINUX:=y" >>$CONFIG
		echo "yes"

		echo 'LDLIBS +=' "$(${PKG_CONFIG} --libs libselinux)" >>$CONFIG
		echo 'CFLAGS += -DHAVE_SELINUX' "$(${PKG_CONFIG} --cflags libselinux)" >>$CONFIG
	else
		echo "no"
	fi
}

check_tirpc()
{
	if ${PKG_CONFIG} libtirpc --exists; then
		echo "HAVE_RPC:=y" >>$CONFIG
		echo "yes"

		echo 'LDLIBS +=' "$(${PKG_CONFIG} --libs libtirpc)" >>$CONFIG
		echo 'CFLAGS += -DHAVE_RPC' "$(${PKG_CONFIG} --cflags libtirpc)" >>$CONFIG
	else
		echo "no"
	fi
}

check_mnl()
{
	if ${PKG_CONFIG} libmnl --exists; then
		echo "HAVE_MNL:=y" >>$CONFIG
		echo "yes"

		echo 'CFLAGS += -DHAVE_LIBMNL' "$(${PKG_CONFIG} libmnl --cflags)" >>$CONFIG
		echo 'LDLIBS +=' "$(${PKG_CONFIG} libmnl --libs)" >> $CONFIG
	else
		echo "no"
	fi
}

check_berkeley_db()
{
    cat >$TMPDIR/dbtest.c <<EOF
#include <fcntl.h>
#include <stdlib.h>
#include <db_185.h>
int main(int argc, char **argv) {
	dbopen("/tmp/xxx_test_db.db", O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
	return 0;
}
EOF
    if $CC -I$INCLUDE -o $TMPDIR/dbtest $TMPDIR/dbtest.c -ldb >/dev/null 2>&1; then
	echo "HAVE_BERKELEY_DB:=y" >>$CONFIG
	echo "yes"
    else
	echo "no"
    fi
    rm -f $TMPDIR/dbtest.c $TMPDIR/dbtest
}

check_strlcpy()
{
    cat >$TMPDIR/strtest.c <<EOF
#define _GNU_SOURCE
#include <string.h>
int main(int argc, char **argv) {
	char dst[10];
	strlcpy(dst, "test", sizeof(dst));
	return 0;
}
EOF
    if $CC -I$INCLUDE -o $TMPDIR/strtest $TMPDIR/strtest.c >/dev/null 2>&1; then
	echo "no"
    else
	if ${PKG_CONFIG} libbsd --exists; then
		echo 'CFLAGS += -DHAVE_LIBBSD' "$(${PKG_CONFIG} libbsd --cflags)" >>$CONFIG
		echo 'LDLIBS +=' "$(${PKG_CONFIG} libbsd --libs)" >> $CONFIG
		echo "no"
	else
		echo 'CFLAGS += -DNEED_STRLCPY' >>$CONFIG
		echo "yes"
	fi
    fi
    rm -f $TMPDIR/strtest.c $TMPDIR/strtest
}

check_cap()
{
	if ${PKG_CONFIG} libcap --exists; then
		echo "HAVE_CAP:=y" >>$CONFIG
		echo "yes"

		echo 'CFLAGS += -DHAVE_LIBCAP' "$(${PKG_CONFIG} libcap --cflags)" >>$CONFIG
		echo 'LDLIBS +=' "$(${PKG_CONFIG} libcap --libs)" >> $CONFIG
	else
		echo "no"
	fi
}

check_color()
{
	case "$COLOR" in
		never)
			echo 'CONF_COLOR:=COLOR_OPT_NEVER' >> $CONFIG
			echo 'never'
			;;
		auto)
			echo 'CONF_COLOR:=COLOR_OPT_AUTO' >> $CONFIG
			echo 'auto'
			;;
		always)
			echo 'CONF_COLOR:=COLOR_OPT_ALWAYS' >> $CONFIG
			echo 'always'
			;;
	esac
}

quiet_config()
{
	cat <<EOF
# user can control verbosity similar to kernel builds (e.g., V=1)
ifeq ("\$(origin V)", "command line")
  VERBOSE = \$(V)
endif
ifndef VERBOSE
  VERBOSE = 0
endif
ifeq (\$(VERBOSE),1)
  Q =
else
  Q = @
endif

ifeq (\$(VERBOSE), 0)
    QUIET_CC       = @echo '    CC       '\$@;
    QUIET_AR       = @echo '    AR       '\$@;
    QUIET_LINK     = @echo '    LINK     '\$@;
    QUIET_YACC     = @echo '    YACC     '\$@;
    QUIET_LEX      = @echo '    LEX      '\$@;
endif
EOF
}

usage()
{
	cat <<EOF
Usage: $0 [OPTIONS]
	--color <never|auto|always>	Default color output configuration. Available options:
					  never: color output is disabled (default)
					  auto: color output is enabled if stdout is a terminal
					  always: color output is enabled regardless of stdout state
	--include_dir <dir>		Path to iproute2 include dir
	--libdir <dir>			Path to iproute2 lib dir
	--libbpf_dir <dir>		Path to libbpf DESTDIR
	--libbpf_force <on|off>		Enable/disable libbpf by force. Available options:
					  on: require link against libbpf, quit config if no libbpf support
					  off: disable libbpf probing
	--prefix <dir>			Path prefix of the lib files to install
	-h | --help			Show this usage info
EOF
	exit $1
}

# Compat with the old INCLUDE path setting method.
if [ $# -eq 1 ] && [ "$(echo $1 | cut -c 1)" != '-' ]; then
	INCLUDE="$1"
else
	while [ "$#" -gt 0 ]; do
		case "$1" in
			--color)
				shift
				COLOR="$1" ;;
			--color=*)
				COLOR="${1#*=}" ;;
			--include_dir)
				shift
				INCLUDE="$1" ;;
			--include_dir=*)
				INCLUDE="${1#*=}" ;;
			--libdir)
				shift
				LIBDIR="$1" ;;
			--libdir=*)
				LIBDIR="${1#*=}" ;;
			--libbpf_dir)
				shift
				LIBBPF_DIR="$1" ;;
			--libbpf_dir=*)
				LIBBPF_DIR="${1#*=}" ;;
			--libbpf_force)
				shift
				LIBBPF_FORCE="$1" ;;
			--libbpf_force=*)
				LIBBPF_FORCE="${1#*=}" ;;
			--prefix)
				shift
				PREFIX="$1" ;;
			--prefix=*)
				PREFIX="${1#*=}" ;;
			-h | --help)
				usage 0 ;;
			--*)
				;;
			*)
				usage 1 ;;
		esac
		[ "$#" -gt 0 ] && shift
	done
fi

case "$COLOR" in
	never) ;;
	auto) ;;
	always) ;;
	*) usage 1 ;;
esac
[ -d "$INCLUDE" ] || usage 1
if [ "${LIBBPF_DIR-unused}" != "unused" ]; then
	[ -d "$LIBBPF_DIR" ] || usage 1
fi
if [ "${LIBBPF_FORCE-unused}" != "unused" ]; then
	if [ "$LIBBPF_FORCE" != 'on' ] && [ "$LIBBPF_FORCE" != 'off' ]; then
		usage 1
	fi
fi
[ -z "$PREFIX" ] && usage 1
[ -z "$LIBDIR" ] && usage 1

echo "# Generated config based on" $INCLUDE >$CONFIG
quiet_config >> $CONFIG

check_toolchain

echo "TC schedulers"

check_xtables
if ! grep -q TC_CONFIG_NO_XT $CONFIG; then
	echo -n " IPT	"
	check_xt
	check_xt_old
	check_xt_old_internal_h

	echo -n " IPSET  "
	check_ipset
fi

echo
check_lib_dir

echo -n "libc has setns: "
check_setns

echo -n "libc has name_to_handle_at: "
check_name_to_handle_at

echo -n "SELinux support: "
check_selinux

echo -n "libtirpc support: "
check_tirpc

echo -n "libbpf support: "
check_libbpf

echo -n "ELF support: "
check_elf

echo -n "libmnl support: "
check_mnl

echo -n "Berkeley DB: "
check_berkeley_db

echo -n "need for strlcpy: "
check_strlcpy

echo -n "libcap support: "
check_cap

echo -n "color output: "
check_color

echo >> $CONFIG
echo "%.o: %.c" >> $CONFIG
echo '	$(QUIET_CC)$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CPPFLAGS) -c -o $@ $<' >> $CONFIG