xref: /libCEED/Makefile (revision 14c1ded135f10c3b0691bf35549a2c2918ae06a6)
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
19NVCC = nvcc
20
21NDEBUG ?=
22LDFLAGS ?=
23LOADLIBES ?=
24TARGET_ARCH ?=
25UNDERSCORE ?= 1
26
27# env variable OCCA_DIR should point to OCCA-1.0 branch
28OCCA_DIR ?= ../occa
29
30# env variable MAGMA_DIR should point to MAGMA branch
31MAGMA_DIR ?= ../../magma
32CUDA_DIR  ?= /usr/local/cuda
33
34SANTIZ = -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer
35CFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP -march=native
36FFLAGS = -cpp     -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP -march=native
37
38CFLAGS += $(if $(NDEBUG),-O2,-g)
39ifeq ($(UNDERSCORE), 1)
40  CFLAGS += -DUNDERSCORE
41endif
42
43FFLAGS += $(if $(NDEBUG),-O2,-g)
44
45CFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
46FFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
47LDFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
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 & magma]
86ref.c     := $(sort $(wildcard backends/ref/*.c))
87occa.c    := $(sort $(wildcard backends/occa/*.c))
88magma_preprocessor := python backends/magma/gccm.py
89magma_pre_src := $(filter-out %_tmp.c, $(wildcard backends/magma/*.c))
90magma.c       := $(magma_pre_src:%.c=%_tmp.c)
91magma.cu      := $(magma_pre_src:%.c=%_cuda.cu)
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.SECONDARY:
115
116%/.DIR :
117	@mkdir -p $(@D)
118	@touch $@
119
120.PRECIOUS: %/.DIR
121
122this: $(libceed) $(ceed.pc)
123all:;@$(MAKE) $(MFLAGS) V=$(V) this
124
125$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name $(abspath $(libceed)))
126
127libceed.c += $(ref.c)
128ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
129  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
130  $(libceed) : LDLIBS += -locca #-lrt -ldl
131  libceed.c += $(occa.c)
132  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
133endif
134ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
135  $(libceed) : LDFLAGS += -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib)
136  $(libceed) : LDLIBS += -lmagma -L$(CUDA_DIR)/lib -lcudart
137  $(libceed) : $(magma.o)
138  libceed.c += $(magma.c)
139  libceed.cu += $(magma.cu)
140  $(magma.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
141  $(magma.cu:%.cu=$(OBJDIR)/%.o) : NVCCFLAGS += -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
142endif
143
144# generate magma_tmp.c and magma_cuda.cu from magma.c
145%_tmp.c %_cuda.cu: %.c | $$(@D)/.DIR
146	$(magma_preprocessor) $<
147
148$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
149	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
150
151$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
152	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
153
154$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
155	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
156
157$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
158	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
159
160$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
161	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
162
163$(OBJDIR)/% : examples/%.c | $$(@D)/.DIR
164	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
165
166$(OBJDIR)/% : examples/%.f | $$(@D)/.DIR
167	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
168
169$(tests) $(examples) : $(libceed)
170$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
171
172run-% : $(OBJDIR)/%
173	@tests/tap.sh $(<:build/%=%)
174
175test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
176
177prove : $(tests) $(examples)
178	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
179
180examples : $(examples)
181
182$(ceed.pc) : pkgconfig-prefix = $(abspath .)
183$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
184.INTERMEDIATE: $(OBJDIR)/ceed.pc
185%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
186	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
187
188install : $(libceed) $(OBJDIR)/ceed.pc
189	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(pkgconfigdir)"
190	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
191	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
192	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
193
194.PHONY: all cln clean print test tst examples astyle install doc
195cln clean :
196	$(RM) *.o *.d $(libceed)
197	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
198	$(MAKE) -C examples clean
199	$(MAKE) -C examples/mfem clean
200	cd examples/nek5000; bash make-nek-examples.sh clean; cd ../..;
201	$(RM) $(magma.c) $(magma.cu) backends/magma/*~ backends/magma/*.o
202
203distclean : clean
204	rm -rf doc/html
205
206doc :
207	doxygen Doxyfile
208
209astyle :
210	astyle --style=google --indent=spaces=2 --max-code-length=80 \
211            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
212            --suffix=none --preserve-date --formatted \
213            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*.[ch] examples/mfem/*.[ch]pp
214
215print :
216	@echo $(VAR)=$($(VAR))
217
218print-%:
219	$(info [ variable name]: $*)
220	$(info [        origin]: $(origin $*))
221	$(info [         value]: $(value $*))
222	$(info [expanded value]: $($*))
223	$(info )
224	@true
225
226-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
227