xref: /libCEED/Makefile (revision 53ee81eed7fe6a54158b819b478dad66ca478dc3)
1# Copyright (c) 2017-2026, 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
35DARWIN := $(filter Darwin,$(shell uname -s))
36
37
38# ------------------------------------------------------------
39# Root directories for backend dependencies
40# ------------------------------------------------------------
41
42# XSMM_DIR env variable should point to XSMM main (github.com/hfp/libxsmm)
43XSMM_DIR ?= ../libxsmm
44
45# Often /opt/cuda or /usr/local/cuda, but sometimes present on machines that don't support CUDA
46CUDA_DIR  ?=
47CUDA_ARCH ?=
48
49# Often /opt/rocm, but sometimes present on machines that don't support HIP
50ROCM_DIR ?=
51HIP_ARCH ?=
52
53# env variable MAGMA_DIR can be used too
54MAGMA_DIR ?= ../magma
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 = $(if $(DARWIN),cr,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# SANTIZ options
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# Host architecture for setting appropriate flags
116UNAME_M := $(shell uname -m)
117
118# Default extra flags by vendor
119# GCC: use -march=native only on x86 (where -mcpu doesn't exist); use -mcpu=native elsewhere
120MARCHFLAG.gcc           := $(if $(filter x86_64 i%86,$(UNAME_M)),-march=native,-mcpu=native)
121MARCHFLAG.clang         := $(MARCHFLAG.gcc)
122MARCHFLAG.icc           :=
123MARCHFLAG.oneAPI        := $(MARCHFLAG.clang)
124OMP_SIMD_FLAG.gcc       := -fopenmp-simd
125OMP_SIMD_FLAG.clang     := $(OMP_SIMD_FLAG.gcc)
126OMP_SIMD_FLAG.icc       := -qopenmp-simd
127OMP_SIMD_FLAG.oneAPI    := $(OMP_SIMD_FLAG.icc)
128OMP_FLAG.gcc            := -fopenmp
129OMP_FLAG.clang          := $(OMP_FLAG.gcc)
130OMP_FLAG.icc            := -qopenmp
131OMP_FLAG.oneAPI         := $(OMP_FLAG.icc)
132SYCL_FLAG.gcc           :=
133SYCL_FLAG.clang         := -fsycl
134SYCL_FLAG.icc           :=
135SYCL_FLAG.oneAPI        := -fsycl -fno-sycl-id-queries-fit-in-int
136OPT.gcc                 := -g -ffp-contract=fast
137OPT.clang               := $(OPT.gcc)
138OPT.icc                 := $(OPT.gcc)
139OPT.oneAPI              := $(OPT.clang)
140OPT.emcc                :=
141CFLAGS.gcc              := $(if $(STATIC),,-fPIC) -std=c11 -Wall -Wextra -Wno-unused-parameter -MMD -MP
142CFLAGS.clang            := $(CFLAGS.gcc)
143CFLAGS.icc              := $(CFLAGS.gcc)
144CFLAGS.oneAPI           := $(CFLAGS.clang)
145CFLAGS.XL               := $(if $(STATIC),,-qpic) -MMD
146CFLAGS.emcc             := $(CFLAGS.clang)
147CXXFLAGS.gcc            := $(if $(STATIC),,-fPIC) -std=c++11 -Wall -Wextra -Wno-unused-parameter -MMD -MP
148CXXFLAGS.clang          := $(CXXFLAGS.gcc)
149CXXFLAGS.icc            := $(CXXFLAGS.gcc)
150CXXFLAGS.oneAPI         := $(CXXFLAGS.clang)
151CXXFLAGS.XL             := $(if $(STATIC),,-qpic) -std=c++11 -MMD
152CXXFLAGS.emcc           := $(CXXFLAGS.clang)
153FFLAGS.GNU              := $(if $(STATIC),,-fPIC) -cpp -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -MMD -MP
154FFLAGS.ifort            := $(if $(STATIC),,-fPIC) -cpp
155FFLAGS.ifx              := $(FFLAGS.ifort)
156FFLAGS.XL               := $(if $(STATIC),,-qpic) -ffree-form -qpreprocess -qextname -MMD
157
158# This check works with compilers that use gcc and clang.  It fails with some
159# compilers; e.g., xlc apparently ignores all options when -E is passed, thus
160# succeeds with any flags.  Users can pass MARCHFLAG=... if desired.
161cc_check_flag = $(shell $(CC) -E -Werror $(1) -x c /dev/null > /dev/null 2>&1 && echo 1)
162MARCHFLAG := $(MARCHFLAG.$(CC_VENDOR))
163MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG),-mcpu=native)
164MARCHFLAG := $(if $(call cc_check_flag,$(MARCHFLAG)),$(MARCHFLAG))
165
166OMP_SIMD_FLAG := $(OMP_SIMD_FLAG.$(CC_VENDOR))
167OMP_SIMD_FLAG := $(if $(call cc_check_flag,$(OMP_SIMD_FLAG)),$(OMP_SIMD_FLAG))
168
169# Error checking flags
170PEDANTIC      ?=
171PEDANTICFLAGS ?= -Werror -pedantic
172
173# Compiler flags
174OPT    ?= -O $(MARCHFLAG) $(OPT.$(CC_VENDOR)) $(OMP_SIMD_FLAG)
175CFLAGS ?= $(OPT) $(CFLAGS.$(CC_VENDOR)) $(if $(PEDANTIC),$(PEDANTICFLAGS))
176CXXFLAGS ?= $(OPT) $(CXXFLAGS.$(CC_VENDOR)) $(if $(PEDANTIC),$(PEDANTICFLAGS))
177FFLAGS ?= $(OPT) $(FFLAGS.$(FC_VENDOR))
178LIBCXX ?= -lstdc++
179NVCCFLAGS ?= -ccbin $(CXX) -Xcompiler '$(OPT)' -Xcompiler -fPIC
180ifneq ($(CUDA_ARCH),)
181  NVCCFLAGS += -arch=$(CUDA_ARCH)
182endif
183HIPCCFLAGS ?= $(filter-out $(OMP_SIMD_FLAG),$(OPT)) -fPIC -munsafe-fp-atomics
184ifneq ($(HIP_ARCH),)
185  HIPCCFLAGS += --offload-arch=$(HIP_ARCH)
186endif
187SYCL_FLAG := $(SYCL_FLAG.$(CC_VENDOR))
188SYCLFLAGS ?= $(SYCL_FLAG) -fPIC -std=c++17 $(filter-out -std=c++11,$(CXXFLAGS)) $(filter-out $(OMP_SIMD_FLAG),$(OPT))
189
190OPENMP ?=
191ifneq ($(OPENMP),)
192  OMP_FLAG := $(OMP_FLAG.$(CC_VENDOR))
193  OMP_FLAG := $(if $(call cc_check_flag,$(OMP_FLAG)),$(OMP_FLAG))
194  CFLAGS += $(OMP_FLAG)
195  CEED_LDFLAGS += $(OMP_FLAG)
196endif
197
198ifeq ($(COVERAGE), 1)
199  CFLAGS += --coverage
200  CXXFLAGS += --coverage
201  CEED_LDFLAGS += --coverage
202endif
203
204CFLAGS += $(if $(ASAN),$(AFLAGS))
205FFLAGS += $(if $(ASAN),$(AFLAGS))
206CEED_LDFLAGS += $(if $(ASAN),$(AFLAGS))
207CPPFLAGS += -I./include
208CEED_LDLIBS = -lm
209OBJDIR := build
210for_install := $(filter install,$(MAKECMDGOALS))
211LIBDIR := $(if $(for_install),$(OBJDIR),lib)
212
213# Installation variables
214prefix ?= /usr/local
215bindir = $(prefix)/bin
216libdir = $(prefix)/lib
217includedir = $(prefix)/include
218pkgconfigdir = $(libdir)/pkgconfig
219INSTALL = install
220INSTALL_PROGRAM = $(INSTALL)
221INSTALL_DATA = $(INSTALL) -m644
222
223# Get number of processors of the machine
224NPROCS := $(shell getconf _NPROCESSORS_ONLN)
225# prepare make options to run in parallel
226MFLAGS := -j $(NPROCS) --warn-undefined-variables \
227                       --no-print-directory --no-keep-going
228
229PYTHON ?= python3
230PROVE ?= prove
231PROVE_OPTS ?= -j $(NPROCS)
232SO_EXT := $(if $(DARWIN),dylib,so)
233
234ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
235libceed.so := $(LIBDIR)/libceed.$(SO_EXT)
236libceed.a := $(LIBDIR)/libceed.a
237libceed := $(if $(STATIC),$(libceed.a),$(libceed.so))
238CEED_LIBS = -lceed
239libceeds = $(libceed)
240BACKENDS_BUILTIN := /cpu/self/ref/serial /cpu/self/ref/blocked /cpu/self/opt/serial /cpu/self/opt/blocked
241BACKENDS_MAKE := $(BACKENDS_BUILTIN)
242
243
244# ------------------------------------------------------------
245# Root directories for examples using external libraries
246# ------------------------------------------------------------
247
248# DEAL_II_DIR env variable should point to sibling directory
249ifneq ($(wildcard ../dealii/install/lib/libdeal_II.*),)
250  DEAL_II_DIR ?= ../dealii/install
251endif
252# Export for deal.II testing
253export DEAL_II_DIR
254
255# MFEM_DIR env variable should point to sibling directory
256ifneq ($(wildcard ../mfem/libmfem.*),)
257  MFEM_DIR ?= ../mfem
258endif
259
260# NEK5K_DIR env variable should point to sibling directory
261ifneq ($(wildcard ../Nek5000/*),)
262  NEK5K_DIR ?= $(abspath ../Nek5000)
263endif
264# Exports for NEK5K testing
265export CEED_DIR = $(abspath .)
266export NEK5K_DIR
267MPI ?= 1
268
269# Check for PETSc in ../petsc
270ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
271  PETSC_DIR ?= ../petsc
272endif
273
274# ------------------------------------------------------------
275# Build the library (default target)
276# ------------------------------------------------------------
277
278lib: $(libceed) $(ceed.pc)
279# run 'lib' target in parallel
280par:;@$(MAKE) $(MFLAGS) V=$(V) lib
281
282$(libceed.so) : CEED_LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed.so)))
283
284# ------------------------------------------------------------
285# Source files
286# ------------------------------------------------------------
287
288# Interface and gallery
289libceed.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/weak/*.c gallery/*.c))
290gallery.c := $(wildcard gallery/*/ceed*.c)
291libceed.c += $(gallery.c)
292
293# Backends
294# - CPU
295ref.c          := $(sort $(wildcard backends/ref/*.c))
296blocked.c      := $(sort $(wildcard backends/blocked/*.c))
297ceedmemcheck.c := $(sort $(wildcard backends/memcheck/*.c))
298opt.c          := $(sort $(wildcard backends/opt/*.c))
299avx.c          := $(sort $(wildcard backends/avx/*.c))
300xsmm.c         := $(sort $(wildcard backends/xsmm/*.c))
301# - GPU
302cuda.c         := $(sort $(wildcard backends/cuda/*.c))
303cuda.cpp       := $(sort $(wildcard backends/cuda/*.cpp))
304cuda-ref.c     := $(sort $(wildcard backends/cuda-ref/*.c))
305cuda-ref.cpp   := $(sort $(wildcard backends/cuda-ref/*.cpp))
306cuda-ref.cu    := $(sort $(wildcard backends/cuda-ref/kernels/*.cu))
307cuda-shared.c  := $(sort $(wildcard backends/cuda-shared/*.c))
308cuda-gen.c     := $(sort $(wildcard backends/cuda-gen/*.c))
309cuda-gen.cpp   := $(sort $(wildcard backends/cuda-gen/*.cpp))
310cuda-all.c     := interface/ceed-cuda.c $(cuda.c) $(cuda-ref.c) $(cuda-shared.c) $(cuda-gen.c)
311cuda-all.cpp   := $(cuda.cpp) $(cuda-ref.cpp) $(cuda-gen.cpp)
312cuda-all.cu    := $(cuda-ref.cu)
313hip.c          := $(sort $(wildcard backends/hip/*.c))
314hip.cpp        := $(sort $(wildcard backends/hip/*.cpp))
315hip-ref.c      := $(sort $(wildcard backends/hip-ref/*.c))
316hip-ref.cpp    := $(sort $(wildcard backends/hip-ref/*.cpp))
317hip-ref.hip    := $(sort $(wildcard backends/hip-ref/kernels/*.hip.cpp))
318hip-shared.c   := $(sort $(wildcard backends/hip-shared/*.c))
319hip-gen.c      := $(sort $(wildcard backends/hip-gen/*.c))
320hip-gen.cpp    := $(sort $(wildcard backends/hip-gen/*.cpp))
321hip-all.c      := interface/ceed-hip.c $(hip.c) $(hip-ref.c) $(hip-shared.c) $(hip-gen.c)
322hip-all.cpp    := $(hip.cpp) $(hip-ref.cpp) $(hip-gen.cpp)
323hip-all.hip    := $(hip-ref.hip)
324sycl-core.cpp  := $(sort $(wildcard backends/sycl/*.sycl.cpp))
325sycl-ref.cpp   := $(sort $(wildcard backends/sycl-ref/*.sycl.cpp))
326sycl-shared.cpp:= $(sort $(wildcard backends/sycl-shared/*.sycl.cpp))
327sycl-gen.cpp   := $(sort $(wildcard backends/sycl-gen/*.sycl.cpp))
328magma.c        := $(sort $(wildcard backends/magma/*.c))
329magma.cpp      := $(sort $(wildcard backends/magma/*.cpp))
330
331# Tests
332tests.c := $(sort $(wildcard tests/t[0-9][0-9][0-9]-*.c))
333tests.f := $(if $(FC),$(sort $(wildcard tests/t[0-9][0-9][0-9]-*.f90)))
334tests   := $(tests.c:tests/%.c=$(OBJDIR)/%$(EXE_SUFFIX))
335ctests  := $(tests)
336tests   += $(tests.f:tests/%.f90=$(OBJDIR)/%$(EXE_SUFFIX))
337
338# Examples
339examples.c := $(sort $(wildcard examples/ceed/*.c))
340examples.f := $(if $(FC),$(sort $(wildcard examples/ceed/*.f90)))
341examples   := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%$(EXE_SUFFIX))
342examples   += $(examples.f:examples/ceed/%.f90=$(OBJDIR)/%$(EXE_SUFFIX))
343
344# deal.II Examples
345dealiiexamples.cc := $(sort $(wildcard examples/deal.II/*.cc))
346dealiiexamples    := $(dealiiexamples.cc:examples/deal.II/%.cc=$(OBJDIR)/dealii-%)
347
348# MFEM Examples
349mfemexamples.cpp := $(sort $(wildcard examples/mfem/*.cpp))
350mfemexamples     := $(mfemexamples.cpp:examples/mfem/%.cpp=$(OBJDIR)/mfem-%)
351
352# Nek5K Examples
353nekexamples := $(OBJDIR)/nek-bps
354
355# Rust QFunction Examples
356rustqfunctions.c       := $(sort $(wildcard examples/rust-qfunctions/*.c))
357rustqfunctionsexamples := $(rustqfunctions.c:examples/rust-qfunctions/%.c=$(OBJDIR)/rustqfunctions-%)
358
359# PETSc Examples
360petscexamples.c := $(wildcard examples/petsc/*.c)
361petscexamples   := $(petscexamples.c:examples/petsc/%.c=$(OBJDIR)/petsc-%)
362
363# Fluid Dynamics Example
364fluidsexamples.c := $(sort $(wildcard examples/fluids/*.c))
365fluidsexamples   := $(fluidsexamples.c:examples/fluids/%.c=$(OBJDIR)/fluids-%)
366
367# Solid Mechanics Example
368solidsexamples.c := $(sort $(wildcard examples/solids/*.c))
369solidsexamples   := $(solidsexamples.c:examples/solids/%.c=$(OBJDIR)/solids-%)
370
371
372# ------------------------------------------------------------
373# View configuration options
374# ------------------------------------------------------------
375
376backend_status = $(if $(filter $1,$(BACKENDS_MAKE)), [backends: $1], [not found])
377
378info-basic:
379	$(info -----------------------------------------)
380	$(info |     ___ __    ______________________  |)
381	$(info |    / (_) /_  / ____/ ____/ ____/ __ \ |)
382	$(info |   / / / __ \/ /   / __/ / __/ / / / / |)
383	$(info |  / / / /_/ / /___/ /___/ /___/ /_/ /  |)
384	$(info | /_/_/_.___/\____/_____/_____/_____/   |)
385	$(info -----------------------------------------)
386	$(info )
387	$(info -----------------------------------------)
388	$(info )
389	$(info Built-in Backends:)
390	$(info   $(BACKENDS_BUILTIN))
391	$(info )
392	$(info Additional Backends:)
393	$(info   $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
394	$(info )
395	$(info -----------------------------------------)
396	$(info )
397	@true
398
399info:
400	$(info -----------------------------------------)
401	$(info |     ___ __    ______________________  |)
402	$(info |    / (_) /_  / ____/ ____/ ____/ __ \ |)
403	$(info |   / / / __ \/ /   / __/ / __/ / / / / |)
404	$(info |  / / / /_/ / /___/ /___/ /___/ /_/ /  |)
405	$(info | /_/_/_.___/\____/_____/_____/_____/   |)
406	$(info -----------------------------------------)
407	$(info )
408	$(info -----------------------------------------)
409	$(info )
410	$(info Built-in Backends:)
411	$(info   $(BACKENDS_BUILTIN))
412	$(info )
413	$(info Additional Backends:)
414	$(info   $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
415	$(info )
416	$(info -----------------------------------------)
417	$(info )
418	$(info Compiler Flags:)
419	$(info CC            = $(CC))
420	$(info CXX           = $(CXX))
421	$(info FC            = $(FC))
422	$(info CPPFLAGS      = $(CPPFLAGS))
423	$(info CFLAGS        = $(CFLAGS))
424	$(info CXXFLAGS      = $(CXXFLAGS))
425	$(info FFLAGS        = $(FFLAGS))
426	$(info NVCCFLAGS     = $(NVCCFLAGS))
427	$(info HIPCCFLAGS    = $(HIPCCFLAGS))
428	$(info SYCLFLAGS     = $(SYCLFLAGS))
429	$(info CEED_LDFLAGS  = $(CEED_LDFLAGS))
430	$(info CEED_LDLIBS   = $(CEED_LDLIBS))
431	$(info AR            = $(AR))
432	$(info ARFLAGS       = $(ARFLAGS))
433	$(info OPT           = $(OPT))
434	$(info AFLAGS        = $(AFLAGS))
435	$(info ASAN          = $(or $(ASAN),(empty)))
436	$(info VERBOSE       = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
437	$(info )
438	$(info -----------------------------------------)
439	$(info )
440	$(info Backend Dependencies:)
441	$(info MEMCHK_STATUS = $(MEMCHK_STATUS)$(call backend_status,$(MEMCHK_BACKENDS)))
442	$(info AVX_STATUS    = $(AVX_STATUS)$(call backend_status,$(AVX_BACKENDS)))
443	$(info XSMM_DIR      = $(XSMM_DIR)$(call backend_status,$(XSMM_BACKENDS)))
444	$(info CUDA_DIR      = $(CUDA_DIR)$(call backend_status,$(CUDA_BACKENDS)))
445	$(info ROCM_DIR      = $(ROCM_DIR)$(call backend_status,$(HIP_BACKENDS)))
446	$(info SYCL_DIR      = $(SYCL_DIR)$(call backend_status,$(SYCL_BACKENDS)))
447	$(info MAGMA_DIR     = $(MAGMA_DIR)$(call backend_status,$(MAGMA_BACKENDS)))
448	$(info )
449	$(info -----------------------------------------)
450	$(info )
451	$(info Example Dependencies:)
452	$(info MFEM_DIR      = $(MFEM_DIR))
453	$(info NEK5K_DIR     = $(NEK5K_DIR))
454	$(info PETSC_DIR     = $(PETSC_DIR))
455	$(info DEAL_II_DIR   = $(DEAL_II_DIR))
456	$(info )
457	$(info -----------------------------------------)
458	$(info )
459	$(info Install Options:)
460	$(info prefix        = $(prefix))
461	$(info includedir    = $(value includedir))
462	$(info libdir        = $(value libdir))
463	$(info pkgconfigdir  = $(value pkgconfigdir))
464	$(info )
465	$(info -----------------------------------------)
466	$(info )
467	$(info Git:)
468	$(info describe      = $(GIT_DESCRIBE))
469	$(info )
470	$(info -----------------------------------------)
471	@true
472
473info-backends:
474	$(info make: 'lib' with optional backends: $(filter-out $(BACKENDS_BUILTIN),$(BACKENDS)))
475	@true
476
477info-backends-all:
478	$(info make: 'lib' with backends: $(BACKENDS))
479	@true
480
481
482# ------------------------------------------------------------
483# Backends
484# ------------------------------------------------------------
485
486# Standard Backends
487libceed.c += $(ref.c)
488libceed.c += $(blocked.c)
489libceed.c += $(opt.c)
490
491# Memcheck Backends
492MEMCHK_STATUS   = Disabled
493MEMCHK         := $(shell echo "$(HASH)include <valgrind/memcheck.h>" | $(CC) $(CPPFLAGS) -E - >/dev/null 2>&1 && echo 1)
494MEMCHK_BACKENDS = /cpu/self/memcheck/serial /cpu/self/memcheck/blocked
495ifeq ($(MEMCHK),1)
496  MEMCHK_STATUS = Enabled
497  libceed.c += $(ceedmemcheck.c)
498  BACKENDS_MAKE += $(MEMCHK_BACKENDS)
499endif
500
501# AVX Backeds
502AVX_STATUS   = Disabled
503AVX_FLAG    := $(if $(filter clang,$(CC_VENDOR)),+avx,-mavx)
504AVX         := $(filter $(AVX_FLAG),$(shell $(CC) $(CFLAGS:-M%=) -v -E -x c /dev/null 2>&1))
505AVX_BACKENDS = /cpu/self/avx/serial /cpu/self/avx/blocked
506ifneq ($(AVX),)
507  AVX_STATUS = Enabled
508  libceed.c += $(avx.c)
509  BACKENDS_MAKE += $(AVX_BACKENDS)
510endif
511
512# Collect list of libraries and paths for use in linking and pkg-config
513PKG_LIBS =
514# Stubs that will not be RPATH'd
515PKG_STUBS_LIBS =
516
517# libXSMM Backends
518XSMM_BACKENDS = /cpu/self/xsmm/serial /cpu/self/xsmm/blocked
519ifneq ($(wildcard $(XSMM_DIR)/lib/libxsmm.*),)
520  PKG_LIBS += -L$(abspath $(XSMM_DIR))/lib -lxsmm
521  MKL ?=
522  ifeq (,$(MKL)$(MKLROOT))
523    BLAS_LIB ?= -lblas -ldl
524  else
525    ifneq ($(MKLROOT),)
526      # Some installs put everything inside an intel64 subdirectory, others not
527      MKL_LIBDIR = $(dir $(firstword $(wildcard $(MKLROOT)/lib/intel64/libmkl_sequential.* $(MKLROOT)/lib/libmkl_sequential.*)))
528      MKL_LINK = -L$(MKL_LIBDIR)
529    endif
530    BLAS_LIB ?= $(MKL_LINK) -Wl,--push-state,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl -Wl,--pop-state
531  endif
532  PKG_LIBS += $(BLAS_LIB)
533  libceed.c += $(xsmm.c)
534  $(xsmm.c:%.c=$(OBJDIR)/%.o) $(xsmm.c:%=%.tidy) : CPPFLAGS += -I$(XSMM_DIR)/include
535  BACKENDS_MAKE += $(XSMM_BACKENDS)
536endif
537
538# CUDA Backends
539ifneq ($(CUDA_DIR),)
540  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64 lib/x86_64-linux-gnu,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
541  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
542endif
543CUDA_LIB_DIR_STUBS := $(CUDA_LIB_DIR)/stubs
544CUDA_BACKENDS = /gpu/cuda/ref /gpu/cuda/shared /gpu/cuda/gen
545ifneq ($(CUDA_LIB_DIR),)
546  $(libceeds) : CPPFLAGS += -I$(CUDA_DIR)/include
547  PKG_LIBS += -L$(abspath $(CUDA_LIB_DIR)) -lcudart -lnvrtc -lcuda -lcublas
548  PKG_STUBS_LIBS += -L$(CUDA_LIB_DIR_STUBS)
549  LIBCEED_CONTAINS_CXX = 1
550  libceed.c     += interface/ceed-cuda.c
551  libceed.c     += $(cuda-all.c)
552  libceed.cpp   += $(cuda-all.cpp)
553  libceed.cu    += $(cuda-all.cu)
554  BACKENDS_MAKE += $(CUDA_BACKENDS)
555endif
556
557# HIP Backends
558HIP_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(ROCM_DIR)/$d/libamdhip64.${SO_EXT}))
559HIP_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(HIP_LIB_DIR))))
560HIP_BACKENDS = /gpu/hip/ref /gpu/hip/shared /gpu/hip/gen
561ifneq ($(HIP_LIB_DIR),)
562  HIPCONFIG_CPPFLAGS := $(subst =,,$(shell $(ROCM_DIR)/bin/hipconfig -C))
563  $(hip-all.c:%.c=$(OBJDIR)/%.o) $(hip-all.c:%=%.tidy): CPPFLAGS += $(HIPCONFIG_CPPFLAGS)
564  ifneq ($(CXX), $(HIPCC))
565    $(hip-all.cpp:%.cpp=$(OBJDIR)/%.o) $(hip-all.cpp:%=%.tidy): CPPFLAGS += $(HIPCONFIG_CPPFLAGS)
566  endif
567  PKG_LIBS += -L$(abspath $(HIP_LIB_DIR)) -lamdhip64 -lhipblas
568  LIBCEED_CONTAINS_CXX = 1
569  libceed.c     += $(hip-all.c)
570  libceed.cpp   += $(hip-all.cpp)
571  libceed.hip   += $(hip-all.hip)
572  BACKENDS_MAKE += $(HIP_BACKENDS)
573endif
574
575# SYCL Backends
576SYCL_BACKENDS = /gpu/sycl/ref /gpu/sycl/shared /gpu/sycl/gen
577ifneq ($(SYCL_DIR),)
578  SYCL_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(SYCL_DIR)/$d/libsycl.${SO_EXT}))
579  SYCL_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(SYCL_LIB_DIR))))
580endif
581ifneq ($(SYCL_LIB_DIR),)
582  PKG_LIBS += $(SYCL_FLAG) -lze_loader
583  LIBCEED_CONTAINS_CXX = 1
584  libceed.sycl  += $(sycl-core.cpp) $(sycl-ref.cpp) $(sycl-shared.cpp) $(sycl-gen.cpp)
585  BACKENDS_MAKE += $(SYCL_BACKENDS)
586endif
587
588# MAGMA Backends
589ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
590  MAGMA_ARCH=$(shell nm -g $(MAGMA_DIR)/lib/libmagma.* | grep -c "hipblas")
591  ifeq ($(MAGMA_ARCH), 0)  # CUDA MAGMA
592    ifneq ($(CUDA_LIB_DIR),)
593      cuda_link = $(if $(STATIC),,-Wl,-rpath,$(CUDA_LIB_DIR)) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
594      omp_link = -fopenmp
595      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
596      magma_link_shared = -L$(MAGMA_DIR)/lib $(if $(STATIC),,-Wl,-rpath,$(abspath $(MAGMA_DIR)/lib)) -lmagma
597      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
598      PKG_LIBS += $(magma_link)
599      libceed.c   += $(magma.c)
600      libceed.cpp += $(magma.cpp)
601      $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
602      $(magma.cpp:%.cpp=$(OBJDIR)/%.o) $(magma.cpp:%=%.tidy) : CPPFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
603      MAGMA_BACKENDS = /gpu/cuda/magma /gpu/cuda/magma/det
604    endif
605  else  # HIP MAGMA
606    ifneq ($(HIP_LIB_DIR),)
607      omp_link = -fopenmp
608      hip_link = $(if $(STATIC),,-Wl,-rpath,$(HIP_LIB_DIR)) -L$(HIP_LIB_DIR) -lhipblas -lhipsparse -lamdhip64
609      magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(hip_link) $(omp_link)
610      magma_link_shared = -L$(MAGMA_DIR)/lib $(hip_link) $(omp_link) $(if $(STATIC),,-Wl,-rpath,$(abspath $(MAGMA_DIR)/lib)) -lmagma
611      magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
612      PKG_LIBS += $(magma_link)
613      libceed.c   += $(magma.c)
614      libceed.cpp += $(magma.cpp)
615      $(magma.c:%.c=$(OBJDIR)/%.o) $(magma.c:%=%.tidy) : CPPFLAGS += $(HIPCONFIG_CPPFLAGS) -I$(MAGMA_DIR)/include -I$(ROCM_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
616      $(magma.cpp:%.cpp=$(OBJDIR)/%.o) $(magma.cpp:%=%.tidy) : CPPFLAGS += $(HIPCONFIG_CPPFLAGS) -I$(MAGMA_DIR)/include -I$(ROCM_DIR)/include -DCEED_MAGMA_USE_HIP -DADD_
617      MAGMA_BACKENDS = /gpu/hip/magma /gpu/hip/magma/det
618    endif
619  endif
620  LIBCEED_CONTAINS_CXX = 1
621  BACKENDS_MAKE += $(MAGMA_BACKENDS)
622endif
623
624BACKENDS ?= $(BACKENDS_MAKE)
625export BACKENDS
626
627
628# ------------------------------------------------------------
629# Linker Flags
630# ------------------------------------------------------------
631
632_pkg_ldflags = $(filter -L%,$(PKG_LIBS))
633_pkg_ldlibs = $(filter-out -L%,$(PKG_LIBS))
634$(libceeds) : CEED_LDFLAGS += $(_pkg_ldflags) $(if $(STATIC),,$(_pkg_ldflags:-L%=-Wl,-rpath,%)) $(PKG_STUBS_LIBS)
635$(libceeds) : CEED_LDLIBS += $(_pkg_ldlibs)
636ifeq ($(STATIC),1)
637  $(examples) $(tests) : CEED_LDFLAGS += $(EM_LDFLAGS) $(_pkg_ldflags) $(if $(STATIC),,$(_pkg_ldflags:-L%=-Wl,-rpath,%)) $(PKG_STUBS_LIBS)
638  $(examples) $(tests) : CEED_LDLIBS += $(_pkg_ldlibs)
639endif
640
641pkgconfig-libs-private = $(PKG_LIBS)
642ifeq ($(LIBCEED_CONTAINS_CXX),1)
643  $(libceeds) : LINK = $(CXX)
644  ifeq ($(STATIC),1)
645    $(examples) $(tests) : CEED_LDLIBS += $(LIBCXX)
646    pkgconfig-libs-private += $(LIBCXX)
647  endif
648endif
649
650
651# ------------------------------------------------------------
652# Building core library components
653# ------------------------------------------------------------
654
655# File names *-weak.c contain weak symbol definitions, which must be listed last
656# when creating shared or static libraries.
657weak_last = $(filter-out %-weak.o,$(1)) $(filter %-weak.o,$(1))
658
659libceed.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)
660$(filter %fortran.o,$(libceed.o)) : CPPFLAGS += $(if $(filter 1,$(UNDERSCORE)),-DUNDERSCORE)
661$(libceed.o): | info-backends
662$(libceed.so) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
663	$(call quiet,LINK) $(LDFLAGS) $(CEED_LDFLAGS) -shared -o $@ $^ $(CEED_LDLIBS) $(LDLIBS)
664
665$(libceed.a) : $(call weak_last,$(libceed.o)) | $$(@D)/.DIR
666	$(call quiet,AR) $(ARFLAGS) $@ $^
667
668$(OBJDIR)/%.o : $(CURDIR)/%.c | $$(@D)/.DIR
669	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(CONFIGFLAGS) -c -o $@ $(abspath $<)
670
671$(OBJDIR)/%.o : $(CURDIR)/%.cpp | $$(@D)/.DIR
672	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
673
674$(OBJDIR)/%.o : $(CURDIR)/%.cu | $$(@D)/.DIR
675	$(call quiet,NVCC) $(filter-out -Wno-unused-function, $(CPPFLAGS)) $(NVCCFLAGS) -c -o $@ $(abspath $<)
676
677$(OBJDIR)/%.o : $(CURDIR)/%.hip.cpp | $$(@D)/.DIR
678	$(call quiet,HIPCC) $(CPPFLAGS) $(HIPCCFLAGS) -c -o $@ $(abspath $<)
679
680$(OBJDIR)/%.o : $(CURDIR)/%.sycl.cpp | $$(@D)/.DIR
681	$(call quiet,SYCLCXX) $(SYCLFLAGS) $(CPPFLAGS) -c -o $@ $(abspath $<)
682
683$(OBJDIR)/%.o : $(CURDIR)/%.sycl.cpp | $$(@D)/.DIR
684	$(call quiet,SYCLCXX) $(SYCLFLAGS) $(CPPFLAGS) -c -o $@ $(abspath $<)
685
686$(OBJDIR)/%$(EXE_SUFFIX) : tests/%.c | $$(@D)/.DIR
687	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS) -I./tests/test-include
688
689$(OBJDIR)/%$(EXE_SUFFIX) : tests/%.f90 | $$(@D)/.DIR
690	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
691
692$(OBJDIR)/%$(EXE_SUFFIX) : examples/ceed/%.c | $$(@D)/.DIR
693	$(call quiet,LINK.c) $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
694
695$(OBJDIR)/%$(EXE_SUFFIX) : examples/ceed/%.f90 | $$(@D)/.DIR
696	$(call quiet,LINK.F) -DSOURCE_DIR='"$(abspath $(<D))/"' $(CEED_LDFLAGS) -o $@ $(abspath $<) $(CEED_LIBS) $(CEED_LDLIBS) $(LDLIBS)
697
698
699# ------------------------------------------------------------
700# Building examples
701# ------------------------------------------------------------
702
703# deal.II
704# Note: Invoking deal.II's CMAKE build system here
705$(OBJDIR)/dealii-% : examples/deal.II/*.cc examples/deal.II/*.h $(libceed) | $$(@D)/.DIR
706	mkdir -p examples/deal.II/build
707	cmake -B examples/deal.II/build -S examples/deal.II -DDEAL_II_DIR=$(DEAL_II_DIR) -DCEED_DIR=$(PWD)
708	+$(call quiet,MAKE) -C examples/deal.II/build
709	cp examples/deal.II/build/$* $@
710
711# MFEM
712$(OBJDIR)/mfem-% : examples/mfem/%.cpp $(libceed) | $$(@D)/.DIR
713	+$(MAKE) -C examples/mfem CEED_DIR=`pwd` \
714	  MFEM_DIR="$(abspath $(MFEM_DIR))" CXX=$(CXX) $*
715	cp examples/mfem/$* $@
716
717# Nek5000
718# Note: Multiple Nek files cannot be built in parallel. The '+' here enables
719#       this single Nek bps file to be built in parallel with other examples,
720#       such as when calling `make prove-all -j2`.
721$(OBJDIR)/nek-bps : examples/nek/bps/bps.usr examples/nek/nek-examples.sh $(libceed) | $$(@D)/.DIR
722	+$(MAKE) -C examples MPI=$(MPI) CEED_DIR=`pwd` NEK5K_DIR="$(abspath $(NEK5K_DIR))" nek
723	mv examples/nek/build/bps $(OBJDIR)/bps
724	cp examples/nek/nek-examples.sh $(OBJDIR)/nek-bps
725
726# Rust QFunctions
727$(OBJDIR)/rustqfunctions-% : examples/rust-qfunctions/%.c $(libceed) | $$(@D)/.DIR
728	+$(MAKE) -C examples/rust-qfunctions CEED_DIR=`pwd`
729	cp examples/rust-qfunctions/$* $@
730
731# PETSc
732# Several executables have common utilities, but we can't build the utilities
733# from separate submake invocations because they'll compete with each
734# other/corrupt output. So we put it in this utility library, but we don't want
735# to manually list source dependencies up at this level, so we'll just always
736# call recursive make to check that this utility is up to date.
737examples/petsc/libutils.a.PHONY: $(libceed) $(ceed.pc)
738	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` AR=$(AR) ARFLAGS=$(ARFLAGS) \
739	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $(basename $(@F))
740
741$(OBJDIR)/petsc-% : examples/petsc/%.c examples/petsc/libutils.a.PHONY $(libceed) $(ceed.pc) | $$(@D)/.DIR
742	+$(call quiet,MAKE) -C examples/petsc CEED_DIR=`pwd` \
743	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
744	cp examples/petsc/$* $@
745
746# Fluid dynamics proxy application
747$(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
748	+$(call quiet,MAKE) -C examples/fluids CEED_DIR=`pwd` \
749	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
750	cp examples/fluids/$* $@
751
752# Solid mechanics proxy application
753$(OBJDIR)/solids-% : examples/solids/%.c examples/solids/%.h \
754    examples/solids/problems/*.c examples/solids/src/*.c \
755    examples/solids/include/*.h examples/solids/problems/*.h examples/solids/qfunctions/*.h \
756    $(libceed) $(ceed.pc) | $$(@D)/.DIR
757	+$(call quiet,MAKE) -C examples/solids CEED_DIR=`pwd` \
758	  PETSC_DIR="$(abspath $(PETSC_DIR))" OPT="$(OPT)" $*
759	cp examples/solids/$* $@
760
761examples      : $(allexamples)
762ceedexamples  : $(examples)
763nekexamples   : $(nekexamples)
764mfemexamples  : $(mfemexamples)
765petscexamples : $(petscexamples)
766
767rustqfunctionsexamples : $(rustqfunctionsexamples)
768
769external_examples := \
770	$(if $(MFEM_DIR),$(mfemexamples)) \
771	$(if $(PETSC_DIR),$(petscexamples)) \
772	$(if $(NEK5K_DIR),$(nekexamples)) \
773	$(if $(DEAL_II_DIR),$(dealiiexamples)) \
774	$(if $(PETSC_DIR),$(fluidsexamples)) \
775	$(if $(PETSC_DIR),$(solidsexamples)) \
776	$(if $(or $(RUST_QF),$(GPU_CLANG)),$(rustqfunctionsexamples))
777
778allexamples = $(examples) $(external_examples)
779
780$(examples) : $(libceed)
781$(tests) : $(libceed)
782$(tests) $(examples) : override LDFLAGS += $(if $(STATIC),,-Wl,-rpath,$(abspath $(LIBDIR))) -L$(LIBDIR)
783
784
785# ------------------------------------------------------------
786# Testing
787# ------------------------------------------------------------
788
789# Set number processes for testing
790NPROC_TEST ?= 1
791export NPROC_TEST
792
793# Set pool size for testing
794NPROC_POOL ?= 1
795export NPROC_POOL
796
797run-% : $(OBJDIR)/%
798	@$(PYTHON) tests/junit.py --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)' $(<:$(OBJDIR)/%=%)
799
800# The test and prove targets can be controlled via pattern searches.  The
801# default is to run tests and those examples that have no external dependencies.
802# Examples of finer grained control:
803#
804#   make test search='petsc mfem'      # PETSc and MFEM examples
805#   make prove search='t3'             # t3xx series tests
806#   make junit search='ex petsc'       # core ex* and PETSc tests
807search ?= t ex
808realsearch = $(search:%=%%)
809matched = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(allexamples)))
810subsearch ?= .*
811JUNIT_BATCH ?= ''
812
813# Test core libCEED
814test : $(matched:$(OBJDIR)/%=run-%)
815
816# Run test target in parallel
817tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
818# CPU C tests only for backend %
819ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
820
821# Testing with TAP format
822# https://testanything.org/tap-specification.html
823prove : $(matched)
824	$(info Testing backends: $(BACKENDS))
825	$(PROVE) $(PROVE_OPTS) --exec '$(PYTHON) tests/junit.py' $(matched:$(OBJDIR)/%=%) :: --mode tap --ceed-backends $(BACKENDS) --nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) --search '$(subsearch)'
826# Run prove target in parallel
827prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
828
829prove-all :
830	+$(MAKE) prove realsearch=%
831
832junit-% : $(OBJDIR)/%
833	@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)/%=%)
834
835junit : $(matched:$(OBJDIR)/%=junit-%)
836
837all: $(alltests)
838
839# Benchmarks
840allbenchmarks = petsc-bps
841bench_targets = $(addprefix bench-,$(allbenchmarks))
842.PHONY: $(bench_targets) benchmarks
843$(bench_targets): bench-%: $(OBJDIR)/%
844	cd benchmarks && ./benchmark.sh --ceed "$(BACKENDS)" -r $(*).sh
845benchmarks: $(bench_targets)
846
847$(ceed.pc) : pkgconfig-prefix = $(abspath .)
848$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
849.INTERMEDIATE : $(OBJDIR)/ceed.pc
850%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
851	@$(SED) \
852	    -e "s:%prefix%:$(pkgconfig-prefix):" \
853	    -e "s:%opt%:$(OPT):" \
854	    -e "s:%libs_private%:$(pkgconfig-libs-private):" $< > $@
855
856GIT_DESCRIBE = $(shell git -c safe.directory=$PWD describe --always --dirty 2>/dev/null || printf "unknown\n")
857
858$(OBJDIR)/interface/ceed-config.o: Makefile
859$(OBJDIR)/interface/ceed-config.o: CONFIGFLAGS += -DCEED_GIT_VERSION="\"$(GIT_DESCRIBE)\""
860$(OBJDIR)/interface/ceed-config.o: CONFIGFLAGS += -DCEED_BUILD_CONFIGURATION="\"// Build Configuration:$(foreach v,$(CONFIG_VARS),\n$(v) = $($(v)))\""
861
862$(OBJDIR)/interface/ceed-jit-source-root-default.o : CPPFLAGS += -DCEED_JIT_SOURCE_ROOT_DEFAULT="\"$(abspath ./include)/\""
863$(OBJDIR)/interface/ceed-jit-source-root-install.o : CPPFLAGS += -DCEED_JIT_SOURCE_ROOT_DEFAULT="\"$(abspath $(includedir))/\""
864
865
866# ------------------------------------------------------------
867# Installation
868# ------------------------------------------------------------
869
870install : $(libceed) $(OBJDIR)/ceed.pc
871	$(INSTALL) -d $(addprefix $(if $(DESTDIR),"$(DESTDIR)"),"$(includedir)"\
872	  "$(includedir)/ceed/" "$(includedir)/ceed/jit-source/"\
873	  "$(includedir)/ceed/jit-source/cuda/" "$(includedir)/ceed/jit-source/hip/"\
874	  "$(includedir)/ceed/jit-source/gallery/" "$(includedir)/ceed/jit-source/magma/"\
875	  "$(includedir)/ceed/jit-source/sycl/" "$(libdir)" "$(pkgconfigdir)")
876	$(INSTALL_DATA) include/ceed/ceed.h "$(DESTDIR)$(includedir)/ceed/"
877	$(INSTALL_DATA) include/ceed/deprecated.h "$(DESTDIR)$(includedir)/ceed/"
878	$(INSTALL_DATA) include/ceed/types.h "$(DESTDIR)$(includedir)/ceed/"
879	$(INSTALL_DATA) include/ceed/ceed-f32.h "$(DESTDIR)$(includedir)/ceed/"
880	$(INSTALL_DATA) include/ceed/ceed-f64.h "$(DESTDIR)$(includedir)/ceed/"
881	$(INSTALL_DATA) include/ceed/fortran.h "$(DESTDIR)$(includedir)/ceed/"
882	$(INSTALL_DATA) include/ceed/backend.h "$(DESTDIR)$(includedir)/ceed/"
883	$(INSTALL_DATA) include/ceed/cuda.h "$(DESTDIR)$(includedir)/ceed/"
884	$(INSTALL_DATA) include/ceed/hip.h "$(DESTDIR)$(includedir)/ceed/"
885	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
886	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
887	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
888	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
889	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/cuda/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/cuda/"
890	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/hip/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/hip/"
891	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/gallery/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/gallery/"
892	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/magma/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/magma/"
893	$(INSTALL_DATA) $(wildcard include/ceed/jit-source/sycl/*.h) "$(DESTDIR)$(includedir)/ceed/jit-source/sycl/"
894
895
896# ------------------------------------------------------------
897# Cleaning
898# ------------------------------------------------------------
899
900cln clean :
901	$(RM) -r $(OBJDIR) $(LIBDIR) dist *egg* .pytest_cache *cffi*
902	$(call quiet,MAKE) -C examples clean NEK5K_DIR="$(abspath $(NEK5K_DIR))"
903	$(call quiet,MAKE) -C python/tests clean
904	$(RM) benchmarks/*output.txt
905	$(RM) -rf temp
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) $(wildcard examples/ceed/ex*-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$(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 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