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/ceed-*.c)) 90magma_dsrc := $(wildcard backends/magma/magma_d*.c) 91magma_tmp.c := $(magma_pre_src:%.c=%_tmp.c) 92magma_tmp.cu := $(magma_pre_src:%.c=%_cuda.cu) 93magma_allsrc.c := $(magma_dsrc) $(magma_tmp.c) 94magma_allsrc.cu:= $(magma_tmp.cu) 95 96# Output using the 216-color rules mode 97rule_file = $(notdir $(1)) 98rule_path = $(patsubst %/,%,$(dir $(1))) 99last_path = $(notdir $(patsubst %/,%,$(dir $(1)))) 100ansicolor = $(shell echo $(call last_path,$(1)) | cksum | cut -b1-2 | xargs -IS expr 2 \* S + 17) 101emacs_out = @printf " %10s %s/%s\n" $(1) $(call rule_path,$(2)) $(call rule_file,$(2)) 102color_out = @if [ -t 1 ]; then \ 103 printf " %10s \033[38;5;%d;1m%s\033[m/%s\n" \ 104 $(1) $(call ansicolor,$(2)) \ 105 $(call rule_path,$(2)) $(call rule_file,$(2)); else \ 106 printf " %10s %s\n" $(1) $(2); fi 107# if TERM=dumb, use it, otherwise switch to the term one 108output = $(if $(TERM:dumb=),$(call color_out,$1,$2),$(call emacs_out,$1,$2)) 109 110# if V is set to non-nil, turn the verbose mode 111quiet = $(if $(V),$($(1)),$(call output,$1,$@);$($(1))) 112 113.SUFFIXES: 114.SUFFIXES: .c .o .d 115.SECONDEXPANSION: # to expand $$(@D)/.DIR 116 117.SECONDARY: $(magma_tmp.c) $(magma_tmp.cu) 118 119%/.DIR : 120 @mkdir -p $(@D) 121 @touch $@ 122 123.PRECIOUS: %/.DIR 124 125this: $(libceed) $(ceed.pc) 126all:;@$(MAKE) $(MFLAGS) V=$(V) this 127 128$(libceed) : LDFLAGS += $(if $(DARWIN), -install_name $(abspath $(libceed))) 129 130libceed.c += $(ref.c) 131ifneq ($(wildcard $(OCCA_DIR)/lib/libocca.*),) 132 $(libceed) : LDFLAGS += -L$(OCCA_DIR)/lib -Wl,-rpath,$(abspath $(OCCA_DIR)/lib) 133 $(libceed) : LDLIBS += -locca #-lrt -ldl 134 libceed.c += $(occa.c) 135 $(occa.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -I$(OCCA_DIR)/include 136endif 137ifneq ($(wildcard $(MAGMA_DIR)/lib/libmagma.*),) 138 magma_allsrc.o = $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) $(magma_allsrc.cu:%.cu=$(OBJDIR)/%.o) 139 $(libceed) : LDFLAGS += -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) 140 $(tests) $(examples) : LDFLAGS += -L$(MAGMA_DIR)/lib -Wl,-rpath,$(abspath $(MAGMA_DIR)/lib) 141 $(libceed) : LDLIBS += -lmagma -L$(CUDA_DIR)/lib -lcudart 142 $(tests) $(examples) : LDLIBS += -lmagma -L$(CUDA_DIR)/lib -lcudart 143 $(libceed) : $(magma_allsrc.o) 144 libceed.c += $(magma_allsrc.c) 145 libceed.cu += $(magma_allsrc.cu) 146 $(magma_allsrc.c:%.c=$(OBJDIR)/%.o) : CFLAGS += -DADD_ -I$(MAGMA_DIR)/include -I$(CUDA_DIR)/include 147 $(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 148endif 149 150# generate magma_tmp.c and magma_cuda.cu from magma.c 151$(magma_tmp.c) $(magma_tmp.cu): $(magma_pre_src) | $$(@D)/.DIR 152 $(magma_preprocessor) $< 153 154$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(libceed.cu:%.cu=$(OBJDIR)/%.o) | $$(@D)/.DIR 155 $(call quiet,CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS) 156 157$(OBJDIR)/%.o : %.c | $$(@D)/.DIR 158 $(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<) 159 160$(OBJDIR)/%.o : %.cu | $$(@D)/.DIR 161 $(call quiet,NVCC) $(CPPFLAGS) $(NVCCFLAGS) -c -o $@ $(abspath $<) 162 163$(OBJDIR)/% : tests/%.c | $$(@D)/.DIR 164 $(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS) 165 166$(OBJDIR)/% : tests/%.f | $$(@D)/.DIR 167 $(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS) 168 169$(OBJDIR)/% : examples/%.c | $$(@D)/.DIR 170 $(call quiet,CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS) 171 172$(OBJDIR)/% : examples/%.f | $$(@D)/.DIR 173 $(call quiet,FC) $(CPPFLAGS) $(FFLAGS) $(LDFLAGS) -o $@ $(abspath $<) -lceed $(LDLIBS) 174 175$(tests) $(examples) : $(libceed) 176$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(abspath $(LIBDIR)) -L$(LIBDIR) 177 178run-% : $(OBJDIR)/% 179 @tests/tap.sh $(<:build/%=%) 180 181test : $(tests:$(OBJDIR)/%=run-%) $(examples:$(OBJDIR)/%=run-%) 182 183prove : $(tests) $(examples) 184 $(PROVE) $(PROVE_OPTS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%) $(examples:$(OBJDIR)/%=%) 185 186examples : $(examples) 187 188$(ceed.pc) : pkgconfig-prefix = $(abspath .) 189$(OBJDIR)/ceed.pc : pkgconfig-prefix = $(prefix) 190.INTERMEDIATE: $(OBJDIR)/ceed.pc 191%/ceed.pc : ceed.pc.template | $$(@D)/.DIR 192 @sed "s:%prefix%:$(pkgconfig-prefix):" $< > $@ 193 194install : $(libceed) $(OBJDIR)/ceed.pc 195 $(INSTALL) -d "$(DESTDIR)$(includedir)" "$(DESTDIR)$(libdir)" "$(DESTDIR)$(pkgconfigdir)" 196 $(INSTALL_DATA) include/ceed.h "$(DESTDIR)$(includedir)/" 197 $(INSTALL_DATA) $(libceed) "$(DESTDIR)$(libdir)/" 198 $(INSTALL_DATA) $(OBJDIR)/ceed.pc "$(DESTDIR)$(pkgconfigdir)/" 199 200.PHONY: all cln clean print test tst examples astyle install doc 201cln clean : 202 $(RM) *.o *.d $(libceed) 203 $(RM) -r *.dSYM $(OBJDIR) $(LIBDIR)/pkgconfig 204 $(MAKE) -C examples clean 205 $(MAKE) -C examples/mfem clean 206 cd examples/nek5000; bash make-nek-examples.sh clean; cd ../..; 207 $(RM) $(magma_tmp.c) $(magma_tmp.cu) backends/magma/*~ backends/magma/*.o 208 209distclean : clean 210 rm -rf doc/html 211 212doc : 213 doxygen Doxyfile 214 215astyle : 216 astyle --style=google --indent=spaces=2 --max-code-length=80 \ 217 --keep-one-line-statements --keep-one-line-blocks --lineend=linux \ 218 --suffix=none --preserve-date --formatted \ 219 *.[ch] tests/*.[ch] backends/*/*.[ch] examples/*.[ch] examples/mfem/*.[ch]pp 220 221print : 222 @echo $(VAR)=$($(VAR)) 223 224print-%: 225 $(info [ variable name]: $*) 226 $(info [ origin]: $(origin $*)) 227 $(info [ value]: $(value $*)) 228 $(info [expanded value]: $($*)) 229 $(info ) 230 @true 231 232-include $(libceed.c:%.c=build/%.d) $(tests.c:tests/%.c=build/%.d) 233