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