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