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