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 8MODDIR := $(PETSC_ARCH)/include 9LIBDIR := $(abspath $(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) 22ifeq ($(PETSC_VERSION_RELEASE),0) 23 PETSC_VERSION_MINOR := 0$(PETSC_VERSION_MINOR) 24endif 25 26libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(PETSC_VERSION_MINOR) 27libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR) 28soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version)) 29libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version)) 30basename_all = $(basename $(basename $(basename $(basename $(1))))) 31sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version)) 32 33libpetsc_shared := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX) 34libpetsc_soname := $(call soname_function,$(LIBDIR)/libpetsc) 35libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc) 36libpetsc_static := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX) 37libpetscpkgs_shared := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 38libpetscpkgs_soname := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg))) 39libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg))) 40libpetscpkgs_static := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX)) 41 42ifeq ($(PETSC_WITH_EXTERNAL_LIB),) 43 libpetscall_shared := $(libpetscpkgs_shared) 44 libpetscall_soname := $(libpetscpkgs_soname) 45 libpetscall_libname := $(libpetscpkgs_libname) 46 libpetscall_static := $(libpetscpkgs_static) 47else 48 libpetscall_shared := $(libpetsc_shared) 49 libpetscall_soname := $(libpetsc_soname) 50 libpetscall_libname := $(libpetsc_libname) 51 libpetscall_static := $(libpetsc_static) 52endif 53libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared) $(libpetscall_soname),$(libpetscall_static)) 54 55generated := $(PETSC_ARCH)/lib/petsc/conf/files 56petscconf := $(PETSC_ARCH)/include/petscconf.h 57petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables 58 59all : $(generated) $(libpetscall) 60 61.SECONDEXPANSION: # to expand $$(@D)/.DIR 62 63#workarround old cygwin versions 64ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1) 65ifeq ($(shell basename $(AR)),ar) 66 V ?=1 67endif 68endif 69ifeq ($(V),) 70 quiet_HELP := "Use \"$(MAKE) V=1\" to see the verbose compile lines.\n" 71 quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)" %10s %s\n" "$1$2" "$@"; $($1) 72else ifeq ($(V),0) # Same, but do not print any help 73 quiet = @printf " %10s %s\n" "$1$2" "$@"; $($1) 74else # Show the full command line 75 quiet = $($1) 76endif 77 78$(generated) : $(petscconf) $(petscvariables) 79 $(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH) 80 81-include $(generated) 82 83ifeq ($(PETSC_LANGUAGE),CXXONLY) 84 cc_name := CXX 85else 86 cc_name := CC 87endif 88 89PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS) 90PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS) 91PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 92PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 93PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) 94 95langs := c cu cxx F 96concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o)) 97srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 98 99# with-single-library=1 (default) 100$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 101 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 102ifneq ($(DSYMUTIL),true) 103 $(call quiet,DSYMUTIL) $@ 104endif 105 106$(libpetsc_static) : obj := $(srcs.o) 107 108define ARCHIVE_RECIPE_WIN32FE_LIB 109 @$(RM) $@ $@.args 110 @cygpath -w $^ > $@.args 111 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 112 @$(RM) $@.args 113endef 114 115define ARCHIVE_RECIPE_DEFAULT 116 @$(RM) $@ 117 $(call quiet,AR) $(AR_FLAGS) $@ $^ 118 $(call quiet,RANLIB) $@ 119endef 120 121%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 122 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 123 124# with-single-library=0 125libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 126define pkg_template 127 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 128 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 129endef 130$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 131$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 132$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 133$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 134$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 135$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 136$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 137$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 138 139# The package libraries technically depend on each other (not just in an order-only way), but only 140# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 141# only occur when a header is changed, which would trigger recompilation and relinking anyway. 142# RELINK=1 causes dependent libraries to be relinked anyway. 143ifeq ($(RELINK),1) 144 libdep_true = $$(libdep) 145 libdep_order = 146else 147 libdep_true = 148 libdep_order = $$(libdep) 149endif 150$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 151 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 152ifneq ($(DSYMUTIL),true) 153 $(call quiet,DSYMUTIL) $@ 154endif 155 156%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) 157 @ln -sf $(notdir $<) $@ 158 159$(call soname_function,%) : $(call libname_function,%) 160 @ln -sf $(notdir $<) $@ 161 162$(OBJDIR)/%.o : %.c | $$(@D)/.DIR 163 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 164 165$(OBJDIR)/%.o : %.cxx | $$(@D)/.DIR 166 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 167 168$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR 169 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 170 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 171 172$(OBJDIR)/%.o : %.F | $$(@D)/.DIR 173ifeq ($(FC_MODULE_OUTPUT_FLAG),) 174 cd $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 175else 176 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 177endif 178 179# Hack: manual dependencies on object files 180ifeq ($(MPI_IS_MPIUNI),1) 181 MPIUNI_MOD := $(MODDIR)/mpi.mod 182endif 183$(OBJDIR)/src/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o) 184$(OBJDIR)/src/vec/f90-mod/petscvecmod.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 185$(OBJDIR)/src/mat/f90-mod/petscmatmod.o : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o 186$(OBJDIR)/src/dm/f90-mod/petscdmmod.o : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o 187$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o 188$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o 189$(OBJDIR)/src/ts/f90-mod/petsctsmod.o : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o 190 191# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 192$(srcs.o) : $(petscvariables) 193 194%/.DIR : 195 @mkdir -p $(@D) 196 @touch $@ 197 198.PRECIOUS: %/.DIR 199 200.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules 201.DELETE_ON_ERROR: # Delete likely-corrupt target file if rule fails 202 203.PHONY: clean all print 204 205clean: 206 ${RM} -rf $(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