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