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