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