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