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