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