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 ($(SL_LINKER_FUNCTION),-LD) 72$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS 73endif 74 75langs := F F90 cu cxx c 76concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):src/%.$(lang)=$(OBJDIR)/%.o)) 77srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 78 79define SHARED_RECIPE_DLL 80 @$(RM) $@ dllcmd.${PETSC_ARCH} 81 @cygpath -w $^ > dllcmd.${PETSC_ARCH} 82 $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC) 83 @$(RM) dllcmd.${PETSC_ARCH} 84endef 85 86define SHARED_RECIPE_DEFAULT 87 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 88endef 89 90SHARED_RECIPE = $(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT)) 91 92# with-single-library=1 (default) 93$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 94 $(SHARED_RECIPE) 95ifneq ($(DSYMUTIL),true) 96 $(call quiet,DSYMUTIL) $@ 97endif 98 99$(libpetsc_static) : obj := $(srcs.o) 100 101define ARCHIVE_RECIPE_WIN32FE_LIB 102 @$(RM) $@ $@.args 103 @cygpath -w $^ > $@.args 104 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 105 @$(RM) $@.args 106endef 107 108define ARCHIVE_RECIPE_DEFAULT 109 @$(RM) $@ 110 $(call quiet,AR) $(AR_FLAGS) $@ $^ 111 $(call quiet,RANLIB) $@ 112endef 113 114%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 115 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 116 117# with-single-library=0 118libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 119define pkg_template 120 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 121 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 122endef 123$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 124$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 125$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 126$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 127$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 128$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 129$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 130$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 131 132# The package libraries technically depend on each other (not just in an order-only way), but only 133# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 134# only occur when a header is changed, which would trigger recompilation and relinking anyway. 135# RELINK=1 causes dependent libraries to be relinked anyway. 136ifeq ($(RELINK),1) 137 libdep_true = $$(libdep) 138 libdep_order = 139else 140 libdep_true = 141 libdep_order = $$(libdep) 142endif 143$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 144 $(SHARED_RECIPE) 145ifneq ($(DSYMUTIL),true) 146 $(call quiet,DSYMUTIL) $@ 147endif 148 149%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) | $(call soname_function,%) 150 @ln -sf $(notdir $<) $@ 151 152$(call soname_function,%) : $(call libname_function,%) 153 @ln -sf $(notdir $<) $@ 154 155.PRECIOUS: $(call soname_function,%) 156 157$(OBJDIR)/%.o : src/%.c | $$(@D)/.DIR 158 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 159 160$(OBJDIR)/%.o : src/%.cxx | $$(@D)/.DIR 161 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 162 163$(OBJDIR)/%.o : src/%.cu | $$(@D)/.DIR 164 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 165 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 166 167FCMOD = cd 168$(OBJDIR)/%.o : src/%.F | $$(@D)/.DIR 169ifeq ($(FC_MODULE_OUTPUT_FLAG),) 170 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 171else 172 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 173endif 174 -@$(GFORTRAN_DEP_CLEANUP) 175 176$(OBJDIR)/%.o : src/%.F90 | $$(@D)/.DIR 177ifeq ($(FC_MODULE_OUTPUT_FLAG),) 178 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 179else 180 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 181endif 182 -@$(GFORTRAN_DEP_CLEANUP) 183 184# Hack: manual dependencies on object files 185ifeq ($(MPI_IS_MPIUNI),1) 186 MPIUNI_MOD := $(MODDIR)/mpiuni.mod 187 $(OBJDIR)/sys/mpiuni/fsrc/somempifort.o : $(OBJDIR)/sys/mpiuni/f90-mod/mpiunimod.o 188endif 189$(OBJDIR)/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/sys/mpiuni/f90-mod/mpiunimod.o) 190$(OBJDIR)/vec/f90-mod/petscvecmod.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 191$(OBJDIR)/mat/f90-mod/petscmatmod.o : $(OBJDIR)/vec/f90-mod/petscvecmod.o 192$(OBJDIR)/dm/f90-mod/petscdmmod.o : $(OBJDIR)/mat/f90-mod/petscmatmod.o 193$(OBJDIR)/dm/f90-mod/petscdmdamod.o : $(OBJDIR)/dm/f90-mod/petscdmmod.o 194$(OBJDIR)/dm/f90-mod/petscdmplexmod.o : $(OBJDIR)/dm/f90-mod/petscdmmod.o 195$(OBJDIR)/ksp/f90-mod/petsckspdefmod.o : $(OBJDIR)/dm/f90-mod/petscdmplexmod.o 196$(OBJDIR)/ksp/f90-mod/petscpcmod.o : $(OBJDIR)/ksp/f90-mod/petsckspdefmod.o 197$(OBJDIR)/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/ksp/f90-mod/petscpcmod.o 198$(OBJDIR)/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/ksp/f90-mod/petsckspmod.o 199$(OBJDIR)/ts/f90-mod/petsctsmod.o : $(OBJDIR)/snes/f90-mod/petscsnesmod.o 200$(OBJDIR)/tao/f90-mod/petsctaomod.o : $(OBJDIR)/ts/f90-mod/petsctsmod.o 201# F2003 interface 202$(OBJDIR)/sys/objects/f2003-src/fsrc/optionenum.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 203$(OBJDIR)/sys/classes/bag/f2003-src/fsrc/bagenum.o : $(OBJDIR)/sys/f90-mod/petscsysmod.o 204 205# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 206$(srcs.o) : $(petscvariables) 207 208.PHONY: clean all print 209 210clean: 211 ${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated) 212 213# make print VAR=the-variable 214print: 215 @echo $($(VAR)) 216 217allobj.d := $(srcs.o:%.o=%.d) 218# Tell make that allobj.d are all up to date. Without this, the include 219# below has quadratic complexity, taking more than one second for a 220# do-nothing build of PETSc (much worse for larger projects) 221$(allobj.d) : ; 222 223-include $(allobj.d) 224