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