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