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 8MODDIR := $(PETSC_ARCH)/include 9LIBDIR := $(abspath $(PETSC_ARCH)/lib) 10TESTLOGFILE = $(TESTDIR)/examples-$(PETSC_ARCH).log 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) 23 24libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(if $(filter $(PETSC_VERSION_RELEASE), 0 -2 -3 -4 -5),0)$(PETSC_VERSION_MINOR) 25libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR) 26soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version)) 27libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version)) 28basename_all = $(basename $(basename $(basename $(basename $(1))))) 29sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version)) 30 31libpetsc_shared := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX) 32libpetsc_soname := $(call soname_function,$(LIBDIR)/libpetsc) 33libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc) 34libpetsc_static := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX) 35libpetscpkgs_shared := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 36libpetscpkgs_soname := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg))) 37libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg))) 38libpetscpkgs_static := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX)) 39 40ifeq ($(PETSC_WITH_EXTERNAL_LIB),) 41 libpetscall_shared := $(libpetscpkgs_shared) 42 libpetscall_soname := $(libpetscpkgs_soname) 43 libpetscall_libname := $(libpetscpkgs_libname) 44 libpetscall_static := $(libpetscpkgs_static) 45else 46 libpetscall_shared := $(libpetsc_shared) 47 libpetscall_soname := $(libpetsc_soname) 48 libpetscall_libname := $(libpetsc_libname) 49 libpetscall_static := $(libpetsc_static) 50endif 51libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared) $(libpetscall_soname),$(libpetscall_static)) 52 53generated := $(PETSC_ARCH)/lib/petsc/conf/files 54petscconf := $(PETSC_ARCH)/include/petscconf.h 55petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables 56 57all : $(generated) $(libpetscall) 58 59.SECONDEXPANSION: # to expand $$(@D)/.DIR 60 61 62#workarround old cygwin versions 63ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1) 64ifeq ($(shell basename $(AR)),ar) 65 V ?=1 66endif 67endif 68V ?= $(if $(findstring s,$(MAKEFLAGS)),0) 69ifeq ($(V),) # Default 70 quiet_HELP := "Use \"$(MAKE) V=1\" to see verbose compile lines, \"$(MAKE) V=0\" to suppress.\n" 71 quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)" %10s %s\n" "$1$2" "$@"; $($1) 72else ifeq ($(V),0) # Suppress entire command 73 quiet = @$($1) 74else # Show the full command line 75 quiet = $($1) 76endif 77 78 79$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py 80 $(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH) 81 82-include $(generated) 83 84ifeq ($(PETSC_LANGUAGE),CXXONLY) 85 cc_name := CXX 86else 87 cc_name := CC 88endif 89 90# implies shared libraries with MS compilers 91ifeq ($(SL_LINKER_FUNCTION),-LD) 92$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS 93endif 94 95PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS) 96PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS) 97PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 98PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 99PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) 100 101langs := F cu cxx c 102concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o)) 103srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 104 105define SHARED_RECIPE_DLL 106 @$(RM) $@ dllcmd.${PETSC_ARCH} 107 @cygpath -w $^ > dllcmd.${PETSC_ARCH} 108 $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC) 109 @$(RM) dllcmd.${PETSC_ARCH} 110endef 111 112define SHARED_RECIPE_DEFAULT 113 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 114endef 115 116# with-single-library=1 (default) 117$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 118 $(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT)) 119ifneq ($(DSYMUTIL),true) 120 $(call quiet,DSYMUTIL) $@ 121endif 122 123$(libpetsc_static) : obj := $(srcs.o) 124 125define ARCHIVE_RECIPE_WIN32FE_LIB 126 @$(RM) $@ $@.args 127 @cygpath -w $^ > $@.args 128 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 129 @$(RM) $@.args 130endef 131 132define ARCHIVE_RECIPE_DEFAULT 133 @$(RM) $@ 134 $(call quiet,AR) $(AR_FLAGS) $@ $^ 135 $(call quiet,RANLIB) $@ 136endef 137 138%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 139 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 140 141# with-single-library=0 142libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 143define pkg_template 144 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 145 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 146endef 147$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 148$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 149$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 150$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 151$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 152$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 153$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 154$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 155 156# The package libraries technically depend on each other (not just in an order-only way), but only 157# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 158# only occur when a header is changed, which would trigger recompilation and relinking anyway. 159# RELINK=1 causes dependent libraries to be relinked anyway. 160ifeq ($(RELINK),1) 161 libdep_true = $$(libdep) 162 libdep_order = 163else 164 libdep_true = 165 libdep_order = $$(libdep) 166endif 167$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 168 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 169ifneq ($(DSYMUTIL),true) 170 $(call quiet,DSYMUTIL) $@ 171endif 172 173%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) 174 @ln -sf $(notdir $<) $@ 175 176$(call soname_function,%) : $(call libname_function,%) 177 @ln -sf $(notdir $<) $@ 178 179$(OBJDIR)/%.o : %.c | $$(@D)/.DIR 180 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 181 182$(OBJDIR)/%.o : %.cxx | $$(@D)/.DIR 183 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 184 185$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR 186 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 187 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 188 189FCMOD = cd 190$(OBJDIR)/%.o : %.F | $$(@D)/.DIR 191ifeq ($(FC_MODULE_OUTPUT_FLAG),) 192 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 193else 194 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 195endif 196 197$(OBJDIR)/%.o : %.F90 | $$(@D)/.DIR 198ifeq ($(FC_MODULE_OUTPUT_FLAG),) 199 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 200else 201 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 202endif 203 204# Hack: manual dependencies on object files 205ifeq ($(MPI_IS_MPIUNI),1) 206 MPIUNI_MOD := $(MODDIR)/mpi.mod 207endif 208$(OBJDIR)/src/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o) 209$(OBJDIR)/src/vec/f90-mod/petscvecmod.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 210$(OBJDIR)/src/mat/f90-mod/petscmatmod.o : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o 211$(OBJDIR)/src/dm/f90-mod/petscdmmod.o : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o 212$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o 213$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o 214$(OBJDIR)/src/ts/f90-mod/petsctsmod.o : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o 215$(OBJDIR)/src/tao/f90-mod/petsctaomod.o : $(OBJDIR)/src/ts/f90-mod/petsctsmod.o 216# F2003 interface 217$(OBJDIR)/src/sys/objects/f2003-src/fsrc/optionenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 218$(OBJDIR)/src/sys/classes/bag/f2003-src/fsrc/bagenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 219 220# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 221$(srcs.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 all print 233 234clean: 235 ${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated) 236 237# make print VAR=the-variable 238print: 239 @echo $($(VAR)) 240 241allobj.d := $(srcs.o:%.o=%.d) 242# Tell make that allobj.d are all up to date. Without this, the include 243# below has quadratic complexity, taking more than one second for a 244# do-nothing build of PETSc (much worse for larger projects) 245$(allobj.d) : ; 246 247-include $(allobj.d) 248 249help: help-make help-targets 250 -@echo "Above is from: help-make help-targets" 251 252help-make: 253 -@echo "Basic build usage:" 254 -@echo " make -f gmakefile <options>" 255 -@echo 256 -@echo "Options:" 257 -@echo " V=0 Very quiet builds" 258 -@echo " V=1 Verbose builds" 259 -@echo 260 261help-targets: 262 -@echo "All makefile targets and their dependencies:" 263 -@grep ^[a-z] gmakefile | grep : | grep -v = 264 -@echo 265 -@echo 266