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