xref: /petsc/gmakefile (revision 2ec85ea456d4da44b4dd09fd939e615b8c7fb52b)
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
8TESTDIR := $(PETSC_ARCH)/tests
9MODDIR := $(PETSC_ARCH)/include
10LIBDIR := $(abspath $(PETSC_ARCH)/lib)
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)
23ifeq ($(PETSC_VERSION_RELEASE),0)
24  PETSC_VERSION_MINOR := 0$(PETSC_VERSION_MINOR)
25endif
26
27libpetsc_abi_version := $(PETSC_VERSION_MAJOR).$(PETSC_VERSION_MINOR)
28libpetsc_lib_version := $(libpetsc_abi_version).$(PETSC_VERSION_SUBMINOR)
29soname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_abi_version))
30libname_function = $(call SONAME_FUNCTION,$(1),$(libpetsc_lib_version))
31basename_all = $(basename $(basename $(basename $(basename $(1)))))
32sl_linker_args = $(call SL_LINKER_FUNCTION,$(call basename_all,$@),$(libpetsc_abi_version),$(libpetsc_lib_version))
33
34libpetsc_shared  := $(LIBDIR)/libpetsc.$(SL_LINKER_SUFFIX)
35libpetsc_soname  := $(call soname_function,$(LIBDIR)/libpetsc)
36libpetsc_libname := $(call libname_function,$(LIBDIR)/libpetsc)
37libpetsc_static  := $(LIBDIR)/libpetsc.$(AR_LIB_SUFFIX)
38libpetscpkgs_shared  := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX))
39libpetscpkgs_soname  := $(foreach pkg, $(pkgs), $(call soname_function,$(LIBDIR)/libpetsc$(pkg)))
40libpetscpkgs_libname := $(foreach pkg, $(pkgs), $(call libname_function,$(LIBDIR)/libpetsc$(pkg)))
41libpetscpkgs_static  := $(foreach pkg, $(pkgs), $(LIBDIR)/libpetsc$(pkg).$(AR_LIB_SUFFIX))
42
43ifeq ($(PETSC_WITH_EXTERNAL_LIB),)
44  libpetscall_shared  := $(libpetscpkgs_shared)
45  libpetscall_soname  := $(libpetscpkgs_soname)
46  libpetscall_libname := $(libpetscpkgs_libname)
47  libpetscall_static  := $(libpetscpkgs_static)
48else
49  libpetscall_shared  := $(libpetsc_shared)
50  libpetscall_soname  := $(libpetsc_soname)
51  libpetscall_libname := $(libpetsc_libname)
52  libpetscall_static  := $(libpetsc_static)
53endif
54libpetscall := $(if $(filter-out no,$(BUILDSHAREDLIB)),$(libpetscall_shared) $(libpetscall_soname),$(libpetscall_static))
55
56generated      := $(PETSC_ARCH)/lib/petsc/conf/files
57petscconf      := $(PETSC_ARCH)/include/petscconf.h
58petscvariables := $(PETSC_ARCH)/lib/petsc/conf/petscvariables
59generatedtest      := $(PETSC_ARCH)/lib/petsc/conf/testfiles
60
61all : $(generated) $(libpetscall)
62
63.SECONDEXPANSION:		# to expand $$(@D)/.DIR
64
65TESTFLAGS :=             # Initialize as simple variable
66
67#workarround old cygwin versions
68ifeq ($(PETSC_CYGWIN_BROKEN_PIPE),1)
69ifeq ($(shell basename $(AR)),ar)
70  V ?=1
71endif
72endif
73V ?= $(if $(findstring s,$(MAKEFLAGS)),0)
74ifeq ($(V),)  # Default
75  quiet_HELP := "Use \"$(MAKE) V=1\" to see verbose compile lines, \"$(MAKE) V=0\" to suppress.\n"
76  quiet = @printf $(quiet_HELP)$(eval quiet_HELP:=)"  %10s %s\n" "$1$2" "$@"; $($1)
77  quiettest = @printf "  %10s %s\n" "TEST" "$(@:$(TESTDIR)/counts/%.counts=%)";
78else ifeq ($(V),0)		# Suppress entire command
79  quiet = @$($1)
80  quiettest = @
81else				# Show the full command line
82  quiet = $($1)
83  quiettest =
84  TESTFLAGS += -v
85endif
86
87ifeq ($(REPLACE),1)
88  TESTFLAGS += -m    # Replace results by passing -m to petscdiff
89endif
90ifeq ($(DIFF_NUMBERS),1)
91  TESTFLAGS += -j    # Pass -j to petscdiff to diff the actual numbers
92endif
93ifdef OPTIONS
94  TESTFLAGS += -a '$(OPTIONS)'   # override arguments
95endif
96ifdef EXTRA_OPTIONS
97  TESTFLAGS += -e '$(EXTRA_OPTIONS)'  # add extra arguments
98endif
99
100
101$(generated) : $(petscconf) $(petscvariables) config/gmakegen.py
102	$(PYTHON) ./config/gmakegen.py --petsc-arch=$(PETSC_ARCH)
103
104$(generatedtest) : $(petscconf) $(petscvariables) config/gmakegentest.py | $$(@D)/.DIR
105	$(PYTHON) ./config/gmakegentest.py --petsc-arch=$(PETSC_ARCH)
106
107-include $(generated)
108-include $(generatedtest)
109
110ifeq ($(PETSC_LANGUAGE),CXXONLY)
111  cc_name := CXX
112else
113  cc_name := CC
114endif
115
116# implies shared libraries with MS compilers
117ifeq ($(SL_LINKER_FUNCTION),-LD)
118$(OBJDIR)/%.o : CCPPFLAGS+=-Dpetsc_EXPORTS
119endif
120
121PETSC_COMPILE.c = $(call quiet,$(cc_name)) -c $(PCC_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(C_DEPFLAGS)
122PETSC_COMPILE.cxx = $(call quiet,CXX) -c $(CXX_FLAGS) $(CFLAGS) $(CCPPFLAGS) $(CXX_DEPFLAGS)
123PETSC_COMPILE.cu = $(call quiet,CUDAC) -c $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
124PETSC_GENDEPS.cu = $(call quiet,CUDAC,.dep) --generate-dependencies --output-directory=$(@D) $(CUDAC_FLAGS) --compiler-options="$(PCC_FLAGS) $(CXXFLAGS) $(CCPPFLAGS)"
125PETSC_COMPILE.F = $(call quiet,FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS)
126
127langs := F cu cxx c
128concatlang = $(foreach lang, $(langs), $(srcs-$(1).$(lang):%.$(lang)=$(OBJDIR)/%.o))
129srcs.o := $(foreach pkg, $(pkgs), $(call concatlang,$(pkg)))
130testlangs := c cu cxx F F90
131concattestlang = $(foreach lang, $(2), $(testsrcs-$(1).$(lang):%.$(lang)=$(TESTDIR)/%.o))
132testsrcs.o := $(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),$(testlangs)))
133
134define SHARED_RECIPE_DLL
135  @$(RM) $@ dllcmd.${PETSC_ARCH}
136  @cygpath -w $^ > dllcmd.${PETSC_ARCH}
137  $(call quiet,CLINKER) $(sl_linker_args) -o $@ @dllcmd.${PETSC_ARCH} $(PETSC_EXTERNAL_LIB_BASIC)
138  @$(RM) dllcmd.${PETSC_ARCH}
139endef
140
141define SHARED_RECIPE_DEFAULT
142  $(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC)
143endef
144
145# with-single-library=1 (default)
146$(libpetsc_libname) : $(srcs.o) | $$(@D)/.DIR
147	$(if $(findstring -LD,$(SL_LINKER_FUNCTION)),$(SHARED_RECIPE_DLL),$(SHARED_RECIPE_DEFAULT))
148ifneq ($(DSYMUTIL),true)
149	$(call quiet,DSYMUTIL) $@
150endif
151
152$(libpetsc_static) : obj := $(srcs.o)
153
154define ARCHIVE_RECIPE_WIN32FE_LIB
155  @$(RM) $@ $@.args
156  @cygpath -w $^ > $@.args
157  $(call quiet,AR) $(AR_FLAGS) $@ @$@.args
158  @$(RM) $@.args
159endef
160
161define ARCHIVE_RECIPE_DEFAULT
162  @$(RM) $@
163  $(call quiet,AR) $(AR_FLAGS) $@ $^
164  $(call quiet,RANLIB) $@
165endef
166
167%.$(AR_LIB_SUFFIX) : $$(obj) | $$(@D)/.DIR
168	$(if $(findstring win32fe lib,$(AR)),$(ARCHIVE_RECIPE_WIN32FE_LIB),$(ARCHIVE_RECIPE_DEFAULT))
169
170# with-single-library=0
171libpkg = $(foreach pkg, $1, $(LIBDIR)/libpetsc$(pkg).$(SL_LINKER_SUFFIX))
172define pkg_template
173  $(LIBDIR)/libpetsc$(1).$(AR_LIB_SUFFIX)  : $(call concatlang,$(1))
174  $(call libname_function,$(LIBDIR)/libpetsc$(1)) : $(call concatlang,$(1))
175endef
176$(foreach pkg,$(pkgs),$(eval $(call pkg_template,$(pkg))))
177$(call libname_function,$(LIBDIR)/libpetscvec)  : libdep := $(call libpkg,sys)
178$(call libname_function,$(LIBDIR)/libpetscmat)  : libdep := $(call libpkg,vec sys)
179$(call libname_function,$(LIBDIR)/libpetscdm)   : libdep := $(call libpkg,mat vec sys)
180$(call libname_function,$(LIBDIR)/libpetscksp)  : libdep := $(call libpkg,dm mat vec sys)
181$(call libname_function,$(LIBDIR)/libpetscsnes) : libdep := $(call libpkg,ksp dm mat vec sys)
182$(call libname_function,$(LIBDIR)/libpetscts)   : libdep := $(call libpkg,snes ksp dm mat vec sys)
183$(call libname_function,$(LIBDIR)/libpetsctao)  : libdep := $(call libpkg,snes ksp dm mat vec sys)
184
185# The package libraries technically depend on each other (not just in an order-only way), but only
186# ABI changes like new or removed symbols requires relinking the dependent libraries.  ABI should
187# only occur when a header is changed, which would trigger recompilation and relinking anyway.
188# RELINK=1 causes dependent libraries to be relinked anyway.
189ifeq ($(RELINK),1)
190  libdep_true = $$(libdep)
191  libdep_order =
192else
193  libdep_true =
194  libdep_order = $$(libdep)
195endif
196$(libpetscpkgs_libname) : $(libdep_true) | $(libdep_order) $$(@D)/.DIR
197	$(call quiet,CLINKER) $(sl_linker_args) -o $@ $^ $(PETSC_EXTERNAL_LIB_BASIC)
198ifneq ($(DSYMUTIL),true)
199	$(call quiet,DSYMUTIL) $@
200endif
201
202%.$(SL_LINKER_SUFFIX) : $(call libname_function,%)
203	@ln -sf $(notdir $<) $@
204
205$(call soname_function,%) : $(call libname_function,%)
206	@ln -sf $(notdir $<) $@
207
208$(OBJDIR)/%.o $(TESTDIR)/%.o : %.c | $$(@D)/.DIR
209	$(PETSC_COMPILE.c) $(abspath $<) -o $@
210
211$(OBJDIR)/%.o $(TESTDIR)/%.o : %.cxx | $$(@D)/.DIR
212	$(PETSC_COMPILE.cxx) $(abspath $<) -o $@
213
214$(OBJDIR)/%.o $(TESTDIR)/%.o : %.cu | $$(@D)/.DIR
215	$(PETSC_COMPILE.cu) $(abspath $<) -o $@ # Compile first so that if there is an error, it comes from a normal compile
216	@$(PETSC_GENDEPS.cu) $(abspath $<) -o $(@:%.o=%.d) # Generate the dependencies for later
217
218# Test modules go in a different directory
219$(TESTDIR)/%.o : MODDIR = $(@D)
220FCMOD = cd
221$(OBJDIR)/%.o $(TESTDIR)/%.o : %.F | $$(@D)/.DIR
222ifeq ($(FC_MODULE_OUTPUT_FLAG),)
223	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
224else
225	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
226endif
227
228$(OBJDIR)/%.o $(TESTDIR)/%.o : %.F90 | $$(@D)/.DIR
229ifeq ($(FC_MODULE_OUTPUT_FLAG),)
230	$(call quiet,FCMOD) $(MODDIR) && $(FC) -c $(FC_FLAGS) $(FFLAGS) $(FCPPFLAGS) $(FC_DEPFLAGS) $(abspath $<) -o $(abspath $@)
231else
232	$(PETSC_COMPILE.F) $(abspath $<) -o $@ $(FC_MODULE_OUTPUT_FLAG)$(MODDIR)
233endif
234
235# Test executables
236$(TESTDIR)/%f : $(TESTDIR)/%f.o $(libpetscall)
237	$(call quiet,FLINKER) -o $@ $< $(PETSC_LIB)
238
239$(TESTDIR)/%f90 : $(TESTDIR)/%f90.o $(libpetscall)
240	$(call quiet,FLINKER) -o $@ $< $(PETSC_LIB)
241
242$(TESTDIR)/% : $(TESTDIR)/%.o $(libpetscall)
243	$(call quiet,CLINKER) -o $@ $< $(PETSC_LIB)
244
245# Fortran source files need petsc*.mod, which isn't explicitly managed in the makefile.
246$(foreach pkg, $(pkgs), $(call concattestlang,$(pkg),F F90)) : $(libpetscall)
247
248# Testing convenience targets
249.PHONY: alltest test pre-clean
250test: pre-clean report_tests
251pre-clean:
252	@$(RM) -r $(TESTDIR)/counts
253.PHONY: $(foreach pkg, $(pkgs), test-$(pkg) $(foreach lang, $(testlangs), test-$(pkg).$(lang) test-rm-$(pkg).$(lang)))
254testpkgs := $(foreach pkg, $(pkgs), test-$(pkg))
255# Targets to run tests in test-$pkg.$lang and delete the executables, language by language
256$(testpkgs) : test-% : $(foreach lang, $(testlangs), test-rm-%.$(lang))
257# List of raw test run targets
258alltesttargets := $(foreach tp, $(testpkgs), $(foreach lang, $(testlangs), $($(tp).$(lang))))
259
260# Run targets
261$(alltesttargets) : % : $(TESTDIR)/counts/%.counts
262.PHONY: $(alltesttargets)
263
264$(TESTDIR)/counts/%.counts :
265	$(quiettest) $< $(TESTFLAGS)
266
267# Targets to run tests and remove executables, by package-lang pairs.
268# Run the tests in each batch using recursive invocation of make because
269# we need all of them to complete before removing the executables.  Make
270# doesn't guarantee an exploration order for the graph.  Only recursive
271# if there is something to be done.
272alltest-rm := $(foreach pkg, $(pkgs), $(foreach lang, $(testlangs), test-rm-$(pkg).$(lang)))
273$(alltest-rm) : test-rm-% : test-%
274ifneq ($(NO_RM),1)
275	$(call quiet,RM) $(addprefix $(TESTDIR)/,$(basename $($(@:test-rm-%=testsrcs-%))))
276endif
277
278# Remove intermediate .o files
279# This only removes the files at the end which is insufficient
280#.INTERMEDIATE: $(testsrcs.o:%.o=%)
281
282# Hack: manual dependencies on object files
283ifeq ($(MPI_IS_MPIUNI),1)
284  MPIUNI_MOD := $(MODDIR)/mpi.mod
285endif
286$(OBJDIR)/src/sys/f90-mod/petscsysmod.o   : $(if $(MPIUNI_MOD),$(OBJDIR)/src/sys/mpiuni/f90-mod/mpiunimod.o)
287$(OBJDIR)/src/vec/f90-mod/petscvecmod.o   : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
288$(OBJDIR)/src/mat/f90-mod/petscmatmod.o   : $(OBJDIR)/src/vec/f90-mod/petscvecmod.o
289$(OBJDIR)/src/dm/f90-mod/petscdmmod.o     : $(OBJDIR)/src/mat/f90-mod/petscmatmod.o
290$(OBJDIR)/src/ksp/f90-mod/petsckspmod.o   : $(OBJDIR)/src/dm/f90-mod/petscdmmod.o
291$(OBJDIR)/src/snes/f90-mod/petscsnesmod.o : $(OBJDIR)/src/ksp/f90-mod/petsckspmod.o
292$(OBJDIR)/src/ts/f90-mod/petsctsmod.o     : $(OBJDIR)/src/snes/f90-mod/petscsnesmod.o
293$(OBJDIR)/src/tao/f90-mod/petsctaomod.o   : $(OBJDIR)/src/ts/f90-mod/petsctsmod.o
294# F2003 interface
295$(OBJDIR)/src/sys/objects/f2003-src/fsrc/optionenum.o   : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
296$(OBJDIR)/src/sys/classes/bag/f2003-src/fsrc/bagenum.o  : $(OBJDIR)/src/sys/f90-mod/petscsysmod.o
297
298# all sources should get recompiled when petscvariables changes (i.e when configure is rerun or when petscvariables is manually edited.)
299$(srcs.o) : $(petscvariables)
300$(testsrcs.o) : $(petscvariables)
301
302%/.DIR :
303	@mkdir -p $(@D)
304	@touch $@
305
306.PRECIOUS: %/.DIR
307
308.SUFFIXES: # Clear .SUFFIXES because we don't use implicit rules
309.DELETE_ON_ERROR:               # Delete likely-corrupt target file if rule fails
310
311.PHONY: clean cleantest all print
312
313cleantest:
314	${RM} -r $(TESTDIR) $(generatedtest)
315
316clean: cleantest
317	${RM} -r $(OBJDIR) $(LIBDIR)/libpetsc* $(MODDIR)/petsc*.mod $(MPIUNI_MOD) $(generated)
318
319# make print VAR=the-variable
320print:
321	@echo $($(VAR))
322
323allobj.d := $(srcs.o:%.o=%.d)
324alltest.d := $(testsrcs.o:%.o=%.d)
325# Tell make that allobj.d are all up to date.  Without this, the include
326# below has quadratic complexity, taking more than one second for a
327# do-nothing build of PETSc (much worse for larger projects)
328$(allobj.d) : ;
329$(alltest.d) : ;
330
331-include $(allobj.d)
332-include $(alltest.d)
333
334# Tests can be generated by searching
335# Percent is a wildcard (only one allowed):
336#    make -f gmakefile test search=sys%ex2
337# To match internal substrings (matches *ex2*):
338#    make -f gmakefile test searchin=ex2
339# Search and searchin can be combined:
340#    make -f gmakefile test search='sys%' searchin=ex2
341# For args:
342#    make -f gmakefile test argsearch=cuda
343# For general glob-style searching using python:
344# NOTE: uses shell which is possibly slower and is possibly more brittle
345#    make -f gmakefile test pysearch='sys*ex2*'
346ifdef search
347  TESTTARGETS := $(filter $(search),$(alltesttargets))
348  ifdef searchin
349    TESTTARGETS2 := $(foreach v,$(TESTTARGETS),$(if $(findstring $(searchin),$(v)),$(v)))
350    TESTTARGETS := $(TESTTARGETS2)
351  endif
352else ifdef searchin
353  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(searchin),$(v)),$(v)))
354else ifdef argsearch
355  TESTTARGETS := $(foreach v,$(alltesttargets),$(if $(findstring $(argsearch),$($(v)_ARGS)),$(v)))
356else ifdef pysearch
357  TESTTARGETS := $(shell $(PYTHON) -c"import sys,fnmatch; print ' '.join(fnmatch.filter(sys.argv[2].split(),sys.argv[1]))" '$(pysearch)' '$(alltesttargets)')
358else                            # No filter - run them all, but delete the executables as we go
359  TESTTARGETS := $(testpkgs)
360endif
361
362.PHONY: report_tests echo_test
363
364echo_test:
365	-@echo $(TESTTARGETS)
366
367report_tests: $(TESTTARGETS)
368	-@$(PYTHON) config/report_tests.py -d $(PETSC_ARCH)/tests/counts
369