Makefile.feature 10 KB

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