xref: /libCEED/Makefile (revision 7692a9b0d0d14c276ad1ae7b71706887646b682a)
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
17ifeq (,$(filter-out undefined default,$(origin CC)))
18  CC = gcc
19endif
20ifeq (,$(filter-out undefined default,$(origin FC)))
21  FC = gfortran
22endif
23NVCC = $(CUDA_DIR)/bin/nvcc
24
25# ASAN must be left empty if you don't want to use it
26ASAN ?=
27NDEBUG ?= 1
28
29LDFLAGS ?=
30UNDERSCORE ?= 1
31
32# OCCA_DIR env variable should point to OCCA master (github.com/libocca/occa)
33OCCA_DIR ?= ../occa
34
35# env variable MAGMA_DIR can be used too
36MAGMA_DIR ?= ../magma
37# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
38CUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
39               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
40
41# Warning: SANTIZ options still don't run with /gpu/occa
42# export LSAN_OPTIONS=suppressions=.asanignore
43AFLAGS = -fsanitize=address #-fsanitize=undefined -fno-omit-frame-pointer
44
45CFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP -O2 -g
46FFLAGS = -cpp     -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP
47
48CFLAGS += $(if $(NDEBUG),-DNDEBUG=1)
49
50ifeq ($(UNDERSCORE), 1)
51  CFLAGS += -DUNDERSCORE
52endif
53
54FFLAGS += $(if $(NDEBUG),-O2 -DNDEBUG,-g)
55
56CFLAGS += $(if $(ASAN),$(AFLAGS))
57FFLAGS += $(if $(ASAN),$(AFLAGS))
58LDFLAGS += $(if $(ASAN),$(AFLAGS))
59CPPFLAGS = -I./include
60LDLIBS = -lm
61OBJDIR := build
62LIBDIR := lib
63
64# Installation variables
65prefix ?= /usr/local
66bindir = $(prefix)/bin
67libdir = $(prefix)/lib
68okldir = $(prefix)/lib/okl
69includedir = $(prefix)/include
70pkgconfigdir = $(libdir)/pkgconfig
71INSTALL = install
72INSTALL_PROGRAM = $(INSTALL)
73INSTALL_DATA = $(INSTALL) -m644
74
75# Get number of processors of the machine
76NPROCS := $(shell getconf _NPROCESSORS_ONLN)
77# prepare make options to run in parallel
78MFLAGS := -j $(NPROCS) --warn-undefined-variables \
79                       --no-print-directory --no-keep-going
80
81PROVE ?= prove
82PROVE_OPTS ?= -j $(NPROCS)
83DARWIN := $(filter Darwin,$(shell uname -s))
84SO_EXT := $(if $(DARWIN),dylib,so)
85
86ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
87libceed := $(LIBDIR)/libceed.$(SO_EXT)
88libceed.c := $(wildcard ceed*.c)
89
90# Tests
91tests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
92tests.f   := $(sort $(wildcard tests/t[0-9][0-9]-*.f))
93tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
94ctests    := $(tests)
95tests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
96#examples
97examples.c := $(sort $(wildcard examples/ceed/*.c))
98examples.f := $(sort $(wildcard examples/ceed/*.f))
99examples  := $(examples.c:examples/ceed/%.c=$(OBJDIR)/%)
100examples  += $(examples.f:examples/ceed/%.f=$(OBJDIR)/%)
101# backends/[ref & occa  & magma]
102ref.c     := $(sort $(wildcard backends/ref/*.c))
103occa.c    := $(sort $(wildcard backends/occa/*.c))
104magma_preprocessor := python backends/magma/gccm.py
105magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
106magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
107magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
108magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
109magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
110magma_allsrc.cu:= $(magma_tmp.cu)
111
112# Output using the 216-color rules mode
113rule_file = $(notdir $(1))
114rule_path = $(patsubst %/,%,$(dir $(1)))
115last_path = $(notdir $(patsubst %/,%,$(dir $(1))))
116ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17)
117emacs_out = @printf "  %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2))
118color_out = @if [ -t 1 ]; then \
119				printf "  %10s \033[38;5;%d;1m%s\033[m/%s\n" \
120					$(1) $(call ansicolor,$(2)) \
121					$(call rule_path,$(2)) $(call rule_file,$(2)); else \
122				printf "  %10s %s\n" $(1) $(2); fi
123# if TERM=dumb, use it, otherwise switch to the term one
124output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2))
125
126# if V is set to non-nil, turn the verbose mode
127quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1)))
128
129.SUFFIXES:
130.SUFFIXES: .c .o .d
131.SECONDEXPANSION: # to expand $$(@D)/.DIR
132
133.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
134
135%/.DIR :
136	@mkdir -p $(@D)
137	@touch $@
138
139.PRECIOUS: %/.DIR
140
141this: $(libceed) $(ceed.pc)
142# run 'this' target in parallel
143all:;@$(MAKE) $(MFLAGS) V=$(V) this
144
145$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name @rpath/$(notdir $(libceed)))
146
147libceed.c += $(ref.c)
148ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
149  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
150  $(libceed) : LDLIBS += -locca
151  libceed.c += $(occa.c)
152  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
153endif
154ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
155  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
156  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
157  ifneq ($(CUDA_LIB_DIR),)
158  cuda_link = -Wl,-rpath,$(CUDA_LIB_DIR) -L$(CUDA_LIB_DIR) -lcudart
159  magma_link_static = -L$(MAGMA_DIR)/lib -lmagma $(cuda_link)
160  magma_link_shared = -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) -lmagma
161  magma_link := $(if $(wildcard $(MAGMA_DIR)/lib/libmagma.${SO_EXT}),$(magma_link_shared),$(magma_link_static))
162  magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o)
163  $(libceed)           : LDLIBS += $(magma_link)
164  $(tests) $(examples) : LDLIBS += $(magma_link)
165  $(libceed) : $(magma_allsrc.o)
166  libceed.c  += $(magma_allsrc.c)
167  libceed.cu += $(magma_allsrc.cu)
168  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
169  $(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
170  endif
171endif
172
173# generate magma_tmp.c and magma_cuda.cu from magma.c
174$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR
175	$(magma_preprocessor) $<
176
177$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
178	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
179
180$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
181	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
182
183$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
184	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
185
186$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
187	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
188
189$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
190	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
191
192$(OBJDIR)/% : examples/ceed/%.c | $$(@D)/.DIR
193	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
194
195$(OBJDIR)/% : examples/ceed/%.f | $$(@D)/.DIR
196	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
197
198$(tests) $(examples) : $(libceed)
199$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
200
201run-% : $(OBJDIR)/%
202	@tests/tap.sh $(<:build/%=%)
203
204test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
205# run test target in parallel
206tst : ;@$(MAKE) $(MFLAGS) V=$(V) test
207# CPU C tests only for backend %
208ctc-% : $(ctests);@$(foreach tst,$(ctests),$(tst) /cpu/$*;)
209
210prove : $(tests) $(examples)
211	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
212# run prove target in parallel
213prv : ;@$(MAKE) $(MFLAGS) V=$(V) prove
214
215examples : $(examples)
216
217$(ceed.pc) : pkgconfig-prefix = $(abspath .)
218$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
219.INTERMEDIATE : $(OBJDIR)/ceed.pc
220%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
221	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
222
223# The occa executable is not linked with RPATH by default, so we need it to find its libocca.so
224OCCA               := $(if $(DARWIN),DYLD_LIBRARY_PATH,LD_LIBRARY_PATH)=$(OCCA_DIR)/lib $(OCCA_DIR)/bin/occa
225OKL_KERNELS        := $(wildcard backends/occa/*.okl)
226
227okl-cache :
228	$(OCCA) cache ceed $(OKL_KERNELS)
229
230okl-clear:
231	$(OCCA) clear -y -l ceed
232
233install : $(libceed) $(OBJDIR)/ceed.pc
234	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(okldir)" "$(DESTDIR)$(pkgconfigdir)"
235	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
236	$(INSTALL_DATA) include/ceedf.h "$(DESTDIR)$(includedir)/"
237	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
238	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
239	$(INSTALL_DATA) $(OKL_KERNELS) "$(DESTDIR)$(okldir)/"
240
241.PHONY : all cln clean print test tst prove prv examples style install doc okl-cache okl-clear
242
243cln clean :
244	$(RM) *.o *.d $(libceed)
245	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
246	$(MAKE) -C examples/ceed clean
247	$(MAKE) -C examples/mfem clean
248	$(MAKE) -C examples/petsc clean
249	(cd examples/nek5000 && bash make-nek-examples.sh clean)
250	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
251
252distclean : clean
253	rm -rf doc/html
254
255doc :
256	doxygen Doxyfile
257
258style :
259	astyle --style=google --indent=spaces=2 --max-code-length=80 \
260            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
261            --suffix=none --preserve-date --formatted \
262            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*/*.[ch] examples/*/*.[ch]pp
263
264print :
265	@echo $(VAR)=$($(VAR))
266
267print-% :
268	$(info [ variable name]: $*)
269	$(info [        origin]: $(origin $*))
270	$(info [         value]: $(value $*))
271	$(info [expanded value]: $($*))
272	$(info )
273	@true
274
275-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
276