github.com/cilium/ebpf@v0.10.0/Makefile (about)

     1  # The development version of clang is distributed as the 'clang' binary,
     2  # while stable/released versions have a version number attached.
     3  # Pin the default clang to a stable version.
     4  CLANG ?= clang-14
     5  STRIP ?= llvm-strip-14
     6  OBJCOPY ?= llvm-objcopy-14
     7  CFLAGS := -O2 -g -Wall -Werror $(CFLAGS)
     8  
     9  CI_KERNEL_URL ?= https://github.com/cilium/ci-kernels/raw/master/
    10  
    11  # Obtain an absolute path to the directory of the Makefile.
    12  # Assume the Makefile is in the root of the repository.
    13  REPODIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
    14  UIDGID := $(shell stat -c '%u:%g' ${REPODIR})
    15  
    16  # Prefer podman if installed, otherwise use docker.
    17  # Note: Setting the var at runtime will always override.
    18  CONTAINER_ENGINE ?= $(if $(shell command -v podman), podman, docker)
    19  CONTAINER_RUN_ARGS ?= $(if $(filter ${CONTAINER_ENGINE}, podman), --log-driver=none, --user "${UIDGID}")
    20  
    21  IMAGE := $(shell cat ${REPODIR}/testdata/docker/IMAGE)
    22  VERSION := $(shell cat ${REPODIR}/testdata/docker/VERSION)
    23  
    24  
    25  # clang <8 doesn't tag relocs properly (STT_NOTYPE)
    26  # clang 9 is the first version emitting BTF
    27  TARGETS := \
    28  	testdata/loader-clang-7 \
    29  	testdata/loader-clang-9 \
    30  	testdata/loader-$(CLANG) \
    31  	testdata/btf_map_init \
    32  	testdata/invalid_map \
    33  	testdata/raw_tracepoint \
    34  	testdata/invalid_map_static \
    35  	testdata/invalid_btf_map_init \
    36  	testdata/strings \
    37  	testdata/freplace \
    38  	testdata/iproute2_map_compat \
    39  	testdata/map_spin_lock \
    40  	testdata/subprog_reloc \
    41  	testdata/fwd_decl \
    42  	btf/testdata/relocs \
    43  	btf/testdata/relocs_read \
    44  	btf/testdata/relocs_read_tgt
    45  
    46  .PHONY: all clean container-all container-shell generate
    47  
    48  .DEFAULT_TARGET = container-all
    49  
    50  # Build all ELF binaries using a containerized LLVM toolchain.
    51  container-all:
    52  	${CONTAINER_ENGINE} run --rm ${CONTAINER_RUN_ARGS} \
    53  		-v "${REPODIR}":/ebpf -w /ebpf --env MAKEFLAGS \
    54  		--env CFLAGS="-fdebug-prefix-map=/ebpf=." \
    55  		--env HOME="/tmp" \
    56  		"${IMAGE}:${VERSION}" \
    57  		$(MAKE) all
    58  
    59  # (debug) Drop the user into a shell inside the container as root.
    60  container-shell:
    61  	${CONTAINER_ENGINE} run --rm -ti \
    62  		-v "${REPODIR}":/ebpf -w /ebpf \
    63  		"${IMAGE}:${VERSION}"
    64  
    65  clean:
    66  	-$(RM) testdata/*.elf
    67  	-$(RM) btf/testdata/*.elf
    68  
    69  format:
    70  	find . -type f -name "*.c" | xargs clang-format -i
    71  
    72  all: format $(addsuffix -el.elf,$(TARGETS)) $(addsuffix -eb.elf,$(TARGETS)) generate
    73  	ln -srf testdata/loader-$(CLANG)-el.elf testdata/loader-el.elf
    74  	ln -srf testdata/loader-$(CLANG)-eb.elf testdata/loader-eb.elf
    75  
    76  # $BPF_CLANG is used in go:generate invocations.
    77  generate: export BPF_CLANG := $(CLANG)
    78  generate: export BPF_CFLAGS := $(CFLAGS)
    79  generate:
    80  	go generate ./...
    81  
    82  testdata/loader-%-el.elf: testdata/loader.c
    83  	$* $(CFLAGS) -target bpfel -c $< -o $@
    84  	$(STRIP) -g $@
    85  
    86  testdata/loader-%-eb.elf: testdata/loader.c
    87  	$* $(CFLAGS) -target bpfeb -c $< -o $@
    88  	$(STRIP) -g $@
    89  
    90  %-el.elf: %.c
    91  	$(CLANG) $(CFLAGS) -target bpfel -c $< -o $@
    92  	$(STRIP) -g $@
    93  
    94  %-eb.elf : %.c
    95  	$(CLANG) $(CFLAGS) -target bpfeb -c $< -o $@
    96  	$(STRIP) -g $@
    97  
    98  .PHONY: generate-btf
    99  generate-btf: KERNEL_VERSION?=5.18
   100  generate-btf:
   101  	$(eval TMP := $(shell mktemp -d))
   102  	curl -fL "$(CI_KERNEL_URL)/linux-$(KERNEL_VERSION).bz" -o "$(TMP)/bzImage"
   103  	./testdata/extract-vmlinux "$(TMP)/bzImage" > "$(TMP)/vmlinux"
   104  	$(OBJCOPY) --dump-section .BTF=/dev/stdout "$(TMP)/vmlinux" /dev/null | gzip > "btf/testdata/vmlinux.btf.gz"
   105  	curl -fL "$(CI_KERNEL_URL)/linux-$(KERNEL_VERSION)-selftests-bpf.tgz" -o "$(TMP)/selftests.tgz"
   106  	tar -xf "$(TMP)/selftests.tgz" --to-stdout tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.ko | \
   107  		$(OBJCOPY) --dump-section .BTF="btf/testdata/btf_testmod.btf" - /dev/null
   108  	$(RM) -r "$(TMP)"