1ec8f24b7SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only 2e6c76d62SJiri Olsafeature_dir := $(srctree)/tools/build/feature 3e6c76d62SJiri Olsa 4e6c76d62SJiri Olsaifneq ($(OUTPUT),) 5e6c76d62SJiri Olsa OUTPUT_FEATURES = $(OUTPUT)feature/ 6e6c76d62SJiri Olsa $(shell mkdir -p $(OUTPUT_FEATURES)) 7e6c76d62SJiri Olsaendif 8e6c76d62SJiri Olsa 9e6c76d62SJiri Olsafeature_check = $(eval $(feature_check_code)) 10e6c76d62SJiri Olsadefine feature_check_code 11fa5c8931SDaniel Díaz 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) 12e6c76d62SJiri Olsaendef 13e6c76d62SJiri Olsa 14e6c76d62SJiri Olsafeature_set = $(eval $(feature_set_code)) 15e6c76d62SJiri Olsadefine feature_set_code 16e6c76d62SJiri Olsa feature-$(1) := 1 17e6c76d62SJiri Olsaendef 18e6c76d62SJiri Olsa 19e6c76d62SJiri Olsa# 20e6c76d62SJiri Olsa# Build the feature check binaries in parallel, ignore errors, ignore return value and suppress output: 21e6c76d62SJiri Olsa# 22e6c76d62SJiri Olsa 23e6c76d62SJiri Olsa# 24e6c76d62SJiri Olsa# Note that this is not a complete list of all feature tests, just 25e6c76d62SJiri Olsa# those that are typically built on a fully configured system. 26e6c76d62SJiri Olsa# 27e6c76d62SJiri Olsa# [ Feature tests not mentioned here have to be built explicitly in 28e6c76d62SJiri Olsa# the rule that uses them - an example for that is the 'bionic' 29e6c76d62SJiri Olsa# feature check. ] 30e6c76d62SJiri Olsa# 3120ed5325SArnaldo Carvalho de Melo# These + the ones in FEATURE_TESTS_EXTRA are included in 3220ed5325SArnaldo Carvalho de Melo# tools/build/feature/test-all.c and we try to build it all together 3320ed5325SArnaldo Carvalho de Melo# then setting all those features to '1' meaning they are all enabled. 3420ed5325SArnaldo Carvalho de Melo# 3520ed5325SArnaldo Carvalho de Melo# There are things like fortify-source that will be set to 1 because test-all 3620ed5325SArnaldo Carvalho de Melo# is built with the flags needed to test if its enabled, resulting in 3720ed5325SArnaldo Carvalho de Melo# 3820ed5325SArnaldo Carvalho de Melo# $ rm -rf /tmp/b ; mkdir /tmp/b ; make -C tools/perf O=/tmp/b feature-dump 3920ed5325SArnaldo Carvalho de Melo# $ grep fortify-source /tmp/b/FEATURE-DUMP 4020ed5325SArnaldo Carvalho de Melo# feature-fortify-source=1 4120ed5325SArnaldo Carvalho de Melo# $ 4220ed5325SArnaldo Carvalho de Melo# 4320ed5325SArnaldo Carvalho de Melo# All the others should have lines in tools/build/feature/test-all.c like: 4420ed5325SArnaldo Carvalho de Melo# 4520ed5325SArnaldo Carvalho de Melo# #define main main_test_disassembler_init_styled 4620ed5325SArnaldo Carvalho de Melo# # include "test-disassembler-init-styled.c" 4720ed5325SArnaldo Carvalho de Melo# #undef main 4820ed5325SArnaldo Carvalho de Melo# 4920ed5325SArnaldo Carvalho de Melo# #define main main_test_libzstd 5020ed5325SArnaldo Carvalho de Melo# # include "test-libzstd.c" 5120ed5325SArnaldo Carvalho de Melo# #undef main 5220ed5325SArnaldo Carvalho de Melo# 5320ed5325SArnaldo Carvalho de Melo# int main(int argc, char *argv[]) 5420ed5325SArnaldo Carvalho de Melo# { 5520ed5325SArnaldo Carvalho de Melo# main_test_disassembler_four_args(); 5620ed5325SArnaldo Carvalho de Melo# main_test_libzstd(); 5720ed5325SArnaldo Carvalho de Melo# return 0; 5820ed5325SArnaldo Carvalho de Melo# } 5920ed5325SArnaldo Carvalho de Melo# 6020ed5325SArnaldo Carvalho de Melo# If the sample above works, then we end up with these lines in the FEATURE-DUMP 6120ed5325SArnaldo Carvalho de Melo# file: 6220ed5325SArnaldo Carvalho de Melo# 6320ed5325SArnaldo Carvalho de Melo# feature-disassembler-four-args=1 6420ed5325SArnaldo Carvalho de Melo# feature-libzstd=1 6520ed5325SArnaldo Carvalho de Melo# 669fd4186aSWang NanFEATURE_TESTS_BASIC := \ 67e6c76d62SJiri Olsa backtrace \ 687c943261SIan Rogers libdw \ 6911c6cbe7SArnaldo Carvalho de Melo eventfd \ 70e6c76d62SJiri Olsa fortify-source \ 714541a8bbSArnaldo Carvalho de Melo gettid \ 72e6c76d62SJiri Olsa glibc \ 73e6c76d62SJiri Olsa libbfd \ 74cff602f6SJames Clark libbfd-threadsafe \ 75e6c76d62SJiri Olsa libelf \ 76e6c76d62SJiri Olsa libelf-getphdrnum \ 771c1a3a47SArnaldo Carvalho de Melo libelf-gelf_getnote \ 782492c465SArnaldo Carvalho de Melo libelf-getshdrstrndx \ 79d557814cSLeo Yan libelf-zstd \ 80e6c76d62SJiri Olsa libnuma \ 81f8ac8606SArnaldo Carvalho de Melo numa_num_possible_cpus \ 82e6c76d62SJiri Olsa libpython \ 83e6c76d62SJiri Olsa libslang \ 8456d32d4cSMichael Petlan libtraceevent \ 850f59a6c9STomas Glozar libcpupower \ 86e6c76d62SJiri Olsa pthread-attr-setaffinity-np \ 8725ab5abfSArnaldo Carvalho de Melo pthread-barrier \ 88db42a21aSJakub Kicinski reallocarray \ 89e6c76d62SJiri Olsa stackprotector-all \ 90e6c76d62SJiri Olsa timerfd \ 916c6f0f61SJiri Olsa zlib \ 92b0063dbfSArnaldo Carvalho de Melo lzma \ 93e26e63beSMasami Hiramatsu bpf \ 949e03608eSArnaldo Carvalho de Melo scandirat \ 95120010cbSArnaldo Carvalho de Melo sched_getcpu \ 9686bcdb5aSArnaldo Carvalho de Melo sdt \ 97aa6292f4SMathieu Poirier setns \ 988a1b1718SSong Liu libaio \ 993b1c5d96SAlexey Budankov libzstd \ 10049f550eaSNamhyung Kim disassembler-four-args \ 101cfd59ca9SAndres Freund disassembler-init-styled \ 10276785231SNamhyung Kim file-handle \ 1033f5dfa47SArnaldo Carvalho de Melo libopenssl 104e6c76d62SJiri Olsa 1059fd4186aSWang Nan# FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list 1069fd4186aSWang Nan# of all feature tests 1076d13d53bSIan Rogers 1086d13d53bSIan RogersLIBUNWIND_ARCHS:=aarch64 arm loongarch64 mips ppc32 ppc64 riscv s390x x86 x86_64 1096d13d53bSIan Rogers 1109fd4186aSWang NanFEATURE_TESTS_EXTRA := \ 1119fd4186aSWang Nan bionic \ 1129fd4186aSWang Nan compile-32 \ 1139fd4186aSWang Nan compile-x32 \ 1149fd4186aSWang Nan cplus-demangle \ 1154c72e2b3SIan Rogers cxa-demangle \ 1164751bdddSArnaldo Carvalho de Melo gtk2 \ 1174751bdddSArnaldo Carvalho de Melo gtk2-infobar \ 1189fd4186aSWang Nan hello \ 119*536e81f0SMichael Jeanson babeltrace2-ctf-writer \ 1208b767db3SChangbin Du libcapstone \ 121153e211fSCosta Shulyupin libcheck \ 12214541b1eSStanislav Fomichev libbfd-liberty \ 12314541b1eSStanislav Fomichev libbfd-liberty-z \ 1241c3b28fdSArnaldo Carvalho de Melo libopencsd \ 1255519b691SIan Rogers libperl \ 126bd476684SJiri Olsa llvm \ 1275ef86146SStephane Eranian libbpf \ 128c7a14fdcSFrank Ch. Eigler libpfm4 \ 129fbcdaa19SSong Liu libdebuginfod \ 1308a635c38STomas Glozar clang-bpf-co-re \ 1316d13d53bSIan Rogers bpftool-skeletons \ 1326d13d53bSIan Rogers libunwind \ 1336d13d53bSIan Rogers libunwind-debug-frame \ 1346d13d53bSIan Rogers $(foreach arch,$(LIBUNWIND_ARCHS),libunwind-$(arch) libunwind-debug-frame-$(arch)) 135fbcdaa19SSong Liu 1369fd4186aSWang Nan 1379fd4186aSWang NanFEATURE_TESTS ?= $(FEATURE_TESTS_BASIC) 1389fd4186aSWang Nan 1399fd4186aSWang Nanifeq ($(FEATURE_TESTS),all) 1409fd4186aSWang Nan FEATURE_TESTS := $(FEATURE_TESTS_BASIC) $(FEATURE_TESTS_EXTRA) 1419fd4186aSWang Nanendif 1429fd4186aSWang Nan 1438135c8c7SWang NanFEATURE_DISPLAY ?= \ 1447c943261SIan Rogers libdw \ 145e6c76d62SJiri Olsa glibc \ 146e6c76d62SJiri Olsa libelf \ 147e6c76d62SJiri Olsa libnuma \ 148f8ac8606SArnaldo Carvalho de Melo numa_num_possible_cpus \ 149e6c76d62SJiri Olsa libpython \ 1508b767db3SChangbin Du libcapstone \ 151206dcfcaSJames Clark llvm-perf \ 1526c6f0f61SJiri Olsa zlib \ 153b0063dbfSArnaldo Carvalho de Melo lzma \ 1542a07d814SAlexey Budankov bpf \ 1558a1b1718SSong Liu libaio \ 15676785231SNamhyung Kim libzstd \ 1576a32fa5cSDmitrii Dolgov libopenssl \ 1586a32fa5cSDmitrii Dolgov rust 159e6c76d62SJiri Olsa 16074ef1cc9SRoberto Sassu# 16174ef1cc9SRoberto Sassu# Declare group members of a feature to display the logical OR of the detection 16274ef1cc9SRoberto Sassu# result instead of each member result. 16374ef1cc9SRoberto Sassu# 16474ef1cc9SRoberto SassuFEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z 16574ef1cc9SRoberto Sassu 1668f61e98aSGuilherme Amadio# 1678f61e98aSGuilherme Amadio# Declare list of feature dependency packages that provide pkg-config files. 1688f61e98aSGuilherme Amadio# 1698f61e98aSGuilherme AmadioFEATURE_PKG_CONFIG ?= \ 170*536e81f0SMichael Jeanson babeltrace2-ctf-writer \ 1718f61e98aSGuilherme Amadio libtraceevent \ 1728f61e98aSGuilherme Amadio libtracefs 1738f61e98aSGuilherme Amadio 1748f61e98aSGuilherme Amadiofeature_pkg_config = $(eval $(feature_pkg_config_code)) 1758f61e98aSGuilherme Amadiodefine feature_pkg_config_code 1768f61e98aSGuilherme Amadio FEATURE_CHECK_CFLAGS-$(1) := $(shell $(PKG_CONFIG) --cflags $(1) 2>/dev/null) 1778f61e98aSGuilherme Amadio FEATURE_CHECK_LDFLAGS-$(1) := $(shell $(PKG_CONFIG) --libs $(1) 2>/dev/null) 1788f61e98aSGuilherme Amadioendef 1798f61e98aSGuilherme Amadio 1808f61e98aSGuilherme Amadio# Set FEATURE_CHECK_(C|LD)FLAGS-$(package) for packages using pkg-config. 1818f61e98aSGuilherme Amadioifneq ($(PKG_CONFIG),) 1828f61e98aSGuilherme Amadio $(foreach package,$(FEATURE_PKG_CONFIG),$(call feature_pkg_config,$(package))) 1838f61e98aSGuilherme Amadioendif 1848f61e98aSGuilherme Amadio 185153e211fSCosta ShulyupinFEATURE_CHECK_LDFLAGS-libcheck = -lcheck 186153e211fSCosta Shulyupin 187e6c76d62SJiri Olsa# Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. 188e6c76d62SJiri Olsa# If in the future we need per-feature checks/flags for features not 189e6c76d62SJiri Olsa# mentioned in this list we need to refactor this ;-). 190e6c76d62SJiri Olsaset_test_all_flags = $(eval $(set_test_all_flags_code)) 191e6c76d62SJiri Olsadefine set_test_all_flags_code 192e6c76d62SJiri Olsa FEATURE_CHECK_CFLAGS-all += $(FEATURE_CHECK_CFLAGS-$(1)) 193e6c76d62SJiri Olsa FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1)) 194e6c76d62SJiri Olsaendef 195e6c76d62SJiri Olsa 196e6c76d62SJiri Olsa$(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat))) 197e6c76d62SJiri Olsa 198e6c76d62SJiri Olsa# 199e6c76d62SJiri Olsa# Special fast-path for the 'all features are available' case: 200e6c76d62SJiri Olsa# 201e6c76d62SJiri Olsa$(call feature_check,all,$(MSG)) 202e6c76d62SJiri Olsa 203e6c76d62SJiri Olsa# 204e6c76d62SJiri Olsa# Just in case the build freshly failed, make sure we print the 205e6c76d62SJiri Olsa# feature matrix: 206e6c76d62SJiri Olsa# 207e6c76d62SJiri Olsaifeq ($(feature-all), 1) 208e6c76d62SJiri Olsa # 209e6c76d62SJiri Olsa # test-all.c passed - just set all the core feature flags to 1: 210e6c76d62SJiri Olsa # 211e6c76d62SJiri Olsa $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat))) 21276c4aaecSJiri Olsa # 21376c4aaecSJiri Olsa # test-all.c does not comprise these tests, so we need to 21476c4aaecSJiri Olsa # for this case to get features proper values 21576c4aaecSJiri Olsa # 21676c4aaecSJiri Olsa $(call feature_check,compile-32) 21776c4aaecSJiri Olsa $(call feature_check,compile-x32) 21876c4aaecSJiri Olsa $(call feature_check,bionic) 219*536e81f0SMichael Jeanson $(call feature_check,babeltrace2-ctf-writer) 2206d13d53bSIan Rogers $(call feature_check,libunwind) 2216d13d53bSIan Rogers $(call feature_check,libunwind-debug-frame) 2226d13d53bSIan Rogers $(foreach arch,$(LIBUNWIND_ARCHS),$(call feature_check,libunwind-$(arch))) 2236d13d53bSIan Rogers $(foreach arch,$(LIBUNWIND_ARCHS),$(call feature_check,libunwind-debug-frame-$(arch))) 224e6c76d62SJiri Olsaelse 225e6c76d62SJiri Olsa $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat))) 226e6c76d62SJiri Olsaendif 227e6c76d62SJiri Olsa 228e6c76d62SJiri Olsa# 229e6c76d62SJiri Olsa# Print the result of the feature test: 230e6c76d62SJiri Olsa# 231709533e5SRoberto Sassufeature_print_status = $(eval $(feature_print_status_code)) 232e6c76d62SJiri Olsa 23374ef1cc9SRoberto Sassufeature_group = $(eval $(feature_gen_group)) $(GROUP) 23474ef1cc9SRoberto Sassu 23574ef1cc9SRoberto Sassudefine feature_gen_group 23674ef1cc9SRoberto Sassu GROUP := $(1) 23774ef1cc9SRoberto Sassu ifneq ($(feature_verbose),1) 23874ef1cc9SRoberto Sassu GROUP += $(FEATURE_GROUP_MEMBERS-$(1)) 23974ef1cc9SRoberto Sassu endif 24074ef1cc9SRoberto Sassuendef 24174ef1cc9SRoberto Sassu 242e6c76d62SJiri Olsadefine feature_print_status_code 24374ef1cc9SRoberto Sassu ifneq (,$(filter 1,$(foreach feat,$(call feature_group,$(feat)),$(feature-$(feat))))) 24474da7697SRoberto Sassu MSG = $(shell printf '...%40s: [ \033[32mon\033[m ]' $(1)) 245e6c76d62SJiri Olsa else 24674da7697SRoberto Sassu MSG = $(shell printf '...%40s: [ \033[31mOFF\033[m ]' $(1)) 247e6c76d62SJiri Olsa endif 248e6c76d62SJiri Olsaendef 249e6c76d62SJiri Olsa 250709533e5SRoberto Sassufeature_print_text = $(eval $(feature_print_text_code)) 251e6c76d62SJiri Olsadefine feature_print_text_code 25274da7697SRoberto Sassu MSG = $(shell printf '...%40s: %s' $(1) $(2)) 253e6c76d62SJiri Olsaendef 254e6c76d62SJiri Olsa 255c6a5f88fSJiri Olsa# 256c6a5f88fSJiri Olsa# generates feature value assignment for name, like: 2577c943261SIan Rogers# $(call feature_assign,libdw) == feature-libdw=1 258c6a5f88fSJiri Olsa# 259c6a5f88fSJiri Olsafeature_assign = feature-$(1)=$(feature-$(1)) 260c6a5f88fSJiri Olsa 26113e96db6SArnaldo Carvalho de MeloFEATURE_DUMP_FILENAME = $(OUTPUT)FEATURE-DUMP$(FEATURE_USER) 262936d120dSJiri OlsaFEATURE_DUMP := $(shell touch $(FEATURE_DUMP_FILENAME); cat $(FEATURE_DUMP_FILENAME)) 263936d120dSJiri Olsa 264936d120dSJiri Olsafeature_dump_check = $(eval $(feature_dump_check_code)) 265936d120dSJiri Olsadefine feature_dump_check_code 266936d120dSJiri Olsa ifeq ($(findstring $(1),$(FEATURE_DUMP)),) 267936d120dSJiri Olsa $(2) := 1 268936d120dSJiri Olsa endif 269936d120dSJiri Olsaendef 270936d120dSJiri Olsa 271936d120dSJiri Olsa# 272936d120dSJiri Olsa# First check if any test from FEATURE_DISPLAY 273936d120dSJiri Olsa# and set feature_display := 1 if it does 274936d120dSJiri Olsa$(foreach feat,$(FEATURE_DISPLAY),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_display)) 275936d120dSJiri Olsa 276936d120dSJiri Olsa# 277936d120dSJiri Olsa# Now also check if any other test changed, 278936d120dSJiri Olsa# so we force FEATURE-DUMP generation 279936d120dSJiri Olsa$(foreach feat,$(FEATURE_TESTS),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_dump_changed)) 280e6c76d62SJiri Olsa 281e6c76d62SJiri Olsa# The $(feature_display) controls the default detection message 282e6c76d62SJiri Olsa# output. It's set if: 283e6c76d62SJiri Olsa# - detected features differes from stored features from 28413e96db6SArnaldo Carvalho de Melo# last build (in $(FEATURE_DUMP_FILENAME) file) 285e6c76d62SJiri Olsa# - one of the $(FEATURE_DISPLAY) is not detected 286e6c76d62SJiri Olsa# - VF is enabled 287e6c76d62SJiri Olsa 288936d120dSJiri Olsaifeq ($(feature_dump_changed),1) 289936d120dSJiri Olsa $(shell rm -f $(FEATURE_DUMP_FILENAME)) 290936d120dSJiri Olsa $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME))) 291e6c76d62SJiri Olsaendif 292e6c76d62SJiri Olsa 2936076e2a4SArnaldo Carvalho de Melofeature_display_check = $(eval $(feature_check_display_code)) 294d0018b49SJiri Olsadefine feature_check_display_code 295e6c76d62SJiri Olsa ifneq ($(feature-$(1)), 1) 296e6c76d62SJiri Olsa feature_display := 1 297e6c76d62SJiri Olsa endif 298e6c76d62SJiri Olsaendef 299e6c76d62SJiri Olsa 300e6c76d62SJiri Olsa$(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat))) 301e6c76d62SJiri Olsa 302e6c76d62SJiri Olsaifeq ($(VF),1) 303e6c76d62SJiri Olsa feature_display := 1 304e6c76d62SJiri Olsa feature_verbose := 1 305e6c76d62SJiri Olsaendif 306e6c76d62SJiri Olsa 30774ef1cc9SRoberto Sassuifneq ($(feature_verbose),1) 30874ef1cc9SRoberto Sassu # 30974ef1cc9SRoberto Sassu # Determine the features to omit from the displayed message, as only the 31074ef1cc9SRoberto Sassu # logical OR of the detection result will be shown. 31174ef1cc9SRoberto Sassu # 31274ef1cc9SRoberto Sassu FEATURE_OMIT := $(foreach feat,$(FEATURE_DISPLAY),$(FEATURE_GROUP_MEMBERS-$(feat))) 31374ef1cc9SRoberto Sassuendif 31474ef1cc9SRoberto Sassu 31519177bc3SArnaldo Carvalho de Melofeature_display_entries = $(eval $(feature_display_entries_code)) 31619177bc3SArnaldo Carvalho de Melodefine feature_display_entries_code 317e6c76d62SJiri Olsa ifeq ($(feature_display),1) 318709533e5SRoberto Sassu $$(info ) 319709533e5SRoberto Sassu $$(info Auto-detecting system features:) 32074ef1cc9SRoberto Sassu $(foreach feat,$(filter-out $(FEATURE_OMIT),$(FEATURE_DISPLAY)),$(call feature_print_status,$(feat),) $$(info $(MSG))) 321e6c76d62SJiri Olsa endif 322e6c76d62SJiri Olsa 323e6c76d62SJiri Olsa ifeq ($(feature_verbose),1) 324709533e5SRoberto Sassu $(eval TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS))) 325709533e5SRoberto Sassu $(foreach feat,$(TMP),$(call feature_print_status,$(feat),) $$(info $(MSG))) 326e6c76d62SJiri Olsa endif 32719177bc3SArnaldo Carvalho de Meloendef 32819177bc3SArnaldo Carvalho de Melo 32919177bc3SArnaldo Carvalho de Meloifeq ($(FEATURE_DISPLAY_DEFERRED),) 33019177bc3SArnaldo Carvalho de Melo $(call feature_display_entries) 331f348a44cSAlexandre Chartre ifeq ($(feature_display),1) 332709533e5SRoberto Sassu $(info ) 33319177bc3SArnaldo Carvalho de Melo endif 334f348a44cSAlexandre Chartreendif 335