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