xref: /petsc/gmakefile.test (revision 49da0fa4021f16468818664da3206a57a78e64f0)
1# -*- mode: makefile-gmake -*-
2
3CONFIGDIR := config
4
5# If $(PETSC_ARCH) is empty, this defines it and PETSC_DIR
6include $(PETSC_ARCH)/lib/petsc/conf/petscvariables
7include ./lib/petsc/conf/variables
8
9ifeq ($(TESTDIR),)  # Default
10TESTDIR := $(PETSC_ARCH)/tests
11gmakegentest_opts=
12else
13gmakegentest_opts="-t $(TESTDIR)"
14TESTDIR := $(TESTDIR)
15endif
16MODDIR := $(PETSC_ARCH)/include
17TESTLOGFILE = $(TESTDIR)/examples-$(PETSC_ARCH).log
18
19pkgs := sys vec mat dm ksp snes ts tao
20
21petscconf      := $(PETSC_ARCH)/include/petscconf.h
22petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables
23generatedtest  := $(PETSC_ARCH)/lib/petsc/conf/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 = @
43else				# Show the full command line
44  quiet = $($1)
45  quiettest =
46  TESTFLAGS += -v
47endif
48
49ifeq ($(FORCE),1)
50  TESTFLAGS += -f    # force test execution
51endif
52ifeq ($(VALGRIND),1)
53  TESTFLAGS += -V    # Add valgrind to the flags
54endif
55ifeq ($(REPLACE),1)
56  TESTFLAGS += -m    # Replace results by passing -m to petscdiff
57endif
58ifeq ($(DIFF_NUMBERS),1)
59  TESTFLAGS += -j    # Pass -j to petscdiff to diff the actual numbers
60endif
61ifdef OPTIONS
62  TESTFLAGS += -a '$(OPTIONS)'   # override arguments
63endif
64ifdef EXTRA_OPTIONS
65  TESTFLAGS += -e '$(EXTRA_OPTIONS)'  # add extra arguments
66endif
67ifdef NP
68  TESTFLAGS += -n $(NP)  # set number of processes
69endif
70# Override the default timeout that may be found at the top of config/petsc_harness.sh
71# This must be an integer.  It is given in seconds.
72ifdef TIMEOUT
73  TESTFLAGS += -t $(TIMEOUT)  # Override the default timeout
74endif
75
76
77
78$(generatedtest) : $(petscconf) $(petscvariables) $(CONFIGDIR)/gmakegentest.py | $$(@D)/.DIR
79	$(PYTHON) $(CONFIGDIR)/gmakegentest.py --petsc-arch=$(PETSC_ARCH) $(gmakegentest_opts)
80
81-include $(generatedtest)
82
83ifeq ($(PETSC_LANGUAGE),CXXONLY)
84  cc_name := CXX
85else
86  cc_name := CC
87endif
88
89PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS)
90PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS)
91PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
92PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
93PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS)
94
95testlangs := c cu cxx F F90
96concattestlang = $(foreach lang, $(2), $(testsrcs-$(1).$(lang):%.$(lang)=$(TESTDIR)/%.o))
97testsrcs.o := $(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),$(testlangs)))
98testsrcs := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), $(testsrcs-$(pkg).$(lang))))
99
100# Refresh testfiles when sources change, but don't balk if the source file is nonexistent (deleted)
101$(generatedtest) : $(testsrcs)
102$(testsrcs) :
103
104$(TESTDIR)/%.o : %.c | $$(@D)/.DIR
105	$(PETSC_COMPILE.c) $(abspath $<) -o $@
106
107$(TESTDIR)/%.o : %.cxx | $$(@D)/.DIR
108	$(PETSC_COMPILE.cxx) $(abspath $<) -o $@
109
110$(TESTDIR)/%.o : %.cu | $$(@D)/.DIR
111	$(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile
112	@$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later
113
114# Test modules go in a different directory
115$(TESTDIR)/%.o : MODDIR = $(@D)
116FCMOD = cd
117$(TESTDIR)/%.o : %.F | $$(@D)/.DIR
118ifeq ($(FC_MODULE_OUTPUT_FLAG),)
119	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
120else
121	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
122endif
123
124$(TESTDIR)/%.o : %.F90 | $$(@D)/.DIR
125ifeq ($(FC_MODULE_OUTPUT_FLAG),)
126	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
127else
128	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
129endif
130
131# Test executables
132$(TESTDIR)/%f : $(TESTDIR)/%f.o $(libpetscall)
133	$(call quiet,FLINKER) -o $@ $< $(PETSC_LIB)
134
135$(TESTDIR)/%f90 : $(TESTDIR)/%f90.o $(libpetscall)
136	$(call quiet,FLINKER) -o $@ $< $(PETSC_LIB)
137
138$(TESTDIR)/% : $(TESTDIR)/%.o $(libpetscall)
139	$(call quiet,CLINKER) -o $@ $< $(PETSC_LIB)
140
141# Fortran source files need petsc*.mod, which isn't explicitly managed in the makefile.
142$(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),F F90)) : $(libpetscall)
143
144# Testing convenience targets
145.PHONY: alltest test pre-clean
146test: pre-clean report_tests
147pre-clean:
148	@$(RM) -r $(TESTDIR)/counts $(TESTLOGFILE)
149	@touch $(TESTLOGFILE)
150
151.PHONY: $(foreach pkg, $(pkgs), test-$(pkg) $(foreach lang, $(testlangs), test-$(pkg).$(lang) test-rm-$(pkg).$(lang)))
152testpkgs := $(foreach pkg, $(pkgs), test-$(pkg))
153# Targets to run tests in test-$pkg.$lang and delete the executables, language by language
154$(testpkgs) : test-% : $(foreach lang, $(testlangs), test-rm-%.$(lang))
155# List of raw test run targets
156alltesttargets := $(foreach tp, $(testpkgs), $(foreach lang, $(testlangs), $($(tp).$(lang))))
157
158# Run targets
159$(alltesttargets) : % : $(TESTDIR)/counts/%.counts
160.PHONY: $(alltesttargets)
161
162$(TESTDIR)/counts/%.counts :
163	$(quiettest) $< $(TESTFLAGS)
164
165# Targets to run tests and remove executables, by package-lang pairs.
166# Run the tests in each batch using recursive invocation of make because
167# we need all of them to complete before removing the executables.  Make
168# doesn't guarantee an exploration order for the graph.  Only recursive
169# if there is something to be done.
170alltest-rm := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), test-rm-$(pkg).$(lang)))
171$(alltest-rm) : test-rm-% : test-%
172ifneq ($(NO_RM),1)
173	$(call quiet,RM) $(addprefix $(TESTDIR)/,$(basename $($(@:test-rm-%=testsrcs-%))))
174endif
175
176# Remove intermediate .o files
177# This only removes the files at the end which is insufficient
178#.INTERMEDIATE: $(testsrcs.o:%.o=%)
179
180# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.)
181$(testsrcs.o) : $(petscvariables)
182
183%/.DIR :
184	@mkdir -p $(@D)
185	@touch $@
186
187.PRECIOUS: %/.DIR
188
189.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules
190.DELETE_ON_ERROR:               # Delete likely-corrupt target file if rule fails
191
192.PHONY: clean cleantest all print
193
194cleantest:
195	${RM} -r $(TESTDIR) $(generatedtest)
196
197clean: cleantest
198
199# make print VAR=the-variable
200print:
201	@echo $($(VAR))
202
203alltest.d := $(testsrcs.o:%.o=%.d)
204# Tell make that alltest.d are all up to date.  Without this, the include
205# below has quadratic complexity, taking more than one second for a
206# do-nothing build of PETSc (much worse for larger projects)
207$(alltest.d) : ;
208
209-include $(alltest.d)
210
211# Tests can be generated by searching
212# Percent is a wildcard (only one allowed):
213#    make -f gmakefile test search=sys%ex2
214# To match internal substrings (matches *ex2*):
215#    make -f gmakefile test searchin=ex2
216# Search and searchin can be combined:
217#    make -f gmakefile test search='sys%' searchin=ex2
218# For args:
219#    make -f gmakefile test argsearch=cuda
220# For general glob-style searching using python:
221# NOTE: uses shell which is possibly slower and is possibly more brittle
222#    make -f gmakefile test globsearch='sys*ex2*'
223ifdef search
224  TESTTARGETS := $(filter $(search),$(alltesttargets))
225  ifdef searchin
226    TESTTARGETS2 := $(foreach v,$(TESTTARGETS),$(if $(findstring $(searchin),$(v)),$(v)))
227    TESTTARGETS := $(TESTTARGETS2)
228  endif
229else ifdef searchin
230  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(searchin),$(v)),$(v)))
231else ifdef argsearch
232  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(argsearch),$($(v)_ARGS)),$(v)))
233else ifdef globsearch
234  TESTTARGETS := $(shell $(PYTHON) -c"import sys,fnmatch; print ' '.join(fnmatch.filter(sys.argv[2].split(),sys.argv[1]))" '$(globsearch)' '$(alltesttargets)')
235else                            # No filter - run them all, but delete the executables as we go
236  TESTTARGETS := $(testpkgs)
237endif
238
239.PHONY: report_tests print-test
240
241print-test:
242	-@echo $(TESTTARGETS)
243
244report_tests: $(TESTTARGETS)
245	-@$(PYTHON) $(CONFIGDIR)/report_tests.py -d $(PETSC_ARCH)/tests/counts
246
247# Do not how how to invoke test from makefile
248HASGMAKEFILE := $(filter gmakefile,$(MAKEFILE_LIST))
249ifeq ($(HASGMAKEFILE),gmakefile)
250helpdeps:=help-make help-targets
251makefile="gmakefile"
252else
253helpdeps:=help-make help-targets help-test
254makefile="gmakefile.test"
255endif
256
257help: ${helpdeps}
258	-@echo "Above is from: ${helpdeps}"
259
260help-make:
261	-@echo
262	-@echo "Basic build usage:"
263	-@echo "   make -f ${makefile} <options>"
264	-@echo
265	-@echo "Options:"
266	-@echo "  V=0           Very quiet builds"
267	-@echo "  V=1           Verbose builds"
268	-@echo
269
270help-targets:
271	-@echo "All makefile targets and their dependencies:"
272	-@grep ^[a-z] ${makefile} | grep : | grep -v =
273	-@echo
274	-@echo
275
276help-test:
277	-@echo "Basic test usage:"
278	-@echo "   make -f ${makefile} test <options>"
279	-@echo
280	-@echo "Options:"
281	-@echo "  NO_RM=1           Do not remove the executables after running"
282	-@echo "  REPLACE=1         Replace the output in the PETSC_DIR source tree (-m to test scripts)"
283	-@echo "  DIFF_NUMBERS=1    Diff the numbers in the output (-j to test scripts and petscdiff)"
284	-@echo "  VALGRIND=1        Execute the tests using valgrind (-V to test scripts)"
285	-@echo "  TESTDIR='tests'   Subdirectory where the tests are run ($${PETSC_DIR}/$${PETSC_ARCH}/$${TESTDIR}"
286	-@echo "                    or $${PREFIX_DIR}/$${TESTDIR}"
287	-@echo "                    or $${PREFIX_DIR}/share/petsc/examples/$${TESTDIR})"
288	-@echo "  OPTIONS='<args>'  Override options to scripts (-a to test scripts)"
289	-@echo "  EXTRA_OPTIONS='<args>'  Add options to scripts (-e to test scripts)"
290	-@echo
291	-@echo "Tests can be generated by searching:"
292	-@echo "  Percent is a wildcard (only one allowed):"
293	-@echo "    make -f ${makefile} test search=sys%ex2"
294	-@echo
295	-@echo "  To match internal substrings (matches *ex2*):"
296	-@echo "    make -f ${makefile} test searchin=ex2"
297	-@echo
298	-@echo "  Search and searchin can be combined:"
299	-@echo "    make -f ${makefile} test search='sys%' searchin=ex2"
300	-@echo
301	-@echo "  To match patterns in the arguments:"
302	-@echo "    make -f ${makefile} test argsearch=cuda"
303	-@echo
304	-@echo "  For general glob-style searching using python:"
305	-@echo "   NOTE: uses shell which is possibly slower and more brittle"
306	-@echo "    make -f ${makefile} test globsearch='sys*ex2*'"
307	-@echo
308	-@echo "  To see which targets match a given pattern (useful for doing a specific target):"
309	-@echo "    make -f ${makefile} print-test search=sys%"
310	-@echo "  which is equivalent to:"
311	-@echo "    make -f ${makefile} print VAR=TESTTARGETS search='sys%'"
312	-@echo
313	-@echo "  To build an executable, give full path to location:"
314	-@echo '    make -f ${makefile} $${PETSC_ARCH}/tests/src/sys/examples/tests/ex1'
315	-@echo "  or make the test with NO_RM=1"
316	-@echo
317