selftests: net: Add cross-compilation support for BPF programs
[linux-2.6-block.git] / tools / testing / selftests / net / bpf / Makefile
CommitLineData
edae34a3
LW
1# SPDX-License-Identifier: GPL-2.0
2
3CLANG ?= clang
837a3d66
BT
4SCRATCH_DIR := $(OUTPUT)/tools
5BUILD_DIR := $(SCRATCH_DIR)/build
6BPFDIR := $(abspath ../../../lib/bpf)
7APIDIR := $(abspath ../../../include/uapi)
8
edae34a3
LW
9CCINCLUDE += -I../../bpf
10CCINCLUDE += -I../../../../../usr/include/
837a3d66
BT
11CCINCLUDE += -I$(SCRATCH_DIR)/include
12
13BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
14
15MAKE_DIRS := $(BUILD_DIR)/libbpf
16$(MAKE_DIRS):
17 mkdir -p $@
edae34a3
LW
18
19TEST_CUSTOM_PROGS = $(OUTPUT)/bpf/nat6to4.o
20all: $(TEST_CUSTOM_PROGS)
21
837a3d66
BT
22# Get Clang's default includes on this system, as opposed to those seen by
23# '-target bpf'. This fixes "missing" files on some architectures/distros,
24# such as asm/byteorder.h, asm/socket.h, asm/sockios.h, sys/cdefs.h etc.
25#
26# Use '-idirafter': Don't interfere with include mechanics except where the
27# build would have failed anyways.
28define get_sys_includes
29$(shell $(1) $(2) -v -E - </dev/null 2>&1 \
30 | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
31$(shell $(1) $(2) -dM -E - </dev/null | grep '__riscv_xlen ' | awk '{printf("-D__riscv_xlen=%d -D__BITS_PER_LONG=%d", $$3, $$3)}')
32endef
33
34ifneq ($(CROSS_COMPILE),)
35CLANG_TARGET_ARCH = --target=$(notdir $(CROSS_COMPILE:%-=%))
36endif
37
38CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG),$(CLANG_TARGET_ARCH))
39
40$(TEST_CUSTOM_PROGS): $(BPFOBJ)
41 $(CLANG) -O2 -target bpf -c $(@:.o=.c) $(CCINCLUDE) $(CLANG_SYS_INCLUDES) -o $@
42
43$(BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile) \
44 $(APIDIR)/linux/bpf.h \
45 | $(BUILD_DIR)/libbpf
46 $(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
47 EXTRA_CFLAGS='-g -O0' \
48 DESTDIR=$(SCRATCH_DIR) prefix= all install_headers
49
50EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR)
edae34a3 51