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