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