xref: /linux/tools/build/Makefile.feature (revision 8dc364fa484df22d301a16148d1b356931d0cbb0)
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#
31# These + the ones in FEATURE_TESTS_EXTRA are included in
32# tools/build/feature/test-all.c and we try to build it all together
33# then setting all those features to '1' meaning they are all enabled.
34#
35# There are things like fortify-source that will be set to 1 because test-all
36# is built with the flags needed to test if its enabled, resulting in
37#
38#   $ rm -rf /tmp/b ; mkdir /tmp/b ; make -C tools/perf O=/tmp/b feature-dump
39#   $ grep fortify-source /tmp/b/FEATURE-DUMP
40#   feature-fortify-source=1
41#   $
42#
43#   All the others should have lines in tools/build/feature/test-all.c like:
44#
45#    #define main main_test_disassembler_init_styled
46#    # include "test-disassembler-init-styled.c"
47#    #undef main
48#
49#    #define main main_test_libzstd
50#    # include "test-libzstd.c"
51#    #undef main
52#
53#    int main(int argc, char *argv[])
54#    {
55#      main_test_disassembler_four_args();
56#      main_test_libzstd();
57#      return 0;
58#    }
59#
60#    If the sample above works, then we end up with these lines in the FEATURE-DUMP
61#    file:
62#
63#    feature-disassembler-four-args=1
64#    feature-libzstd=1
65#
66FEATURE_TESTS_BASIC :=                  \
67        backtrace                       \
68        libdw                           \
69        eventfd                         \
70        fortify-source                  \
71        get_current_dir_name            \
72        gettid				\
73        glibc                           \
74        libbfd                          \
75        libbfd-buildid			\
76        libelf                          \
77        libelf-getphdrnum               \
78        libelf-gelf_getnote             \
79        libelf-getshdrstrndx            \
80        libelf-zstd                     \
81        libnuma                         \
82        numa_num_possible_cpus          \
83        libpython                       \
84        libslang                        \
85        libtraceevent                   \
86        libcpupower                     \
87        pthread-attr-setaffinity-np     \
88        pthread-barrier     		\
89        reallocarray                    \
90        stackprotector-all              \
91        timerfd                         \
92        zlib                            \
93        lzma                            \
94        get_cpuid                       \
95        bpf                             \
96        scandirat			\
97        sched_getcpu			\
98        sdt				\
99        setns				\
100        libaio				\
101        libzstd				\
102        disassembler-four-args		\
103        disassembler-init-styled	\
104        file-handle
105
106# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
107# of all feature tests
108FEATURE_TESTS_EXTRA :=                  \
109         bionic                         \
110         compile-32                     \
111         compile-x32                    \
112         cplus-demangle                 \
113         cxa-demangle                   \
114         gtk2                           \
115         gtk2-infobar                   \
116         hello                          \
117         libbabeltrace                  \
118         libcapstone                    \
119         libbfd-liberty                 \
120         libbfd-liberty-z               \
121         libopencsd                     \
122         libperl                        \
123         cxx                            \
124         llvm                           \
125         clang                          \
126         libbpf                         \
127         libbpf-strings                 \
128         libpfm4                        \
129         libdebuginfod			\
130         clang-bpf-co-re		\
131         bpftool-skeletons
132
133
134FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
135
136ifeq ($(FEATURE_TESTS),all)
137  FEATURE_TESTS := $(FEATURE_TESTS_BASIC) $(FEATURE_TESTS_EXTRA)
138endif
139
140FEATURE_DISPLAY ?=              \
141         libdw                  \
142         glibc                  \
143         libelf                 \
144         libnuma                \
145         numa_num_possible_cpus \
146         libpython              \
147         libcapstone            \
148         llvm-perf              \
149         zlib                   \
150         lzma                   \
151         get_cpuid              \
152         bpf			\
153         libaio			\
154         libzstd
155
156#
157# Declare group members of a feature to display the logical OR of the detection
158# result instead of each member result.
159#
160FEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z
161
162#
163# Declare list of feature dependency packages that provide pkg-config files.
164#
165FEATURE_PKG_CONFIG ?=           \
166         libtraceevent          \
167         libtracefs
168
169feature_pkg_config = $(eval $(feature_pkg_config_code))
170define feature_pkg_config_code
171  FEATURE_CHECK_CFLAGS-$(1) := $(shell $(PKG_CONFIG) --cflags $(1) 2>/dev/null)
172  FEATURE_CHECK_LDFLAGS-$(1) := $(shell $(PKG_CONFIG) --libs $(1) 2>/dev/null)
173endef
174
175# Set FEATURE_CHECK_(C|LD)FLAGS-$(package) for packages using pkg-config.
176ifneq ($(PKG_CONFIG),)
177  $(foreach package,$(FEATURE_PKG_CONFIG),$(call feature_pkg_config,$(package)))
178endif
179
180# Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features.
181# If in the future we need per-feature checks/flags for features not
182# mentioned in this list we need to refactor this ;-).
183set_test_all_flags = $(eval $(set_test_all_flags_code))
184define set_test_all_flags_code
185  FEATURE_CHECK_CFLAGS-all  += $(FEATURE_CHECK_CFLAGS-$(1))
186  FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1))
187endef
188
189$(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat)))
190
191#
192# Special fast-path for the 'all features are available' case:
193#
194$(call feature_check,all,$(MSG))
195
196#
197# Just in case the build freshly failed, make sure we print the
198# feature matrix:
199#
200ifeq ($(feature-all), 1)
201  #
202  # test-all.c passed - just set all the core feature flags to 1:
203  #
204  $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat)))
205  #
206  # test-all.c does not comprise these tests, so we need to
207  # for this case to get features proper values
208  #
209  $(call feature_check,compile-32)
210  $(call feature_check,compile-x32)
211  $(call feature_check,bionic)
212  $(call feature_check,libbabeltrace)
213else
214  $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat)))
215endif
216
217#
218# Print the result of the feature test:
219#
220feature_print_status = $(eval $(feature_print_status_code))
221
222feature_group = $(eval $(feature_gen_group)) $(GROUP)
223
224define feature_gen_group
225  GROUP := $(1)
226  ifneq ($(feature_verbose),1)
227    GROUP += $(FEATURE_GROUP_MEMBERS-$(1))
228  endif
229endef
230
231define feature_print_status_code
232  ifneq (,$(filter 1,$(foreach feat,$(call feature_group,$(feat)),$(feature-$(feat)))))
233    MSG = $(shell printf '...%40s: [ \033[32mon\033[m  ]' $(1))
234  else
235    MSG = $(shell printf '...%40s: [ \033[31mOFF\033[m ]' $(1))
236  endif
237endef
238
239feature_print_text = $(eval $(feature_print_text_code))
240define feature_print_text_code
241    MSG = $(shell printf '...%40s: %s' $(1) $(2))
242endef
243
244#
245# generates feature value assignment for name, like:
246#   $(call feature_assign,libdw) == feature-libdw=1
247#
248feature_assign = feature-$(1)=$(feature-$(1))
249
250FEATURE_DUMP_FILENAME = $(OUTPUT)FEATURE-DUMP$(FEATURE_USER)
251FEATURE_DUMP := $(shell touch $(FEATURE_DUMP_FILENAME); cat $(FEATURE_DUMP_FILENAME))
252
253feature_dump_check = $(eval $(feature_dump_check_code))
254define feature_dump_check_code
255  ifeq ($(findstring $(1),$(FEATURE_DUMP)),)
256    $(2) := 1
257  endif
258endef
259
260#
261# First check if any test from FEATURE_DISPLAY
262# and set feature_display := 1 if it does
263$(foreach feat,$(FEATURE_DISPLAY),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_display))
264
265#
266# Now also check if any other test changed,
267# so we force FEATURE-DUMP generation
268$(foreach feat,$(FEATURE_TESTS),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_dump_changed))
269
270# The $(feature_display) controls the default detection message
271# output. It's set if:
272# - detected features differes from stored features from
273#   last build (in $(FEATURE_DUMP_FILENAME) file)
274# - one of the $(FEATURE_DISPLAY) is not detected
275# - VF is enabled
276
277ifeq ($(feature_dump_changed),1)
278  $(shell rm -f $(FEATURE_DUMP_FILENAME))
279  $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
280endif
281
282feature_display_check = $(eval $(feature_check_display_code))
283define feature_check_display_code
284  ifneq ($(feature-$(1)), 1)
285    feature_display := 1
286  endif
287endef
288
289$(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat)))
290
291ifeq ($(VF),1)
292  feature_display := 1
293  feature_verbose := 1
294endif
295
296ifneq ($(feature_verbose),1)
297  #
298  # Determine the features to omit from the displayed message, as only the
299  # logical OR of the detection result will be shown.
300  #
301  FEATURE_OMIT := $(foreach feat,$(FEATURE_DISPLAY),$(FEATURE_GROUP_MEMBERS-$(feat)))
302endif
303
304feature_display_entries = $(eval $(feature_display_entries_code))
305define feature_display_entries_code
306  ifeq ($(feature_display),1)
307    $$(info )
308    $$(info Auto-detecting system features:)
309    $(foreach feat,$(filter-out $(FEATURE_OMIT),$(FEATURE_DISPLAY)),$(call feature_print_status,$(feat),) $$(info $(MSG)))
310  endif
311
312  ifeq ($(feature_verbose),1)
313    $(eval TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS)))
314    $(foreach feat,$(TMP),$(call feature_print_status,$(feat),) $$(info $(MSG)))
315  endif
316endef
317
318ifeq ($(FEATURE_DISPLAY_DEFERRED),)
319  $(call feature_display_entries)
320  $(info )
321endif
322