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