xref: /libCEED/Makefile (revision 868539c291cd6e4adc5c1e2f0ea123f6c9e198f6)
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
76MARCH := $(shell $(CC) --version -v < /dev/null 2>&1 | grep -c ' -march=')
77ifeq ($(MARCH),1)
78  MARCHFLAG := -march=native
79else
80  MARCHFLAG := -mtune=native
81endif
82OPT    = -O -g $(MARCHFLAG) -ffp-contract=fast -fopenmp-simd
83CFLAGS = -std=c99 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
84CXXFLAGS = -std=c++11 $(OPT) -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
85NVCCFLAGS = -ccbin $(CXX) -Xcompiler "$(OPT)" -Xcompiler -fPIC
86# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately:
87ifneq ($(filter %xlf %xlf_r,$(FC)),)
88  FFLAGS = $(OPT) -ffree-form -qpreprocess -qextname -qpic -MMD -DSOURCE_DIR='"$(abspath $(<D))/"'
89else # gfortran/Intel-style options
90  FFLAGS = -cpp     $(OPT) -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP -DSOURCE_DIR='"$(abspath $(<D))/"'
91endif
92
93ifeq ($(UNDERSCORE), 1)
94  CFLAGS += -DUNDERSCORE
95endif
96
97ifeq ($(COVERAGE), 1)
98  CFLAGS += --coverage
99  LDFLAGS += --coverage
100endif
101
102CFLAGS += $(if $(ASAN),$(AFLAGS))
103FFLAGS += $(if $(ASAN),$(AFLAGS))
104LDFLAGS += $(if $(ASAN),$(AFLAGS))
105CPPFLAGS = -I./include
106LDLIBS = -lm
107OBJDIR := build
108LIBDIR := lib
109
110# Installation variables
111prefix ?= /usr/local
112bindir = $(prefix)/bin
113libdir = $(prefix)/lib
114okldir = $(libdir)/okl
115includedir = $(prefix)/include
116pkgconfigdir = $(libdir)/pkgconfig
117INSTALL = install
118INSTALL_PROGRAM = $(INSTALL)
119INSTALL_DATA = $(INSTALL) -m644
120
121# Get number of processors of the machine
122NPROCS := $(shell getconf _NPROCESSORS_ONLN)
123# prepare make options to run in parallel
124MFLAGS := -j $(NPROCS) --warn-undefined-variables \
125                       --no-print-directory --no-keep-going
126
127PYTHON ?= python3
128PROVE ?= prove
129PROVE_OPTS ?= -j $(NPROCS)
130DARWIN := $(filter Darwin,$(shell uname -s))
131SO_EXT := $(if $(DARWIN),dylib,so)
132
133ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
134libceed := $(LIBDIR)/libceed.$(SO_EXT)
135CEED_LIBS = -lceed
136libceed.c := $(wildcard interface/ceed*.c)
137gallery.c := $(wildcard gallery/*/ceed*.c)
138libceed.c += $(gallery.c)
139libceed_test := $(LIBDIR)/libceed_test.$(SO_EXT)
140libceeds = $(libceed) $(libceed_test)
141BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
142BACKENDS := $(BACKENDS_BUILTIN)
143
144# Tests
145tests.c   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
146tests.f   := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90))
147tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
148ctests    := $(tests)
149tests     += $(tests.f:tests/%.f90=$(OBJDIR)/%)
150# Examples
151examples.c := $(sort $(wildcard examples/ceed/*.c))
152examples.f := $(sort $(wildcard examples/ceed/*.f))
153examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
154examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
155# MFEM Examples
156mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
157mfemexamples  := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
158# Nek5K Examples
159nekexamples  := $(OBJDIR)/nek-bps
160# PETSc Examples
161petscexamples.c := $(wildcard examples/petsc/*.c)
162petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
163# Navier-Stokes Example
164navierstokesexample.c := $(sort $(wildcard examples/navier-stokes/*.c))
165navierstokesexample  := $(navierstokesexample.c:examples/navier-stokes/%.c=$(OBJDIR)/navier-stokes-%)
166
167# Backends/[ref, blocked, template, memcheck, opt, avx, occa, magma]
168ref.c          := $(sort $(wildcard backends/ref/*.c))
169blocked.c      := $(sort $(wildcard backends/blocked/*.c))
170template.c     := $(sort $(wildcard backends/template/*.c))
171ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
172opt.c          := $(sort $(wildcard backends/opt/*.c))
173avx.c          := $(sort $(wildcard backends/avx/*.c))
174xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
175cuda.c         := $(sort $(wildcard backends/cuda/*.c))
176cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
177cuda.cu        := $(sort $(wildcard backends/cuda/*.cu))
178cuda-reg.c     := $(sort $(wildcard backends/cuda-reg/*.c))
179cuda-reg.cu    := $(sort $(wildcard backends/cuda-reg/*.cu))
180cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
181cuda-shared.cu := $(sort $(wildcard backends/cuda-shared/*.cu))
182cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
183cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
184cuda-gen.cu    := $(sort $(wildcard backends/cuda-gen/*.cu))
185occa.c         := $(sort $(wildcard backends/occa/*.c))
186magma_preprocessor := python backends/magma/gccm.py
187magma_pre_src  := $(filter-out %ceed-magma.c %_tmp.c, $(wildcard backends/magma/ceed-*.c))
188magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
189magma_dsrc     += backends/magma/ceed-magma.c
190magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
191magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
192magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
193magma_allsrc.cu:= $(magma_tmp.cu) backends/magma/magma_devptr.cu
194magma_allsrc.cu+= backends/magma/magma_dbasisApply_grad.cu backends/magma/magma_dbasisApply_interp.cu backends/magma/magma_dbasisApply_weight.cu
195magma_allsrc.cu+= backends/magma/magma_drestrictApply.cu
196
197# Output using the 216-color rules mode
198rule_file = $(notdir $(1))
199rule_path = $(patsubst %/,%,$(dir $(1)))
200last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
201ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
202emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
203color_out = @if [ -t 1 ]; then \
204				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
205					$(1) $(call ansicolor,$(2)) \
206					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
207				printf "  %10s %s\n" $(1) $(2); fi
208# if TERM=dumb, use it, otherwise switch to the term one
209output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
210
211# if V is set to non-nil, turn the verbose mode
212quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
213
214# Cancel built-in and old-fashioned implicit rules which we don't use
215.SUFFIXES:
216
217.SECONDEXPANSION: # to expand $$(@D)/.DIR
218
219.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
220
221%/.DIR :
222	@mkdir -p $(@D)
223	@touch $@
224
225.PRECIOUS: %/.DIR
226
227lib: $(libceed) $(ceed.pc)
228# run 'lib' target in parallel
229par:;@$(MAKE) $(MFLAGS) V=$(V) lib
230backend_status = $(if $(filter $1,$(BACKENDS)), [backends: $1], [not found])
231info:
232	$(info ------------------------------------)
233	$(info CC            = $(CC))
234	$(info CXX           = $(CXX))
235	$(info FC            = $(FC))
236	$(info CPPFLAGS      = $(CPPFLAGS))
237	$(info CFLAGS        = $(value CFLAGS))
238	$(info CXXFLAGS      = $(value CXXFLAGS))
239	$(info FFLAGS        = $(value FFLAGS))
240	$(info NVCCFLAGS     = $(value NVCCFLAGS))
241	$(info LDFLAGS       = $(value LDFLAGS))
242	$(info LDLIBS        = $(LDLIBS))
243	$(info OPT           = $(OPT))
244	$(info AFLAGS        = $(AFLAGS))
245	$(info ASAN          = $(or $(ASAN),(empty)))
246	$(info V             = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
247	$(info ------------------------------------)
248	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,/cpu/self/memcheck/serial /cpu/sef/memcheck/blocked))
249	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,/cpu/self/avx/serial /cpu/self/avx/blocked))
250	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,/cpu/self/xsmm/serial /cpu/self/xsmm/blocked))
251	$(info OCCA_DIR      = $(OCCA_DIR)$(call backend_status,/cpu/occa /gpu/occa /omp/occa))
252	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,/gpu/magma))
253	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
254	$(info ------------------------------------)
255	$(info MFEM_DIR      = $(MFEM_DIR))
256	$(info NEK5K_DIR     = $(NEK5K_DIR))
257	$(info PETSC_DIR     = $(PETSC_DIR))
258	$(info ------------------------------------)
259	$(info prefix        = $(prefix))
260	$(info includedir    = $(value includedir))
261	$(info libdir        = $(value libdir))
262	$(info okldir        = $(value okldir))
263	$(info pkgconfigdir  = $(value pkgconfigdir))
264	$(info ------------------------------------)
265	@true
266info-backends:
267	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
268.PHONY: lib all par info info-backends
269
270$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
271$(libceed_test) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed_test)))
272
273# Standard Backends
274libceed.c += $(ref.c)
275libceed.c += $(blocked.c)
276libceed.c += $(opt.c)
277
278# Testing Backends
279test_backends.c := $(template.c)
280TEST_BACKENDS := /cpu/self/tmpl /cpu/self/tmpl/sub
281
282# Memcheck Backend
283MEMCHK_STATUS = Disabled
284MEMCHK := $(shell echo "\#include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
285ifeq ($(MEMCHK),1)
286  MEMCHK_STATUS = Enabled
287  libceed.c += $(ceedmemcheck.c)
288  BACKENDS += /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
289endif
290
291# AVX Backed
292AVX_STATUS = Disabled
293AVX := $(shell $(CC) $(OPT) -v -E - < /dev/null 2>&1 | grep -c ' -mavx')
294ifeq ($(AVX),1)
295  AVX_STATUS = Enabled
296  libceed.c += $(avx.c)
297  BACKENDS += /cpu/self/avx/serial /cpu/self/avx/blocked
298endif
299
300# libXSMM Backends
301ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
302  $(libceeds) : LDFLAGS += -L$(XSMM_DIR)/lib -Wl,-rpath,$(abspath $(XSMM_DIR)/lib)
303  $(libceeds) : LDLIBS += -lxsmm -ldl
304  MKL ?=
305  ifeq (,$(MKL)$(MKLROOT))
306    BLAS_LIB = -lblas
307  else
308    ifneq ($(MKLROOT),)
309      MKL_LINK = -L$(MKLROOT)/lib/intel64 -Wl,-rpath,$(MKLROOT)/lib/intel64
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) : CFLAGS += -I$(CUDA_DIR)/include
335  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
336  $(libceeds) : LDFLAGS += -L$(CUDA_LIB_DIR) -Wl,-rpath,$(abspath $(CUDA_LIB_DIR))
337  $(libceeds) : LDLIBS += -lcudart -lnvrtc -lcuda
338  $(libceeds) : LINK = $(CXX)
339  libceed.c   += $(cuda.c) $(cuda-reg.c) $(cuda-shared.c) $(cuda-gen.c)
340  libceed.cpp += $(cuda.cpp) $(cuda-gen.cpp)
341  libceed.cu  += $(cuda.cu) $(cuda-reg.cu) $(cuda-shared.cu) $(cuda-gen.cu)
342  BACKENDS += $(CUDA_BACKENDS)
343endif
344
345# MAGMA Backend
346ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
347  ifneq ($(CUDA_LIB_DIR),)
348  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
349  omp_link = -fopenmp
350  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
351  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
352  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
353  $(libceeds)           : LDLIBS += $(magma_link)
354  $(tests) $(examples) : LDLIBS += $(magma_link)
355  libceed.c  += $(magma_allsrc.c)
356  libceed.cu += $(magma_allsrc.cu)
357  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
358  $(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
359  BACKENDS += /gpu/magma
360  endif
361endif
362
363export BACKENDS
364
365# Generate magma_tmp.c and magma_cuda.cu from magma.c
366%_tmp.c %_cuda.cu : %.c
367	$(magma_preprocessor) $<
368
369libceed.o = $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cpp:%.cpp=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o)
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) -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) -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)/navier-stokes-% : 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
436allexamples = $(examples) $(external_examples)
437
438# The test and prove targets can be controlled via pattern searches.  The
439# default is to run tests and those examples that have no external dependencies.
440# Examples of finer grained control:
441#
442#   make test search='petsc mfem'      # PETSc and MFEM examples
443#   make prove search='t3'             # t3xx series tests
444#   make junit search='ex petsc'       # core ex* and PETSc tests
445search ?= t ex
446realsearch = $(search:%=%%)
447matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
448
449# Test core libCEED
450test : $(matched:$(OBJDIR)/%=run-%)
451
452# Run test target in parallel
453tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
454# CPU C tests only for backend %
455ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
456
457prove : BACKENDS += $(TEST_BACKENDS)
458prove : $(matched)
459	$(info Testing backends: $(BACKENDS))
460	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(matched:$(OBJDIR)/%=%)
461# Run prove target in parallel
462prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
463
464prove-all :
465	+$(MAKE) prove realsearch=%
466
467junit-t% : BACKENDS += $(TEST_BACKENDS)
468junit-% : $(OBJDIR)/%
469	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py $(<:$(OBJDIR)/%=%)
470
471junit : $(matched:$(OBJDIR)/%=junit-%)
472
473all: $(alltests)
474
475examples : $(allexamples)
476ceedexamples : $(examples)
477nekexamples : $(nekexamples)
478mfemexamples : $(mfemexamples)
479petscexamples : $(petscexamples)
480
481# Benchmarks
482allbenchmarks = petsc-bps
483bench_targets = $(addprefix bench-,$(allbenchmarks))
484.PHONY: $(bench_targets) benchmarks
485$(bench_targets): bench-%: $(OBJDIR)/%
486	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS)" -r $(*).sh
487benchmarks: $(bench_targets)
488
489$(ceed.pc) : pkgconfig-prefix = $(abspath .)
490$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
491.INTERMEDIATE : $(OBJDIR)/ceed.pc
492%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
493	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
494
495OCCA        := $(OCCA_DIR)/bin/occa
496OKL_KERNELS := $(wildcard backends/occa/*.okl)
497
498okl-cache :
499	$(OCCA) cache ceed $(OKL_KERNELS)
500
501okl-clear:
502	$(OCCA) clear -y -l ceed
503
504install : $(libceed) $(OBJDIR)/ceed.pc
505	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
506	  "$(libdir)" "$(pkgconfigdir)" $(if $(OCCA_ON),"$(okldir)"))
507	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
508	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
509	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
510	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
511	$(if $(OCCA_ON),$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/")
512
513.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
514
515cln clean :
516	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
517	$(MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
518	$(MAKE) -C tests/python clean
519	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
520	$(RM) benchmarks/*output.txt
521
522distclean : clean
523	$(RM) -r doc/html config.mk
524
525doc :
526	doxygen Doxyfile
527
528style :
529	@astyle --options=.astylerc \
530          $(filter-out include/ceedf.h tests/t320-basis-f.h, \
531            $(wildcard include/*.h interface/*.[ch] tests/*.[ch] backends/*/*.[ch] \
532              examples/*/*.[ch] examples/*/*.[ch]pp gallery/*/*.[ch]))
533
534CLANG_TIDY ?= clang-tidy
535%.c.tidy : %.c
536	$(CLANG_TIDY) $^ -- $(CPPFLAGS) --std=c99
537
538tidy : $(libceed.c:%=%.tidy)
539
540print :
541	@echo $(VAR)=$($(VAR))
542
543print-% :
544	$(info [ variable name]: $*)
545	$(info [        origin]: $(origin $*))
546	$(info [         value]: $(value $*))
547	$(info [expanded value]: $($*))
548	$(info )
549	@true
550
551# "make configure" will autodetect any variables not passed on the
552# command line, caching the result in config.mk to be used on any
553# subsequent invocations of make.  For example,
554#
555#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
556#   make
557#   make prove
558configure :
559	@: > config.mk
560	@echo "CC = $(CC)" | tee -a config.mk
561	@echo "CXX = $(CXX)" | tee -a config.mk
562	@echo "FC = $(FC)" | tee -a config.mk
563	@echo "NVCC = $(NVCC)" | tee -a config.mk
564	@echo "NVCC_CXX = $(NVCC_CXX)" | tee -a config.mk
565	@echo "CFLAGS = $(CFLAGS)" | tee -a config.mk
566	@echo "CPPFLAGS = $(CPPFLAGS)" | tee -a config.mk
567	@echo "FFLAGS = $(FFLAGS)" | tee -a config.mk
568	@echo "NVCCFLAGS = $(NVCCFLAGS)" | tee -a config.mk
569	@echo "LDFLAGS = $(LDFLAGS)" | tee -a config.mk
570	@echo "LDLIBS = $(LDLIBS)" | tee -a config.mk
571	@echo "MAGMA_DIR = $(MAGMA_DIR)" | tee -a config.mk
572	@echo "XSMM_DIR = $(XSMM_DIR)" | tee -a config.mk
573	@echo "CUDA_DIR = $(CUDA_DIR)" | tee -a config.mk
574	@echo "MFEM_DIR = $(MFEM_DIR)" | tee -a config.mk
575	@echo "PETSC_DIR = $(PETSC_DIR)" | tee -a config.mk
576	@echo "NEK5K_DIR = $(NEK5K_DIR)" | tee -a config.mk
577	@echo "Configuration cached in config.mk"
578
579.PHONY : configure
580
581-include $(libceed.c:%.c=$(OBJDIR)/%.d) $(tests.c:tests/%.c=$(OBJDIR)/%.d)
582