xref: /libCEED/Makefile (revision 5a9ca9ad5fd061dce90f8686460d79eaf35523f3)
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 can be used too (need OCCA v1.0)
28OCCA_DIR ?= ../occa
29
30# env variable MAGMA_DIR can be used too
31MAGMA_DIR ?= ../magma
32# If CUDA_DIR is not set, check for nvcc, or resort to /usr/local/cuda
33CUDA_DIR  ?= $(or $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir \
34               $(shell which nvcc 2> /dev/null))))),/usr/local/cuda)
35
36SANTIZ = -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer
37CFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP -march=native
38FFLAGS = -cpp     -Wall -Wextra -Wno-unused-parameter -Wno-unused-dummy-argument -fPIC -MMD -MP -march=native
39
40CFLAGS += $(if $(NDEBUG),-O2,-g)
41ifeq ($(UNDERSCORE), 1)
42  CFLAGS += -DUNDERSCORE
43endif
44
45FFLAGS += $(if $(NDEBUG),-O2,-g)
46
47CFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
48FFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
49LDFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
50CPPFLAGS = -I./include
51LDLIBS = -lm
52OBJDIR := build
53LIBDIR := lib
54
55# Installation variables
56prefix ?= /usr/local
57bindir = $(prefix)/bin
58libdir = $(prefix)/lib
59includedir = $(prefix)/include
60pkgconfigdir = $(libdir)/pkgconfig
61INSTALL = install
62INSTALL_PROGRAM = $(INSTALL)
63INSTALL_DATA = $(INSTALL) -m644
64
65NPROCS := $(shell getconf _NPROCESSORS_ONLN)
66MFLAGS := -j $(NPROCS) --warn-undefined-variables \
67                       --no-print-directory --no-keep-going
68
69PROVE ?= prove
70PROVE_OPTS ?= -j $(NPROCS)
71DARWIN := $(filter Darwin,$(shell uname -s))
72SO_EXT := $(if $(DARWIN),dylib,so)
73
74ceed.pc := $(LIBDIR)/pkgconfig/ceed.pc
75libceed := $(LIBDIR)/libceed.$(SO_EXT)
76libceed.c := $(wildcard ceed*.c)
77# tests
78tests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
79tests.f   := $(sort $(wildcard tests/t[0-9][0-9]-*.f))
80tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
81tests     += $(tests.f:tests/%.f=$(OBJDIR)/%)
82#examples
83examples.c := $(sort $(wildcard examples/*.c))
84examples.f := $(sort $(wildcard examples/*.f))
85examples  := $(examples.c:examples/%.c=$(OBJDIR)/%)
86examples  += $(examples.f:examples/%.f=$(OBJDIR)/%)
87# backends/[ref & occa & magma]
88ref.c     := $(sort $(wildcard backends/ref/*.c))
89occa.c    := $(sort $(wildcard backends/occa/*.c))
90magma_preprocessor := python backends/magma/gccm.py
91magma_pre_src  := $(filter-out %_tmp.c, $(wildcard backends/magma/ceed-*.c))
92magma_dsrc     := $(wildcard backends/magma/magma_d*.c)
93magma_tmp.c    := $(magma_pre_src:%.c=%_tmp.c)
94magma_tmp.cu   := $(magma_pre_src:%.c=%_cuda.cu)
95magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c)
96magma_allsrc.cu:= $(magma_tmp.cu)
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.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu)
120
121%/.DIR :
122	@mkdir -p $(@D)
123	@touch $@
124
125.PRECIOUS: %/.DIR
126
127this: $(libceed) $(ceed.pc)
128all:;@$(MAKE) $(MFLAGS) V=$(V) this
129
130$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name $(abspath $(libceed)))
131
132libceed.c += $(ref.c)
133ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),)
134  $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib)
135  $(libceed) : LDLIBS += -locca #-lrt -ldl
136  libceed.c += $(occa.c)
137  $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include
138endif
139ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),)
140  CUDA_LIB_DIR := $(wildcard $(foreach d,lib lib64,$(CUDA_DIR)/$d/libcudart.${SO_EXT}))
141  CUDA_LIB_DIR := $(patsubst %/,%,$(dir $(firstword $(CUDA_LIB_DIR))))
142  ifneq ($(CUDA_LIB_DIR),)
143  magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o)
144  $(libceed)           : LDFLAGS += -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib)
145  $(tests) $(examples) : LDFLAGS += -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib)
146  $(libceed)           : LDLIBS += -lmagma -L$(CUDA_LIB_DIR) -lcudart
147  $(tests) $(examples) : LDLIBS += -lmagma -L$(CUDA_LIB_DIR) -lcudart
148  $(libceed) : $(magma_allsrc.o)
149  libceed.c  += $(magma_allsrc.c)
150  libceed.cu += $(magma_allsrc.cu)
151  $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include
152  $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) : NVCCFLAGS += --compiler-options=-fPIC -DADD_ -I$(MAGMA_DIR)/include -I$(MAGMA_DIR)/magmablas -I$(MAGMA_DIR)/control -I$(CUDA_DIR)/include
153  endif
154endif
155
156# generate magma_tmp.c and magma_cuda.cu from magma.c
157$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR
158	$(magma_preprocessor) $<
159
160$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR
161	$(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
162
163$(OBJDIR)/%.o : %.c | $$(@D)/.DIR
164	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
165
166$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR
167	$(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<)
168
169$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR
170	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
171
172$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR
173	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
174
175$(OBJDIR)/% : examples/%.c | $$(@D)/.DIR
176	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
177
178$(OBJDIR)/% : examples/%.f | $$(@D)/.DIR
179	$(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS)
180
181$(tests) $(examples) : $(libceed)
182$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR)
183
184run-% : $(OBJDIR)/%
185	@tests/tap.sh $(<:build/%=%)
186
187test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%)
188
189prove : $(tests) $(examples)
190	$(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%)
191
192examples : $(examples)
193
194$(ceed.pc) : pkgconfig-prefix = $(abspath .)
195$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix)
196.INTERMEDIATE: $(OBJDIR)/ceed.pc
197%/ceed.pc : ceed.pc.template | $$(@D)/.DIR
198	@sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@
199
200install : $(libceed) $(OBJDIR)/ceed.pc
201	$(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(pkgconfigdir)"
202	$(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/"
203	$(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/"
204	$(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/"
205
206.PHONY: all cln clean print test tst examples astyle install doc
207cln clean :
208	$(RM) *.o *.d $(libceed)
209	$(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig
210	$(MAKE) -C examples clean
211	$(MAKE) -C examples/mfem clean
212	cd examples/nek5000; bash make-nek-examples.sh clean; cd ../..;
213	$(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o
214
215distclean : clean
216	rm -rf doc/html
217
218doc :
219	doxygen Doxyfile
220
221astyle :
222	astyle --style=google --indent=spaces=2 --max-code-length=80 \
223            --keep-one-line-statements --keep-one-line-blocks --lineend=linux \
224            --suffix=none --preserve-date --formatted \
225            *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*.[ch] examples/mfem/*.[ch]pp
226
227print :
228	@echo $(VAR)=$($(VAR))
229
230print-%:
231	$(info [ variable name]: $*)
232	$(info [        origin]: $(origin $*))
233	$(info [         value]: $(value $*))
234	$(info [expanded value]: $($*))
235	$(info )
236	@true
237
238-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d)
239