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