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) 10TESTLOGFILE = $(TESTDIR)/examples-$(PETSC_ARCH).log 11 12pkgs := sys vec mat dm ksp snes ts tao 13 14# $(call SONAME_FUNCTION,libfoo,abiversion) 15SONAME_FUNCTION ?= $(1).$(SL_LINKER_SUFFIX).$(2) 16# $(call SL_LINKER_FUNCTION,libfoo,abiversion,libversion) 17SL_LINKER_FUNCTION ?= -shared -Wl,-soname,$(call SONAME_FUNCTION,$(notdir $(1)),$(2)) 18 19PETSC_VERSION_MAJOR := $(shell awk '/\#define PETSC_VERSION_MAJOR/{print $$3;}' ./include/petscversion.h) 20PETSC_VERSION_MINOR := $(shell awk '/\#define PETSC_VERSION_MINOR/{print $$3;}' ./include/petscversion.h) 21PETSC_VERSION_SUBMINOR := $(shell awk '/\#define PETSC_VERSION_SUBMINOR/{print $$3;}' ./include/petscversion.h) 22PETSC_VERSION_RELEASE := $(shell awk '/\#define PETSC_VERSION_RELEASE/{print $$3;}' ./include/petscversion.h) 23 24libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(if $(filter $(PETSC_VERSION_RELEASE), 0 -2 -3 -4 -5),0)$(PETSC_VERSION_MINOR) 25libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR) 26soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version)) 27libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version)) 28basename_all = $(basename $(basename $(basename $(basename $(1))))) 29sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version)) 30 31libpetsc_shared := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX) 32libpetsc_soname := $(call soname_function,$(LIBDIR)/libpetsc) 33libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc) 34libpetsc_static := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX) 35libpetscpkgs_shared := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 36libpetscpkgs_soname := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg))) 37libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg))) 38libpetscpkgs_static := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX)) 39 40ifeq ($(PETSC_WITH_EXTERNAL_LIB),) 41 libpetscall_shared := $(libpetscpkgs_shared) 42 libpetscall_soname := $(libpetscpkgs_soname) 43 libpetscall_libname := $(libpetscpkgs_libname) 44 libpetscall_static := $(libpetscpkgs_static) 45else 46 libpetscall_shared := $(libpetsc_shared) 47 libpetscall_soname := $(libpetsc_soname) 48 libpetscall_libname := $(libpetsc_libname) 49 libpetscall_static := $(libpetsc_static) 50endif 51libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared) $(libpetscall_soname),$(libpetscall_static)) 52 53generated := $(PETSC_ARCH)/lib/petsc/conf/files 54petscconf := $(PETSC_ARCH)/include/petscconf.h 55petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables 56 57all : $(generated) $(libpetscall) 58 59.SECONDEXPANSION: # to expand $$(@D)/.DIR 60 61 62#workarround old cygwin versions 63ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1) 64ifeq ($(shell basename $(AR)),ar) 65 V ?=1 66endif 67endif 68V ?= $(if $(findstring s,$(MAKEFLAGS)),0) 69ifeq ($(V),) # Default 70 quiet_HELP := "Use \"$(MAKE) V=1\" to see verbose compile lines, \"$(MAKE) V=0\" to suppress.\n" 71 quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)" %10s %s\n" "$1$2" "$@"; $($1) 72else ifeq ($(V),0) # Suppress entire command 73 quiet = @$($1) 74else # Show the full command line 75 quiet = $($1) 76endif 77 78$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py 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 89# implies shared libraries with MS compilers 90ifeq ($(SL_LINKER_FUNCTION),-LD) 91$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS 92endif 93 94PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS) 95PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS) 96PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 97PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)" 98PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) 99 100langs := F cu cxx c 101concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o)) 102srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg))) 103 104define SHARED_RECIPE_DLL 105 @$(RM) $@ dllcmd.${PETSC_ARCH} 106 @cygpath -w $^ > dllcmd.${PETSC_ARCH} 107 $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC) 108 @$(RM) dllcmd.${PETSC_ARCH} 109endef 110 111define SHARED_RECIPE_DEFAULT 112 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 113endef 114 115# with-single-library=1 (default) 116$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR 117 $(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT)) 118ifneq ($(DSYMUTIL),true) 119 $(call quiet,DSYMUTIL) $@ 120endif 121 122$(libpetsc_static) : obj := $(srcs.o) 123 124define ARCHIVE_RECIPE_WIN32FE_LIB 125 @$(RM) $@ $@.args 126 @cygpath -w $^ > $@.args 127 $(call quiet,AR) $(AR_FLAGS) $@ @$@.args 128 @$(RM) $@.args 129endef 130 131define ARCHIVE_RECIPE_DEFAULT 132 @$(RM) $@ 133 $(call quiet,AR) $(AR_FLAGS) $@ $^ 134 $(call quiet,RANLIB) $@ 135endef 136 137%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR 138 $(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT)) 139 140# with-single-library=0 141libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX)) 142define pkg_template 143 $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX) : $(call concatlang,$(1)) 144 $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1)) 145endef 146$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg)))) 147$(call libname_function,$(LIBDIR)/libpetscvec) : libdep := $(call libpkg,sys) 148$(call libname_function,$(LIBDIR)/libpetscmat) : libdep := $(call libpkg,vec sys) 149$(call libname_function,$(LIBDIR)/libpetscdm) : libdep := $(call libpkg,mat vec sys) 150$(call libname_function,$(LIBDIR)/libpetscksp) : libdep := $(call libpkg,dm mat vec sys) 151$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys) 152$(call libname_function,$(LIBDIR)/libpetscts) : libdep := $(call libpkg,snes ksp dm mat vec sys) 153$(call libname_function,$(LIBDIR)/libpetsctao) : libdep := $(call libpkg,snes ksp dm mat vec sys) 154 155# The package libraries technically depend on each other (not just in an order-only way), but only 156# ABI changes like new or removed symbols requires relinking the dependent libraries. ABI should 157# only occur when a header is changed, which would trigger recompilation and relinking anyway. 158# RELINK=1 causes dependent libraries to be relinked anyway. 159ifeq ($(RELINK),1) 160 libdep_true = $$(libdep) 161 libdep_order = 162else 163 libdep_true = 164 libdep_order = $$(libdep) 165endif 166$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR 167 $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC) 168ifneq ($(DSYMUTIL),true) 169 $(call quiet,DSYMUTIL) $@ 170endif 171 172%.$(SL_LINKER_SUFFIX) : $(call libname_function,%) 173 @ln -sf $(notdir $<) $@ 174 175$(call soname_function,%) : $(call libname_function,%) 176 @ln -sf $(notdir $<) $@ 177 178$(OBJDIR)/%.o : %.c | $$(@D)/.DIR 179 $(PETSC_COMPILE.c) $(abspath $<) -o $@ 180 181$(OBJDIR)/%.o : %.cxx | $$(@D)/.DIR 182 $(PETSC_COMPILE.cxx) $(abspath $<) -o $@ 183 184$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR 185 $(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile 186 @$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later 187 188FCMOD = cd 189$(OBJDIR)/%.o : %.F | $$(@D)/.DIR 190ifeq ($(FC_MODULE_OUTPUT_FLAG),) 191 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 192else 193 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 194endif 195 196$(OBJDIR)/%.o : %.F90 | $$(@D)/.DIR 197ifeq ($(FC_MODULE_OUTPUT_FLAG),) 198 $(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@) 199else 200 $(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR) 201endif 202 203# Hack: manual dependencies on object files 204ifeq ($(MPI_IS_MPIUNI),1) 205 MPIUNI_MOD := $(MODDIR)/mpi.mod 206endif 207$(OBJDIR)/src/sys/f90-mod/petscsysmod.o : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o) 208$(OBJDIR)/src/vec/f90-mod/petscvecmod.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 209$(OBJDIR)/src/mat/f90-mod/petscmatmod.o : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o 210$(OBJDIR)/src/dm/f90-mod/petscdmmod.o : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o 211$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o 212$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o 213$(OBJDIR)/src/ts/f90-mod/petsctsmod.o : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o 214$(OBJDIR)/src/tao/f90-mod/petsctaomod.o : $(OBJDIR)/src/ts/f90-mod/petsctsmod.o 215# F2003 interface 216$(OBJDIR)/src/sys/objects/f2003-src/fsrc/optionenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 217$(OBJDIR)/src/sys/classes/bag/f2003-src/fsrc/bagenum.o : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o 218 219# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.) 220$(srcs.o) : $(petscvariables) 221 222%/.DIR : 223 @mkdir -p $(@D) 224 @touch $@ 225 226.PRECIOUS: %/.DIR 227 228.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules 229.DELETE_ON_ERROR: # Delete likely-corrupt target file if rule fails 230 231.PHONY: clean all print 232 233clean: 234 ${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated) 235 236# make print VAR=the-variable 237print: 238 @echo $($(VAR)) 239 240allobj.d := $(srcs.o:%.o=%.d) 241# Tell make that allobj.d are all up to date. Without this, the include 242# below has quadratic complexity, taking more than one second for a 243# do-nothing build of PETSc (much worse for larger projects) 244$(allobj.d) : ; 245 246-include $(allobj.d) 247 248help: help-make help-targets 249 -@echo "Above is from: help-make help-targets" 250 251help-make: 252 -@echo "Basic build usage:" 253 -@echo " make -f gmakefile <options>" 254 -@echo 255 -@echo "Options:" 256 -@echo " V=0 Very quiet builds" 257 -@echo " V=1 Verbose builds" 258 -@echo 259 260help-targets: 261 -@echo "All makefile targets and their dependencies:" 262 -@grep ^[a-z] gmakefile | grep : | grep -v = 263 -@echo 264 -@echo 265