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