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 ($(JOBS),)
32    cores := $(shell (getconf _NPROCESSORS_ONLN || grep -E -c '^processor|^CPU[0-9]' /proc/cpuinfo) 2>/dev/null)
33    ifeq ($(cores),0)
34      cores := 1
35    endif
36  else
37    cores=$(JOBS)
38  endif
39  PARALLEL_OPT="-j$(cores)"
40endif
41
42# As per kernel Makefile, avoid funny character set dependencies
43unexport LC_ALL
44LC_COLLATE=C
45LC_NUMERIC=C
46export LC_COLLATE LC_NUMERIC
47
48ifeq ($(srctree),)
49srctree := $(patsubst %/,%,$(dir $(CURDIR)))
50srctree := $(patsubst %/,%,$(dir $(srctree)))
51#$(info Determined 'srctree' to be $(srctree))
52endif
53
54include $(srctree)/tools/scripts/Makefile.arch
55
56# FIXME looks like x86 is the only arch running tests ;-)
57# we need some IS_(32/64) flag to make this generic
58ifeq ($(ARCH)$(IS_64_BIT), x861)
59lib = lib64
60else
61lib = lib
62endif
63
64has = $(shell which $1 2>/dev/null)
65python_perf_so := $(shell $(MAKE) python_perf_target|grep "Target is:"|awk '{print $$3}')
66
67# standard single make variable specified
68make_clean_all      := clean all
69make_python_perf_so := $(python_perf_so)
70make_debug          := DEBUG=1
71make_nondistro      := BUILD_NONDISTRO=1
72make_extra_tests    := EXTRA_TESTS=1
73make_no_bpf_skel    := BUILD_BPF_SKEL=0
74make_gen_vmlinux_h  := GEN_VMLINUX_H=1
75make_no_libperl     := NO_LIBPERL=1
76make_no_libpython   := NO_LIBPYTHON=1
77make_no_scripts     := NO_LIBPYTHON=1 NO_LIBPERL=1
78make_no_slang       := NO_SLANG=1
79make_no_gtk2        := NO_GTK2=1
80make_no_ui          := NO_SLANG=1 NO_GTK2=1
81make_no_demangle    := NO_DEMANGLE=1
82make_no_libelf      := NO_LIBELF=1
83make_no_libunwind   := NO_LIBUNWIND=1
84make_no_libdw_dwarf_unwind := NO_LIBDW_DWARF_UNWIND=1
85make_no_backtrace   := NO_BACKTRACE=1
86make_no_libcapstone := NO_CAPSTONE=1
87make_no_libnuma     := NO_LIBNUMA=1
88make_no_libaudit    := NO_LIBAUDIT=1
89make_no_libbionic   := NO_LIBBIONIC=1
90make_no_auxtrace    := NO_AUXTRACE=1
91make_no_libbpf	    := NO_LIBBPF=1
92make_libbpf_dynamic := LIBBPF_DYNAMIC=1
93make_no_libbpf_DEBUG := NO_LIBBPF=1 DEBUG=1
94make_no_libcrypto   := NO_LIBCRYPTO=1
95make_with_babeltrace:= LIBBABELTRACE=1
96make_with_coresight := CORESIGHT=1
97make_no_sdt	    := NO_SDT=1
98make_no_syscall_tbl := NO_SYSCALL_TABLE=1
99make_no_libpfm4     := NO_LIBPFM4=1
100make_with_gtk2      := GTK2=1
101make_refcnt_check   := EXTRA_CFLAGS="-DREFCNT_CHECKING=1"
102make_tags           := tags
103make_cscope         := cscope
104make_help           := help
105make_doc            := doc
106make_perf_o           := perf.o
107make_util_map_o       := util/map.o
108make_util_pmu_bison_o := util/pmu-bison.o
109make_install        := install
110make_install_bin    := install-bin
111make_install_doc    := install-doc
112make_install_man    := install-man
113make_install_html   := install-html
114make_install_info   := install-info
115make_install_pdf    := install-pdf
116make_install_prefix       := install prefix=/tmp/krava
117make_install_prefix_slash := install prefix=/tmp/krava/
118make_static         := LDFLAGS=-static NO_PERF_READ_VDSO32=1 NO_PERF_READ_VDSOX32=1 NO_JVMTI=1 NO_LIBTRACEEVENT=1 NO_LIBELF=1
119
120# all the NO_* variable combined
121make_minimal        := NO_LIBPERL=1 NO_LIBPYTHON=1 NO_GTK2=1
122make_minimal        += NO_DEMANGLE=1 NO_LIBELF=1 NO_LIBUNWIND=1 NO_BACKTRACE=1
123make_minimal        += NO_LIBNUMA=1 NO_LIBAUDIT=1 NO_LIBBIONIC=1
124make_minimal        += NO_LIBDW_DWARF_UNWIND=1 NO_AUXTRACE=1 NO_LIBBPF=1
125make_minimal        += NO_LIBCRYPTO=1 NO_SDT=1 NO_JVMTI=1 NO_LIBZSTD=1
126make_minimal        += NO_LIBCAP=1 NO_SYSCALL_TABLE=1 NO_CAPSTONE=1
127
128# $(run) contains all available tests
129run := make_pure
130# Targets 'clean all' can be run together only through top level
131# Makefile because we detect clean target in Makefile.perf and
132# disable features detection
133ifeq ($(MK),Makefile)
134run += make_clean_all
135MAKE_F := $(MAKE)
136else
137MAKE_F := $(MAKE) -f $(MK)
138endif
139run += make_python_perf_so
140run += make_debug
141run += make_nondistro
142run += make_extra_tests
143run += make_no_bpf_skel
144run += make_gen_vmlinux_h
145run += make_no_libperl
146run += make_no_libpython
147run += make_no_scripts
148run += make_no_slang
149run += make_no_gtk2
150run += make_no_ui
151run += make_no_demangle
152run += make_no_libelf
153run += make_no_libunwind
154run += make_no_libdw_dwarf_unwind
155run += make_no_backtrace
156run += make_no_libcapstone
157run += make_no_libnuma
158run += make_no_libaudit
159run += make_no_libbionic
160run += make_no_auxtrace
161run += make_no_libbpf
162run += make_no_libbpf_DEBUG
163run += make_no_libcrypto
164run += make_no_sdt
165run += make_no_syscall_tbl
166run += make_with_babeltrace
167run += make_with_coresight
168run += make_with_clangllvm
169run += make_no_libpfm4
170run += make_refcnt_check
171run += make_help
172run += make_doc
173run += make_perf_o
174run += make_util_map_o
175run += make_util_pmu_bison_o
176run += make_install
177run += make_install_bin
178run += make_install_prefix
179run += make_install_prefix_slash
180# FIXME 'install-*' commented out till they're fixed
181# run += make_install_doc
182# run += make_install_man
183# run += make_install_html
184# run += make_install_info
185# run += make_install_pdf
186run += make_minimal
187
188old_libbpf := $(shell echo '\#include <bpf/libbpf.h>' | $(CC) -E -dM -x c -| grep -q -E "define[[:space:]]+LIBBPF_MAJOR_VERSION[[:space:]]+0{1}")
189
190ifneq ($(old_libbpf),)
191run += make_libbpf_dynamic
192endif
193
194ifneq ($(call has,ctags),)
195run += make_tags
196endif
197ifneq ($(call has,cscope),)
198run += make_cscope
199endif
200
201# $(run_O) contains same portion of $(run) tests with '_O' attached
202# to distinguish O=... tests
203run_O := $(addsuffix _O,$(run))
204
205# disable some tests for O=...
206run_O := $(filter-out make_python_perf_so_O,$(run_O))
207
208# define test for each compile as 'test_NAME' variable
209# with the test itself as a value
210test_make_tags   = test -f tags
211test_make_cscope = test -f cscope.out
212
213test_make_tags_O   := $(test_make_tags)
214test_make_cscope_O := $(test_make_cscope)
215
216test_ok          := true
217test_make_help   := $(test_ok)
218test_make_doc    := $(test_ok)
219test_make_help_O := $(test_ok)
220test_make_doc_O  := $(test_ok)
221
222test_make_python_perf_so := test -f $(PERF_O)/$(python_perf_so)
223
224test_make_perf_o           := test -f $(PERF_O)/perf.o
225test_make_util_map_o       := test -f $(PERF_O)/util/map.o
226test_make_util_pmu_bison_o := test -f $(PERF_O)/util/pmu-bison.o
227
228define test_dest_files
229  for file in $(1); do				\
230    if [ ! -x $$TMP_DEST/$$file ]; then		\
231      echo "  failed to find: $$file";		\
232    fi						\
233  done
234endef
235
236installed_files_bin := bin/perf
237installed_files_bin += etc/bash_completion.d/perf
238installed_files_bin += libexec/perf-core/perf-archive
239
240installed_files_all := $(installed_files_bin)
241
242test_make_install       := $(call test_dest_files,$(installed_files_all))
243test_make_install_O     := $(call test_dest_files,$(installed_files_all))
244test_make_install_bin   := $(call test_dest_files,$(installed_files_bin))
245test_make_install_bin_O := $(call test_dest_files,$(installed_files_bin))
246
247# We prefix all installed files for make_install_prefix(_slash)
248# with '/tmp/krava' to match installed/prefix-ed files.
249installed_files_all_prefix := $(addprefix /tmp/krava/,$(installed_files_all))
250test_make_install_prefix   :=  $(call test_dest_files,$(installed_files_all_prefix))
251test_make_install_prefix_O :=  $(call test_dest_files,$(installed_files_all_prefix))
252
253test_make_install_prefix_slash   := $(test_make_install_prefix)
254test_make_install_prefix_slash_O := $(test_make_install_prefix_O)
255
256# FIXME nothing gets installed
257test_make_install_man    := test -f $$TMP_DEST/share/man/man1/perf.1
258test_make_install_man_O  := $(test_make_install_man)
259
260# FIXME nothing gets installed
261test_make_install_doc    := $(test_ok)
262test_make_install_doc_O  := $(test_ok)
263
264# FIXME nothing gets installed
265test_make_install_html   := $(test_ok)
266test_make_install_html_O := $(test_ok)
267
268# FIXME nothing gets installed
269test_make_install_info   := $(test_ok)
270test_make_install_info_O := $(test_ok)
271
272# FIXME nothing gets installed
273test_make_install_pdf    := $(test_ok)
274test_make_install_pdf_O  := $(test_ok)
275
276test_make_libbpf_dynamic :=   ldd $(PERF_O)/perf | grep -q libbpf
277test_make_libbpf_dynamic_O := ldd $$TMP_O/perf | grep -q libbpf
278
279test_make_python_perf_so_O    := test -f $$TMP_O/python/perf.so
280test_make_perf_o_O            := test -f $$TMP_O/perf.o
281test_make_util_map_o_O        := test -f $$TMP_O/util/map.o
282test_make_util_pmu_bison_o_O := test -f $$TMP_O/util/pmu-bison.o
283
284test_default = test -x $(PERF_O)/perf
285test = $(if $(test_$1),$(test_$1),$(test_default))
286
287test_default_O = test -x $$TMP_O/perf
288test_O = $(if $(test_$1),$(test_$1),$(test_default_O))
289
290all:
291
292ifdef SHUF
293run := $(shell shuf -e $(run))
294run_O := $(shell shuf -e $(run_O))
295endif
296
297max_width := $(shell echo $(run_O) | sed 's/ /\n/g' | wc -L)
298
299ifdef DEBUG
300d := $(info run   $(run))
301d := $(info run_O $(run_O))
302endif
303
304MAKEFLAGS := --no-print-directory
305
306clean := @(cd $(PERF); $(MAKE_F) -s $(O_OPT) clean >/dev/null && $(MAKE) -s $(O_OPT) -C ../build clean >/dev/null)
307
308$(run):
309	$(call clean)
310	@TMP_DEST=$$(mktemp -d); \
311	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
312	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
313	( eval $$cmd ) >> $@ 2>&1; \
314	echo "  test: $(call test,$@)" >> $@ 2>&1; \
315	$(call test,$@) && \
316	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
317
318make_with_gtk2:
319	$(call clean)
320	@TMP_DEST=$$(mktemp -d); \
321	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
322	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
323	( eval $$cmd ) >> $@ 2>&1; \
324	echo "  test: $(call test,$@)" >> $@ 2>&1; \
325	$(call test,$@) && \
326	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
327
328make_static:
329	$(call clean)
330	@TMP_DEST=$$(mktemp -d); \
331	cmd="cd $(PERF) && $(MAKE_F) $($@) $(PARALLEL_OPT) $(O_OPT) DESTDIR=$$TMP_DEST"; \
332	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
333	( eval $$cmd ) >> $@ 2>&1; \
334	echo "  test: $(call test,$@)" >> $@ 2>&1; \
335	$(call test,$@) && \
336	rm -rf $@ $$TMP_DEST || (cat $@ ; false)
337
338$(run_O):
339	$(call clean)
340	@TMP_O=$$(mktemp -d); \
341	TMP_DEST=$$(mktemp -d); \
342	cmd="cd $(PERF) && $(MAKE_F) $($(patsubst %_O,%,$@)) $(PARALLEL_OPT) O=$$TMP_O DESTDIR=$$TMP_DEST"; \
343	printf "%*.*s: %s\n" $(max_width) $(max_width) "$@" "$$cmd" && echo $$cmd > $@ && \
344	( eval $$cmd ) >> $@ 2>&1 && \
345	echo "  test: $(call test_O,$@)" >> $@ 2>&1; \
346	$(call test_O,$@) && \
347	rm -rf $@ $$TMP_O $$TMP_DEST || (cat $@ ; false)
348
349tarpkg:
350	@cmd="$(PERF)/tests/perf-targz-src-pkg $(PERF)"; \
351	echo "- $@: $$cmd" && echo $$cmd > $@ && \
352	( eval $$cmd ) >> $@ 2>&1 && \
353	rm -f $@
354
355KERNEL_O := ../..
356ifneq ($(O),)
357  KERNEL_O := $(O)
358endif
359
360make_kernelsrc:
361	@echo "- make -C <kernelsrc> $(PARALLEL_OPT) $(K_O_OPT) tools/perf"
362	$(call clean); \
363	(make -C ../.. $(PARALLEL_OPT) $(K_O_OPT) tools/perf) > $@ 2>&1 && \
364	test -x $(KERNEL_O)/tools/perf/perf && rm -f $@ || (cat $@ ; false)
365
366make_kernelsrc_tools:
367	@echo "- make -C <kernelsrc>/tools $(PARALLEL_OPT) $(K_O_OPT) perf"
368	$(call clean); \
369	(make -C ../../tools $(PARALLEL_OPT) $(K_O_OPT) perf) > $@ 2>&1 && \
370	test -x $(KERNEL_O)/tools/perf/perf && rm -f $@ || (cat $@ ; false)
371
372make_libperf:
373	@echo "- make -C lib";
374	make -C lib clean >$@ 2>&1; make -C lib >>$@ 2>&1 && rm $@
375
376FEATURES_DUMP_FILE := $(FULL_O)/BUILD_TEST_FEATURE_DUMP
377FEATURES_DUMP_FILE_STATIC := $(FULL_O)/BUILD_TEST_FEATURE_DUMP_STATIC
378
379all: $(run) $(run_O) tarpkg make_kernelsrc make_kernelsrc_tools
380	@echo OK
381	@rm -f $(FEATURES_DUMP_FILE) $(FEATURES_DUMP_FILE_STATIC)
382
383out: $(run_O)
384	@echo OK
385	@rm -f $(FEATURES_DUMP_FILE) $(FEATURES_DUMP_FILE_STATIC)
386
387ifeq ($(REUSE_FEATURES_DUMP),1)
388$(FEATURES_DUMP_FILE):
389	$(call clean)
390	@cmd="cd $(PERF) && make FEATURE_DUMP_COPY=$@ $(O_OPT) feature-dump"; \
391	echo "- $@: $$cmd" && echo $$cmd && \
392	( eval $$cmd ) > /dev/null 2>&1
393
394$(FEATURES_DUMP_FILE_STATIC):
395	$(call clean)
396	@cmd="cd $(PERF) && make FEATURE_DUMP_COPY=$@ $(O_OPT) LDFLAGS='-static' feature-dump"; \
397	echo "- $@: $$cmd" && echo $$cmd && \
398	( eval $$cmd ) > /dev/null 2>&1
399
400# Add feature dump dependency for run/run_O targets
401$(foreach t,$(run) $(run_O),$(eval \
402	$(t): $(if $(findstring make_static,$(t)),\
403		$(FEATURES_DUMP_FILE_STATIC),\
404		$(FEATURES_DUMP_FILE))))
405
406# Append 'FEATURES_DUMP=' option to all test cases. For example:
407# make_no_libbpf: NO_LIBBPF=1  --> NO_LIBBPF=1 FEATURES_DUMP=/a/b/BUILD_TEST_FEATURE_DUMP
408# make_static: LDFLAGS=-static --> LDFLAGS=-static FEATURES_DUMP=/a/b/BUILD_TEST_FEATURE_DUMP_STATIC
409$(foreach t,$(run),$(if $(findstring make_static,$(t)),\
410			$(eval $(t) := $($(t)) FEATURES_DUMP=$(FEATURES_DUMP_FILE_STATIC)),\
411			$(eval $(t) := $($(t)) FEATURES_DUMP=$(FEATURES_DUMP_FILE))))
412endif
413
414.PHONY: all $(run) $(run_O) tarpkg clean make_kernelsrc make_kernelsrc_tools make_libperf
415endif # ifndef MK
416