Makefile.feature 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. feature_dir := $(srctree)/tools/build/feature
  3. ifneq ($(OUTPUT),)
  4. OUTPUT_FEATURES = $(OUTPUT)feature/
  5. $(shell mkdir -p $(OUTPUT_FEATURES))
  6. endif
  7. feature_check = $(eval $(feature_check_code))
  8. define feature_check_code
  9. 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)
  10. endef
  11. feature_set = $(eval $(feature_set_code))
  12. define feature_set_code
  13. feature-$(1) := 1
  14. endef
  15. #
  16. # Build the feature check binaries in parallel, ignore errors, ignore return value and suppress output:
  17. #
  18. #
  19. # Note that this is not a complete list of all feature tests, just
  20. # those that are typically built on a fully configured system.
  21. #
  22. # [ Feature tests not mentioned here have to be built explicitly in
  23. # the rule that uses them - an example for that is the 'bionic'
  24. # feature check. ]
  25. #
  26. FEATURE_TESTS_BASIC := \
  27. backtrace \
  28. dwarf \
  29. dwarf_getlocations \
  30. eventfd \
  31. fortify-source \
  32. get_current_dir_name \
  33. gettid \
  34. glibc \
  35. libbfd \
  36. libbfd-buildid \
  37. libcap \
  38. libelf \
  39. libelf-getphdrnum \
  40. libelf-gelf_getnote \
  41. libelf-getshdrstrndx \
  42. libnuma \
  43. numa_num_possible_cpus \
  44. libperl \
  45. libpython \
  46. libslang \
  47. libslang-include-subdir \
  48. libtraceevent \
  49. libtracefs \
  50. libcrypto \
  51. libunwind \
  52. pthread-attr-setaffinity-np \
  53. pthread-barrier \
  54. reallocarray \
  55. stackprotector-all \
  56. timerfd \
  57. libdw-dwarf-unwind \
  58. zlib \
  59. lzma \
  60. get_cpuid \
  61. bpf \
  62. sched_getcpu \
  63. sdt \
  64. setns \
  65. libaio \
  66. libzstd \
  67. disassembler-four-args \
  68. disassembler-init-styled \
  69. file-handle
  70. # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
  71. # of all feature tests
  72. FEATURE_TESTS_EXTRA := \
  73. bionic \
  74. compile-32 \
  75. compile-x32 \
  76. cplus-demangle \
  77. gtk2 \
  78. gtk2-infobar \
  79. hello \
  80. libbabeltrace \
  81. libbfd-liberty \
  82. libbfd-liberty-z \
  83. libopencsd \
  84. libunwind-x86 \
  85. libunwind-x86_64 \
  86. libunwind-arm \
  87. libunwind-aarch64 \
  88. libunwind-debug-frame \
  89. libunwind-debug-frame-arm \
  90. libunwind-debug-frame-aarch64 \
  91. cxx \
  92. llvm \
  93. llvm-version \
  94. clang \
  95. libbpf \
  96. libbpf-btf__load_from_kernel_by_id \
  97. libbpf-bpf_prog_load \
  98. libbpf-bpf_object__next_program \
  99. libbpf-bpf_object__next_map \
  100. libbpf-bpf_program__set_insns \
  101. libbpf-bpf_create_map \
  102. libpfm4 \
  103. libdebuginfod \
  104. clang-bpf-co-re
  105. FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
  106. ifeq ($(FEATURE_TESTS),all)
  107. FEATURE_TESTS := $(FEATURE_TESTS_BASIC) $(FEATURE_TESTS_EXTRA)
  108. endif
  109. FEATURE_DISPLAY ?= \
  110. dwarf \
  111. dwarf_getlocations \
  112. glibc \
  113. libbfd \
  114. libbfd-buildid \
  115. libcap \
  116. libelf \
  117. libnuma \
  118. numa_num_possible_cpus \
  119. libperl \
  120. libpython \
  121. libcrypto \
  122. libunwind \
  123. libdw-dwarf-unwind \
  124. zlib \
  125. lzma \
  126. get_cpuid \
  127. bpf \
  128. libaio \
  129. libzstd
  130. #
  131. # Declare group members of a feature to display the logical OR of the detection
  132. # result instead of each member result.
  133. #
  134. FEATURE_GROUP_MEMBERS-libbfd = libbfd-liberty libbfd-liberty-z
  135. # Set FEATURE_CHECK_(C|LD)FLAGS-all for all FEATURE_TESTS features.
  136. # If in the future we need per-feature checks/flags for features not
  137. # mentioned in this list we need to refactor this ;-).
  138. set_test_all_flags = $(eval $(set_test_all_flags_code))
  139. define set_test_all_flags_code
  140. FEATURE_CHECK_CFLAGS-all += $(FEATURE_CHECK_CFLAGS-$(1))
  141. FEATURE_CHECK_LDFLAGS-all += $(FEATURE_CHECK_LDFLAGS-$(1))
  142. endef
  143. $(foreach feat,$(FEATURE_TESTS),$(call set_test_all_flags,$(feat)))
  144. #
  145. # Special fast-path for the 'all features are available' case:
  146. #
  147. $(call feature_check,all,$(MSG))
  148. #
  149. # Just in case the build freshly failed, make sure we print the
  150. # feature matrix:
  151. #
  152. ifeq ($(feature-all), 1)
  153. #
  154. # test-all.c passed - just set all the core feature flags to 1:
  155. #
  156. $(foreach feat,$(FEATURE_TESTS),$(call feature_set,$(feat)))
  157. #
  158. # test-all.c does not comprise these tests, so we need to
  159. # for this case to get features proper values
  160. #
  161. $(call feature_check,compile-32)
  162. $(call feature_check,compile-x32)
  163. $(call feature_check,bionic)
  164. $(call feature_check,libbabeltrace)
  165. else
  166. $(foreach feat,$(FEATURE_TESTS),$(call feature_check,$(feat)))
  167. endif
  168. #
  169. # Print the result of the feature test:
  170. #
  171. feature_print_status = $(eval $(feature_print_status_code))
  172. feature_group = $(eval $(feature_gen_group)) $(GROUP)
  173. define feature_gen_group
  174. GROUP := $(1)
  175. ifneq ($(feature_verbose),1)
  176. GROUP += $(FEATURE_GROUP_MEMBERS-$(1))
  177. endif
  178. endef
  179. define feature_print_status_code
  180. ifneq (,$(filter 1,$(foreach feat,$(call feature_group,$(feat)),$(feature-$(feat)))))
  181. MSG = $(shell printf '...%40s: [ \033[32mon\033[m ]' $(1))
  182. else
  183. MSG = $(shell printf '...%40s: [ \033[31mOFF\033[m ]' $(1))
  184. endif
  185. endef
  186. feature_print_text = $(eval $(feature_print_text_code))
  187. define feature_print_text_code
  188. MSG = $(shell printf '...%40s: %s' $(1) $(2))
  189. endef
  190. #
  191. # generates feature value assignment for name, like:
  192. # $(call feature_assign,dwarf) == feature-dwarf=1
  193. #
  194. feature_assign = feature-$(1)=$(feature-$(1))
  195. FEATURE_DUMP_FILENAME = $(OUTPUT)FEATURE-DUMP$(FEATURE_USER)
  196. FEATURE_DUMP := $(shell touch $(FEATURE_DUMP_FILENAME); cat $(FEATURE_DUMP_FILENAME))
  197. feature_dump_check = $(eval $(feature_dump_check_code))
  198. define feature_dump_check_code
  199. ifeq ($(findstring $(1),$(FEATURE_DUMP)),)
  200. $(2) := 1
  201. endif
  202. endef
  203. #
  204. # First check if any test from FEATURE_DISPLAY
  205. # and set feature_display := 1 if it does
  206. $(foreach feat,$(FEATURE_DISPLAY),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_display))
  207. #
  208. # Now also check if any other test changed,
  209. # so we force FEATURE-DUMP generation
  210. $(foreach feat,$(FEATURE_TESTS),$(call feature_dump_check,$(call feature_assign,$(feat)),feature_dump_changed))
  211. # The $(feature_display) controls the default detection message
  212. # output. It's set if:
  213. # - detected features differes from stored features from
  214. # last build (in $(FEATURE_DUMP_FILENAME) file)
  215. # - one of the $(FEATURE_DISPLAY) is not detected
  216. # - VF is enabled
  217. ifeq ($(feature_dump_changed),1)
  218. $(shell rm -f $(FEATURE_DUMP_FILENAME))
  219. $(foreach feat,$(FEATURE_TESTS),$(shell echo "$(call feature_assign,$(feat))" >> $(FEATURE_DUMP_FILENAME)))
  220. endif
  221. feature_display_check = $(eval $(feature_check_display_code))
  222. define feature_check_display_code
  223. ifneq ($(feature-$(1)), 1)
  224. feature_display := 1
  225. endif
  226. endef
  227. $(foreach feat,$(FEATURE_DISPLAY),$(call feature_display_check,$(feat)))
  228. ifeq ($(VF),1)
  229. feature_display := 1
  230. feature_verbose := 1
  231. endif
  232. ifneq ($(feature_verbose),1)
  233. #
  234. # Determine the features to omit from the displayed message, as only the
  235. # logical OR of the detection result will be shown.
  236. #
  237. FEATURE_OMIT := $(foreach feat,$(FEATURE_DISPLAY),$(FEATURE_GROUP_MEMBERS-$(feat)))
  238. endif
  239. feature_display_entries = $(eval $(feature_display_entries_code))
  240. define feature_display_entries_code
  241. ifeq ($(feature_display),1)
  242. $$(info )
  243. $$(info Auto-detecting system features:)
  244. $(foreach feat,$(filter-out $(FEATURE_OMIT),$(FEATURE_DISPLAY)),$(call feature_print_status,$(feat),) $$(info $(MSG)))
  245. endif
  246. ifeq ($(feature_verbose),1)
  247. $(eval TMP := $(filter-out $(FEATURE_DISPLAY),$(FEATURE_TESTS)))
  248. $(foreach feat,$(TMP),$(call feature_print_status,$(feat),) $$(info $(MSG)))
  249. endif
  250. endef
  251. ifeq ($(FEATURE_DISPLAY_DEFERRED),)
  252. $(call feature_display_entries)
  253. $(info )
  254. endif