1# -*- mode: makefile-gmake -*- 2 3# If $(PETSC_ARCH) is empty, this defines it and PETSC_DIR 4include ./$(PETSC_ARCH)/lib/petsc/conf/petscvariables 5include ./lib/petsc/conf/variables 6 7OBJDIR := $(PETSC_ARCH)/obj 8TESTDIR := $(PETSC_ARCH)/tests 9MODDIR := $(PETSC_ARCH)/include 10LIBDIR := $(abspath $(PETSC_ARCH)/lib) 11 12pkgs := sys vec mat dm ksp snes ts tao 13 14# $(call SONAME_FUNCTION,libfoo,abiversion) 15SONAME_FUNCTION ?= $(1).$(SL_LINKER_SUFFIX).$(2) 16# $(call SL_LINKER_FUNCTION,libfoo,abiversion,libversion) 17SL_LINKER_FUNCTION ?= -shared -Wl,-soname,$(call SONAME_FUNCTION,$(notdir $(1)),$(2)) 18 19PETSC_VERSION_MAJOR := $(shell awk '/\#define PETSC_VERSION_MAJOR/{print $$3;}' ./include/petscversion.h) 20PETSC_VERSION_MINOR := $(shell awk '/\#define PETSC_VERSION_MINOR/{print $$3;}' ./include/petscversion.h) 21PETSC_VERSION_SUBMINOR := $(shell awk '/\#define PETSC_VERSION_SUBMINOR/{print $$3;}' ./include/petscversion.h) 22PETSC_VERSION_RELEASE := $(shell awk '/\#define PETSC_VERSION_RELEASE/{print $$3;}' ./include/petscversion.h) 23ifeq ($(PETSC_VERSION_RELEASE),0) 24 PETSC_VERSION_MINOR := 0$(PETSC_VERSION_MINOR) 25endif 26 27libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(PETSC_VERSION_MINOR) 28libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR) 29soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version)) 30libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version)) 31basename_all = $(basename $(basename $(basename $(basename $(1))))) 32sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version)) 33 34libpetsc_shared := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX) 35libpetsc_soname := $(call soname_function,$(LIBDIR)/libpetsc) 36libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc) 37libpetsc_static := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX) 38libpetscpkgs_shared := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 39libpetscpkgs_soname := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg))) 40libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg))) 41libpetscpkgs_static := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX)) 42 43ifeq ($(PETSC_WITH_EXTERNAL_LIB),) 44 libpetscall_shared := $(libpetscpkgs_shared) 45 libpetscall_soname := $(libpetscpkgs_soname) 46 libpetscall_libname := $(libpetscpkgs_libname) 47 libpetscall_static := $(libpetscpkgs_static) 48else 49 libpetscall_shared := $(libpetsc_shared) 50 libpetscall_soname := $(libpetsc_soname) 51 libpetscall_libname := $(libpetsc_libname) 52 libpetscall_static := $(libpetsc_static) 53endif 54libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared) $(libpetscall_soname),$(libpetscall_static)) 55 56generated := $(PETSC_ARCH)/lib/petsc/conf/files 57petscconf := $(PETSC_ARCH)/include/petscconf.h 58petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables 59generatedtest := $(PETSC_ARCH)/lib/petsc/conf/testfiles 60 61all : $(generated) $(libpetscall) 62 63.SECONDEXPANSION: # to expand $$(@D)/.DIR 64 65TESTFLAGS := # Initialize as simple variable 66 67#workarround old cygwin versions 68ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1) 69ifeq ($(shell basename $(AR)),ar) 70 V ?=1 71endif 72endif 73V ?= $(if $(findstring s,$(MAKEFLAGS)),0) 74ifeq ($(V),) # Default 75 quiet_HELP := "Use \"$(MAKE) V=1\" to see verbose compile lines, \"$(MAKE) V=0\" to suppress.\n" 76 quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)" %10s %s\n" "$1$2" "$@"; $($1) 77 quiettest = @printf " %10s %s\n" "TEST" "$(@:$(TESTDIR)/counts/%.counts=%)"; 78else ifeq ($(V),0) # Suppress entire command 79 quiet = @$($1) 80 quiettest = @ 81else # Show the full command line 82 quiet = $($1) 83 quiettest = 84 TESTFLAGS += -v 85endif 86 87ifeq ($(VALGRIND),1) 88 TESTFLAGS += -V # Add valgrind to the flags 89endif 90ifeq ($(REPLACE),1) 91 TESTFLAGS += -m # Replace results by passing -m to petscdiff 92endif 93ifeq ($(DIFF_NUMBERS),1) 94 TESTFLAGS += -j # Pass -j to petscdiff to diff the actual numbers 95endif 96ifdef OPTIONS 97 TESTFLAGS += -a '$(OPTIONS)' # override arguments 98endif 99ifdef EXTRA_OPTIONS 100 TESTFLAGS += -e '$(EXTRA_OPTIONS)' # add extra arguments 101endif 102ifdef NP 103 TESTFLAGS += -n $(NP) # set number of processes 104endif 105 106 107$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py 108 $(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH) 109 110$(generatedtest) : $(petscconf) $(petscvariables) config/gmakegentest.py | $$(@D)/.DIR 111 $(PYTHON) ./config/gmakegentest.py --petsc-arch=$(PETSC_ARCH) 112 113-include $(generated) 114-include $(generatedtest) 115 116ifeq ($(PETSC_LANGUAGE),CXXONLY) 117 cc_name := CXX 118else 119 cc_name := CC 120endif 121 122# implies shared libraries with MS compilers 123ifeq ($(SL_LINKER_FUNCTION),-LD) 124$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS 125endif 126 127PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS) 128PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS) 129PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 130PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 131PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) 132 133langs := F cu cxx c 134concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o)) 135srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 136testlangs := c cu cxx F F90 137concattestlang = $(foreach lang, $(2), $(testsrcs-$(1).$(lang):%.$(lang)=$(TESTDIR)/%.o)) 138testsrcs.o := $(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),$(testlangs))) 139testsrcs := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), $(testsrcs-$(pkg).$(lang)))) 140 141# Refresh testfiles when sources change, but don't balk if the source file is nonexistent (deleted) 142$(generatedtest) : $(testsrcs) 143$(testsrcs) : 144 145 146define SHARED_RECIPE_DLL 147 @$(RM) $@ dllcmd.${PETSC_ARCH} 148 @cygpath -w $^ > dllcmd.${PETSC_ARCH} 149 $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC) 150 @$(RM) dllcmd.${PETSC_ARCH} 151endef 152 153define SHARED_RECIPE_DEFAULT 154 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 155endef 156 157# with-single-library=1 (default) 158$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 159 $(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT)) 160ifneq ($(DSYMUTIL),true) 161 $(call quiet,DSYMUTIL) $@ 162endif 163 164$(libpetsc_static) : obj := $(srcs.o) 165 166define ARCHIVE_RECIPE_WIN32FE_LIB 167 @$(RM) $@ $@.args 168 @cygpath -w $^ > $@.args 169 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 170 @$(RM) $@.args 171endef 172 173define ARCHIVE_RECIPE_DEFAULT 174 @$(RM) $@ 175 $(call quiet,AR) $(AR_FLAGS) $@ $^ 176 $(call quiet,RANLIB) $@ 177endef 178 179%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 180 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 181 182# with-single-library=0 183libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 184define pkg_template 185 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 186 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 187endef 188$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 189$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 190$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 191$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 192$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 193$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 194$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 195$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 196 197# The package libraries technically depend on each other (not just in an order-only way), but only 198# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 199# only occur when a header is changed, which would trigger recompilation and relinking anyway. 200# RELINK=1 causes dependent libraries to be relinked anyway. 201ifeq ($(RELINK),1) 202 libdep_true = $$(libdep) 203 libdep_order = 204else 205 libdep_true = 206 libdep_order = $$(libdep) 207endif 208$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 209 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 210ifneq ($(DSYMUTIL),true) 211 $(call quiet,DSYMUTIL) $@ 212endif 213 214%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) 215 @ln -sf $(notdir $<) $@ 216 217$(call soname_function,%) : $(call libname_function,%) 218 @ln -sf $(notdir $<) $@ 219 220$(OBJDIR)/%.o $(TESTDIR)/%.o : %.c | $$(@D)/.DIR 221 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 222 223$(OBJDIR)/%.o $(TESTDIR)/%.o : %.cxx | $$(@D)/.DIR 224 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 225 226$(OBJDIR)/%.o $(TESTDIR)/%.o : %.cu | $$(@D)/.DIR 227 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 228 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 229 230# Test modules go in a different directory 231$(TESTDIR)/%.o : MODDIR = $(@D) 232FCMOD = cd 233$(OBJDIR)/%.o $(TESTDIR)/%.o : %.F | $$(@D)/.DIR 234ifeq ($(FC_MODULE_OUTPUT_FLAG),) 235 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 236else 237 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 238endif 239 240$(OBJDIR)/%.o $(TESTDIR)/%.o : %.F90 | $$(@D)/.DIR 241ifeq ($(FC_MODULE_OUTPUT_FLAG),) 242 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 243else 244 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 245endif 246 247# Test executables 248$(TESTDIR)/%f : $(TESTDIR)/%f.o $(libpetscall) 249 $(call quiet,FLINKER) -o $@ $< $(PETSC_LIB) 250 251$(TESTDIR)/%f90 : $(TESTDIR)/%f90.o $(libpetscall) 252 $(call quiet,FLINKER) -o $@ $< $(PETSC_LIB) 253 254$(TESTDIR)/% : $(TESTDIR)/%.o $(libpetscall) 255 $(call quiet,CLINKER) -o $@ $< $(PETSC_LIB) 256 257# Fortran source files need petsc*.mod, which isn't explicitly managed in the makefile. 258$(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),F F90)) : $(libpetscall) 259 260# Testing convenience targets 261.PHONY: alltest test pre-clean 262test: pre-clean report_tests 263pre-clean: 264 @$(RM) -r $(TESTDIR)/counts 265.PHONY: $(foreach pkg, $(pkgs), test-$(pkg) $(foreach lang, $(testlangs), test-$(pkg).$(lang) test-rm-$(pkg).$(lang))) 266testpkgs := $(foreach pkg, $(pkgs), test-$(pkg)) 267# Targets to run tests in test-$pkg.$lang and delete the executables, language by language 268$(testpkgs) : test-% : $(foreach lang, $(testlangs), test-rm-%.$(lang)) 269# List of raw test run targets 270alltesttargets := $(foreach tp, $(testpkgs), $(foreach lang, $(testlangs), $($(tp).$(lang)))) 271 272# Run targets 273$(alltesttargets) : % : $(TESTDIR)/counts/%.counts 274.PHONY: $(alltesttargets) 275 276$(TESTDIR)/counts/%.counts : 277 $(quiettest) $< $(TESTFLAGS) 278 279# Targets to run tests and remove executables, by package-lang pairs. 280# Run the tests in each batch using recursive invocation of make because 281# we need all of them to complete before removing the executables. Make 282# doesn't guarantee an exploration order for the graph. Only recursive 283# if there is something to be done. 284alltest-rm := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), test-rm-$(pkg).$(lang))) 285$(alltest-rm) : test-rm-% : test-% 286ifneq ($(NO_RM),1) 287 $(call quiet,RM) $(addprefix $(TESTDIR)/,$(basename $($(@:test-rm-%=testsrcs-%)))) 288endif 289 290# Remove intermediate .o files 291# This only removes the files at the end which is insufficient 292#.INTERMEDIATE: $(testsrcs.o:%.o=%) 293 294# Hack: manual dependencies on object files 295ifeq ($(MPI_IS_MPIUNI),1) 296 MPIUNI_MOD := $(MODDIR)/mpi.mod 297endif 298$(OBJDIR)/src/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o) 299$(OBJDIR)/src/vec/f90-mod/petscvecmod.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 300$(OBJDIR)/src/mat/f90-mod/petscmatmod.o : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o 301$(OBJDIR)/src/dm/f90-mod/petscdmmod.o : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o 302$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o 303$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o 304$(OBJDIR)/src/ts/f90-mod/petsctsmod.o : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o 305$(OBJDIR)/src/tao/f90-mod/petsctaomod.o : $(OBJDIR)/src/ts/f90-mod/petsctsmod.o 306# F2003 interface 307$(OBJDIR)/src/sys/objects/f2003-src/fsrc/optionenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 308$(OBJDIR)/src/sys/classes/bag/f2003-src/fsrc/bagenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 309 310# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 311$(srcs.o) : $(petscvariables) 312$(testsrcs.o) : $(petscvariables) 313 314%/.DIR : 315 @mkdir -p $(@D) 316 @touch $@ 317 318.PRECIOUS: %/.DIR 319 320.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules 321.DELETE_ON_ERROR: # Delete likely-corrupt target file if rule fails 322 323.PHONY: clean cleantest all print 324 325cleantest: 326 ${RM} -r $(TESTDIR) $(generatedtest) 327 328clean: cleantest 329 ${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated) 330 331# make print VAR=the-variable 332print: 333 @echo $($(VAR)) 334 335allobj.d := $(srcs.o:%.o=%.d) 336alltest.d := $(testsrcs.o:%.o=%.d) 337# Tell make that allobj.d are all up to date. Without this, the include 338# below has quadratic complexity, taking more than one second for a 339# do-nothing build of PETSc (much worse for larger projects) 340$(allobj.d) : ; 341$(alltest.d) : ; 342 343-include $(allobj.d) 344-include $(alltest.d) 345 346# Tests can be generated by searching 347# Percent is a wildcard (only one allowed): 348# make -f gmakefile test search=sys%ex2 349# To match internal substrings (matches *ex2*): 350# make -f gmakefile test searchin=ex2 351# Search and searchin can be combined: 352# make -f gmakefile test search='sys%' searchin=ex2 353# For args: 354# make -f gmakefile test argsearch=cuda 355# For general glob-style searching using python: 356# NOTE: uses shell which is possibly slower and is possibly more brittle 357# make -f gmakefile test pysearch='sys*ex2*' 358ifdef search 359 TESTTARGETS := $(filter $(search),$(alltesttargets)) 360 ifdef searchin 361 TESTTARGETS2 := $(foreach v,$(TESTTARGETS),$(if $(findstring $(searchin),$(v)),$(v))) 362 TESTTARGETS := $(TESTTARGETS2) 363 endif 364else ifdef searchin 365 TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(searchin),$(v)),$(v))) 366else ifdef argsearch 367 TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(argsearch),$($(v)_ARGS)),$(v))) 368else ifdef pysearch 369 TESTTARGETS := $(shell $(PYTHON) -c"import sys,fnmatch; print ' '.join(fnmatch.filter(sys.argv[2].split(),sys.argv[1]))" '$(pysearch)' '$(alltesttargets)') 370else # No filter - run them all, but delete the executables as we go 371 TESTTARGETS := $(testpkgs) 372endif 373 374.PHONY: report_tests echo_test 375 376echo_test: 377 -@echo $(TESTTARGETS) 378 379report_tests: $(TESTTARGETS) 380 -@$(PYTHON) config/report_tests.py -d $(PETSC_ARCH)/tests/counts 381