xref: /libCEED/Makefile (revision f797d7b258c6dd07a5191b9f0f4c981dc8d1e3b2)
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
18
19NDEBUG ?=
20LDFLAGS ?=
21LOADLIBES ?=
22TARGET_ARCH ?=
23
24OCCA_DIR ?= ../occa
25
26pwd = $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
27
28SANTIZ = -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer
29CFLAGS = -std=c99 -Wall -Wextra -Wno-unused-parameter -fPIC -MMD -MP -march=native
30CFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
31CFLAGS += $(if $(NDEBUG),-O2,-g)
32LDFLAGS += $(if $(NDEBUG),,)#$(SANTIZ))
33CPPFLAGS = -I. -I$(OCCA_DIR)/include
34LDLIBS = -lm
35LDLIBS += -L$(OCCA_DIR)/lib -locca -lrt -ldl
36OBJDIR := build
37LIBDIR := .
38NPROCS := $(shell getconf _NPROCESSORS_ONLN)
39MFLAGS := -j $(NPROCS) --warn-undefined-variables \
40			--no-print-directory --quiet --no-keep-going
41
42PROVE ?= prove
43DARWIN := $(filter Darwin,$(shell uname -s))
44SO_EXT := $(if $(DARWIN),dylib,so)
45
46libceed := $(LIBDIR)/libceed.$(SO_EXT)
47libceed.c := $(wildcard ceed*.c)
48tests.c   := $(sort $(wildcard tests/t[0-9][0-9]-*.c))
49tests     := $(tests.c:tests/%.c=$(OBJDIR)/%)
50examples.c := $(sort $(wildcard examples/*.c))
51examples  := $(examples.c:examples/%.c=$(OBJDIR)/%)
52# backends/[ref & occa]
53ref.c     := $(sort $(wildcard backends/ref/*.c))
54ref       := $(ref.c:backends/ref/%.c=$(OBJDIR)/%)
55occa.c    := $(sort $(wildcard backends/occa/*.c))
56occa      := $(occa.c:backends/occa/%.c=$(OBJDIR)/%)
57
58# Output color rules
59COLOR_OFFSET = 3
60COLOR = $(shell echo $(rule_path)|cksum|cut -b1-2)
61rule_path = $(notdir $(patsubst %/,%,$(dir $<)))
62rule_file = $(basename $(notdir $@))
63rule_dumb = @echo -e $(rule_path)/$(rule_file)
64rule_term = @echo -e \\e[38\;5\;$(shell echo $(COLOR)+$(COLOR_OFFSET)|bc -l)\;1m\
65             $(rule_path)\\033[m/\\033[\m$(rule_file)\\033[m
66# if TERM=dumb, use it, otherwise switch to the term one
67output = $(if $(TERM:dumb=),$(rule_term),$(rule_dumb))
68
69.SUFFIXES:
70.SUFFIXES: .c .o .d
71.SECONDEXPANSION:		# to expand $$(@D)/.DIR
72
73%/.DIR :
74	@mkdir -p $(@D)
75	@touch $@
76
77.PRECIOUS: %/.DIR
78
79all dbg:;$(MAKE) $(MFLAGS) $(libceed) $(tests)
80opt:;NDEBUG=1 $(MAKE) $(MFLAGS) $(libceed) $(tests)
81
82$(libceed) : $(libceed.c:%.c=$(OBJDIR)/%.o) $(ref.c:%.c=$(OBJDIR)/%.o) $(occa.c:%.c=$(OBJDIR)/%.o);$(output)
83	$(CC) $(LDFLAGS) -shared -o $@ $^ $(LDLIBS)
84
85$(OBJDIR)/%.o : $(pwd)/%.c | $$(@D)/.DIR;$(output)
86	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $^
87
88$(OBJDIR)/%.o : $(pwd)/backends/ref/%.c | $$(@D)/.DIR;$(output)
89	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $^
90
91$(OBJDIR)/%.o : $(pwd)/backends/occa/%.c | $$(@D)/.DIR;$(output)
92	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $^
93
94$(OBJDIR)/%.o : $(pwd)/tests/%.c | $$(@D)/.DIR;$(output)
95	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -c -o $@ $^ $(LDLIBS)
96
97$(OBJDIR)/%.o : $(pwd)/examples/%.c | $$(@D)/.DIR;$(output)
98	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $^
99
100$(tests) $(examples) : $(libceed)
101$(tests) $(examples) : LDFLAGS += -Wl,-rpath,$(LIBDIR) -L$(LIBDIR)
102$(OBJDIR)/t% : tests/t%.c $(libceed)
103$(OBJDIR)/ex% : examples/ex%.c $(libceed)
104
105run-t% : $(OBJDIR)/t%
106	tests/tap.sh $(<:build/%=%)
107
108test : $(tests:$(OBJDIR)/t%=run-t%)
109tst:;@$(MAKE) $(MFLAGS) test
110
111prove : $(tests)
112	$(PROVE) -j $(NPROCS) --exec 'tests/tap.sh' $(tests:$(OBJDIR)/%=%)
113
114examples : $(examples)
115
116.PHONY: clean print test examples astyle
117cln clean :
118	$(RM) *.o $(OBJDIR)/*.o *.d $(OBJDIR)/*.d $(libceed) $(tests)
119	$(RM) -r *.dSYM $(OBJDIR)/backends
120
121### ASTYLE ###
122ASTYLE = astyle --options=.astylerc
123FORMAT_FILES = $(foreach dir,. tests examples occa,$(dir)/*.[ch])
124style:
125	@if ! $(ASTYLE) $(FORMAT_FILES) | grep Formatted; then\
126	   echo "No source files were changed.";\
127	fi
128
129print :
130	@echo $(VAR)=$($(VAR))
131
132print-%:
133	$(info [ variable name]: $*)
134	$(info [        origin]: $(origin $*))
135	$(info [         value]: $(value $*))
136	$(info [expanded value]: $($*))
137	$(info )
138	@true
139
140-include $(libceed.c:%.c=%.d) $(tests.c:%.c=%.d)
141