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