xref: /petsc/gmakefile.test (revision 4e22ec7992ea0a5a8658409cbeedb7110d765e7d)
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
10include $(PETSC_DIR)/$(PETSC_ARCH)/lib/petsc/conf/petscvariables
11include $(PETSC_DIR)/lib/petsc/conf/variables
12
13ifeq ($(TESTBASE),)  # Default
14TESTDIR := $(PETSC_DIR)/$(PETSC_ARCH)/tests
15gmakegentest_opts=
16else
17TESTDIR := $(PETSC_DIR)/$(PETSC_ARCH)/$(TESTBASE)
18gmakegentest_opts=-t $(TESTDIR)
19endif
20MODDIR := $(PETSC_DIR)/$(PETSC_ARCH)/include
21TESTLOGFILE = $(TESTDIR)/examples_$(PETSC_ARCH).log
22EXAMPLESDIR := .
23
24pkgs := sys vec mat dm ksp snes ts tao
25
26petscconf      := $(PETSC_DIR)/$(PETSC_ARCH)/include/petscconf.h
27petscvariables := $(PETSC_DIR)/$(PETSC_ARCH)/lib/petsc/conf/petscvariables
28generatedtest  := $(PETSC_DIR)/$(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-dir=$(PETSC_DIR) --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
104$(foreach lang, $(testlangs), $(eval \
105  testexe.$(lang) = $(foreach pkg, $(pkgs), $(testsrcs-$(pkg).$(lang):%.$(lang)=$(TESTDIR)/%))))
106concattestlang = $(foreach lang, $(2), $(testsrcs-$(1).$(lang):%.$(lang)=$(TESTDIR)/%.o))
107testsrcs.o := $(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),$(testlangs)))
108testsrcs-rel := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), $(testsrcs-$(pkg).$(lang))))
109testsrcs := $(foreach sfile, $(testsrcs-rel), $(TESTSRCDIR)/$(sfile))
110
111# Refresh testfiles when sources change, but don't balk if the source file is nonexistent (deleted)
112$(generatedtest) : $(testsrcs)
113$(testsrcs) :
114
115$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.c | $$(@D)/.DIR
116	$(PETSC_COMPILE.c) $(abspath $<) -o $@
117
118$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.cxx | $$(@D)/.DIR
119	$(PETSC_COMPILE.cxx) $(abspath $<) -o $@
120
121$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.cu | $$(@D)/.DIR
122	$(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile
123	@$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later
124
125# Test modules go in a different directory
126$(TESTDIR)/%.o : MODDIR = $(@D)
127FCMOD = cd
128$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.F | $$(@D)/.DIR
129ifeq ($(FC_MODULE_OUTPUT_FLAG),)
130	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) -I$(dir $<) $(abspath $<) -o $(abspath $@)
131else
132	$(PETSC_COMPILE.F) -I$(dir $<) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
133endif
134	-@$(GFORTRAN_DEP_CLEANUP)
135
136$(TESTDIR)/%.o : $(EXAMPLESDIR)/%.F90 | $$(@D)/.DIR
137ifeq ($(FC_MODULE_OUTPUT_FLAG),)
138	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) -I$(dir $<) $(abspath $<) -o $(abspath $@)
139else
140	$(PETSC_COMPILE.F) -I$(dir $<) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
141endif
142	-@$(GFORTRAN_DEP_CLEANUP)
143
144#   This is a hack to fix a broken gfortran.
145define GFORTRAN_DEP_CLEANUP
146  if test -e "$(@:%.o=%.d)" && head -1 "$(@:%.o=%.d)" | fgrep -q -v : ; then\
147    echo "$(@): \\" > $(@:%.o=%.dtemp) ; \
148    tr '\n' '@' < $(@:%.o=%.d) | cut -d: -f2- | tr '@' '\n' >> $(@:%.o=%.dtemp) ; \
149    mv $(@:%.o=%.dtemp) $(@:%.o=%.d); \
150  fi
151endef
152
153# link line constructed differently for gmakefile vs gmakefile.test invocation
154ifeq ($(libpetscall),)
155PETSC_TEST_LIB = $(PETSC_LIB)
156else
157PETSC_TEST_LIB = $(C_SH_LIB_PATH) $(PETSC_EXTERNAL_LIB_BASIC)
158endif
159
160# Test executables
161$(testexe.F) $(testexe.F90) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
162	$(call quiet,FLINKER) -o $@ $^ $(PETSC_TEST_LIB)
163
164$(testexe.c) $(testexe.cu) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
165	$(call quiet,CLINKER) -o $@ $^ $(PETSC_TEST_LIB)
166
167$(testexe.cxx) : $(TESTDIR)/% : $(TESTDIR)/%.o $$^ $(libpetscall)
168	$(call quiet,CXX) -o $@ $^ $(PETSC_TEST_LIB)
169
170# Fortran source files need petsc*.mod, which isn't explicitly managed in the makefile.
171$(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),F F90)) : $(libpetscall)
172
173# Testing convenience targets
174.PHONY: test pre-clean
175
176test: pre-clean report_tests
177
178pre-clean:
179	@$(RM) -rf $(TESTDIR)/counts $(TESTLOGFILE)
180	@touch $(TESTLOGFILE)
181
182.PHONY: $(foreach pkg, $(pkgs), test-$(pkg) $(foreach lang, $(testlangs), test-$(pkg).$(lang) test-rm-$(pkg).$(lang)))
183testpkgs := $(foreach pkg, $(pkgs), test-$(pkg))
184# Targets to run tests in test-$pkg.$lang and delete the executables, language by language
185$(testpkgs) : test-% : $(foreach lang, $(testlangs), test-rm-%.$(lang))
186# List of raw test run targets
187alltesttargets := $(foreach tp, $(testpkgs), $(foreach lang, $(testlangs), $($(tp).$(lang))))
188
189# Run targets
190$(alltesttargets) : % : $(TESTDIR)/counts/%.counts
191.PHONY: $(alltesttargets)
192
193$(TESTDIR)/counts/%.counts :
194	$(quiettest) $< $(TESTFLAGS)
195
196# Targets to run tests and remove executables, by package-lang pairs.
197# Run the tests in each batch using recursive invocation of make because
198# we need all of them to complete before removing the executables.  Make
199# doesn't guarantee an exploration order for the graph.  Only recursive
200# if there is something to be done.
201alltest-rm := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), test-rm-$(pkg).$(lang)))
202$(alltest-rm) : test-rm-% : test-%
203ifneq ($(NO_RM),1)
204	$(call quiet,RM) $(addprefix $(TESTDIR)/,$(basename $($(@:test-rm-%=testsrcs-%))))
205endif
206
207# Remove intermediate .o files
208# This only removes the files at the end which is insufficient
209#.INTERMEDIATE: $(testsrcs.o:%.o=%)
210
211# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.)
212$(testsrcs.o) : $(petscvariables)
213
214%/.DIR :
215	@mkdir -p $(@D)
216	@touch $@
217
218.PRECIOUS: %/.DIR
219
220.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules
221.DELETE_ON_ERROR:               # Delete likely-corrupt target file if rule fails
222
223.PHONY: clean cleantest all print
224
225cleantest:
226	${RM} -r $(TESTDIR) $(generatedtest)
227
228clean: cleantest
229
230# make print VAR=the-variable
231print:
232	@echo $($(VAR))
233
234alltest.d := $(testsrcs.o:%.o=%.d)
235# Tell make that alltest.d are all up to date.  Without this, the include
236# below has quadratic complexity, taking more than one second for a
237# do-nothing build of PETSc (much worse for larger projects)
238$(alltest.d) : ;
239
240-include $(alltest.d)
241
242# Tests can be generated by searching
243# Percent is a wildcard (only one allowed):
244#    make -f gmakefile test search=sys%ex2
245# To match internal substrings (matches *ex2*):
246#    make -f gmakefile test searchin=ex2
247# Search and searchin can be combined:
248#    make -f gmakefile test search='sys%' searchin=ex2
249# For args:
250#    make -f gmakefile test argsearch=cuda
251# For general glob-style searching using python:
252# NOTE: uses shell which is possibly slower and is possibly more brittle
253#    make -f gmakefile test globsearch='sys*ex2*'
254ifdef search
255  TESTTARGETS := $(filter $(search),$(alltesttargets))
256  ifdef searchin
257    TESTTARGETS2 := $(foreach v,$(TESTTARGETS),$(if $(findstring $(searchin),$(v)),$(v)))
258    TESTTARGETS := $(TESTTARGETS2)
259  endif
260else ifdef searchin
261  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(searchin),$(v)),$(v)))
262else ifdef argsearch
263  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(argsearch),$($(v)_ARGS)),$(v)))
264else ifdef globsearch
265  TESTTARGETS := $(shell $(PYTHON) -c"import sys,fnmatch; print ' '.join(fnmatch.filter(sys.argv[2].split(),sys.argv[1]))" '$(globsearch)' '$(alltesttargets)')
266else                            # No filter - run them all, but delete the executables as we go
267  TESTTARGETS := $(testpkgs)
268endif
269
270.PHONY: report_tests print-test
271
272print-test:
273	-@echo $(TESTTARGETS)
274
275report_tests: $(TESTTARGETS)
276	-@$(PYTHON) $(CONFIGDIR)/report_tests.py -m $(MAKE) -d $(PETSC_DIR)/$(PETSC_ARCH)/tests/counts -t 5
277
278# Do not how how to invoke test from makefile
279HASGMAKEFILE := $(filter gmakefile,$(MAKEFILE_LIST))
280ifeq ($(HASGMAKEFILE),gmakefile)
281helpdeps:=help-make help-targets
282makefile="gmakefile"
283other_help="To see full test help: make -f gmakefile.test help"
284else
285helpdeps:=help-make help-targets help-test
286makefile="gmakefile.test"
287other_help=""
288endif
289
290help: ${helpdeps}
291	-@echo "Above is from: ${helpdeps}"
292	-@echo "${other_help}"
293
294help-make:
295	-@echo
296	-@echo "Basic build usage:"
297	-@echo "   make -f ${makefile} <options>"
298	-@echo
299	-@echo "Options:"
300	-@echo "  V=0           Very quiet builds"
301	-@echo "  V=1           Verbose builds"
302	-@echo
303
304help-targets:
305	-@echo "All makefile targets and their dependencies:"
306	-@grep ^[a-z] ${makefile} | grep : | grep -v =
307	-@echo
308	-@echo
309
310help-test:
311	-@echo "Basic test usage:"
312	-@echo "   make -f ${makefile} test <options>"
313	-@echo
314	-@echo "Options:"
315	-@echo "  NO_RM=1           Do not remove the executables after running"
316	-@echo "  REPLACE=1         Replace the output in PETSC_DIR source tree (-m to test scripts)"
317	-@echo "  ALT=1             Replace 'alt' output in PETSC_DIR source tree (-M to test scripts)"
318	-@echo "  DIFF_NUMBERS=1    Diff the numbers in the output (-j to test scripts and petscdiff)"
319	-@echo "  VALGRIND=1        Execute the tests using valgrind (-V to test scripts)"
320	-@echo "  NP=<num proc>     Set a number of processors to pass to scripts."
321	-@echo "  FORCE=1           Force SKIP or TODO tests to run"
322	-@echo "  TIMEOUT=<time>    Test timeout limit in seconds (default in config/petsc_harness.sh)"
323	-@echo "  TESTDIR='tests'   Subdirectory where tests are run ($${PETSC_DIR}/$${PETSC_ARCH}/$${TESTDIR}"
324	-@echo "                    or $${PREFIX_DIR}/$${TESTDIR}"
325	-@echo "                    or $${PREFIX_DIR}/share/petsc/examples/$${TESTDIR})"
326	-@echo "  TESTBASE='tests'   Subdirectory where tests are run ($${PETSC_DIR}/$${PETSC_ARCH}/$${TESTDIR}"
327	-@echo "  OPTIONS='<args>'  Override options to scripts (-a to test scripts)"
328	-@echo "  EXTRA_OPTIONS='<args>'  Add options to scripts (-e to test scripts)"
329	-@echo
330	-@echo "Tests can be generated by searching:"
331	-@echo "  Percent is a wildcard (only one allowed):"
332	-@echo "    make -f ${makefile} test search=sys%ex2"
333	-@echo
334	-@echo "  To match internal substrings (matches *ex2*):"
335	-@echo "    make -f ${makefile} test searchin=ex2"
336	-@echo
337	-@echo "  Search and searchin can be combined:"
338	-@echo "    make -f ${makefile} test search='sys%' searchin=ex2"
339	-@echo
340	-@echo "  To match patterns in the arguments:"
341	-@echo "    make -f ${makefile} test argsearch=cuda"
342	-@echo
343	-@echo "  For general glob-style searching using python:"
344	-@echo "   NOTE: uses shell which is possibly slower and more brittle"
345	-@echo "    make -f ${makefile} test globsearch='sys*ex2*'"
346	-@echo
347	-@echo "  To see which targets match a given pattern (useful for doing a specific target):"
348	-@echo "    make -f ${makefile} print-test search=sys%"
349	-@echo "  which is equivalent to:"
350	-@echo "    make -f ${makefile} print VAR=TESTTARGETS search='sys%'"
351	-@echo
352	-@echo "  To build an executable, give full path to location:"
353	-@echo '    make -f ${makefile} $${PETSC_ARCH}/tests/src/sys/examples/tests/ex1'
354	-@echo "  or make the test with NO_RM=1"
355	-@echo
356