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