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