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