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