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