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