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