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