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