xref: /petsc/gmakefile.test (revision f2ed2dc71a2ab9ffda85eae8afa0cbea9ed570de)
1# -*- mode: makefile-gmake -*-
2-include petscdir.mk
3
4CONFIGDIR := $(PETSC_DIR)/config
5
6# TESTSRCDIR is always relative to gmakefile.test
7#  This must be before includes
8mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
9TESTSRCDIR := $(dir $(mkfile_path))src
10
11-include $(PETSC_DIR)/$(PETSC_ARCH)/lib/petsc/conf/petscvariables
12-include $(PETSC_DIR)/lib/petsc/conf/variables
13
14TESTDIR ?= ./$(PETSC_ARCH)/tests
15MODDIR := $(PETSC_DIR)/$(PETSC_ARCH)/include
16TESTLOGTAPFILE ?= $(TESTDIR)/test_$(PETSC_ARCH)_tap.log
17TESTLOGERRFILE ?= $(TESTDIR)/test_$(PETSC_ARCH)_err.log
18EXAMPLESDIR := $(TESTSRCDIR)
19
20pkgs := sys vec mat dm ksp snes ts tao
21
22petscconf      := $(PETSC_DIR)/$(PETSC_ARCH)/include/petscconf.h
23petscvariables := $(PETSC_DIR)/$(PETSC_ARCH)/lib/petsc/conf/petscvariables
24generatedtest  := $(TESTDIR)/testfiles
25
26.SECONDEXPANSION:		# to expand $$(@D)/.DIR
27
28TESTFLAGS :=             # Initialize as simple variable
29
30#workarround old cygwin versions
31ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1)
32ifeq ($(shell basename $(AR)),ar)
33  V ?=1
34endif
35endif
36V ?= $(if $(findstring s,$(MAKEFLAGS)),0)
37ifeq ($(V),)  # Default
38  quiet_HELP := "Use \"$(MAKE) V=1\" to see verbose compile lines, \"$(MAKE) V=0\" to suppress.\n"
39  quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)"  %10s %s\n" "$1$2" "$@"; $($1)
40  quiettest = @printf "  %10s %s\n" "TEST" "$(@:$(TESTDIR)/counts/%.counts=%)";
41else ifeq ($(V),0)		# Suppress entire command
42  quiet = @$($1)
43  quiettest = @
44  TESTFLAGS += -o err_only
45else				# Show the full command line
46  quiet = $($1)
47  quiettest =
48  TESTFLAGS += -v
49endif
50
51ifeq ($(FORCE),1)
52  TESTFLAGS += -f    # force test execution
53endif
54ifeq ($(VALGRIND),1)
55  TESTFLAGS += -V    # Add valgrind to the flags
56endif
57ifeq ($(REPLACE),1)
58  TESTFLAGS += -m    # Replace results by passing -m to petscdiff
59endif
60ifeq ($(OUTPUT),1)
61  TESTFLAGS += -o 'err_only'    # Show only the errors on stdout
62endif
63ifeq ($(ALT),1)
64  TESTFLAGS += -M    # Replace alt files by passing -M to petscdiff
65endif
66PRINTONLY ?= 0
67ifeq ($(PRINTONLY),1)
68  TESTFLAGS += -p    # Pass -p to petscdiff to print only command
69endif
70ifeq ($(DIFF_NUMBERS),1)
71  TESTFLAGS += -j    # Pass -j to petscdiff to diff the actual numbers
72endif
73ifdef OPTIONS
74  TESTFLAGS += -a '$(OPTIONS)'   # override arguments
75endif
76ifdef EXTRA_OPTIONS
77  TESTFLAGS += -e '$(EXTRA_OPTIONS)'  # add extra arguments
78endif
79ifdef NP
80  TESTFLAGS += -n $(NP)  # set number of processes
81endif
82# Override the default timeout that may be found at the top of config/petsc_harness.sh
83# This must be an integer.  It is given in seconds.
84ifdef TIMEOUT
85  TESTFLAGS += -t $(TIMEOUT)  # Override the default timeout
86endif
87
88$(generatedtest) : $(petscconf) $(petscvariables) $(CONFIGDIR)/gmakegentest.py $(TESTDIR)/.DIR | $$(@D)/.DIR
89	$(PYTHON) $(CONFIGDIR)/gmakegentest.py --petsc-dir=$(PETSC_DIR) --petsc-arch=$(PETSC_ARCH) --testdir=$(TESTDIR)
90
91ifneq ($(filter-out clean check,$(MAKECMDGOALS:clean%=clean)),)
92include $(generatedtest)
93endif
94
95ifeq ($(PETSC_LANGUAGE),CXXONLY)
96  cc_name := CXX
97else
98  cc_name := CC
99endif
100
101PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(PFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS)
102PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CXXFLAGS) $(CXXCPPFLAGS) $(CXX_DEPFLAGS)
103PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) $(CUFLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
104PETSC_COMPILE.hip.cpp = $(call quiet,HIPCC) -c $(HIPCC_FLAGS) $(HIPCCFLAGS) $(HIPCCPPFLAGS) $(HIPCC_DEPFLAGS)
105PETSC_COMPILE.sycl.cpp = $(call quiet,SYCLCPP) -c $(SYCLCPP_FLAGS) $(SYCLCPPFLAGS) $(SYCLCPPCPPFLAGS) $(SYCLCPP_DEPFLAGS)
106PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) $(CUFLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
107PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS)
108
109testlangs := c cu cxx F F90 hip.cpp sycl.cpp
110$(foreach lang, $(testlangs), $(eval \
111  testexe.$(lang) = $(foreach pkg, $(pkgs), $(testsrcs-$(pkg).$(lang):%.$(lang)=$(TESTDIR)/%))))
112concattestlang = $(foreach lang, $(2), $(testsrcs-$(1).$(lang):%.$(lang)=$(TESTDIR)/%.o))
113testsrcs.o := $(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),$(testlangs)))
114testsrcs-rel := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), $(testsrcs-$(pkg).$(lang))))
115testsrcs := $(foreach sfile, $(testsrcs-rel), $(TESTSRCDIR)/$(sfile))
116
117# workaround win32fe failure
118ifneq (,$(findstring win32fe,$(call quiet,$(cc_name))))
119$(TESTDIR)/ts/tutorials/multirate/ex6: | $(TESTDIR)/ts/tutorials/multirate/ex5
120$(TESTDIR)/ts/tutorials/multirate/ex8: | $(TESTDIR)/ts/tutorials/multirate/ex6
121endif
122
123# Refresh testfiles when sources change, but don't balk if the source file is nonexistent (deleted)
124$(generatedtest) : $(testsrcs)
125$(testsrcs) :
126
127$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.c | $$(@D)/.DIR
128	$(PETSC_COMPILE.c) $(abspath $<) -o $@
129
130$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.cxx | $$(@D)/.DIR
131	$(PETSC_COMPILE.cxx) $(abspath $<) -o $@
132
133$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.cu | $$(@D)/.DIR
134	$(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile
135	@$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later
136
137# Test modules go in the same directory as the target *.o
138TESTMODDIR = $(@D)
139FCMOD = cd
140$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.F | $$(@D)/.DIR
141ifeq ($(FC_MODULE_OUTPUT_FLAG),)
142	$(call quiet,FCMOD) $(TESTMODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) -I$(dir $<) $(abspath $<) -o $(abspath $@)
143else
144	$(PETSC_COMPILE.F) -I$(dir $<) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(TESTMODDIR) $(FC_MODULE_FLAG)$(TESTMODDIR)
145endif
146	-@$(GFORTRAN_DEP_CLEANUP)
147
148$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.F90 | $$(@D)/.DIR
149ifeq ($(FC_MODULE_OUTPUT_FLAG),)
150	$(call quiet,FCMOD) $(TESTMODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) -I$(dir $<) $(abspath $<) -o $(abspath $@)
151else
152	$(PETSC_COMPILE.F) -I$(dir $<) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(TESTMODDIR) $(FC_MODULE_FLAG)$(TESTMODDIR)
153endif
154	-@$(GFORTRAN_DEP_CLEANUP)
155
156#   This is a hack to fix a broken gfortran.
157define GFORTRAN_DEP_CLEANUP
158  if test -e "$(@:%.o=%.d)" && head -1 "$(@:%.o=%.d)" | fgrep -q -v : ; then\
159    echo "$(@): \\" > $(@:%.o=%.dtemp) ; \
160    tr '\n' '@' < $(@:%.o=%.d) | cut -d: -f2- | tr '@' '\n' >> $(@:%.o=%.dtemp) ; \
161    mv $(@:%.o=%.dtemp) $(@:%.o=%.d); \
162  fi
163endef
164
165# link line constructed differently for gmakefile vs gmakefile.test invocation
166ifeq ($(libpetscall),)
167PETSC_TEST_LIB = $(PETSC_LIB)
168else
169PETSC_TEST_LIB = $(C_SH_LIB_PATH) $(PETSC_EXTERNAL_LIB_BASIC)
170endif
171
172# manually list some some library dependencies to check for circular dependencies
173$(TESTDIR)/sys/tests/ex9: PETSC_TEST_LIB = $(PETSC_SYS_LIB)
174$(TESTDIR)/vec/vec/tests/ex1: PETSC_TEST_LIB = $(PETSC_VEC_LIB)
175$(TESTDIR)/mat/tests/ex1: PETSC_TEST_LIB = $(PETSC_MAT_LIB)
176$(TESTDIR)/dm/tests/ex1: PETSC_TEST_LIB = $(PETSC_DM_LIB)
177$(TESTDIR)/ksp/ksp/tests/ex1: PETSC_TEST_LIB = $(PETSC_KSP_LIB)
178$(TESTDIR)/snes/tests/ex1: PETSC_TEST_LIB = $(PETSC_SNES_LIB)
179$(TESTDIR)/ts/tests/ex2: PETSC_TEST_LIB = $(PETSC_TS_LIB)
180$(TESTDIR)/tao/tutorials/ex1: PETSC_TEST_LIB = $(PETSC_TAO_LIB)
181
182# Test executables
183$(testexe.F) $(testexe.F90) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
184	$(call quiet,FLINKER) -o $@ $^ $(PETSC_TEST_LIB)
185
186$(testexe.c) $(testexe.cu) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
187	$(call quiet,CLINKER) -o $@ $^ $(PETSC_TEST_LIB)
188
189$(testexe.cxx) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
190	$(call quiet,CXXLINKER) -o $@ $^ $(PETSC_TEST_LIB)
191
192# Fortran source files need petsc*.mod, which isn't explicitly managed in the makefile.
193$(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),F F90)) : $(libpetscall)
194
195# Testing convenience targets
196.PHONY: test pre-clean
197
198test: report_tests
199
200pre-clean:
201	@$(RM) -rf $(TESTDIR)/counts $(TESTLOGTAPFILE) $(TESTLOGERRFILE)
202	@touch $(TESTLOGTAPFILE) $(TESTLOGERRFILE)
203	@echo "Using MAKEFLAGS:" ${MAKEFLAGS}
204
205check-test-errors:
206	@grep '^not ok' $(TESTLOGTAPFILE) | grep -v 'Exceeded timeout' | tee $(TESTDIR)/allgtests-tap-err.log
207	@test ! -s $(TESTDIR)/allgtests-tap-err.log
208
209.PHONY: $(foreach pkg, $(pkgs), test-$(pkg) $(foreach lang, $(testlangs), test-$(pkg).$(lang) test-rm-$(pkg).$(lang)))
210testpkgs := $(foreach pkg, $(pkgs), test-$(pkg))
211# Targets to run tests in test-$pkg.$lang and delete the executables, language by language
212$(testpkgs) : test-% : $(foreach lang, $(testlangs), test-rm-%.$(lang))
213# List of raw test run targets
214alltesttargets := $(foreach tp, $(testpkgs), $(foreach lang, $(testlangs), $($(tp).$(lang))))
215
216# Run targets
217$(alltesttargets) : % : $(TESTDIR)/counts/%.counts
218.PHONY: $(alltesttargets)
219
220$(TESTDIR)/counts/%.counts :
221	$(quiettest) $< $(TESTFLAGS)
222
223# Targets to run tests and remove executables, by package-lang pairs.
224# Run the tests in each batch using recursive invocation of make because
225# we need all of them to complete before removing the executables.  Make
226# doesn't guarantee an exploration order for the graph.  Only recursive
227# if there is something to be done.
228alltest-rm := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), test-rm-$(pkg).$(lang)))
229$(alltest-rm) : test-rm-% : test-%
230ifneq ($(NO_RM),1)
231	$(call quiet,RM) $(addprefix $(TESTDIR)/,$(basename $($(@:test-rm-%=testsrcs-%))))
232endif
233
234# Remove intermediate .o files
235# This only removes the files at the end which is insufficient
236#.INTERMEDIATE: $(testsrcs.o:%.o=%)
237
238# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.)
239$(testsrcs.o) : $(petscvariables)
240
241%/.DIR :
242	@mkdir -p $(@D)
243	@touch $@
244
245.PRECIOUS: %/.DIR
246
247.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules
248.DELETE_ON_ERROR:               # Delete likely-corrupt target file if rule fails
249
250.PHONY: clean cleantest all
251
252cleantest:
253	${RM} -r $(TESTDIR) $(generatedtest)
254
255clean: cleantest
256
257alltest.d := $(testsrcs.o:%.o=%.d)
258# Tell make that alltest.d are all up to date.  Without this, the include
259# below has quadratic complexity, taking more than one second for a
260# do-nothing build of PETSc (much worse for larger projects)
261$(alltest.d) : ;
262
263-include $(alltest.d)
264
265# Tests can be generated by searching
266# Percent is a wildcard (only one allowed):
267#    make -f gmakefile test search=sys%ex2
268# To match internal substrings (matches *ex2*):
269#    make -f gmakefile test searchin=ex2
270# Search and searchin can be combined:
271#    make -f gmakefile test search='sys%' searchin=ex2
272# For args:
273#    make -f gmakefile test argsearch=cuda
274# For general glob-style searching using python:
275# NOTE: uses shell which is possibly slower and is possibly more brittle
276#    make -f gmakefile test globsearch='sys*ex2*'
277ifdef search
278  TESTTARGETS := $(filter $(search),$(alltesttargets))
279  ifdef searchin
280    TESTTARGETS2 := $(foreach v,$(TESTTARGETS),$(if $(findstring $(searchin),$(v)),$(v)))
281    TESTTARGETS := $(TESTTARGETS2)
282  endif
283else ifdef searchin
284  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(searchin),$(v)),$(v)))
285else ifdef argsearch
286  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(argsearch),$($(v)_ARGS)),$(v)))
287else ifdef globsearch
288  TESTTARGETS := $(shell $(PYTHON) config/query_tests.py 'name' '$(globsearch)')
289else ifdef test-fail
290  TESTTARGETS := $(shell $(PETSC_ARCH)/tests/echofailures.sh)
291else ifdef query
292  TESTTARGETS := $(shell $(PYTHON) config/query_tests.py '$(query)' '$(queryval)')
293else                            # No filter - run them all, but delete the executables as we go
294  TESTTARGETS := $(testpkgs)
295endif
296
297.PHONY: report_tests print-test
298
299print-test:
300	$(info $(TESTTARGETS))
301	@true
302
303show-fail:
304	-@$(PYTHON) $(CONFIGDIR)/report_tests.py -d $(TESTDIR)/counts -f
305
306
307
308# Don't start running tests until starttime has completed
309$(alltesttargets:%=$(TESTDIR)/counts/%.counts) : starttime
310
311# Ensure that libpetsc (if it is a prerequisite) has been built and clean the counts/logs before starting timer
312starttime: pre-clean $(libpetscall)
313	@$(eval STARTTIME := $(shell date +%s))
314
315report_tests: starttime $(TESTTARGETS)
316	@$(eval ENDTIME := $(shell date +%s))
317	-@if test ${PRINTONLY} -ne 1; then  elapsed_time=$$(($(ENDTIME)- $(STARTTIME))) && \
318	$(PYTHON) $(CONFIGDIR)/report_tests.py -m $(MAKE) -d $(TESTDIR)/counts -t 5 -e $${elapsed_time};\
319    fi
320
321check_output:
322	$(PYTHON) $(CONFIGDIR)/gmakegentest.py --petsc-dir=$(PETSC_DIR) --petsc-arch=$(PETSC_ARCH) --testdir=$(TESTDIR) --check-output
323
324# Do not how how to invoke test from makefile
325HASGMAKEFILE := $(filter gmakefile,$(MAKEFILE_LIST))
326ifeq ($(HASGMAKEFILE),gmakefile)
327helpdeps:=help-make help-targets
328makefile="gmakefile"
329other_help="To see full test help: make -f gmakefile.test help"
330else
331helpdeps:=help-make help-targets help-test
332makefile="gmakefile.test"
333other_help=""
334endif
335
336help: ${helpdeps}
337	-@echo "Above is from: ${helpdeps}"
338	-@echo "${other_help}"
339
340help-make:
341	-@echo
342	-@echo "Basic build usage:"
343	-@echo "   make -f ${makefile} <options>"
344	-@echo
345	-@echo "Options:"
346	-@echo "  V=0           Very quiet builds"
347	-@echo "  V=1           Verbose builds"
348	-@echo
349
350help-targets:
351	-@echo "All makefile targets and their dependencies:"
352	-@grep ^[a-z] ${makefile} | grep : | grep -v =
353	-@echo
354	-@echo
355
356help-test:
357	-@echo "Basic test usage:"
358	-@echo "   make -f ${makefile} test <options>"
359	-@echo
360	-@echo "Options:"
361	-@echo "  NO_RM=1           Do not remove the executables after running"
362	-@echo "  REPLACE=1         Replace the output in PETSC_DIR source tree (-m to test scripts)"
363	-@echo "  OUTPUT=1          Show only the errors on stdout"
364	-@echo "  ALT=1             Replace 'alt' output in PETSC_DIR source tree (-M to test scripts)"
365	-@echo "  DIFF_NUMBERS=1    Diff the numbers in the output (-j to test scripts and petscdiff)"
366	-@echo "  VALGRIND=1        Execute the tests using valgrind (-V to test scripts)"
367	-@echo "  NP=<num proc>     Set a number of processors to pass to scripts."
368	-@echo "  FORCE=1           Force SKIP or TODO tests to run"
369	-@echo "  PRINTONLY=1       Print the command, but do not run.  For loops print first command"
370	-@echo "  TIMEOUT=<time>    Test timeout limit in seconds (default in config/petsc_harness.sh)"
371	-@echo "  TESTDIR='tests'   Subdirectory where tests are run ($${PETSC_DIR}/$${PETSC_ARCH}/$${TESTDIR}"
372	-@echo "                    or $${PREFIX_DIR}/$${TESTDIR}"
373	-@echo "                    or $${PREFIX_DIR}/share/petsc/examples/$${TESTDIR})"
374	-@echo "  TESTBASE='tests'   Subdirectory where tests are run ($${PETSC_DIR}/$${PETSC_ARCH}/$${TESTDIR}"
375	-@echo "  OPTIONS='<args>'  Override options to scripts (-a to test scripts)"
376	-@echo "  EXTRA_OPTIONS='<args>'  Add options to scripts (-e to test scripts)"
377	-@echo
378	-@echo "Tests can be generated by searching:"
379	-@echo "  Percent is a wildcard (only one allowed):"
380	-@echo "    make -f ${makefile} test search=sys%ex2"
381	-@echo
382	-@echo "  To match internal substrings (matches *ex2*):"
383	-@echo "    make -f ${makefile} test searchin=ex2"
384	-@echo
385	-@echo "  Search and searchin can be combined:"
386	-@echo "    make -f ${makefile} test search='sys%' searchin=ex2"
387	-@echo
388	-@echo "  To match patterns in the arguments:"
389	-@echo "    make -f ${makefile} test argsearch=cuda"
390	-@echo
391	-@echo "  For general glob-style searching using python:"
392	-@echo "   NOTE: uses shell which is possibly slower and more brittle"
393	-@echo "    make -f ${makefile} test globsearch='sys*ex2*'"
394	-@echo
395	-@echo "  To re-run the last tests which failed:"
396	-@echo "    make -f ${makefile} test test-fail='1'"
397	-@echo
398	-@echo "  To search for fields from the original test definitions:"
399	-@echo "    make -f ${makefile} test query='requires' queryval='*MPI_PROCESS_SHARED_MEMORY*'"
400	-@echo
401	-@echo "  To see which targets match a given pattern (useful for doing a specific target):"
402	-@echo "    make -f ${makefile} print-test search=sys%"
403	-@echo "  which is equivalent to:"
404	-@echo "    make -f ${makefile} print VAR=TESTTARGETS search='sys%'"
405	-@echo
406	-@echo "  To build an executable, give full path to location:"
407	-@echo '    make -f ${makefile} $${PETSC_ARCH}/tests/sys/tests/ex1'
408	-@echo "  or make the test with NO_RM=1"
409	-@echo
410