xref: /libCEED/Makefile (revision b0ab70dd13713bb77a4685114f435ef96a079a6f)
1# Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2# Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3# All Rights reserved. See files LICENSE and NOTICE for details.
4#
5# This file is part of CEED, a collection of benchmarks, miniapps, software
6# libraries and APIs for efficient high-order finite element and spectral
7# element discretizations for exascale applications. For more information and
8# source code availability see http://github.com/ceed.
9#
10# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11# a collaborative effort of two U.S. Department of Energy organizations (Office
12# of Science and the National Nuclear Security Administration) responsible for
13# the planning and preparation of a capable exascale ecosystem, including
14# software, applications, hardware, advanced system engineering and early
15# testbed platforms, in support of the nation's exascale computing imperative.
16
17CONFIG ?= config.mk
18-include $(CONFIG)
19
20ifeq (,$(filter-out undefined default,$(origin CC)))
21  CC = gcc
22endif
23ifeq (,$(filter-out undefined default,$(origin CXX)))
24  CXX = g++
25endif
26ifeq (,$(filter-out undefined default,$(origin FC)))
27  FC = gfortran
28endif
29ifeq (,$(filter-out undefined default,$(origin LINK)))
30  LINK = $(CC)
31endif
32NVCC ?= $(CUDA_DIR)/bin/nvcc
33NVCC_CXX ?= $(CXX)
34
35# ASAN must be left empty if you don't want to use it
36ASAN ?=
37
38LDFLAGS ?=
39UNDERSCORE ?= 1
40
41# MFEM_DIR env variable should point to sibling directory
42ifneq ($(wildcard ../mfem/libmfem.*),)
43  MFEM_DIR ?= ../mfem
44endif
45
46# NEK5K_DIR env variable should point to sibling directory
47ifneq ($(wildcard ../Nek5000/*),)
48  NEK5K_DIR ?= $(abspath ../Nek5000)
49endif
50export NEK5K_DIR
51MPI ?= 1
52
53# CEED_DIR env for NEK5K testing
54export CEED_DIR = $(abspath .)
55
56# XSMM_DIR env variable should point to XSMM master (github.com/hfp/libxsmm)
57XSMM_DIR ?= ../libxsmm
58
59# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
60OCCA_DIR ?= ../occa
61
62# env variable MAGMA_DIR can be used too
63MAGMA_DIR ?= ../magma
64# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
65CUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
66               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
67
68# Check for PETSc in ../petsc
69ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
70  PETSC_DIR ?= ../petsc
71endif
72
73# Warning: SANTIZ options still don't run with /gpu/occa
74# export LSAN_OPTIONS=suppressions=.asanignore
75AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
76
77MARCHFLAG := $(if $(shell $(CC) -E -march=native -x c /dev/null > /dev/null 2>&1 && echo 1),-march=native,-mtune=native)
78
79OPT    ?= -O -g $(MARCHFLAG) -ffp-contract=fast -fopenmp-simd
80CFLAGS ?= -std=c99 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
81CXXFLAGS ?= -std=c++11 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
82NVCCFLAGS ?= -ccbin $(CXX) -Xcompiler "$(OPT)" -Xcompiler -fPIC
83# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately:
84ifneq ($(filter %xlf %xlf_r,$(FC)),)
85  FFLAGS ?= $(OPT) -ffree-form -qpreprocess -qextname -qpic -MMD
86else # gfortran/Intel-style options
87  FFLAGS ?= -cpp     $(OPT) -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP
88endif
89
90ifeq ($(COVERAGE), 1)
91  CFLAGS += --coverage
92  LDFLAGS += --coverage
93endif
94
95CFLAGS += $(if $(ASAN),$(AFLAGS))
96FFLAGS += $(if $(ASAN),$(AFLAGS))
97LDFLAGS += $(if $(ASAN),$(AFLAGS))
98CPPFLAGS += -I./include
99LDLIBS = -lm
100OBJDIR := build
101LIBDIR := lib
102
103# Installation variables
104prefix ?= /usr/local
105bindir = $(prefix)/bin
106libdir = $(prefix)/lib
107okldir = $(libdir)/okl
108includedir = $(prefix)/include
109pkgconfigdir = $(libdir)/pkgconfig
110INSTALL = install
111INSTALL_PROGRAM = $(INSTALL)
112INSTALL_DATA = $(INSTALL) -m644
113
114# Get number of processors of the machine
115NPROCS := $(shell getconf _NPROCESSORS_ONLN)
116# prepare make options to run in parallel
117MFLAGS := -j $(NPROCS) --warn-undefined-variables \
118                       --no-print-directory --no-keep-going
119
120PYTHON ?= python3
121PROVE ?= prove
122PROVE_OPTS ?= -j $(NPROCS)
123DARWIN := $(filter Darwin,$(shell uname -s))
124SO_EXT := $(if $(DARWIN),dylib,so)
125
126ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
127libceed := $(LIBDIR)/libceed.$(SO_EXT)
128CEED_LIBS = -lceed
129libceed.c := $(wildcard interface/ceed*.c)
130gallery.c := $(wildcard gallery/*/ceed*.c)
131libceed.c += $(gallery.c)
132libceed_test := $(LIBDIR)/libceed_test.$(SO_EXT)
133libceeds = $(libceed) $(libceed_test)
134BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
135BACKENDS := $(BACKENDS_BUILTIN)
136
137# Tests
138tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
139tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90))
140tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
141ctests    := $(tests)
142tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
143# Examples
144examples.c := $(sort $(wildcard examples/ceed/*.c))
145examples.f := $(sort $(wildcard examples/ceed/*.f))
146examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
147examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
148# MFEM Examples
149mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
150mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
151# Nek5K Examples
152nekexamples  := $(OBJDIR)/nek-bps
153# PETSc Examples
154petscexamples.c := $(wildcard examples/petsc/*.c)
155petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
156# Navier-Stokes Examples
157nsexamples.c := $(sort $(wildcard examples/navier-stokes/*.c))
158nsexamples  := $(nsexamples.c:examples/navier-stokes/%.c=$(OBJDIR)/ns-%)
159
160# Backends/[ref, blocked, template, memcheck, opt, avx, occa, magma]
161ref.c          := $(sort $(wildcard backends/ref/*.c))
162blocked.c      := $(sort $(wildcard backends/blocked/*.c))
163template.c     := $(sort $(wildcard backends/template/*.c))
164ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
165opt.c          := $(sort $(wildcard backends/opt/*.c))
166avx.c          := $(sort $(wildcard backends/avx/*.c))
167xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
168cuda.c         := $(sort $(wildcard backends/cuda/*.c))
169cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
170cuda.cu        := $(sort $(wildcard backends/cuda/*.cu))
171cuda-reg.c     := $(sort $(wildcard backends/cuda-reg/*.c))
172cuda-reg.cu    := $(sort $(wildcard backends/cuda-reg/*.cu))
173cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
174cuda-shared.cu := $(sort $(wildcard backends/cuda-shared/*.cu))
175cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
176cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
177cuda-gen.cu    := $(sort $(wildcard backends/cuda-gen/*.cu))
178occa.c         := $(sort $(wildcard backends/occa/*.c))
179magma_preprocessor := python backends/magma/gccm.py
180magma_pre_src  := $(filter-out %ceed-magma.c %_tmp.c, $(wildcard backends/magma/ceed-*.c))
181magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
182magma_dsrc     += backends/magma/ceed-magma.c
183magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
184magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
185magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
186magma_allsrc.cu:= $(magma_tmp.cu) backends/magma/magma_devptr.cu
187magma_allsrc.cu+= backends/magma/magma_dbasisApply_grad.cu backends/magma/magma_dbasisApply_interp.cu backends/magma/magma_dbasisApply_weight.cu
188magma_allsrc.cu+= backends/magma/magma_drestrictApply.cu
189
190# Output using the 216-color rules mode
191rule_file = $(notdir $(1))
192rule_path = $(patsubst %/,%,$(dir $(1)))
193last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
194ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
195emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
196color_out = @if [ -t 1 ]; then \
197				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
198					$(1) $(call ansicolor,$(2)) \
199					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
200				printf "  %10s %s\n" $(1) $(2); fi
201# if TERM=dumb, use it, otherwise switch to the term one
202output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
203
204# if V is set to non-nil, turn the verbose mode
205quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
206
207# Cancel built-in and old-fashioned implicit rules which we don't use
208.SUFFIXES:
209
210.SECONDEXPANSION: # to expand $$(@D)/.DIR
211
212.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
213
214%/.DIR :
215	@mkdir -p $(@D)
216	@touch $@
217
218.PRECIOUS: %/.DIR
219
220lib: $(libceed) $(ceed.pc)
221# run 'lib' target in parallel
222par:;@$(MAKE) $(MFLAGS) V=$(V) lib
223backend_status = $(if $(filter $1,$(BACKENDS)), [backends: $1], [not found])
224info:
225	$(info ------------------------------------)
226	$(info CC            = $(CC))
227	$(info CXX           = $(CXX))
228	$(info FC            = $(FC))
229	$(info CPPFLAGS      = $(CPPFLAGS))
230	$(info CFLAGS        = $(value CFLAGS))
231	$(info CXXFLAGS      = $(value CXXFLAGS))
232	$(info FFLAGS        = $(value FFLAGS))
233	$(info NVCCFLAGS     = $(value NVCCFLAGS))
234	$(info LDFLAGS       = $(value LDFLAGS))
235	$(info LDLIBS        = $(LDLIBS))
236	$(info OPT           = $(OPT))
237	$(info AFLAGS        = $(AFLAGS))
238	$(info ASAN          = $(or $(ASAN),(empty)))
239	$(info V             = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
240	$(info ------------------------------------)
241	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,/cpu/self/memcheck/serial /cpu/sef/memcheck/blocked))
242	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,/cpu/self/avx/serial /cpu/self/avx/blocked))
243	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,/cpu/self/xsmm/serial /cpu/self/xsmm/blocked))
244	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,/cpu/occa /gpu/occa /omp/occa))
245	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,/gpu/magma))
246	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
247	$(info ------------------------------------)
248	$(info MFEM_DIR      = $(MFEM_DIR))
249	$(info NEK5K_DIR     = $(NEK5K_DIR))
250	$(info PETSC_DIR     = $(PETSC_DIR))
251	$(info ------------------------------------)
252	$(info prefix        = $(prefix))
253	$(info includedir    = $(value includedir))
254	$(info libdir        = $(value libdir))
255	$(info okldir        = $(value okldir))
256	$(info pkgconfigdir  = $(value pkgconfigdir))
257	$(info ------------------------------------)
258	@true
259info-backends:
260	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
261.PHONY: lib all par info info-backends
262
263$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
264$(libceed_test) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed_test)))
265
266# Standard Backends
267libceed.c += $(ref.c)
268libceed.c += $(blocked.c)
269libceed.c += $(opt.c)
270
271# Testing Backends
272test_backends.c := $(template.c)
273TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
274
275# Memcheck Backend
276MEMCHK_STATUS = Disabled
277MEMCHK := $(shell echo "\#include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
278ifeq ($(MEMCHK),1)
279  MEMCHK_STATUS = Enabled
280  libceed.c += $(ceedmemcheck.c)
281  BACKENDS += /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
282endif
283
284# AVX Backed
285AVX_STATUS = Disabled
286AVX := $(shell $(CC) $(OPT) -v -E - < /dev/null 2>&1 | grep -c ' -mavx')
287ifeq ($(AVX),1)
288  AVX_STATUS = Enabled
289  libceed.c += $(avx.c)
290  BACKENDS += /cpu/self/avx/serial /cpu/self/avx/blocked
291endif
292
293# libXSMM Backends
294ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
295  $(libceeds) : LDFLAGS += -L$(XSMM_DIR)/lib -Wl,-rpath,$(abspath $(XSMM_DIR)/lib)
296  $(libceeds) : LDLIBS += -lxsmm -ldl
297  MKL ?=
298  ifeq (,$(MKL)$(MKLROOT))
299    BLAS_LIB = -lblas
300  else
301    ifneq ($(MKLROOT),)
302      # Some installs put everything inside an intel64 subdirectory, others not
303      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
304      MKL_LINK = -L$(MKL_LIBDIR) -Wl,-rpath,$(MKL_LIBDIR)
305    endif
306    BLAS_LIB = $(MKL_LINK) -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl
307  endif
308  $(libceeds) : LDLIBS += $(BLAS_LIB)
309  libceed.c += $(xsmm.c)
310  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
311  BACKENDS += /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
312endif
313
314# OCCA Backends
315ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
316  $(libceeds) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
317  $(libceeds) : LDLIBS += -locca
318  libceed.c += $(occa.c)
319  $(occa.c:%.c=$(OBJDIR)/%.o) $(occa.c:%=%.tidy) : CPPFLAGS += -I$(OCCA_DIR)/include
320  BACKENDS += /cpu/occa /gpu/occa /omp/occa
321endif
322
323# CUDA Backends
324CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
325CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
326CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
327CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/reg /gpu/cuda/shared /gpu/cuda/gen
328ifneq ($(CUDA_LIB_DIR),)
329  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
330  $(libceeds) : LDFLAGS += -L$(CUDA_LIB_DIR) -Wl,-rpath,$(abspath $(CUDA_LIB_DIR))
331  $(libceeds) : LDLIBS += -lcudart -lnvrtc -lcuda
332  $(libceeds) : LINK = $(CXX)
333  libceed.c   += $(cuda.c) $(cuda-reg.c) $(cuda-shared.c) $(cuda-gen.c)
334  libceed.cpp += $(cuda.cpp) $(cuda-gen.cpp)
335  libceed.cu  += $(cuda.cu) $(cuda-reg.cu) $(cuda-shared.cu) $(cuda-gen.cu)
336  BACKENDS += $(CUDA_BACKENDS)
337endif
338
339# MAGMA Backend
340ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
341  ifneq ($(CUDA_LIB_DIR),)
342  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
343  omp_link = -fopenmp
344  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
345  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
346  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
347  $(libceeds)           : LDLIBS += $(magma_link)
348  $(tests) $(examples) : LDLIBS += $(magma_link)
349  libceed.c  += $(magma_allsrc.c)
350  libceed.cu += $(magma_allsrc.cu)
351  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
352  $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) : CPPFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
353  BACKENDS += /gpu/magma
354  endif
355endif
356
357export BACKENDS
358
359# Generate magma_tmp.c and magma_cuda.cu from magma.c
360%_tmp.c %_cuda.cu : %.c
361	$(magma_preprocessor) $<
362
363libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cpp:%.cpp=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o)
364$(filter %fortran.o,$(libceed.o)) : CPPFLAGS += $(if $(filter 1,$(UNDERSCORE)),-DUNDERSCORE)
365$(libceed.o): | info-backends
366$(libceed) : $(libceed.o) | $$(@D)/.DIR
367	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
368
369$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
370	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
371
372$(OBJDIR)/%.o : $(CURDIR)/%.cpp | $$(@D)/.DIR
373	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
374
375$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
376	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
377
378$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
379	$(call quiet,LINK.c) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
380
381$(OBJDIR)/% : tests/%.f90 | $$(@D)/.DIR
382	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
383
384$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
385	$(call quiet,LINK.c) -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
386
387$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
388	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' -o $@ $(abspath $<) $(CEED_LIBS) $(LDLIBS)
389
390$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
391	+$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
392	  MFEM_DIR="$(abspath $(MFEM_DIR))" $*
393	mv examples/mfem/$* $@
394
395# Note: Multiple Nek files cannot be built in parallel. The '+' here enables
396#       this single Nek bps file to be built in parallel with other examples,
397#       such as when calling `make prove-all -j2`.
398$(OBJDIR)/nek-bps : examples/nek/bps/bps.usr examples/nek/nek-examples.sh $(libceed) | $$(@D)/.DIR
399	+$(MAKE) -C examples MPI=$(MPI) CEED_DIR=`pwd` NEK5K_DIR="$(abspath $(NEK5K_DIR))" nek
400	mv examples/nek/build/bps $(OBJDIR)/bps
401	cp examples/nek/nek-examples.sh $(OBJDIR)/nek-bps
402
403$(OBJDIR)/petsc-% : examples/petsc/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
404	+$(MAKE) -C examples/petsc CEED_DIR=`pwd` \
405	  PETSC_DIR="$(abspath $(PETSC_DIR))" $*
406	mv examples/petsc/$* $@
407
408$(OBJDIR)/ns-% : examples/navier-stokes/%.c $(libceed) $(ceed.pc) | $$(@D)/.DIR
409	+$(MAKE) -C examples/navier-stokes CEED_DIR=`pwd` \
410	  PETSC_DIR="$(abspath $(PETSC_DIR))" $*
411	mv examples/navier-stokes/$* $@
412
413libceed_test.o = $(test_backends.c:%.c=$(OBJDIR)/%.o)
414$(libceed_test) : $(libceed.o) $(libceed_test.o) | $$(@D)/.DIR
415	$(call quiet,LINK) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
416
417$(examples) : $(libceed)
418$(tests) : $(libceed_test)
419$(tests) : CEED_LIBS = -lceed_test
420$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
421
422run-t% : BACKENDS += $(TEST_BACKENDS)
423run-% : $(OBJDIR)/%
424	@tests/tap.sh $(<:$(OBJDIR)/%=%)
425
426external_examples := \
427	$(if $(MFEM_DIR),$(mfemexamples)) \
428	$(if $(PETSC_DIR),$(petscexamples)) \
429	$(if $(NEK5K_DIR),$(nekexamples)) \
430	$(if $(PETSC_DIR),$(nsexamples))
431
432allexamples = $(examples) $(external_examples)
433
434# The test and prove targets can be controlled via pattern searches.  The
435# default is to run tests and those examples that have no external dependencies.
436# Examples of finer grained control:
437#
438#   make test search='petsc mfem'      # PETSc and MFEM examples
439#   make prove search='t3'             # t3xx series tests
440#   make junit search='ex petsc'       # core ex* and PETSc tests
441search ?= t ex
442realsearch = $(search:%=%%)
443matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
444
445# Test core libCEED
446test : $(matched:$(OBJDIR)/%=run-%)
447
448# Run test target in parallel
449tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
450# CPU C tests only for backend %
451ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
452
453prove : BACKENDS += $(TEST_BACKENDS)
454prove : $(matched)
455	$(info Testing backends: $(BACKENDS))
456	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
457# Run prove target in parallel
458prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
459
460prove-all :
461	+$(MAKE) prove realsearch=%
462
463junit-t% : BACKENDS += $(TEST_BACKENDS)
464junit-% : $(OBJDIR)/%
465	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
466
467junit : $(matched:$(OBJDIR)/%=junit-%)
468
469all: $(alltests)
470
471examples : $(allexamples)
472ceedexamples : $(examples)
473nekexamples : $(nekexamples)
474mfemexamples : $(mfemexamples)
475petscexamples : $(petscexamples)
476
477# Benchmarks
478allbenchmarks = petsc-bps
479bench_targets = $(addprefix bench-,$(allbenchmarks))
480.PHONY: $(bench_targets) benchmarks
481$(bench_targets): bench-%: $(OBJDIR)/%
482	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS)" -r $(*).sh
483benchmarks: $(bench_targets)
484
485$(ceed.pc) : pkgconfig-prefix = $(abspath .)
486$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
487.INTERMEDIATE : $(OBJDIR)/ceed.pc
488%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
489	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
490
491OCCA        := $(OCCA_DIR)/bin/occa
492OKL_KERNELS := $(wildcard backends/occa/*.okl)
493
494okl-cache :
495	$(OCCA) cache ceed $(OKL_KERNELS)
496
497okl-clear:
498	$(OCCA) clear -y -l ceed
499
500install : $(libceed) $(OBJDIR)/ceed.pc
501	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
502	  "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)"))
503	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
504	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
505	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
506	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
507	$(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/")
508
509.PHONY : cln clean doxygen doc lib install all print test tst prove prv prove-all junit examples style tidy okl-cache okl-clear info info-backends
510
511cln clean :
512	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
513	$(MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
514	$(MAKE) -C tests/python clean
515	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
516	$(RM) benchmarks/*output.txt
517
518distclean : clean
519	$(RM) -r doc/html $(CONFIG)
520
521DOXYGEN ?= doxygen
522doxygen :
523	$(DOXYGEN) Doxyfile
524
525# This is a real file, but it doesn't depend on the state of a file,
526# so we make it phony to force it.  We sort the shortlog by author
527# last name.
528.PHONY: AUTHORS
529AUTHORS:
530	git shortlog -s | awk '{$$1 = "placeholder"; print $$NF,$$0}' | sort | cut -d\  -f3- > $@
531
532doc-html doc-latexpdf doc-epub : doc-% : doxygen AUTHORS
533	make -C doc/sphinx $*
534
535doc : doc-html
536
537style :
538	@astyle --options=.astylerc \
539          $(filter-out include/ceedf.h tests/t320-basis-f.h, \
540            $(wildcard include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
541              examples/*/*.[ch] examples/*/*.[ch]pp gallery/*/*.[ch]))
542
543CLANG_TIDY ?= clang-tidy
544%.c.tidy : %.c
545	$(CLANG_TIDY) $^ -- $(CPPFLAGS) --std=c99
546
547tidy : $(libceed.c:%=%.tidy)
548
549print :
550	@echo $(VAR)=$($(VAR))
551
552print-% :
553	$(info [ variable name]: $*)
554	$(info [        origin]: $(origin $*))
555	$(info [        flavor]: $(flavor $*))
556	$(info [         value]: $(value $*))
557	$(info [expanded value]: $($*))
558	$(info )
559	@true
560
561# "make configure" detects any variables passed on the command line or
562# previously set in config.mk, caching them in config.mk as simple
563# (:=) variables.  Variables set in config.mk or on the command line
564# take precedence over the defaults provided in the file.  Typical
565# usage:
566#
567#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
568#   make
569#   make prove
570#
571# The values in the file can be updated by passing them on the command
572# line, e.g.,
573#
574#   make configure CC=/path/to/other/clang
575
576# All variables to consider for caching
577CONFIG_VARS = CC CXX FC NVCC NVCC_CXX \
578	OPT CFLAGS CPPFLAGS CXXFLAGS FFLAGS NVCCFLAGS \
579	LDFLAGS LDLIBS \
580	MAGMA_DIR XSMM_DIR CUDA_DIR MFEM_DIR PETSC_DIR NEK5K_DIR
581
582# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
583# was set on the command line or in config.mk (where it will appear as
584# a simple variable).
585needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
586
587configure :
588	$(file > $(CONFIG))
589	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
590	@echo "Configuration cached in $(CONFIG):"
591	@cat $(CONFIG)
592
593.PHONY : configure
594
595-include $(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d)
596