• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2
3pound := \#
4
5CFLAGS_BACKUP := $(CFLAGS)
6ifneq ($(LLVM),)
7  CFLAGS += -Wno-unused-command-line-argument
8endif
9
10### feature-clang-bpf-co-re
11
12feature-clang-bpf-co-re := \
13  $(shell printf '%s\n' 'struct s { int i; } __attribute__((preserve_access_index)); struct s foo;' | \
14    $(CLANG) -g -target bpf -S -o - -x c - 2>/dev/null | grep -q BTF_KIND_VAR && echo 1)
15
16### feature-reallocarray
17
18# This mirror re-implements reallocarray(), so the function is always available.
19feature-reallocarray := 1
20
21### feature-libbfd
22
23LIBBFD_PROBE := '$(pound)include <bfd.h>\n'
24LIBBFD_PROBE += 'int main(void) {'
25LIBBFD_PROBE += '	bfd_demangle(0, 0, 0);'
26LIBBFD_PROBE += '	return 0;'
27LIBBFD_PROBE += '}'
28
29define libbfd_build
30  $(shell printf '%b\n' $(LIBBFD_PROBE) | \
31    $(CC) $(CFLAGS) -Wall -Werror $(LDFLAGS) -x c - $(1) -S -o - >/dev/null 2>&1 \
32    && echo 1)
33endef
34
35feature-libbfd := \
36  $(findstring 1,$(call libbfd_build,-lbfd -ldl))
37ifneq ($(feature-libbfd),1)
38  feature-libbfd-liberty := \
39    $(findstring 1,$(call libbfd_build,-lbfd -ldl -liberty))
40  ifneq ($(feature-libbfd-liberty),1)
41    $(findstring 1,feature-libbfd-liberty-z := \
42      $(call libbfd_build,-lbfd -ldl -liberty -lz))
43  endif
44endif
45HAS_LIBBFD := $(findstring 1, \
46  $(feature-libbfd)$(feature-libbfd-liberty)$(feature-libbfd-liberty-z))
47
48### feature-disassembler-four-args
49
50DISASSEMBLER_PROBE := '$(pound)include <dis-asm.h>\n'
51DISASSEMBLER_PROBE += 'int main(void) {'
52DISASSEMBLER_PROBE += '	disassembler((enum bfd_architecture)0, 0, 0, NULL);'
53DISASSEMBLER_PROBE += '	return 0;'
54DISASSEMBLER_PROBE += '}'
55
56define disassembler_build
57  $(shell printf '%b\n' $(DISASSEMBLER_PROBE) | \
58    $(CC) $(CFLAGS) -Wall -Werror $(LDFLAGS) -x c - -lbfd -lopcodes -S -o - >/dev/null 2>&1 \
59    && echo 1)
60endef
61
62feature-disassembler-four-args := $(findstring 1, $(call disassembler_build))
63
64### feature-zlib
65
66ZLIB_PROBE := '$(pound)include <zlib.h>\n'
67ZLIB_PROBE += 'int main(void) {'
68ZLIB_PROBE += '	gzopen(NULL, "r");'
69ZLIB_PROBE += '	return 0;'
70ZLIB_PROBE += '}'
71
72define zlib_build
73  $(shell printf '%b\n' $(ZLIB_PROBE) | \
74    $(CC) $(CFLAGS) -Wall -Werror $(LDFLAGS) -x c - -lz -S -o - >/dev/null 2>&1 \
75    && echo 1)
76endef
77
78feature-zlib := $(findstring 1, $(call zlib_build))
79
80### feature-libcap
81
82LIBCAP_PROBE := '$(pound)include <sys/capability.h>\n'
83LIBCAP_PROBE += 'int main(void) {'
84LIBCAP_PROBE += '	cap_free(0);'
85LIBCAP_PROBE += '	return 0;'
86LIBCAP_PROBE += '}'
87
88define libcap_build
89  $(shell printf '%b\n' $(LIBCAP_PROBE) | \
90    $(CC) $(CFLAGS) -Wall -Werror $(LDFLAGS) -x c - -lcap -S -o - >/dev/null 2>&1 \
91    && echo 1)
92endef
93
94feature-libcap := $(findstring 1, $(call libcap_build))
95
96### Print detection results
97
98define print_status
99  ifeq ($(1), 1)
100    MSG = $(shell printf '...%30s: [ \033[32mon\033[m  ]' $(2))
101  else
102    MSG = $(shell printf '...%30s: [ \033[31mOFF\033[m ]' $(2))
103  endif
104endef
105feature_print_status = $(eval $(print_status)) $(info $(MSG))
106
107$(call feature_print_status,$(HAS_LIBBFD),libbfd)
108
109$(foreach feature,$(filter-out libbfd,$(FEATURE_DISPLAY)), \
110  $(call feature_print_status,$(feature-$(feature)),$(feature)))
111
112CFLAGS := $(CFLAGS_BACKUP)
113