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