| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | feature_dir := $(srctree)/tools/build/feature |
| 3 | |
| 4 | ifneq ($(OUTPUT),) |
| 5 | OUTPUT_FEATURES = $(OUTPUT)feature/ |
| 6 | $(shell mkdir -p $(OUTPUT_FEATURES)) |
| 7 | endif |
| 8 | |
| 9 | feature_check = $(eval $(feature_check_code)) |
| 10 | define 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) |
| 12 | endef |
| 13 | |
| 14 | feature_set = $(eval $(feature_set_code)) |
| 15 | define feature_set_code |
| 16 | feature-$(1) := 1 |
| 17 | endef |
| 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 | # |
| 66 | FEATURE_TESTS_BASIC := \ |
| 67 | backtrace \ |
| 68 | libdw \ |
| 69 | eventfd \ |
| 70 | fortify-source \ |
| 71 | gettid \ |
| 72 | glibc \ |
| 73 | libbfd \ |
| 74 | libbfd-threadsafe \ |
| 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 | libopenssl |
| 104 | |
| 105 | # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list |
| 106 | # of all feature tests |
| 107 | |
| 108 | LIBUNWIND_ARCHS:=aarch64 arm loongarch64 mips ppc32 ppc64 riscv s390x x86 x86_64 |
| 109 | |
| 110 | FEATURE_TESTS_EXTRA := \ |
| 111 | bionic \ |
| 112 | compile-32 \ |
| 113 | compile-x32 \ |
| 114 | cplus-demangle \ |
| 115 | cxa-demangle \ |
| 116 | gtk2 \ |
| 117 | gtk2-infobar \ |
| 118 | hello \ |
| 119 | babeltrace2-ctf-writer \ |
| 120 | libcapstone \ |
| 121 | libcheck \ |
| 122 | libbfd-liberty \ |
| 123 | libbfd-liberty-z \ |
| 124 | libopencsd \ |
| 125 | libperl \ |
| 126 | llvm \ |
| 127 | libbpf \ |
| 128 | libpfm4 \ |
| 129 | libdebuginfod \ |
| 130 | clang-bpf-co-re \ |
| 131 | bpftool-skeletons \ |
| 132 | libunwind \ |
| 133 | libunwind-debug-frame \ |
| 134 | $(foreach arch,$(LIBUNWIND_ARCHS),libunwind-$(arch) libunwind-debug-frame-$(arch)) |
| 135 | |
| 136 | |
| 137 | FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC) |
| 138 | |
| 139 | ifeq ($(FEATURE_TESTS),all) |
| 140 | FEATURE_TESTS := $(FEATURE_TESTS_BASIC) $(FEATURE_TESTS_EXTRA) |
| 141 | endif |
| 142 | |
| 143 | FEATURE_DISPLAY ?= \ |
| 144 | libdw \ |
| 145 | glibc \ |
| 146 | libelf \ |
| 147 | libnuma \ |
| 148 | numa_num_possible_cpus \ |
| 149 | libpython \ |
| 150 | libcapstone \ |
| 151 | llvm-perf \ |
| 152 | zlib \ |
| 153 | lzma \ |
| 154 | bpf \ |
| 155 | libaio \ |
| 156 | libzstd \ |
| 157 | libopenssl \ |
| 158 | rust |
| 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 | # |
| 164 | FEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z |
| 165 | |
| 166 | # |
| 167 | # Declare list of feature dependency packages that provide pkg-config files. |
| 168 | # |
| 169 | FEATURE_PKG_CONFIG ?= \ |
| 170 | babeltrace2-ctf-writer \ |
| 171 | libtraceevent \ |
| 172 | libtracefs |
| 173 | |
| 174 | feature_pkg_config = $(eval $(feature_pkg_config_code)) |
| 175 | define feature_pkg_config_code |
| 176 | FEATURE_CHECK_CFLAGS-$(1) := $(shell $(PKG_CONFIG) --cflags $(1) 2>/dev/null) |
| 177 | FEATURE_CHECK_LDFLAGS-$(1) := $(shell $(PKG_CONFIG) --libs $(1) 2>/dev/null) |
| 178 | endef |
| 179 | |
| 180 | # Set FEATURE_CHECK_(C|LD)FLAGS-$(package) for packages using pkg-config. |
| 181 | ifneq ($(PKG_CONFIG),) |
| 182 | $(foreach package,$(FEATURE_PKG_CONFIG),$(call feature_pkg_config,$(package))) |
| 183 | endif |
| 184 | |
| 185 | FEATURE_CHECK_LDFLAGS-libcheck = -lcheck |
| 186 | |
| 187 | # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features. |
| 188 | # If in the future we need per-feature checks/flags for features not |
| 189 | # mentioned in this list we need to refactor this ;-). |
| 190 | set_test_all_flags = $(eval $(set_test_all_flags_code)) |
| 191 | define set_test_all_flags_code |
| 192 | FEATURE_CHECK_CFLAGS-all += $(FEATURE_CHECK_CFLAGS-$(1)) |
| 193 | FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1)) |
| 194 | endef |
| 195 | |
| 196 | $(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat))) |
| 197 | |
| 198 | # |
| 199 | # Special fast-path for the 'all features are available' case: |
| 200 | # |
| 201 | $(call feature_check,all,$(MSG)) |
| 202 | |
| 203 | # |
| 204 | # Just in case the build freshly failed, make sure we print the |
| 205 | # feature matrix: |
| 206 | # |
| 207 | ifeq ($(feature-all), 1) |
| 208 | # |
| 209 | # test-all.c passed - just set all the core feature flags to 1: |
| 210 | # |
| 211 | $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat))) |
| 212 | # |
| 213 | # test-all.c does not comprise these tests, so we need to |
| 214 | # for this case to get features proper values |
| 215 | # |
| 216 | $(call feature_check,compile-32) |
| 217 | $(call feature_check,compile-x32) |
| 218 | $(call feature_check,bionic) |
| 219 | $(call feature_check,babeltrace2-ctf-writer) |
| 220 | $(call feature_check,libunwind) |
| 221 | $(call feature_check,libunwind-debug-frame) |
| 222 | $(foreach arch,$(LIBUNWIND_ARCHS),$(call feature_check,libunwind-$(arch))) |
| 223 | $(foreach arch,$(LIBUNWIND_ARCHS),$(call feature_check,libunwind-debug-frame-$(arch))) |
| 224 | else |
| 225 | $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat))) |
| 226 | endif |
| 227 | |
| 228 | # |
| 229 | # Print the result of the feature test: |
| 230 | # |
| 231 | feature_print_status = $(eval $(feature_print_status_code)) |
| 232 | |
| 233 | feature_group = $(eval $(feature_gen_group)) $(GROUP) |
| 234 | |
| 235 | define feature_gen_group |
| 236 | GROUP := $(1) |
| 237 | ifneq ($(feature_verbose),1) |
| 238 | GROUP += $(FEATURE_GROUP_MEMBERS-$(1)) |
| 239 | endif |
| 240 | endef |
| 241 | |
| 242 | define feature_print_status_code |
| 243 | ifneq (,$(filter 1,$(foreach feat,$(call feature_group,$(feat)),$(feature-$(feat))))) |
| 244 | MSG = $(shell printf '...%40s: [ \033[32mon\033[m ]' $(1)) |
| 245 | else |
| 246 | MSG = $(shell printf '...%40s: [ \033[31mOFF\033[m ]' $(1)) |
| 247 | endif |
| 248 | endef |
| 249 | |
| 250 | feature_print_text = $(eval $(feature_print_text_code)) |
| 251 | define feature_print_text_code |
| 252 | MSG = $(shell printf '...%40s: %s' $(1) $(2)) |
| 253 | endef |
| 254 | |
| 255 | # |
| 256 | # generates feature value assignment for name, like: |
| 257 | # $(call feature_assign,libdw) == feature-libdw=1 |
| 258 | # |
| 259 | feature_assign = feature-$(1)=$(feature-$(1)) |
| 260 | |
| 261 | FEATURE_DUMP_FILENAME = $(OUTPUT)FEATURE-DUMP$(FEATURE_USER) |
| 262 | FEATURE_DUMP := $(shell touch $(FEATURE_DUMP_FILENAME); cat $(FEATURE_DUMP_FILENAME)) |
| 263 | |
| 264 | feature_dump_check = $(eval $(feature_dump_check_code)) |
| 265 | define feature_dump_check_code |
| 266 | ifeq ($(findstring $(1),$(FEATURE_DUMP)),) |
| 267 | $(2) := 1 |
| 268 | endif |
| 269 | endef |
| 270 | |
| 271 | # |
| 272 | # First check if any test from FEATURE_DISPLAY |
| 273 | # and set feature_display := 1 if it does |
| 274 | $(foreach feat,$(FEATURE_DISPLAY),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_display)) |
| 275 | |
| 276 | # |
| 277 | # Now also check if any other test changed, |
| 278 | # so we force FEATURE-DUMP generation |
| 279 | $(foreach feat,$(FEATURE_TESTS),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_dump_changed)) |
| 280 | |
| 281 | # The $(feature_display) controls the default detection message |
| 282 | # output. It's set if: |
| 283 | # - detected features differes from stored features from |
| 284 | # last build (in $(FEATURE_DUMP_FILENAME) file) |
| 285 | # - one of the $(FEATURE_DISPLAY) is not detected |
| 286 | # - VF is enabled |
| 287 | |
| 288 | ifeq ($(feature_dump_changed),1) |
| 289 | $(shell rm -f $(FEATURE_DUMP_FILENAME)) |
| 290 | $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME))) |
| 291 | endif |
| 292 | |
| 293 | feature_display_check = $(eval $(feature_check_display_code)) |
| 294 | define feature_check_display_code |
| 295 | ifneq ($(feature-$(1)), 1) |
| 296 | feature_display := 1 |
| 297 | endif |
| 298 | endef |
| 299 | |
| 300 | $(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat))) |
| 301 | |
| 302 | ifeq ($(VF),1) |
| 303 | feature_display := 1 |
| 304 | feature_verbose := 1 |
| 305 | endif |
| 306 | |
| 307 | ifneq ($(feature_verbose),1) |
| 308 | # |
| 309 | # Determine the features to omit from the displayed message, as only the |
| 310 | # logical OR of the detection result will be shown. |
| 311 | # |
| 312 | FEATURE_OMIT := $(foreach feat,$(FEATURE_DISPLAY),$(FEATURE_GROUP_MEMBERS-$(feat))) |
| 313 | endif |
| 314 | |
| 315 | feature_display_entries = $(eval $(feature_display_entries_code)) |
| 316 | define feature_display_entries_code |
| 317 | ifeq ($(feature_display),1) |
| 318 | $$(info ) |
| 319 | $$(info Auto-detecting system features:) |
| 320 | $(foreach feat,$(filter-out $(FEATURE_OMIT),$(FEATURE_DISPLAY)),$(call feature_print_status,$(feat),) $$(info $(MSG))) |
| 321 | endif |
| 322 | |
| 323 | ifeq ($(feature_verbose),1) |
| 324 | $(eval TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS))) |
| 325 | $(foreach feat,$(TMP),$(call feature_print_status,$(feat),) $$(info $(MSG))) |
| 326 | endif |
| 327 | endef |
| 328 | |
| 329 | ifeq ($(FEATURE_DISPLAY_DEFERRED),) |
| 330 | $(call feature_display_entries) |
| 331 | ifeq ($(feature_display),1) |
| 332 | $(info ) |
| 333 | endif |
| 334 | endif |