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