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