Makefile 3.6 KB

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