xref: /linux/tools/perf/tests/make (revision b02aa1d1343f580e5a43789b392e0c813bc1cba9)
1include ../scripts/Makefile.include
2
3ifndef MK
4ifeq ($(MAKECMDGOALS),)
5# no target specified, trigger the whole suite
6all:
7	@echo "Testing Makefile";      $(MAKE) -sf tests/make MK=Makefile
8	@echo "Testing Makefile.perf"; $(MAKE) -sf tests/make MK=Makefile.perf SET_PARALLEL=1 SET_O=1
9else
10# run only specific test over 'Makefile'
11%:
12	@echo "Testing Makefile";      $(MAKE) -sf tests/make MK=Makefile $@
13endif
14else
15PERF := .
16PERF_O := $(PERF)
17O_OPT :=
18FULL_O := $(shell readlink -f $(PERF_O) || echo $(PERF_O))
19
20ifneq ($(O),)
21  FULL_O := $(shell readlink -f $(O) || echo $(O))
22  PERF_O := $(FULL_O)
23  ifeq ($(SET_O),1)
24    O_OPT := 'O=$(FULL_O)'
25  endif
26  K_O_OPT := 'O=$(FULL_O)'
27endif
28
29PARALLEL_OPT=
30ifeq ($(SET_PARALLEL),1)
31  ifeq ($(findstring jobserver,$(MAKEFLAGS))$(findstring -j,$(MAKEFLAGS)),)
32    ifeq ($(JOBS),)
33      cores := $(shell (getconf _NPROCESSORS_ONLN || grep -E -c '^processor|^CPU[0-9]' /proc/cpuinfo) 2>/dev/null)
34      ifeq ($(cores),0)
35        cores := 1
36      endif
37    else
38      cores=$(JOBS)
39    endif
40    PARALLEL_OPT="-j$(cores)"
41  endif
42endif
43
44# As per kernel Makefile, avoid funny character set dependencies
45unexport LC_ALL
46LC_COLLATE=C
47LC_NUMERIC=C
48export LC_COLLATE LC_NUMERIC
49
50ifeq ($(srctree),)
51srctree := $(patsubst %/,%,$(dir $(CURDIR)))
52srctree := $(patsubst %/,%,$(dir $(srctree)))
53#$(info Determined 'srctree' to be $(srctree))
54endif
55
56include $(srctree)/tools/scripts/Makefile.arch
57
58# FIXME looks like x86 is the only arch running tests ;-)
59# we need some IS_(32/64) flag to make this generic
60ifeq ($(ARCH)$(IS_64_BIT), x861)
61lib = lib64
62else
63lib = lib
64endif
65
66has = $(shell which $1 2>/dev/null)
67python_perf_so := $(shell $(MAKE) python_perf_target|grep "Target is:"|awk '{print $$3}')
68
69# standard single make variable specified
70make_clean_all      := clean all
71make_python_perf_so := $(python_perf_so)
72make_debug          := DEBUG=1
73make_nondistro      := BUILD_NONDISTRO=1
74make_nondistro_no_demangle := BUILD_NONDISTRO=1 NO_DEMANGLE=1
75make_clang          := CC=clang
76make_clang_nondistro := CC=clang BUILD_NONDISTRO=1
77make_extra_tests    := EXTRA_TESTS=1
78make_no_jevents     := NO_JEVENTS=1
79make_jevents_all    := JEVENTS_ARCH=all
80make_no_bpf_skel    := BUILD_BPF_SKEL=0
81make_gen_vmlinux_h  := GEN_VMLINUX_H=1
82make_no_python_module := NO_PYTHON_MODULE=1
83make_no_scripts     := NO_PYTHON_MODULE=1
84make_no_slang       := NO_SLANG=1
85make_no_demangle    := NO_DEMANGLE=1
86make_no_libelf      := NO_LIBELF=1
87make_no_libdw       := NO_LIBDW=1
88make_libunwind      := LIBUNWIND=1
89make_no_backtrace   := NO_BACKTRACE=1
90make_no_libcapstone := NO_CAPSTONE=1
91make_libcapstone_dlopen := LIBCAPSTONE_DLOPEN=1
92make_no_libnuma     := NO_LIBNUMA=1
93make_no_libbionic   := NO_LIBBIONIC=1
94make_no_libbpf	    := NO_LIBBPF=1
95make_libbpf_dynamic := LIBBPF_DYNAMIC=1
96make_no_libbpf_DEBUG := NO_LIBBPF=1 DEBUG=1
97make_no_libllvm     := NO_LIBLLVM=1
98make_no_babeltrace2 := NO_BABELTRACE2=1
99make_with_coresight := CORESIGHT=1
100make_no_sdt	    := NO_SDT=1
101make_no_libpfm4     := NO_LIBPFM4=1
102make_with_gtk4      := GTK4=1
103make_refcnt_check   := EXTRA_CFLAGS="-DREFCNT_CHECKING=1"
104make_tags           := tags
105make_cscope         := cscope
106make_help           := help
107make_doc            := doc
108make_perf_o           := perf.o
109make_util_map_o       := util/map.o
110make_util_pmu_bison_o := util/pmu-bison.o
111make_install        := install
112make_install_bin    := install-bin
113make_install_doc    := install-doc
114make_install_man    := install-man
115make_install_html   := install-html
116make_install_info   := install-info
117make_install_pdf    := install-pdf
118make_install_prefix       := install prefix=/tmp/krava
119make_install_prefix_slash := install prefix=/tmp/krava/
120make_static         := LDFLAGS=-static NO_PERF_READ_VDSO32=1 NO_PERF_READ_VDSOX32=1 NO_JVMTI=1 NO_LIBTRACEEVENT=1 NO_LIBELF=1
121
122# all the NO_* variable combined
123make_minimal        := NO_PYTHON_MODULE=1
124make_minimal        += NO_DEMANGLE=1 NO_LIBELF=1 NO_BACKTRACE=1
125make_minimal        += NO_LIBNUMA=1 NO_LIBBIONIC=1 NO_LIBDW=1
126make_minimal        += NO_LIBBPF=1
127make_minimal        += NO_SDT=1 NO_JVMTI=1 NO_LIBZSTD=1
128make_minimal        += NO_CAPSTONE=1
129
130# binutils 2_42 and newer have bfd_thread_init()
131new_libbfd := $(shell echo '#include <bfd.h>' | $(CC) -E -x c - | grep bfd_thread_init)
132
133# Whether libLLVM can be built, which needs the llvm-devel/llvm-dev headers and
134# libraries of version 13 or newer, not just llvm-config. This mirrors the
135# llvm-perf feature test in tools/build/feature/test-llvm-perf.cpp so that
136# libLLVM is only asserted below when the default compiler can really build it.
137ifneq ($(call has,$(LLVM_CONFIG)),)
138have_libllvm := $(shell printf '#include <llvm/Support/ManagedStatic.h>\n#include <llvm/Support/raw_ostream.h>\n#if LLVM_VERSION_MAJOR < 13\n#error "llvm-devel/llvm-dev version 13 or greater is required"\n#endif\nint main(){llvm::errs()<<"";llvm::llvm_shutdown();return 0;}\n' | \
139	$(CXX) -x c++ -std=gnu++17 -I$(shell $(LLVM_CONFIG) --includedir 2>/dev/null) - -o /dev/null \
140		-L$(shell $(LLVM_CONFIG) --libdir 2>/dev/null) \
141		$(shell $(LLVM_CONFIG) --libs Core BPF 2>/dev/null) \
142		$(shell $(LLVM_CONFIG) --system-libs 2>/dev/null) >/dev/null 2>&1 && echo y)
143endif
144
145# $(run) contains all available tests
146run := make_pure
147# Targets 'clean all' can be run together only through top level
148# Makefile because we detect clean target in Makefile.perf and
149# disable features detection
150ifeq ($(MK),Makefile)
151run += make_clean_all
152MAKE_F := $(MAKE)
153else
154MAKE_F := $(MAKE) -f $(MK)
155endif
156run += make_python_perf_so
157run += make_debug
158ifneq ($(new_libbfd),)
159run += make_nondistro
160# Demangling with libbfd is only built when the C++ ABI's __cxa_demangle
161# isn't available, so it needs a build of its own to be compiled at all.
162run += make_nondistro_no_demangle
163endif
164# CXX is only set to clang++ by LLVM=1, so a CC=clang build has to cope with
165# a C compiler and a C++ compiler that don't match.
166ifneq ($(call has,clang),)
167run += make_clang
168ifneq ($(new_libbfd),)
169run += make_clang_nondistro
170endif
171endif
172run += make_extra_tests
173run += make_no_jevents
174run += make_jevents_all
175run += make_no_bpf_skel
176run += make_gen_vmlinux_h
177run += make_no_python_module
178run += make_no_scripts
179run += make_no_slang
180run += make_no_demangle
181run += make_no_libelf
182run += make_no_libdw
183run += make_libunwind
184run += make_no_backtrace
185run += make_no_libcapstone
186run += make_libcapstone_dlopen
187run += make_no_libnuma
188run += make_no_libbionic
189run += make_no_libbpf
190run += make_no_libbpf_DEBUG
191run += make_no_libllvm
192run += make_no_sdt
193run += make_no_babeltrace2
194run += make_with_coresight
195run += make_no_libpfm4
196run += make_refcnt_check
197run += make_help
198run += make_doc
199run += make_perf_o
200run += make_util_map_o
201run += make_util_pmu_bison_o
202run += make_install
203run += make_install_bin
204run += make_install_prefix
205run += make_install_prefix_slash
206# FIXME 'install-*' commented out till they're fixed
207# run += make_install_doc
208# run += make_install_man
209# run += make_install_html
210# run += make_install_info
211# run += make_install_pdf
212run += make_minimal
213
214old_libbpf := $(shell echo '\#include <bpf/libbpf.h>' | $(CC) -E -dM -x c -| grep -q -E "define[[:space:]]+LIBBPF_MAJOR_VERSION[[:space:]]+0{1}")
215
216ifneq ($(old_libbpf),)
217run += make_libbpf_dynamic
218endif
219
220ifneq ($(call has,ctags),)
221run += make_tags
222endif
223ifneq ($(call has,cscope),)
224run += make_cscope
225endif
226
227# $(run_O) contains same portion of $(run) tests with '_O' attached
228# to distinguish O=... tests
229run_O := $(addsuffix _O,$(run))
230
231# disable some tests for O=...
232run_O := $(filter-out make_python_perf_so_O,$(run_O))
233
234# define test for each compile as 'test_NAME' variable
235# with the test itself as a value
236test_make_tags   = test -f tags
237test_make_cscope = test -f cscope.out
238
239test_make_tags_O   := $(test_make_tags)
240test_make_cscope_O := $(test_make_cscope)
241
242test_ok          := true
243test_make_help   := $(test_ok)
244test_make_doc    := $(test_ok)
245test_make_help_O := $(test_ok)
246test_make_doc_O  := $(test_ok)
247
248test_make_python_perf_so := test -f $(PERF_O)/$(python_perf_so)
249
250test_make_perf_o           := test -f $(PERF_O)/perf.o
251test_make_util_map_o       := test -f $(PERF_O)/util/map.o
252test_make_util_pmu_bison_o := test -f $(PERF_O)/util/pmu-bison.o
253
254define test_dest_files
255  for file in $(1); do				\
256    if [ ! -f $$TMP_DEST/$$file ]; then		\
257      echo "  failed to find: $$file";		\
258    fi						\
259  done
260endef
261
262installed_files_bin := bin/perf
263installed_files_bin += etc/bash_completion.d/perf
264installed_files_bin += libexec/perf-core/perf-archive
265
266installed_files_all := $(installed_files_bin)
267
268test_make_install       := $(call test_dest_files,$(installed_files_all))
269test_make_install_O     := $(call test_dest_files,$(installed_files_all))
270test_make_install_bin   := $(call test_dest_files,$(installed_files_bin))
271test_make_install_bin_O := $(call test_dest_files,$(installed_files_bin))
272
273# We prefix all installed files for make_install_prefix(_slash)
274# with '/tmp/krava' to match installed/prefix-ed files.
275installed_files_all_prefix := $(addprefix /tmp/krava/,$(installed_files_all))
276test_make_install_prefix   :=  $(call test_dest_files,$(installed_files_all_prefix))
277test_make_install_prefix_O :=  $(call test_dest_files,$(installed_files_all_prefix))
278
279test_make_install_prefix_slash   := $(test_make_install_prefix)
280test_make_install_prefix_slash_O := $(test_make_install_prefix_O)
281
282# FIXME nothing gets installed
283test_make_install_man    := test -f $$TMP_DEST/share/man/man1/perf.1
284test_make_install_man_O  := $(test_make_install_man)
285
286# FIXME nothing gets installed
287test_make_install_doc    := $(test_ok)
288test_make_install_doc_O  := $(test_ok)
289
290# FIXME nothing gets installed
291test_make_install_html   := $(test_ok)
292test_make_install_html_O := $(test_ok)
293
294# FIXME nothing gets installed
295test_make_install_info   := $(test_ok)
296test_make_install_info_O := $(test_ok)
297
298# FIXME nothing gets installed
299test_make_install_pdf    := $(test_ok)
300test_make_install_pdf_O  := $(test_ok)
301
302test_make_libbpf_dynamic :=   ldd $(PERF_O)/perf | grep -q libbpf
303test_make_libbpf_dynamic_O := ldd $$TMP_O/perf | grep -q libbpf
304
305# Feature tests are allowed to fail, so a broken C++ compiler command line
306# doesn't fail the build, it just quietly turns off everything that needs a
307# C++ compiler. Check a feature that does, otherwise these tests would pass
308# while producing a perf with less in it than the default build has.
309ifneq ($(have_libllvm),)
310test_make_clang               := test -x $(PERF_O)/perf && $(PERF_O)/perf check -q feature libLLVM
311test_make_clang_O             := test -x $$TMP_O/perf && $$TMP_O/perf check -q feature libLLVM
312test_make_clang_nondistro     := $(test_make_clang)
313test_make_clang_nondistro_O   := $(test_make_clang_O)
314endif
315
316test_make_python_perf_so_O    := test -f $$TMP_O/python/perf.so
317test_make_perf_o_O            := test -f $$TMP_O/perf.o
318test_make_util_map_o_O        := test -f $$TMP_O/util/map.o
319test_make_util_pmu_bison_o_O := test -f $$TMP_O/util/pmu-bison.o
320
321test_default = test -x $(PERF_O)/perf
322test = $(if $(test_$1),$(test_$1),$(test_default))
323
324test_default_O = test -x $$TMP_O/perf
325test_O = $(if $(test_$1),$(test_$1),$(test_default_O))
326
327all:
328
329ifdef SHUF
330run := $(shell shuf -e $(run))
331run_O := $(shell shuf -e $(run_O))
332endif
333
334max_width := $(shell echo $(run_O) | sed 's/ /\n/g' | wc -L)
335
336ifdef DEBUG
337d := $(info run   $(run))
338d := $(info run_O $(run_O))
339endif
340
341MAKEFLAGS := --no-print-directory
342
343clean := @(cd $(PERF); $(MAKE_F) -s $(O_OPT) clean >/dev/null && $(MAKE) -s $(O_OPT) -C ../build clean >/dev/null)
344
345$(run):
346	$(call clean)
347	+@TMP_DEST=$$(mktemp -d); \
348	trap 'rm -rf $@ $$TMP_DEST; exit 130' INT TERM; \
349	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
350	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
351	( eval $$cmd ) >> $@ 2>&1; \
352	echo "  test: $(call test,$@)" >> $@ 2>&1; \
353	$(call test,$@) && \
354	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
355
356make_with_gtk4:
357	$(call clean)
358	+@TMP_DEST=$$(mktemp -d); \
359	trap 'rm -rf $@ $$TMP_DEST; exit 130' INT TERM; \
360	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
361	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
362	( eval $$cmd ) >> $@ 2>&1; \
363	echo "  test: $(call test,$@)" >> $@ 2>&1; \
364	$(call test,$@) && \
365	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
366
367make_static:
368	$(call clean)
369	+@TMP_DEST=$$(mktemp -d); \
370	trap 'rm -rf $@ $$TMP_DEST; exit 130' INT TERM; \
371	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
372	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
373	( eval $$cmd ) >> $@ 2>&1; \
374	echo "  test: $(call test,$@)" >> $@ 2>&1; \
375	$(call test,$@) && \
376	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
377
378$(run_O):
379ifneq ($(O_CLEAN),0)
380	$(call clean)
381endif
382	+@TMP_O=$$(mktemp -d); \
383	TMP_DEST=$$(mktemp -d); \
384	trap 'rm -rf $@ $$TMP_O $$TMP_DEST; exit 130' INT TERM; \
385	cmd="cd $(PERF) && $(MAKE_F) $($(patsubst %_O,%,$@)) $(PARALLEL_OPT) O=$$TMP_O DESTDIR=$$TMP_DEST"; \
386	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
387	( eval $$cmd ) >> $@ 2>&1 && \
388	echo "  test: $(call test_O,$@)" >> $@ 2>&1; \
389	$(call test_O,$@) && \
390	rm -rf $@ $$TMP_O $$TMP_DEST || (cat $@ ; false)
391
392tarpkg:
393	@cmd="$(PERF)/tests/perf-targz-src-pkg $(PERF)"; \
394	echo "- $@: $$cmd" && echo $$cmd > $@ && \
395	( eval $$cmd ) >> $@ 2>&1 && \
396	rm -f $@
397
398KERNEL_O := ../..
399ifneq ($(O),)
400  KERNEL_O := $(O)
401endif
402
403make_kernelsrc:
404	@echo "- make -C <kernelsrc> $(PARALLEL_OPT) $(K_O_OPT) tools/perf"
405	$(call clean); \
406	($(MAKE) -C ../.. $(PARALLEL_OPT) $(K_O_OPT) tools/perf) > $@ 2>&1 && \
407	test -x $(KERNEL_O)/tools/perf/perf && rm -f $@ || (cat $@ ; false)
408
409make_kernelsrc_tools:
410	@echo "- make -C <kernelsrc>/tools $(PARALLEL_OPT) $(K_O_OPT) perf"
411	$(call clean); \
412	($(MAKE) -C ../../tools $(PARALLEL_OPT) $(K_O_OPT) perf) > $@ 2>&1 && \
413	test -x $(KERNEL_O)/tools/perf/perf && rm -f $@ || (cat $@ ; false)
414
415make_libperf:
416	@echo "- make -C lib";
417	$(MAKE) -C lib clean >$@ 2>&1; $(MAKE) -C lib >>$@ 2>&1 && rm $@
418
419FEATURES_DUMP_FILE := $(FULL_O)/BUILD_TEST_FEATURE_DUMP
420FEATURES_DUMP_FILE_STATIC := $(FULL_O)/BUILD_TEST_FEATURE_DUMP_STATIC
421
422all: $(run) $(run_O) tarpkg make_kernelsrc make_kernelsrc_tools
423	@echo OK
424	@rm -f $(FEATURES_DUMP_FILE) $(FEATURES_DUMP_FILE_STATIC)
425
426out:
427	$(call clean)
428	@$(MAKE) -f tests/make $(run_O) PARALLEL_OPT= O_CLEAN=0 $(if $(findstring jobserver,$(MAKEFLAGS))$(findstring -j,$(MAKEFLAGS)),,$(if $(cores),-j$(cores)))
429	@echo OK
430	@rm -f $(FEATURES_DUMP_FILE) $(FEATURES_DUMP_FILE_STATIC)
431
432ifeq ($(REUSE_FEATURES_DUMP),1)
433$(FEATURES_DUMP_FILE):
434	$(call clean)
435	@cmd="cd $(PERF) && $(MAKE) FEATURE_DUMP_COPY=$@ $(O_OPT) feature-dump"; \
436	echo "- $@: $$cmd" && echo $$cmd && \
437	( eval $$cmd ) > /dev/null 2>&1
438
439$(FEATURES_DUMP_FILE_STATIC):
440	$(call clean)
441	@cmd="cd $(PERF) && $(MAKE) FEATURE_DUMP_COPY=$@ $(O_OPT) LDFLAGS='-static' feature-dump"; \
442	echo "- $@: $$cmd" && echo $$cmd && \
443	( eval $$cmd ) > /dev/null 2>&1
444
445# The clang tests exist to run feature detection with a compiler other than
446# the default one, so they have to do their own and are left out of both the
447# dependency and the 'FEATURES_DUMP=' append below.
448no_features_dump := make_clang make_clang_nondistro
449no_features_dump += $(addsuffix _O,$(no_features_dump))
450
451# Add feature dump dependency for run/run_O targets
452$(foreach t,$(filter-out $(no_features_dump),$(run) $(run_O)),$(eval \
453	$(t): $(if $(findstring make_static,$(t)),\
454		$(FEATURES_DUMP_FILE_STATIC),\
455		$(FEATURES_DUMP_FILE))))
456
457# Append 'FEATURES_DUMP=' option to all test cases. For example:
458# make_no_libbpf: NO_LIBBPF=1  --> NO_LIBBPF=1 FEATURES_DUMP=/a/b/BUILD_TEST_FEATURE_DUMP
459# make_static: LDFLAGS=-static --> LDFLAGS=-static FEATURES_DUMP=/a/b/BUILD_TEST_FEATURE_DUMP_STATIC
460$(foreach t,$(filter-out $(no_features_dump),$(run)),$(if $(findstring make_static,$(t)),\
461			$(eval $(t) := $($(t)) FEATURES_DUMP=$(FEATURES_DUMP_FILE_STATIC)),\
462			$(eval $(t) := $($(t)) FEATURES_DUMP=$(FEATURES_DUMP_FILE))))
463endif
464
465.PHONY: all $(run) $(run_O) tarpkg clean make_kernelsrc make_kernelsrc_tools make_libperf
466endif # ifndef MK
467
468# Serialize execution of tasks writing to shared in-tree files
469make_tags_O: | make_clean_all_O
470make_cscope_O: | make_clean_all_O
471