Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Ilya Leoshkevich | 9cae4ac | 2019-07-11 11:12:49 +0200 | [diff] [blame] | 2 | include ../../../../scripts/Kbuild.include |
Ilya Leoshkevich | 748e50c | 2019-07-11 16:29:27 +0200 | [diff] [blame] | 3 | include ../../../scripts/Makefile.arch |
Hendrik Brueckner | 618e165 | 2017-12-04 10:56:48 +0100 | [diff] [blame] | 4 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 5 | CURDIR := $(abspath .) |
| 6 | LIBDIR := $(abspath ../../../lib) |
Zi Shen Lim | e8f1f34 | 2017-03-19 23:03:14 -0700 | [diff] [blame] | 7 | BPFDIR := $(LIBDIR)/bpf |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 8 | TOOLSDIR := $(abspath ../../../include) |
| 9 | APIDIR := $(TOOLSDIR)/uapi |
| 10 | GENDIR := $(abspath ../../../../include/generated) |
Daniel Borkmann | 02ea80b | 2017-03-31 02:24:04 +0200 | [diff] [blame] | 11 | GENHDR := $(GENDIR)/autoconf.h |
Mickaël Salaün | d498f87 | 2017-02-11 23:20:23 +0100 | [diff] [blame] | 12 | |
Daniel Borkmann | 02ea80b | 2017-03-31 02:24:04 +0200 | [diff] [blame] | 13 | ifneq ($(wildcard $(GENHDR)),) |
| 14 | GENFLAGS := -DHAVE_GENHDR |
| 15 | endif |
| 16 | |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 17 | CLANG ?= clang |
| 18 | LLC ?= llc |
| 19 | LLVM_OBJCOPY ?= llvm-objcopy |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 20 | BPF_GCC ?= $(shell command -v bpf-gcc;) |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 21 | CFLAGS += -g -Wall -O2 $(GENFLAGS) -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) \ |
| 22 | -I$(GENDIR) -I$(TOOLSDIR) -I$(CURDIR) \ |
| 23 | -Dbpf_prog_load=bpf_prog_test_load \ |
Jiong Wang | 9d120b4 | 2019-05-24 23:25:21 +0100 | [diff] [blame] | 24 | -Dbpf_load_program=bpf_test_load_program |
Yonghong Song | af32efe | 2018-01-26 15:06:08 -0800 | [diff] [blame] | 25 | LDLIBS += -lcap -lelf -lrt -lpthread |
Daniel Borkmann | 5aa5bd1 | 2016-10-17 14:28:36 +0200 | [diff] [blame] | 26 | |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 27 | # Order correspond to 'make run_tests' order |
David S. Miller | 18b3ad9 | 2017-05-10 11:43:51 -0700 | [diff] [blame] | 28 | TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ |
Andrey Ignatov | 1d43688 | 2018-03-30 15:08:08 -0700 | [diff] [blame] | 29 | test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ |
Hangbin Liu | 25a7991 | 2019-06-04 10:35:05 +0800 | [diff] [blame] | 30 | test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ |
Andrii Nakryiko | 9bc6384 | 2019-10-22 23:09:13 -0700 | [diff] [blame^] | 31 | test_cgroup_storage test_select_reuseport \ |
Andrii Nakryiko | 2d2a3ad | 2019-05-24 11:59:04 -0700 | [diff] [blame] | 32 | test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 33 | test_cgroup_attach xdping test_progs-no_alu32 |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 34 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 35 | # Also test bpf-gcc, if present |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 36 | ifneq ($(BPF_GCC),) |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 37 | TEST_GEN_PROGS += test_progs-bpf_gcc |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 38 | endif |
| 39 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 40 | TEST_GEN_FILES = |
| 41 | TEST_FILES = |
| 42 | |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 43 | # Order correspond to 'make run_tests' order |
| 44 | TEST_PROGS := test_kmod.sh \ |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 45 | test_xdp_redirect.sh \ |
| 46 | test_xdp_meta.sh \ |
Toshiaki Makita | 88091ff | 2019-06-20 11:23:23 +0900 | [diff] [blame] | 47 | test_xdp_veth.sh \ |
Andrey Ignatov | 622adaf | 2018-03-30 15:08:06 -0700 | [diff] [blame] | 48 | test_offload.py \ |
William Tu | 933a741 | 2018-04-26 14:01:39 -0700 | [diff] [blame] | 49 | test_sock_addr.sh \ |
Mathieu Xhonneux | c99a84e | 2018-05-20 14:58:17 +0100 | [diff] [blame] | 50 | test_tunnel.sh \ |
Sean Young | 6bdd533 | 2018-05-27 12:24:10 +0100 | [diff] [blame] | 51 | test_lwt_seg6local.sh \ |
Andrey Ignatov | 5ecd8c2 | 2018-08-12 10:49:30 -0700 | [diff] [blame] | 52 | test_lirc_mode2.sh \ |
Petar Penkov | 50b3ed5 | 2018-09-14 07:46:22 -0700 | [diff] [blame] | 53 | test_skb_cgroup_id.sh \ |
Jesper Dangaard Brouer | 97396ff | 2018-10-09 12:04:53 +0200 | [diff] [blame] | 54 | test_flow_dissector.sh \ |
Jesper Dangaard Brouer | d35661f | 2019-08-01 20:00:21 +0200 | [diff] [blame] | 55 | test_xdp_vlan_mode_generic.sh \ |
| 56 | test_xdp_vlan_mode_native.sh \ |
Lorenz Bauer | bafc0ba | 2019-03-22 09:54:06 +0800 | [diff] [blame] | 57 | test_lwt_ip_encap.sh \ |
Willem de Bruijn | 98cdabc | 2019-03-22 14:32:49 -0400 | [diff] [blame] | 58 | test_tcp_check_syncookie.sh \ |
Peter Oskolkov | 7df5e3d | 2019-03-22 16:40:19 -0700 | [diff] [blame] | 59 | test_tc_tunnel.sh \ |
Alan Maguire | cd53850 | 2019-05-31 18:47:14 +0100 | [diff] [blame] | 60 | test_tc_edt.sh \ |
Quentin Monnet | 45c5589d | 2019-08-30 12:00:38 +0100 | [diff] [blame] | 61 | test_xdping.sh \ |
| 62 | test_bpftool_build.sh |
Daniel Borkmann | 5aa5bd1 | 2016-10-17 14:28:36 +0200 | [diff] [blame] | 63 | |
Anders Roxell | f98937c | 2019-01-08 10:23:03 +0100 | [diff] [blame] | 64 | TEST_PROGS_EXTENDED := with_addr.sh \ |
Stanislav Fomichev | 1be72f2 | 2019-01-14 09:41:41 -0800 | [diff] [blame] | 65 | with_tunnels.sh \ |
Anders Roxell | f98937c | 2019-01-08 10:23:03 +0100 | [diff] [blame] | 66 | tcp_client.py \ |
Anders Roxell | 3035bb7 | 2019-08-20 15:41:21 +0200 | [diff] [blame] | 67 | tcp_server.py \ |
| 68 | test_xdp_vlan.sh |
Anders Roxell | ef4ab84 | 2018-10-10 16:27:04 +0200 | [diff] [blame] | 69 | |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 70 | # Compile but not part of 'make run_tests' |
Andrii Nakryiko | cb79a4e | 2019-10-15 23:00:51 -0700 | [diff] [blame] | 71 | TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ |
Hangbin Liu | 25a7991 | 2019-06-04 10:35:05 +0800 | [diff] [blame] | 72 | flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ |
| 73 | test_lirc_mode2_user |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 74 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 75 | TEST_CUSTOM_PROGS = urandom_read |
| 76 | |
Zi Shen Lim | e8f1f34 | 2017-03-19 23:03:14 -0700 | [diff] [blame] | 77 | include ../lib.mk |
Alexei Starovoitov | 1da8ac7 | 2017-03-10 22:05:55 -0800 | [diff] [blame] | 78 | |
Andrii Nakryiko | 03dcb78 | 2019-10-15 23:00:48 -0700 | [diff] [blame] | 79 | # Define simple and short `make test_progs`, `make test_sysctl`, etc targets |
| 80 | # to build individual tests. |
| 81 | # NOTE: Semicolon at the end is critical to override lib.mk's default static |
| 82 | # rule for binaries. |
| 83 | $(notdir $(TEST_GEN_PROGS) \ |
| 84 | $(TEST_PROGS) \ |
| 85 | $(TEST_PROGS_EXTENDED) \ |
| 86 | $(TEST_GEN_PROGS_EXTENDED) \ |
| 87 | $(TEST_CUSTOM_PROGS)): %: $(OUTPUT)/% ; |
| 88 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 89 | $(OUTPUT)/urandom_read: urandom_read.c |
Ivan Vecera | f682752 | 2019-03-15 21:04:14 +0100 | [diff] [blame] | 90 | $(CC) -o $@ $< -Wl,--build-id |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 91 | |
Jesper Dangaard Brouer | 41757dc | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 92 | BPFOBJ := $(OUTPUT)/libbpf.a |
Zi Shen Lim | e8f1f34 | 2017-03-19 23:03:14 -0700 | [diff] [blame] | 93 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 94 | $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ) |
Jesper Dangaard Brouer | f09b2e3 | 2018-02-08 12:48:27 +0100 | [diff] [blame] | 95 | |
Jesper Dangaard Brouer | 41757dc | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 96 | $(OUTPUT)/test_dev_cgroup: cgroup_helpers.c |
Andrey Ignatov | 5ecd8c2 | 2018-08-12 10:49:30 -0700 | [diff] [blame] | 97 | $(OUTPUT)/test_skb_cgroup_id_user: cgroup_helpers.c |
Andrey Ignatov | 1d43688 | 2018-03-30 15:08:08 -0700 | [diff] [blame] | 98 | $(OUTPUT)/test_sock: cgroup_helpers.c |
Andrey Ignatov | e50b0a6 | 2018-03-30 15:08:03 -0700 | [diff] [blame] | 99 | $(OUTPUT)/test_sock_addr: cgroup_helpers.c |
Andrey Ignatov | 194db0d | 2018-07-30 17:42:31 -0700 | [diff] [blame] | 100 | $(OUTPUT)/test_socket_cookie: cgroup_helpers.c |
John Fastabend | 16962b2 | 2018-04-23 14:30:38 -0700 | [diff] [blame] | 101 | $(OUTPUT)/test_sockmap: cgroup_helpers.c |
Andrey Ignatov | c65267e | 2018-07-11 17:33:35 -0700 | [diff] [blame] | 102 | $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c |
Sowmini Varadhan | 435f90a | 2018-11-07 16:12:02 -0800 | [diff] [blame] | 103 | $(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c |
Yonghong Song | f269099 | 2018-06-03 15:59:43 -0700 | [diff] [blame] | 104 | $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c |
Roman Gushchin | 68cfa3a | 2018-08-02 14:27:29 -0700 | [diff] [blame] | 105 | $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c |
Roman Gushchin | 371e4fc | 2018-09-28 14:46:00 +0000 | [diff] [blame] | 106 | $(OUTPUT)/test_netcnt: cgroup_helpers.c |
Martin KaFai Lau | e0b27b3 | 2019-02-09 23:22:28 -0800 | [diff] [blame] | 107 | $(OUTPUT)/test_sock_fields: cgroup_helpers.c |
Andrey Ignatov | 1f5fa9a | 2019-03-08 15:08:21 -0800 | [diff] [blame] | 108 | $(OUTPUT)/test_sysctl: cgroup_helpers.c |
Roman Gushchin | ba0c0cc | 2019-05-25 09:37:40 -0700 | [diff] [blame] | 109 | $(OUTPUT)/test_cgroup_attach: cgroup_helpers.c |
Jesper Dangaard Brouer | 41757dc | 2018-02-13 14:19:15 +0100 | [diff] [blame] | 110 | |
Zi Shen Lim | e8f1f34 | 2017-03-19 23:03:14 -0700 | [diff] [blame] | 111 | .PHONY: force |
Mickaël Salaün | d498f87 | 2017-02-11 23:20:23 +0100 | [diff] [blame] | 112 | |
Mickaël Salaün | d498f87 | 2017-02-11 23:20:23 +0100 | [diff] [blame] | 113 | # force a rebuild of BPFOBJ when its dependencies are updated |
| 114 | force: |
| 115 | |
Andrii Nakryiko | e01a75c | 2019-10-08 10:59:40 -0700 | [diff] [blame] | 116 | $(BPFOBJ): force |
Zi Shen Lim | e8f1f34 | 2017-03-19 23:03:14 -0700 | [diff] [blame] | 117 | $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/ |
Alexei Starovoitov | 6882804 | 2017-03-30 21:45:41 -0700 | [diff] [blame] | 118 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 119 | BPF_HELPERS := $(BPFDIR)/bpf_helper_defs.h $(wildcard $(BPFDIR)/bpf_*.h) |
| 120 | $(BPFDIR)/bpf_helper_defs.h: |
| 121 | $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/ bpf_helper_defs.h |
| 122 | |
Sirio Balmelli | 167381f | 2018-05-21 09:00:03 +0200 | [diff] [blame] | 123 | # Get Clang's default includes on this system, as opposed to those seen by |
| 124 | # '-target bpf'. This fixes "missing" files on some architectures/distros, |
| 125 | # such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc. |
| 126 | # |
| 127 | # Use '-idirafter': Don't interfere with include mechanics except where the |
| 128 | # build would have failed anyways. |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 129 | define get_sys_includes |
| 130 | $(shell $(1) -v -E - </dev/null 2>&1 \ |
Sirio Balmelli | 167381f | 2018-05-21 09:00:03 +0200 | [diff] [blame] | 131 | | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 132 | endef |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 133 | |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 134 | CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG)) |
Andrii Nakryiko | 598dc04 | 2019-10-11 15:01:46 -0700 | [diff] [blame] | 135 | BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) \ |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 136 | -I. -I./include/uapi -I$(APIDIR) \ |
| 137 | -I$(BPFDIR) -I$(abspath $(OUTPUT)/../usr/include) |
Sirio Balmelli | 167381f | 2018-05-21 09:00:03 +0200 | [diff] [blame] | 138 | |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 139 | CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \ |
| 140 | -Wno-compare-distinct-pointer-types |
Alexei Starovoitov | 3bc35c6 | 2017-12-14 17:55:11 -0800 | [diff] [blame] | 141 | |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 142 | $(OUTPUT)/test_l4lb_noinline.o: BPF_CFLAGS += -fno-inline |
| 143 | $(OUTPUT)/test_xdp_noinline.o: BPF_CFLAGS += -fno-inline |
Alexei Starovoitov | 3bc35c6 | 2017-12-14 17:55:11 -0800 | [diff] [blame] | 144 | |
Stanislav Fomichev | bf0f0fd | 2019-01-28 08:53:55 -0800 | [diff] [blame] | 145 | $(OUTPUT)/flow_dissector_load.o: flow_dissector_load.h |
Stanislav Fomichev | bf0f0fd | 2019-01-28 08:53:55 -0800 | [diff] [blame] | 146 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 147 | # Build BPF object using Clang |
| 148 | # $1 - input .c file |
| 149 | # $2 - output .o file |
| 150 | # $3 - CFLAGS |
| 151 | # $4 - LDFLAGS |
| 152 | define CLANG_BPF_BUILD_RULE |
| 153 | ($(CLANG) $3 -O2 -target bpf -emit-llvm \ |
| 154 | -c $1 -o - || echo "BPF obj compilation failed") | \ |
| 155 | $(LLC) -march=bpf -mcpu=probe $4 -filetype=obj -o $2 |
| 156 | endef |
| 157 | # Similar to CLANG_BPF_BUILD_RULE, but using native Clang and bpf LLC |
| 158 | define CLANG_NATIVE_BPF_BUILD_RULE |
| 159 | ($(CLANG) $3 -O2 -emit-llvm \ |
| 160 | -c $1 -o - || echo "BPF obj compilation failed") | \ |
| 161 | $(LLC) -march=bpf -mcpu=probe $4 -filetype=obj -o $2 |
| 162 | endef |
| 163 | # Build BPF object using GCC |
| 164 | define GCC_BPF_BUILD_RULE |
| 165 | $(BPF_GCC) $3 $4 -O2 -c $1 -o $2 |
| 166 | endef |
Stanislav Fomichev | e78e00b | 2019-03-06 11:59:26 -0800 | [diff] [blame] | 167 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 168 | # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on |
| 169 | # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES. |
| 170 | # Parameters: |
| 171 | # $1 - test runner base binary name (e.g., test_progs) |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 172 | # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 173 | define DEFINE_TEST_RUNNER |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 174 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 175 | TRUNNER_OUTPUT := $(OUTPUT)$(if $2,/)$2 |
| 176 | TRUNNER_BINARY := $1$(if $2,-)$2 |
| 177 | TRUNNER_TEST_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.test.o, \ |
| 178 | $$(notdir $$(wildcard $(TRUNNER_TESTS_DIR)/*.c))) |
| 179 | TRUNNER_EXTRA_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ |
| 180 | $$(filter %.c,$(TRUNNER_EXTRA_SOURCES))) |
| 181 | TRUNNER_EXTRA_HDRS := $$(filter %.h,$(TRUNNER_EXTRA_SOURCES)) |
| 182 | TRUNNER_TESTS_HDR := $(TRUNNER_TESTS_DIR)/tests.h |
| 183 | TRUNNER_BPF_OBJS := $$(patsubst %.c,$$(TRUNNER_OUTPUT)/%.o, \ |
| 184 | $$(notdir $$(wildcard $(TRUNNER_BPF_PROGS_DIR)/*.c))) |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 185 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 186 | # Evaluate rules now with extra TRUNNER_XXX variables above already defined |
| 187 | $$(eval $$(call DEFINE_TEST_RUNNER_RULES,$1,$2)) |
Stanislav Fomichev | 3f30658 | 2019-03-01 19:42:13 -0800 | [diff] [blame] | 188 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 189 | endef |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 190 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 191 | # Using TRUNNER_XXX variables, provided by callers of DEFINE_TEST_RUNNER and |
| 192 | # set up by DEFINE_TEST_RUNNER itself, create test runner build rules with: |
| 193 | # $1 - test runner base binary name (e.g., test_progs) |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 194 | # $2 - test runner extra "flavor" (e.g., no_alu32, gcc-bpf, etc) |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 195 | define DEFINE_TEST_RUNNER_RULES |
| 196 | |
| 197 | ifeq ($($(TRUNNER_OUTPUT)-dir),) |
| 198 | $(TRUNNER_OUTPUT)-dir := y |
| 199 | $(TRUNNER_OUTPUT): |
| 200 | mkdir -p $$@ |
Jiong Wang | 3ef8434 | 2019-01-26 12:26:14 -0500 | [diff] [blame] | 201 | endif |
| 202 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 203 | # ensure we set up BPF objects generation rule just once for a given |
| 204 | # input/output directory combination |
| 205 | ifeq ($($(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs),) |
| 206 | $(TRUNNER_BPF_PROGS_DIR)$(if $2,-)$2-bpfobjs := y |
| 207 | $(TRUNNER_BPF_OBJS): $(TRUNNER_OUTPUT)/%.o: \ |
| 208 | $(TRUNNER_BPF_PROGS_DIR)/%.c \ |
| 209 | $(TRUNNER_BPF_PROGS_DIR)/*.h \ |
| 210 | $$(BPF_HELPERS) | $(TRUNNER_OUTPUT) |
| 211 | $$(call $(TRUNNER_BPF_BUILD_RULE),$$<,$$@, \ |
| 212 | $(TRUNNER_BPF_CFLAGS), \ |
| 213 | $(TRUNNER_BPF_LDFLAGS)) |
| 214 | endif |
| 215 | |
| 216 | # ensure we set up tests.h header generation rule just once |
| 217 | ifeq ($($(TRUNNER_TESTS_DIR)-tests-hdr),) |
| 218 | $(TRUNNER_TESTS_DIR)-tests-hdr := y |
| 219 | $(TRUNNER_TESTS_HDR): $(TRUNNER_TESTS_DIR)/*.c |
| 220 | $$(shell ( cd $(TRUNNER_TESTS_DIR); \ |
| 221 | echo '/* Generated header, do not edit */'; \ |
| 222 | ls *.c 2> /dev/null | \ |
| 223 | sed -e 's@\([^\.]*\)\.c@DEFINE_TEST(\1)@'; \ |
| 224 | ) > $$@) |
| 225 | endif |
| 226 | |
| 227 | # compile individual test files |
| 228 | # Note: we cd into output directory to ensure embedded BPF object is found |
| 229 | $(TRUNNER_TEST_OBJS): $(TRUNNER_OUTPUT)/%.test.o: \ |
| 230 | $(TRUNNER_TESTS_DIR)/%.c \ |
| 231 | $(TRUNNER_EXTRA_HDRS) \ |
| 232 | $(TRUNNER_BPF_OBJS) \ |
| 233 | $$(BPFOBJ) | $(TRUNNER_OUTPUT) |
| 234 | cd $$(@D) && $$(CC) $$(CFLAGS) $$(LDLIBS) -c $(CURDIR)/$$< -o $$(@F) |
| 235 | |
| 236 | $(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \ |
| 237 | %.c \ |
| 238 | $(TRUNNER_EXTRA_HDRS) \ |
| 239 | $(TRUNNER_TESTS_HDR) \ |
| 240 | $$(BPFOBJ) | $(TRUNNER_OUTPUT) |
| 241 | $$(CC) $$(CFLAGS) $$(LDLIBS) -c $$< -o $$@ |
| 242 | |
| 243 | $(TRUNNER_BINARY)-extras: $(TRUNNER_EXTRA_FILES) | $(TRUNNER_OUTPUT) |
| 244 | ifneq ($2,) |
| 245 | # only copy extra resources if in flavored build |
| 246 | cp -a $$^ $(TRUNNER_OUTPUT)/ |
| 247 | endif |
| 248 | |
| 249 | $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \ |
| 250 | $(TRUNNER_EXTRA_OBJS) $$(BPFOBJ) \ |
| 251 | | $(TRUNNER_BINARY)-extras |
| 252 | $$(CC) $$(CFLAGS) $$(LDLIBS) $$(filter %.a %.o,$$^) -o $$@ |
| 253 | |
| 254 | endef |
| 255 | |
| 256 | # Define test_progs test runner. |
| 257 | TRUNNER_TESTS_DIR := prog_tests |
| 258 | TRUNNER_BPF_PROGS_DIR := progs |
| 259 | TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ |
| 260 | flow_dissector_load.h |
| 261 | TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ |
| 262 | $(wildcard progs/btf_dump_test_case_*.c) |
| 263 | TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE |
| 264 | TRUNNER_BPF_CFLAGS := -I. -I$(OUTPUT) $(BPF_CFLAGS) $(CLANG_CFLAGS) |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 265 | TRUNNER_BPF_LDFLAGS := -mattr=+alu32 |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 266 | $(eval $(call DEFINE_TEST_RUNNER,test_progs)) |
| 267 | |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 268 | # Define test_progs-no_alu32 test runner. |
| 269 | TRUNNER_BPF_LDFLAGS := |
| 270 | $(eval $(call DEFINE_TEST_RUNNER,test_progs,no_alu32)) |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 271 | |
| 272 | # Define test_progs BPF-GCC-flavored test runner. |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 273 | ifneq ($(BPF_GCC),) |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 274 | IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - </dev/null | \ |
| 275 | grep 'define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__') |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 276 | MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian) |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 277 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 278 | TRUNNER_BPF_BUILD_RULE := GCC_BPF_BUILD_RULE |
| 279 | TRUNNER_BPF_CFLAGS := $(BPF_CFLAGS) $(call get_sys_includes,gcc) $(MENDIAN) |
| 280 | TRUNNER_BPF_LDFLAGS := |
| 281 | $(eval $(call DEFINE_TEST_RUNNER,test_progs,bpf_gcc)) |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 282 | endif |
| 283 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 284 | # Define test_maps test runner. |
| 285 | TRUNNER_TESTS_DIR := map_tests |
| 286 | TRUNNER_BPF_PROGS_DIR := progs |
| 287 | TRUNNER_EXTRA_SOURCES := test_maps.c |
| 288 | TRUNNER_EXTRA_FILES := |
| 289 | TRUNNER_BPF_BUILD_RULE := $$(error no BPF objects should be built) |
| 290 | TRUNNER_BPF_CFLAGS := |
| 291 | TRUNNER_BPF_LDFLAGS := |
| 292 | $(eval $(call DEFINE_TEST_RUNNER,test_maps)) |
Martin KaFai Lau | c0fa1b6 | 2018-04-18 15:56:06 -0700 | [diff] [blame] | 293 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 294 | # Define test_verifier test runner. |
| 295 | # It is much simpler than test_maps/test_progs and sufficiently different from |
| 296 | # them (e.g., test.h is using completely pattern), that it's worth just |
| 297 | # explicitly defining all the rules explicitly. |
| 298 | verifier/tests.h: verifier/*.c |
Stanislav Fomichev | e78e00b | 2019-03-06 11:59:26 -0800 | [diff] [blame] | 299 | $(shell ( cd verifier/; \ |
Jakub Kicinski | 2dfb401 | 2019-01-25 15:24:42 -0800 | [diff] [blame] | 300 | echo '/* Generated header, do not edit */'; \ |
| 301 | echo '#ifdef FILL_ARRAY'; \ |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 302 | ls *.c 2> /dev/null | sed -e 's@\(.*\)@#include \"\1\"@'; \ |
Jakub Kicinski | 2dfb401 | 2019-01-25 15:24:42 -0800 | [diff] [blame] | 303 | echo '#endif' \ |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 304 | ) > verifier/tests.h) |
| 305 | $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) |
| 306 | $(CC) $(CFLAGS) $(LDLIBS) $(filter %.a %.o %.c,$^) -o $@ |
Jakub Kicinski | 2dfb401 | 2019-01-25 15:24:42 -0800 | [diff] [blame] | 307 | |
Andrii Nakryiko | 74b5a59 | 2019-10-15 23:00:49 -0700 | [diff] [blame] | 308 | EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) \ |
| 309 | prog_tests/tests.h map_tests/tests.h verifier/tests.h \ |
Yonghong Song | e13a2fe | 2019-10-21 21:31:19 -0700 | [diff] [blame] | 310 | feature $(OUTPUT)/*.o $(OUTPUT)/no_alu32 $(OUTPUT)/bpf_gcc |