xref: /petsc/gmakefile (revision 23462f55b239f23cfd635c9e6ef47bacb78c04eb)
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 := $(abspath $(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))
26basename_all = $(basename $(basename $(basename $(basename $(1)))))
27sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_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$(libpetscall_shared): $(libpetscall_soname)
56
57.SECONDEXPANSION:		# to expand $$(@D)/.DIR
58
59$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py
60	$(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH)
61
62-include $(generated)
63
64# implies shared libraries with MS compilers
65ifeq ($(SL_LINKER_FUNCTION),-LD)
66$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS
67endif
68
69langs := F cu cxx c
70concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o))
71srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg)))
72
73define SHARED_RECIPE_DLL
74  @$(RM) $@ dllcmd.${PETSC_ARCH}
75  @cygpath -w $^ > dllcmd.${PETSC_ARCH}
76  $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC)
77  @$(RM) dllcmd.${PETSC_ARCH}
78endef
79
80define SHARED_RECIPE_DEFAULT
81  $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC)
82endef
83
84# with-single-library=1 (default)
85$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR
86	$(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT))
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	$(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC)
137ifneq ($(DSYMUTIL),true)
138	$(call quiet,DSYMUTIL) $@
139endif
140
141%.$(SL_LINKER_SUFFIX) : $(call libname_function,%)
142	@ln -sf $(notdir $<) $@
143
144$(call soname_function,%) : $(call libname_function,%)
145	@ln -sf $(notdir $<) $@
146
147$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
148	$(PETSC_COMPILE.c) $(abspath $<) -o $@
149
150$(OBJDIR)/%.o : %.cxx | $$(@D)/.DIR
151	$(PETSC_COMPILE.cxx) $(abspath $<) -o $@
152
153$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
154	$(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile
155	@$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later
156
157FCMOD = cd
158$(OBJDIR)/%.o : %.F | $$(@D)/.DIR
159ifeq ($(FC_MODULE_OUTPUT_FLAG),)
160	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
161else
162	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
163endif
164
165$(OBJDIR)/%.o : %.F90 | $$(@D)/.DIR
166ifeq ($(FC_MODULE_OUTPUT_FLAG),)
167	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
168else
169	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
170endif
171
172# Hack: manual dependencies on object files
173ifeq ($(MPI_IS_MPIUNI),1)
174  MPIUNI_MOD := $(MODDIR)/mpi.mod
175endif
176$(OBJDIR)/src/sys/f90-mod/petscsysmod.o   : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o)
177$(OBJDIR)/src/vec/f90-mod/petscvecmod.o   : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
178$(OBJDIR)/src/mat/f90-mod/petscmatmod.o   : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o
179$(OBJDIR)/src/dm/f90-mod/petscdmmod.o     : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o
180$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o   : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o
181$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o
182$(OBJDIR)/src/ts/f90-mod/petsctsmod.o     : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o
183$(OBJDIR)/src/tao/f90-mod/petsctaomod.o   : $(OBJDIR)/src/ts/f90-mod/petsctsmod.o
184# F2003 interface
185$(OBJDIR)/src/sys/objects/f2003-src/fsrc/optionenum.o   : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
186$(OBJDIR)/src/sys/classes/bag/f2003-src/fsrc/bagenum.o  : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
187
188# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.)
189$(srcs.o) : $(petscvariables)
190
191.PHONY: clean all
192
193clean:
194	${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated)
195
196allobj.d := $(srcs.o:%.o=%.d)
197# Tell make that allobj.d are all up to date.  Without this, the include
198# below has quadratic complexity, taking more than one second for a
199# do-nothing build of PETSc (much worse for larger projects)
200$(allobj.d) : ;
201
202-include $(allobj.d)
203
204# Handle test framework (also defines compilation lines for both lib and test)
205include ./gmakefile.test        # This must be below the all target
206
207