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