xref: /linux/tools/build/Makefile.feature (revision 5ce42b5de461c3154f61a023b191dd6b77ee66c0)
1# SPDX-License-Identifier: GPL-2.0-only
2feature_dir := $(srctree)/tools/build/feature
3
4ifneq ($(OUTPUT),)
5  OUTPUT_FEATURES = $(OUTPUT)feature/
6  $(shell mkdir -p $(OUTPUT_FEATURES))
7endif
8
9feature_check = $(eval $(feature_check_code))
10define feature_check_code
11  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
12endef
13
14feature_set = $(eval $(feature_set_code))
15define feature_set_code
16  feature-$(1) := 1
17endef
18
19#
20# Build the feature check binaries in parallel, ignore errors, ignore return value and suppress output:
21#
22
23#
24# Note that this is not a complete list of all feature tests, just
25# those that are typically built on a fully configured system.
26#
27# [ Feature tests not mentioned here have to be built explicitly in
28#   the rule that uses them - an example for that is the 'bionic'
29#   feature check. ]
30#
31FEATURE_TESTS_BASIC :=                  \
32        backtrace                       \
33        libdw                           \
34        eventfd                         \
35        fortify-source                  \
36        get_current_dir_name            \
37        gettid				\
38        glibc                           \
39        libbfd                          \
40        libbfd-buildid			\
41        libcap                          \
42        libelf                          \
43        libelf-getphdrnum               \
44        libelf-gelf_getnote             \
45        libelf-getshdrstrndx            \
46        libnuma                         \
47        numa_num_possible_cpus          \
48        libperl                         \
49        libpython                       \
50        libslang                        \
51        libslang-include-subdir         \
52        libtraceevent                   \
53        libtracefs                      \
54        libcrypto                       \
55        libunwind                       \
56        pthread-attr-setaffinity-np     \
57        pthread-barrier     		\
58        reallocarray                    \
59        stackprotector-all              \
60        timerfd                         \
61        zlib                            \
62        lzma                            \
63        get_cpuid                       \
64        bpf                             \
65        scandirat			\
66        sched_getcpu			\
67        sdt				\
68        setns				\
69        libaio				\
70        libzstd				\
71        disassembler-four-args		\
72        disassembler-init-styled	\
73        file-handle
74
75# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
76# of all feature tests
77FEATURE_TESTS_EXTRA :=                  \
78         bionic                         \
79         compile-32                     \
80         compile-x32                    \
81         cplus-demangle                 \
82         cxa-demangle                   \
83         gtk2                           \
84         gtk2-infobar                   \
85         hello                          \
86         libbabeltrace                  \
87         libcapstone                    \
88         libbfd-liberty                 \
89         libbfd-liberty-z               \
90         libopencsd                     \
91         libunwind-x86                  \
92         libunwind-x86_64               \
93         libunwind-arm                  \
94         libunwind-aarch64              \
95         libunwind-debug-frame          \
96         libunwind-debug-frame-arm      \
97         libunwind-debug-frame-aarch64  \
98         cxx                            \
99         llvm                           \
100         clang                          \
101         libbpf                         \
102         libbpf-btf__load_from_kernel_by_id \
103         libbpf-bpf_prog_load           \
104         libbpf-bpf_object__next_program \
105         libbpf-bpf_object__next_map    \
106         libbpf-bpf_program__set_insns  \
107         libbpf-bpf_create_map		\
108         libpfm4                        \
109         libdebuginfod			\
110         clang-bpf-co-re
111
112
113FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
114
115ifeq ($(FEATURE_TESTS),all)
116  FEATURE_TESTS := $(FEATURE_TESTS_BASIC) $(FEATURE_TESTS_EXTRA)
117endif
118
119FEATURE_DISPLAY ?=              \
120         libdw                  \
121         glibc                  \
122         libbfd                 \
123         libbfd-buildid		\
124         libcap                 \
125         libelf                 \
126         libnuma                \
127         numa_num_possible_cpus \
128         libperl                \
129         libpython              \
130         libcrypto              \
131         libunwind              \
132         libcapstone            \
133         llvm-perf              \
134         zlib                   \
135         lzma                   \
136         get_cpuid              \
137         bpf			\
138         libaio			\
139         libzstd
140
141#
142# Declare group members of a feature to display the logical OR of the detection
143# result instead of each member result.
144#
145FEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z
146
147#
148# Declare list of feature dependency packages that provide pkg-config files.
149#
150FEATURE_PKG_CONFIG ?=           \
151         libtraceevent          \
152         libtracefs
153
154feature_pkg_config = $(eval $(feature_pkg_config_code))
155define feature_pkg_config_code
156  FEATURE_CHECK_CFLAGS-$(1) := $(shell $(PKG_CONFIG) --cflags $(1) 2>/dev/null)
157  FEATURE_CHECK_LDFLAGS-$(1) := $(shell $(PKG_CONFIG) --libs $(1) 2>/dev/null)
158endef
159
160# Set FEATURE_CHECK_(C|LD)FLAGS-$(package) for packages using pkg-config.
161ifneq ($(PKG_CONFIG),)
162  $(foreach package,$(FEATURE_PKG_CONFIG),$(call feature_pkg_config,$(package)))
163endif
164
165# Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features.
166# If in the future we need per-feature checks/flags for features not
167# mentioned in this list we need to refactor this ;-).
168set_test_all_flags = $(eval $(set_test_all_flags_code))
169define set_test_all_flags_code
170  FEATURE_CHECK_CFLAGS-all  += $(FEATURE_CHECK_CFLAGS-$(1))
171  FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1))
172endef
173
174$(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat)))
175
176#
177# Special fast-path for the 'all features are available' case:
178#
179$(call feature_check,all,$(MSG))
180
181#
182# Just in case the build freshly failed, make sure we print the
183# feature matrix:
184#
185ifeq ($(feature-all), 1)
186  #
187  # test-all.c passed - just set all the core feature flags to 1:
188  #
189  $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat)))
190  #
191  # test-all.c does not comprise these tests, so we need to
192  # for this case to get features proper values
193  #
194  $(call feature_check,compile-32)
195  $(call feature_check,compile-x32)
196  $(call feature_check,bionic)
197  $(call feature_check,libbabeltrace)
198else
199  $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat)))
200endif
201
202#
203# Print the result of the feature test:
204#
205feature_print_status = $(eval $(feature_print_status_code))
206
207feature_group = $(eval $(feature_gen_group)) $(GROUP)
208
209define feature_gen_group
210  GROUP := $(1)
211  ifneq ($(feature_verbose),1)
212    GROUP += $(FEATURE_GROUP_MEMBERS-$(1))
213  endif
214endef
215
216define feature_print_status_code
217  ifneq (,$(filter 1,$(foreach feat,$(call feature_group,$(feat)),$(feature-$(feat)))))
218    MSG = $(shell printf '...%40s: [ \033[32mon\033[m  ]' $(1))
219  else
220    MSG = $(shell printf '...%40s: [ \033[31mOFF\033[m ]' $(1))
221  endif
222endef
223
224feature_print_text = $(eval $(feature_print_text_code))
225define feature_print_text_code
226    MSG = $(shell printf '...%40s: %s' $(1) $(2))
227endef
228
229#
230# generates feature value assignment for name, like:
231#   $(call feature_assign,libdw) == feature-libdw=1
232#
233feature_assign = feature-$(1)=$(feature-$(1))
234
235FEATURE_DUMP_FILENAME = $(OUTPUT)FEATURE-DUMP$(FEATURE_USER)
236FEATURE_DUMP := $(shell touch $(FEATURE_DUMP_FILENAME); cat $(FEATURE_DUMP_FILENAME))
237
238feature_dump_check = $(eval $(feature_dump_check_code))
239define feature_dump_check_code
240  ifeq ($(findstring $(1),$(FEATURE_DUMP)),)
241    $(2) := 1
242  endif
243endef
244
245#
246# First check if any test from FEATURE_DISPLAY
247# and set feature_display := 1 if it does
248$(foreach feat,$(FEATURE_DISPLAY),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_display))
249
250#
251# Now also check if any other test changed,
252# so we force FEATURE-DUMP generation
253$(foreach feat,$(FEATURE_TESTS),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_dump_changed))
254
255# The $(feature_display) controls the default detection message
256# output. It's set if:
257# - detected features differes from stored features from
258#   last build (in $(FEATURE_DUMP_FILENAME) file)
259# - one of the $(FEATURE_DISPLAY) is not detected
260# - VF is enabled
261
262ifeq ($(feature_dump_changed),1)
263  $(shell rm -f $(FEATURE_DUMP_FILENAME))
264  $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
265endif
266
267feature_display_check = $(eval $(feature_check_display_code))
268define feature_check_display_code
269  ifneq ($(feature-$(1)), 1)
270    feature_display := 1
271  endif
272endef
273
274$(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat)))
275
276ifeq ($(VF),1)
277  feature_display := 1
278  feature_verbose := 1
279endif
280
281ifneq ($(feature_verbose),1)
282  #
283  # Determine the features to omit from the displayed message, as only the
284  # logical OR of the detection result will be shown.
285  #
286  FEATURE_OMIT := $(foreach feat,$(FEATURE_DISPLAY),$(FEATURE_GROUP_MEMBERS-$(feat)))
287endif
288
289feature_display_entries = $(eval $(feature_display_entries_code))
290define feature_display_entries_code
291  ifeq ($(feature_display),1)
292    $$(info )
293    $$(info Auto-detecting system features:)
294    $(foreach feat,$(filter-out $(FEATURE_OMIT),$(FEATURE_DISPLAY)),$(call feature_print_status,$(feat),) $$(info $(MSG)))
295  endif
296
297  ifeq ($(feature_verbose),1)
298    $(eval TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS)))
299    $(foreach feat,$(TMP),$(call feature_print_status,$(feat),) $$(info $(MSG)))
300  endif
301endef
302
303ifeq ($(FEATURE_DISPLAY_DEFERRED),)
304  $(call feature_display_entries)
305  $(info )
306endif
307