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