1# -*- mode: makefile-gmake -*- 2include petscdir.mk 3 4# If $(PETSC_ARCH) is empty, this defines it and PETSC_DIR 5include ./$(PETSC_ARCH)/lib/petsc/conf/petscvariables 6include ./lib/petsc/conf/variables 7 8OBJDIR := $(PETSC_ARCH)/obj 9LIBDIR := $(PETSC_ARCH)/lib 10 11pkgs := sys vec mat dm ksp snes ts tao 12 13# $(call SONAME_FUNCTION,libfoo,abiversion) 14SONAME_FUNCTION ?= $(1).$(SL_LINKER_SUFFIX).$(2) 15# $(call SL_LINKER_FUNCTION,libfoo,abiversion,libversion) 16SL_LINKER_FUNCTION ?= -shared -Wl,-soname,$(call SONAME_FUNCTION,$(notdir $(1)),$(2)) 17 18PETSC_VERSION_MAJOR := $(shell awk '/define PETSC_VERSION_MAJOR/{print $$3;}' ./include/petscversion.h) 19PETSC_VERSION_MINOR := $(shell awk '/define PETSC_VERSION_MINOR/{print $$3;}' ./include/petscversion.h) 20PETSC_VERSION_SUBMINOR := $(shell awk '/define PETSC_VERSION_SUBMINOR/{print $$3;}' ./include/petscversion.h) 21PETSC_VERSION_RELEASE := $(shell awk '/define PETSC_VERSION_RELEASE/{print $$3;}' ./include/petscversion.h) 22 23libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(if $(filter $(PETSC_VERSION_RELEASE), 0 -2 -3 -4 -5),0)$(PETSC_VERSION_MINOR) 24libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR) 25soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version)) 26libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version)) 27absbasename_all = $(basename $(basename $(basename $(basename $(abspath $(1))))))# arch/lib/libpetsc.so.3.8.0 -> /path/to/arch/lib/libpetsc 28sl_linker_args = $(call SL_LINKER_FUNCTION,$(call absbasename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version)) 29 30libpetsc_shared := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX) 31libpetsc_soname := $(call soname_function,$(LIBDIR)/libpetsc) 32libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc) 33libpetsc_static := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX) 34libpetscpkgs_shared := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 35libpetscpkgs_soname := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg))) 36libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg))) 37libpetscpkgs_static := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX)) 38 39ifeq ($(PETSC_WITH_EXTERNAL_LIB),) 40 libpetscall_shared := $(libpetscpkgs_shared) 41 libpetscall_soname := $(libpetscpkgs_soname) 42 libpetscall_libname := $(libpetscpkgs_libname) 43 libpetscall_static := $(libpetscpkgs_static) 44else 45 libpetscall_shared := $(libpetsc_shared) 46 libpetscall_soname := $(libpetsc_soname) 47 libpetscall_libname := $(libpetsc_libname) 48 libpetscall_static := $(libpetsc_static) 49endif 50libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared),$(libpetscall_static)) 51 52generated := $(PETSC_ARCH)/lib/petsc/conf/files 53 54libs : $(libpetscall) 55.PHONY: libs 56 57.SECONDEXPANSION: # to expand $$(@D)/.DIR 58 59# Test framework includes rules and variables relevant to both build and test 60include ./gmakefile.test # This must be below the all target because it includes rules 61 62$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py 63 $(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH) 64 65# Skip including generated files (which triggers rebuilding them) when we're just going to clean anyway. 66ifneq ($(filter-out clean check,$(MAKECMDGOALS:clean%=clean)),) 67include $(generated) 68endif 69 70# implies shared libraries with MS compilers 71ifeq ($(PETSC_DLL_EXPORTS),1) 72$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS 73$(OBJDIR)/%.o : CXXCPPFLAGS+=-Dpetsc_EXPORTS 74endif 75 76ifneq ($(CUDAC),) 77langs := F F90 cu cxx c 78else 79langs := F F90 cxx c 80endif 81concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):src/%.$(lang)=$(OBJDIR)/%.o)) 82srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 83 84define SHARED_RECIPE_DLL 85 @$(RM) $@ dllcmd.${PETSC_ARCH} 86 @cygpath -w $^ > dllcmd.${PETSC_ARCH} 87 $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC) 88 @$(RM) dllcmd.${PETSC_ARCH} 89endef 90 91define SHARED_RECIPE_DEFAULT 92 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 93endef 94 95SHARED_RECIPE = $(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT)) 96 97# with-single-library=1 (default) 98$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 99 $(SHARED_RECIPE) 100ifneq ($(DSYMUTIL),true) 101 $(call quiet,DSYMUTIL) $@ 102endif 103 104$(libpetsc_static) : obj := $(srcs.o) 105 106define ARCHIVE_RECIPE_WIN32FE_LIB 107 @$(RM) $@ $@.args 108 @cygpath -w $^ > $@.args 109 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 110 @$(RM) $@.args 111endef 112 113define ARCHIVE_RECIPE_DEFAULT 114 @$(RM) $@ 115 $(call quiet,AR) $(AR_FLAGS) $@ $^ 116 $(call quiet,RANLIB) $@ 117endef 118 119%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 120 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 121 122# with-single-library=0 123libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 124define pkg_template 125 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 126 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 127endef 128$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 129$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 130$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 131$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 132$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 133$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 134$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 135$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 136 137# The package libraries technically depend on each other (not just in an order-only way), but only 138# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 139# only occur when a header is changed, which would trigger recompilation and relinking anyway. 140# RELINK=1 causes dependent libraries to be relinked anyway. 141ifeq ($(RELINK),1) 142 libdep_true = $$(libdep) 143 libdep_order = 144else 145 libdep_true = 146 libdep_order = $$(libdep) 147endif 148$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 149 $(SHARED_RECIPE) 150ifneq ($(DSYMUTIL),true) 151 $(call quiet,DSYMUTIL) $@ 152endif 153 154%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) | $(call soname_function,%) 155 @ln -sf $(notdir $<) $@ 156 157$(call soname_function,%) : $(call libname_function,%) 158 @ln -sf $(notdir $<) $@ 159 160.PRECIOUS: $(call soname_function,%) 161 162$(OBJDIR)/%.o : src/%.c | $$(@D)/.DIR 163 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 164 165$(OBJDIR)/%.o : src/%.cxx | $$(@D)/.DIR 166 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 167 168$(OBJDIR)/%.o : src/%.cu | $$(@D)/.DIR 169 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 170 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 171 172FCMOD = cd 173$(OBJDIR)/%.o : src/%.F | $$(@D)/.DIR 174ifeq ($(FC_MODULE_OUTPUT_FLAG),) 175 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 176else 177 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 178endif 179 -@$(GFORTRAN_DEP_CLEANUP) 180 181$(OBJDIR)/%.o : src/%.F90 | $$(@D)/.DIR 182ifeq ($(FC_MODULE_OUTPUT_FLAG),) 183 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 184else 185 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 186endif 187 -@$(GFORTRAN_DEP_CLEANUP) 188 189# Hack: manual dependencies on object files 190ifeq ($(MPI_IS_MPIUNI),1) 191 MPIUNI_MOD := $(MODDIR)/mpiuni.mod 192 $(OBJDIR)/sys/mpiuni/fsrc/somempifort.o : $(OBJDIR)/sys/mpiuni/f90-mod/mpiunimod.o 193endif 194$(OBJDIR)/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/sys/mpiuni/f90-mod/mpiunimod.o) 195$(OBJDIR)/vec/f90-mod/petscvecmod.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 196$(OBJDIR)/mat/f90-mod/petscmatmod.o : $(OBJDIR)/vec/f90-mod/petscvecmod.o 197$(OBJDIR)/dm/f90-mod/petscdmmod.o : $(OBJDIR)/mat/f90-mod/petscmatmod.o 198$(OBJDIR)/dm/f90-mod/petscdmdamod.o : $(OBJDIR)/dm/f90-mod/petscdmmod.o 199$(OBJDIR)/dm/f90-mod/petscdmplexmod.o : $(OBJDIR)/dm/f90-mod/petscdmmod.o 200$(OBJDIR)/ksp/f90-mod/petsckspdefmod.o : $(OBJDIR)/dm/f90-mod/petscdmplexmod.o 201$(OBJDIR)/ksp/f90-mod/petscpcmod.o : $(OBJDIR)/ksp/f90-mod/petsckspdefmod.o 202$(OBJDIR)/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/ksp/f90-mod/petscpcmod.o 203$(OBJDIR)/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/ksp/f90-mod/petsckspmod.o 204$(OBJDIR)/ts/f90-mod/petsctsmod.o : $(OBJDIR)/snes/f90-mod/petscsnesmod.o 205$(OBJDIR)/tao/f90-mod/petsctaomod.o : $(OBJDIR)/ts/f90-mod/petsctsmod.o 206# F2003 interface 207$(OBJDIR)/sys/objects/f2003-src/fsrc/optionenum.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 208$(OBJDIR)/sys/classes/bag/f2003-src/fsrc/bagenum.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 209 210# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 211$(srcs.o) : $(petscvariables) 212 213.PHONY: clean all print 214 215clean: 216 ${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated) 217 218# make print VAR=the-variable 219print: 220 $(info $($(VAR))) 221 @true 222 223 224allobj.d := $(srcs.o:%.o=%.d) 225# Tell make that allobj.d are all up to date. Without this, the include 226# below has quadratic complexity, taking more than one second for a 227# do-nothing build of PETSc (much worse for larger projects) 228$(allobj.d) : ; 229 230-include $(allobj.d) 231