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