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