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