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