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