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