xref: /libCEED/Makefile (revision 1dc2661bf4d71c164e61e73459ea159b75c543b7)
10c784865Scamierjs# Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
20c784865Scamierjs# Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
30c784865Scamierjs# All Rights reserved. See files LICENSE and NOTICE for details.
40d9661cdSTzanio#
50d9661cdSTzanio# This file is part of CEED, a collection of benchmarks, miniapps, software
60d9661cdSTzanio# libraries and APIs for efficient high-order finite element and spectral
70d9661cdSTzanio# element discretizations for exascale applications. For more information and
80d9661cdSTzanio# source code availability see http://github.com/ceed.
90d9661cdSTzanio#
100d9661cdSTzanio# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
110d9661cdSTzanio# a collaborative effort of two U.S. Department of Energy organizations (Office
120d9661cdSTzanio# of Science and the National Nuclear Security Administration) responsible for
130d9661cdSTzanio# the planning and preparation of a capable exascale ecosystem, including
140d9661cdSTzanio# software, applications, hardware, advanced system engineering and early
150d9661cdSTzanio# testbed platforms, in support of the nation's exascale computing imperative.
160d9661cdSTzanio
17dc753d40SJed Brownifeq (,$(filter-out undefined default,$(origin CC)))
186ed738d9SJed Brown  CC = gcc
19dc753d40SJed Brownendif
20dc753d40SJed Brownifeq (,$(filter-out undefined default,$(origin FC)))
2181c9eb91SStan Tomov  FC = gfortran
22dc753d40SJed Brownendif
237692a9b0SVeselin DobrevNVCC = $(CUDA_DIR)/bin/nvcc
24e86995feScamierjs
253b56482dScamierjs# ASAN must be left empty if you don't want to use it
260a7eccc5ScamierjsASAN ?=
27f2fa494dScamierjsNDEBUG ?= 1
286850bcb5Scamierjs
29e86995feScamierjsLDFLAGS ?=
3032d74c32SThilina RathnayakeUNDERSCORE ?= 1
311b58aefaScamierjs
3212123fd5Scamierjs# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
337f5ff99bSVeselin DobrevOCCA_DIR ?= ../occa
34f797d7b2Scamierjs
355a9ca9adSVeselin Dobrev# env variable MAGMA_DIR can be used too
365a9ca9adSVeselin DobrevMAGMA_DIR ?= ../magma
375a9ca9adSVeselin Dobrev# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
385a9ca9adSVeselin DobrevCUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
395a9ca9adSVeselin Dobrev               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
4082b77998SStan Tomov
41bf000209STzanio# Warning: SANTIZ options still don't run with /gpu/occa
4212123fd5Scamierjs# export LSAN_OPTIONS=suppressions=.asanignore
43c12ddcb5ScamierjsAFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
4432d74c32SThilina Rathnayake
45*1dc2661bSVeselin DobrevCFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP
4637c58ae6STzanioFFLAGS = -cpp     -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP
47*1dc2661bSVeselin Dobrev# If using the IBM XL Fortran (xlf) replace FFLAGS appropriately:
48*1dc2661bSVeselin Dobrevifneq ($(filter %xlf %xlf_r,$(FC)),)
49*1dc2661bSVeselin Dobrev  FFLAGS = -qpreprocess -qextname -qpic -MMD
50*1dc2661bSVeselin Dobrevendif
511b58aefaScamierjs
52*1dc2661bSVeselin DobrevCFLAGS += $(if $(NDEBUG),-O2 -DNDEBUG=1,-g)
536850bcb5Scamierjs
5432d74c32SThilina Rathnayakeifeq ($(UNDERSCORE), 1)
5532d74c32SThilina Rathnayake  CFLAGS += -DUNDERSCORE
5632d74c32SThilina Rathnayakeendif
5732d74c32SThilina Rathnayake
58*1dc2661bSVeselin DobrevFFLAGS += $(if $(NDEBUG),-O2 -DNDEBUG=1,-g)
5932d74c32SThilina Rathnayake
604dd6f121ScamierjsCFLAGS += $(if $(ASAN),$(AFLAGS))
614dd6f121ScamierjsFFLAGS += $(if $(ASAN),$(AFLAGS))
624dd6f121ScamierjsLDFLAGS += $(if $(ASAN),$(AFLAGS))
636ea7c6c1SJed BrownCPPFLAGS = -I./include
64582447c9SJed BrownLDLIBS = -lm
6591b7489eSJed BrownOBJDIR := build
666ea7c6c1SJed BrownLIBDIR := lib
676ea7c6c1SJed Brown
68d5217624SJed Brown# Installation variables
69d5217624SJed Brownprefix ?= /usr/local
70d5217624SJed Brownbindir = $(prefix)/bin
71d5217624SJed Brownlibdir = $(prefix)/lib
72a5ba6ca9Scamierjsokldir = $(prefix)/lib/okl
73d5217624SJed Brownincludedir = $(prefix)/include
74d5217624SJed Brownpkgconfigdir = $(libdir)/pkgconfig
75d5217624SJed BrownINSTALL = install
76d5217624SJed BrownINSTALL_PROGRAM = $(INSTALL)
77d5217624SJed BrownINSTALL_DATA = $(INSTALL) -m644
78d5217624SJed Brown
793b56482dScamierjs# Get number of processors of the machine
8069762e1fScamierjsNPROCS := $(shell getconf _NPROCESSORS_ONLN)
813b56482dScamierjs# prepare make options to run in parallel
8282985883ScamierjsMFLAGS := -j $(NPROCS) --warn-undefined-variables \
8321ae6867Scamierjs                       --no-print-directory --no-keep-going
8487e762eaSJed Brown
85bfa078e6SJed BrownPROVE ?= prove
863a1ec3cdSJed BrownPROVE_OPTS ?= -j $(NPROCS)
875c719ab0SJed BrownDARWIN := $(filter Darwin,$(shell uname -s))
885c719ab0SJed BrownSO_EXT := $(if $(DARWIN),dylib,so)
899df38c42SVeselin Dobrev
906ea7c6c1SJed Brownceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
9191b7489eSJed Brownlibceed := $(LIBDIR)/libceed.$(SO_EXT)
92bae89548SJed Brownlibceed.c := $(wildcard ceed*.c)
93bf000209STzanio
94bf000209STzanio# Tests
95746e9b11SJed Browntests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
9632d74c32SThilina Rathnayaketests.f   := $(sort $(wildcard tests/t[0-9][0-9]-*.f))
9791b7489eSJed Browntests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
983f3e7340Scamierjsctests    := $(tests)
9932d74c32SThilina Rathnayaketests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
10032d74c32SThilina Rathnayake#examples
101182fbe45STzanioexamples.c := $(sort $(wildcard examples/ceed/*.c))
102182fbe45STzanioexamples.f := $(sort $(wildcard examples/ceed/*.f))
103182fbe45STzanioexamples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
104182fbe45STzanioexamples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
10582b77998SStan Tomov# backends/[ref & occa  & magma]
106ae3cba82Scamierjsref.c     := $(sort $(wildcard backends/ref/*.c))
107ae3cba82Scamierjsocca.c    := $(sort $(wildcard backends/occa/*.c))
108755585ceSStan Tomovmagma_preprocessor := python backends/magma/gccm.py
109c4acb06bSStan Tomovmagma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
110c4acb06bSStan Tomovmagma_dsrc     := $(wildcard backends/magma/magma_d*.c)
111c4acb06bSStan Tomovmagma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
112c4acb06bSStan Tomovmagma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
113c4acb06bSStan Tomovmagma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
114c4acb06bSStan Tomovmagma_allsrc.cu:= $(magma_tmp.cu)
11582985883Scamierjs
1169bdc3d68Scamierjs# Output using the 216-color rules mode
11718a724fcScamierjsrule_file = $(notdir $(1))
11818a724fcScamierjsrule_path = $(patsubst %/,%,$(dir $(1)))
11918a724fcScamierjslast_path = $(notdir $(patsubst %/,%,$(dir $(1))))
1203c5d0cdfSJed Brownansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
121abb87f81Scamierjsemacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
122abb87f81Scamierjscolor_out = @if [ -t 1 ]; then \
12318a724fcScamierjs				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
1243c5d0cdfSJed Brown					$(1) $(call ansicolor,$(2)) \
12518a724fcScamierjs					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
12618a724fcScamierjs				printf "  %10s %s\n" $(1) $(2); fi
127f797d7b2Scamierjs# if TERM=dumb, use it, otherwise switch to the term one
128abb87f81Scamierjsoutput = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
12982985883Scamierjs
1309bdc3d68Scamierjs# if V is set to non-nil, turn the verbose mode
13118a724fcScamierjsquiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
13221ae6867Scamierjs
1339df38c42SVeselin Dobrev.SUFFIXES:
1349df38c42SVeselin Dobrev.SUFFIXES: .c .o .d
13591b7489eSJed Brown.SECONDEXPANSION: # to expand $$(@D)/.DIR
1369df38c42SVeselin Dobrev
137c4acb06bSStan Tomov.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
13814c1ded1SStan Tomov
13991b7489eSJed Brown%/.DIR :
14091b7489eSJed Brown	@mkdir -p $(@D)
14191b7489eSJed Brown	@touch $@
14291b7489eSJed Brown
14391b7489eSJed Brown.PRECIOUS: %/.DIR
14491b7489eSJed Brown
1456ea7c6c1SJed Brownthis: $(libceed) $(ceed.pc)
1463b56482dScamierjs# run 'this' target in parallel
1479bdc3d68Scamierjsall:;@$(MAKE) $(MFLAGS) V=$(V) this
1480e439e50SJed Brown
14924d002f4SJed Brown$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
15069762e1fScamierjs
15187174f08SJed Brownlibceed.c += $(ref.c)
1527f5ff99bSVeselin Dobrevifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
153fc377ca6SJed Brown  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
15412123fd5Scamierjs  $(libceed) : LDLIBS += -locca
15587174f08SJed Brown  libceed.c += $(occa.c)
15687174f08SJed Brown  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
15721ae6867Scamierjsendif
15882b77998SStan Tomovifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
1595a9ca9adSVeselin Dobrev  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
1605a9ca9adSVeselin Dobrev  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
1615a9ca9adSVeselin Dobrev  ifneq ($(CUDA_LIB_DIR),)
162*1dc2661bSVeselin Dobrev  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcublas -lcusparse -lcudart
163*1dc2661bSVeselin Dobrev  omp_link = -fopenmp
164*1dc2661bSVeselin Dobrev  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link) $(omp_link)
1657692a9b0SVeselin Dobrev  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
1667692a9b0SVeselin Dobrev  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
167c4acb06bSStan Tomov  magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o)
1687692a9b0SVeselin Dobrev  $(libceed)           : LDLIBS += $(magma_link)
1697692a9b0SVeselin Dobrev  $(tests) $(examples) : LDLIBS += $(magma_link)
170c4acb06bSStan Tomov  $(libceed) : $(magma_allsrc.o)
171c4acb06bSStan Tomov  libceed.c  += $(magma_allsrc.c)
172c4acb06bSStan Tomov  libceed.cu += $(magma_allsrc.cu)
173c4acb06bSStan Tomov  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
174c4acb06bSStan Tomov  $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) : NVCCFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
17582b77998SStan Tomov  endif
1765a9ca9adSVeselin Dobrevendif
177755585ceSStan Tomov
178755585ceSStan Tomov# generate magma_tmp.c and magma_cuda.cu from magma.c
179c4acb06bSStan Tomov$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR
180755585ceSStan Tomov	$(magma_preprocessor) $<
181755585ceSStan Tomov
182755585ceSStan Tomov$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
18321ae6867Scamierjs	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
18491b7489eSJed Brown
185b5b03409SJed Brown$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
186b5b03409SJed Brown	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
187ae3cba82Scamierjs
188755585ceSStan Tomov$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
189755585ceSStan Tomov	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
190755585ceSStan Tomov
191b5b03409SJed Brown$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
192b5b03409SJed Brown	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
19378b5556aScamierjs
19432d74c32SThilina Rathnayake$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
195a99eee77SThilina Rathnayake	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
19632d74c32SThilina Rathnayake
197182fbe45STzanio$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
19834fd0bf9SThilina Rathnayake	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
19991b7489eSJed Brown
200182fbe45STzanio$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
201a99eee77SThilina Rathnayake	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
202673d494eSThilina Rathnayake
2032c6ea02fSJed Brown$(tests) $(examples) : $(libceed)
204fc377ca6SJed Brown$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
2054859b599SJed Brown
206dad465e7SJed Brownrun-% : $(OBJDIR)/%
207b5b03409SJed Brown	@tests/tap.sh $(<:build/%=%)
2084859b599SJed Brown
209dad465e7SJed Browntest : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
2103b56482dScamierjs# run test target in parallel
2116850bcb5Scamierjstst : ;@$(MAKE) $(MFLAGS) V=$(V) test
2123b56482dScamierjs# CPU C tests only for backend %
2133f3e7340Scamierjsctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
2144859b599SJed Brown
215dad465e7SJed Brownprove : $(tests) $(examples)
216dad465e7SJed Brown	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
2173b56482dScamierjs# run prove target in parallel
2186850bcb5Scamierjsprv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
219bfa078e6SJed Brown
2202c6ea02fSJed Brownexamples : $(examples)
2212c6ea02fSJed Brown
2226ea7c6c1SJed Brown$(ceed.pc) : pkgconfig-prefix = $(abspath .)
223d5217624SJed Brown$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
224d5217624SJed Brown.INTERMEDIATE : $(OBJDIR)/ceed.pc
2256ea7c6c1SJed Brown%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
2266ea7c6c1SJed Brown	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
2270e439e50SJed Brown
228cc6ff0d7SJed Brown# The occa executable is not linked with RPATH by default, so we need it to find its libocca.so
229cc6ff0d7SJed BrownOCCA               := $(if $(DARWIN),DYLD_LIBRARY_PATH,LD_LIBRARY_PATH)=$(OCCA_DIR)/lib $(OCCA_DIR)/bin/occa
230cc6ff0d7SJed BrownOKL_KERNELS        := $(wildcard backends/occa/*.okl)
231cc6ff0d7SJed Brown
232cc6ff0d7SJed Brownokl-cache :
233cc6ff0d7SJed Brown	$(OCCA) cache ceed $(OKL_KERNELS)
234cc6ff0d7SJed Brown
235cc6ff0d7SJed Brownokl-clear:
236cc6ff0d7SJed Brown	$(OCCA) clear -y -l ceed
237a5ba6ca9Scamierjs
238d5217624SJed Browninstall : $(libceed) $(OBJDIR)/ceed.pc
239a5ba6ca9Scamierjs	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(okldir)" "$(DESTDIR)$(pkgconfigdir)"
2401e25a746SJed Brown	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
241f91e0974SThilina Rathnayake	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
2421e25a746SJed Brown	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
2431e25a746SJed Brown	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
244a5ba6ca9Scamierjs	$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/"
245d5217624SJed Brown
246a5ba6ca9Scamierjs.PHONY : all cln clean print test tst prove prv examples style install doc okl-cache okl-clear
247d635b4c6Scamierjs
2486e5d1fd9Scamierjscln clean :
2496ea7c6c1SJed Brown	$(RM) *.o *.d $(libceed)
2506ea7c6c1SJed Brown	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
251182fbe45STzanio	$(MAKE) -C examples/ceed clean
252f3ea3031SVeselin Dobrev	$(MAKE) -C examples/mfem clean
253182fbe45STzanio	$(MAKE) -C examples/petsc clean
254111c3977SJed Brown	(cd examples/nek5000 && bash make-nek-examples.sh clean)
255c4acb06bSStan Tomov	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
2564859b599SJed Brown
25707838a1cSTzaniodistclean : clean
25807838a1cSTzanio	rm -rf doc/html
25907838a1cSTzanio
2600ab95609SJed Browndoc :
2617bd3a522STzanio	doxygen Doxyfile
2627bd3a522STzanio
263f2fa494dScamierjsstyle :
2644e3516aeSJed Brown	astyle --style=google --indent=spaces=2 --max-code-length=80 \
2650dbfdfc5SJed Brown            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
266746e9b11SJed Brown            --suffix=none --preserve-date --formatted \
267182fbe45STzanio            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp
2680dbfdfc5SJed Brown
2694859b599SJed Brownprint :
2704859b599SJed Brown	@echo $(VAR)=$($(VAR))
271582447c9SJed Brown
2729df38c42SVeselin Dobrevprint-% :
2739df38c42SVeselin Dobrev	$(info [ variable name]: $*)
2749df38c42SVeselin Dobrev	$(info [        origin]: $(origin $*))
2759df38c42SVeselin Dobrev	$(info [         value]: $(value $*))
2769df38c42SVeselin Dobrev	$(info [expanded value]: $($*))
2779df38c42SVeselin Dobrev	$(info )
2789df38c42SVeselin Dobrev	@true
2799df38c42SVeselin Dobrev
28043fb898aSJed Brown-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
281