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