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