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