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