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