xref: /honee/Makefile (revision 947fa354726ad943622322ddb2a14816ded9df85)
1CONFIG ?= config.mk
2-include $(CONFIG)
3COMMON ?= common.mk
4-include $(COMMON)
5
6pkgconf-path = $(if $(wildcard $(1)/$(2).pc),$(1)/$(2).pc,$(2))
7
8# Library dependencies
9# Note: PETSC_ARCH can be undefined or empty for installations which do not use
10#       PETSC_ARCH - for example when using PETSc installed through Spack.
11ifneq ($(wildcard ../petsc/lib/libpetsc.*),)
12  PETSC_DIR ?= ../petsc
13endif
14petsc.pc := $(call pkgconf-path,$(PETSC_DIR)/$(PETSC_ARCH)/lib/pkgconfig,petsc)
15
16CEED_DIR ?= $(if $(wildcard $(PETSC_DIR)/$(PETSC_ARCH)/lib/pkgconfig/ceed.pc),$(PETSC_DIR)/$(PETSC_ARCH),../libCEED)
17ceed.pc  := $(call pkgconf-path,$(CEED_DIR)/lib/pkgconfig,ceed)
18
19pkgconf   = $(shell pkg-config $(if $(STATIC),--static) $1 | $(SED) -e 's/^"//g' -e 's/"$$//g')
20
21# Error checking flags
22PEDANTIC      ?=
23PEDANTICFLAGS ?= -Werror -pedantic
24
25CC        = $(call pkgconf, --variable=ccompiler $(petsc.pc) $(ceed.pc))
26CFLAGS    = -std=c99 \
27  $(call pkgconf, --variable=cflags_extra $(petsc.pc)) \
28  $(call pkgconf, --cflags-only-other $(petsc.pc)) \
29  $(OPT)
30CPPFLAGS  = $(call pkgconf, --cflags-only-I $(petsc.pc) $(ceed.pc)) \
31  $(call pkgconf, --variable=cflags_dep $(petsc.pc))
32CXX       = $(call pkgconf, --variable=cxxcompiler $(petsc.pc) $(ceed.pc))
33CXXFLAGS  = -std=c++17 -Wno-deprecated -Wno-tautological-compare
34LDFLAGS   = $(call pkgconf, --libs-only-L --libs-only-other $(petsc.pc) $(ceed.pc))
35LDFLAGS  += $(patsubst -L%, $(call pkgconf, --variable=ldflag_rpath $(petsc.pc))%, $(call pkgconf, --libs-only-L $(petsc.pc) $(ceed.pc)))
36LDLIBS    = $(call pkgconf, --libs-only-l $(petsc.pc) $(ceed.pc)) -lm -lstdc++
37
38# ASAN must be left empty if you don't want to use it
39ASAN    ?=
40
41AFLAGS  ?= -fsanitize=address
42CFLAGS  += $(if $(ASAN),$(AFLAGS))
43FFLAGS  += $(if $(ASAN),$(AFLAGS))
44LDFLAGS += $(if $(ASAN),$(AFLAGS))
45
46CPPFLAGS += -I./include
47
48# External tools
49PYTHON   ?= python3
50SED      ?= sed
51
52# LibTorch
53USE_TORCH ?=
54ifeq ($(USE_TORCH),1)
55  libtorch.pc := $(shell $(PYTHON) ./pytorch_pkgconfig.py)
56  CPPFLAGS    += $(call pkgconf, --cflags-only-I $(libtorch.pc))
57  CXXFLAGS    += $(call pkgconf, --cflags-only-other $(libtorch.pc))
58  LDFLAGS     += $(call pkgconf, --libs-only-L --libs-only-other $(libtorch.pc))
59  LDFLAGS     += $(patsubst -L%, $(call pkgconf, --variable=ldflag_rpath $(petsc.pc))%, $(call pkgconf, --libs-only-L $(libtorch.pc)))
60  LDLIBS      += $(call pkgconf, --libs-only-l $(libtorch.pc))
61
62  src.cpp += $(sort $(wildcard $(PROBLEMDIR)/torch/*.cpp))
63  src.c   += $(sort $(wildcard $(PROBLEMDIR)/torch/*.c))
64
65  # Intel Pytorch EXtension (IPEX)
66  IPEX_DIR ?=
67  ifdef IPEX_DIR
68      LDFLAGS += -L$(IPEX_DIR)/lib/
69      LDFLAGS += -Wl,-rpath,$(IPEX_DIR)/lib/
70      LDLIBS  += -lintel-ext-pt-gpu
71  endif
72endif
73
74# Source Files
75OBJDIR := build
76SRCDIR := src
77PROBLEMDIR := problems
78
79src.c := navierstokes.c $(sort $(wildcard $(PROBLEMDIR)/*.c)) $(sort $(wildcard $(SRCDIR)/*.c))
80src.o = $(src.c:%.c=$(OBJDIR)/%.o) $(src.cpp:%.cpp=$(OBJDIR)/%.o)
81
82# Path to install directory for SmartRedis. Example: /software/smartredis/install
83SMARTREDIS_DIR ?=
84ifdef SMARTREDIS_DIR
85	hiredis.pc := $(SMARTREDIS_DIR)/lib/pkgconfig/hiredis.pc
86	lsmartredis:= -lsmartredis
87	redis++.pc = $(wildcard $(SMARTREDIS_DIR)/lib/pkgconfig/redis++.pc $(SMARTREDIS_DIR)/lib64/pkgconfig/redis++.pc)
88
89	CPPFLAGS += $(call pkgconf, --cflags-only-I $(hiredis.pc) $(redis++.pc))
90	LDFLAGS += $(call pkgconf, --libs-only-L --libs-only-other $(hiredis.pc) $(redis++.pc))
91	LDFLAGS += $(patsubst -L%, $(call pkgconf, --variable=ldflag_rpath $(petsc.pc))%, $(call pkgconf, --libs-only-L $(hiredis.pc) $(redis++.pc)))
92	LDLIBS += $(call pkgconf, --libs-only-l $(hiredis.pc) $(redis++.pc)) $(lsmartredis)
93	src.c += $(sort $(wildcard $(SRCDIR)/smartsim/*.c))
94endif
95
96all: navierstokes
97
98# Diagnostic information
99info-basic:
100	$(info -----------------------------------------)
101	$(info |      __  ______  _   ______________   |)
102	$(info |     / / / / __ \/ | / / ____/ ____/   |)
103	$(info |    / /_/ / / / /  |/ / __/ / __/      |)
104	$(info |   / __  / /_/ / /|  / /___/ /___      |)
105	$(info |  /_/ /_/\____/_/ |_/_____/_____/      |)
106	$(info -----------------------------------------)
107	$(info )
108	$(info -----------------------------------------)
109	$(info )
110	$(info Dependencies:)
111	$(info CEED_DIR       = $(CEED_DIR))
112	$(info PETSC_DIR      = $(PETSC_DIR))
113	$(info PETSC_ARCH     = $(PETSC_ARCH))
114	$(info )
115	$(info Optional Dependencies:)
116	$(info SMARTREDIS_DIR = $(or $(SMARTREDIS_DIR),(not found)))
117	$(info USE_TORCH      = $(USE_TORCH))
118	$(info )
119	$(info -----------------------------------------)
120	$(info )
121	@true
122
123info:
124	$(info -----------------------------------------)
125	$(info |      __  ______  _   ______________   |)
126	$(info |     / / / / __ \/ | / / ____/ ____/   |)
127	$(info |    / /_/ / / / /  |/ / __/ / __/      |)
128	$(info |   / __  / /_/ / /|  / /___/ /___      |)
129	$(info |  /_/ /_/\____/_/ |_/_____/_____/      |)
130	$(info -----------------------------------------)
131	$(info )
132	$(info -----------------------------------------)
133	$(info )
134	$(info Dependencies:)
135	$(info CEED_DIR        = $(CEED_DIR))
136	$(info PETSC_DIR       = $(PETSC_DIR))
137	$(info PETSC_ARCH      = $(PETSC_ARCH))
138	$(info )
139	$(info Optional Dependencies:)
140	$(info SMARTREDIS_DIR = $(or $(SMARTREDIS_DIR),(not found)))
141	$(info USE_TORCH      = $(USE_TORCH))
142	$(info )
143	$(info -----------------------------------------)
144	$(info )
145	$(info Build Options:)
146	$(info CC              = $(CC))
147	$(info CFLAGS          = $(CFLAGS))
148	$(info CPPFLAGS        = $(CPPFLAGS))
149	$(info LDFLAGS         = $(LDFLAGS))
150	$(info LDLIBS          = $(LDLIBS))
151	$(info AR              = $(AR))
152	$(info ARFLAGS         = $(ARFLAGS))
153	$(info OPT             = $(OPT))
154	$(info AFLAGS          = $(AFLAGS))
155	$(info ASAN            = $(or $(ASAN),(empty)))
156	$(info VERBOSE         = $(or $(V),(empty)) [verbose=$(if $(V),on,off)])
157	$(info )
158	$(info -----------------------------------------)
159	$(info )
160	$(info Format and Style Options:)
161	$(info CLANG_FORMAT    = $(CLANG_FORMAT))
162	$(info FORMAT_OPTS     = $(FORMAT_OPTS))
163	$(info CLANG_TIDY      = $(CLANG_TIDY))
164	$(info TIDY_OPTS       = $(TIDY_OPTS))
165	$(info TIDY_FILE_OPTS  = $(TIDY_FILE_OPTS))
166	$(info )
167	$(info -----------------------------------------)
168	$(info )
169	@true
170
171# Get number of processors of the machine
172NPROCS := $(shell getconf _NPROCESSORS_ONLN)
173# prepare make options to run in parallel
174MFLAGS := -j $(NPROCS) --warn-undefined-variables \
175                       --no-print-directory --no-keep-going
176
177$(OBJDIR)/navierstokes: $(src.o) | navierstokes
178navierstokes: $(src.o) | $(petsc.pc) $(ceed.pc)
179	$(call quiet,LINK.o) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $(OBJDIR)/$@
180
181.SECONDEXPANSION: # to expand $$(@D)/.DIR
182%/.DIR :
183	@mkdir -p $(@D)
184	@touch $@
185
186# Quiet, color output
187quiet ?= $($(1))
188
189$(OBJDIR)/%.o : %.c  Makefile | $$(@D)/.DIR
190	$(call quiet,CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(abspath $<)
191
192$(OBJDIR)/%.o : %.cpp Makefile | $$(@D)/.DIR
193	$(call quiet,CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(abspath $<)
194
195print: $(petsc.pc) $(ceed.pc)
196	$(info CC      : $(CC))
197	$(info CFLAGS  : $(CFLAGS))
198	$(info CPPFLAGS: $(CPPFLAGS))
199	$(info LDFLAGS : $(LDFLAGS))
200	$(info LDLIBS  : $(LDLIBS))
201	$(info OPT     : $(OPT))
202	@true
203
204print-% :
205	$(info [ variable name]: $*)
206	$(info [        origin]: $(origin $*))
207	$(info [        flavor]: $(flavor $*))
208	$(info [         value]: $(value $*))
209	$(info [expanded value]: $($*))
210	$(info )
211	@true
212
213clean:
214	$(RM) -r $(OBJDIR) navierstokes *.vtu *.bin* *.csv *.png
215
216$(petsc.pc):
217	$(if $(wildcard $@),,$(error \
218	  PETSc config not found. Please set PETSC_DIR and PETSC_ARCH))
219
220.PHONY: all print clean
221
222# Fluid Dynamics Examples
223fluidsexamples.c  := $(sort $(wildcard *.c))
224fluidsexamples.py := smartsim_regression_framework.py
225fluidsexamples    := $(fluidsexamples.c:%.c=$(OBJDIR)/%)
226fluidsexamples    += $(fluidsexamples.py:%.py=$(OBJDIR)/%)
227
228$(OBJDIR)/$(fluidsexamples.py): $(OBJDIR)/navierstokes
229
230examples := $(fluidsexamples)
231
232$(examples) : $(libceed)
233$(tests) : $(libceed)
234
235# Documentation
236DOXYGEN ?= doxygen
237DOXYGENOPTS ?= -q
238doxygen :
239	$(DOXYGEN) $(DOXYGENOPTS) Doxyfile
240
241SPHINXOPTS      =
242SPHINXBUILD     = sphinx-build
243SPHINXAUTOBUILD = sphinx-autobuild
244SPHINXPROJ      = Ratel
245SPHINXBUILDDIR  = doc/build
246
247doc-html doc-dirhtml doc-latexpdf doc-epub doc-help : doc-% : doxygen
248	@$(SPHINXBUILD) -M $* . "$(SPHINXBUILDDIR)" $(SPHINXOPTS)
249
250doc-livehtml : doxygen
251	@$(SPHINXAUTOBUILD) . "$(SPHINXBUILDDIR)" $(SPHINXOPTS)
252
253doc : doc-html
254
255doc-clean:
256	$(RM) -r doc/html doc/build
257
258# Tidy
259CLANG_TIDY     ?= clang-tidy
260TIDY_OPTS      ?= --quiet
261TIDY_FILE_OPTS += $(CPPFLAGS) --std=c99
262
263%.c.tidy : %.c
264	$(call quiet,CLANG_TIDY) $(TIDY_OPTS) $^ -- $(TIDY_FILE_OPTS)
265
266tidy-c   : $(src.c:%=%.tidy)
267
268tidy     : tidy-c
269
270# Style
271CLANG_FORMAT  ?= clang-format
272FORMAT_OPTS   += -style=file -i
273AUTOPEP8      ?= autopep8
274AUTOPEP8_OPTS += --in-place --aggressive --max-line-length 120
275SED_FMT_OPTS  += -r 's/\s+$$//' -i
276
277%.format : %
278	$(call quiet,CLANG_FORMAT) $(FORMAT_OPTS) $^
279
280format.ch := $(shell git ls-files *.[ch]pp *.[ch])
281format.py := $(filter-out tests/junit-xml/junit_xml/__init__.py, $(shell git ls-files '*.py'))
282format.ot := $(shell git ls-files *.md)
283
284format-c  :
285	$(call quiet,CLANG_FORMAT) $(FORMAT_OPTS) $(format.ch)
286
287format-py :
288	$(call quiet,AUTOPEP8) $(AUTOPEP8_OPTS) $(format.py)
289
290format-ot :
291	$(call quiet,SED) $(SED_FMT_OPTS) $(format.ot)
292
293format    : format-c format-py format-ot
294
295# Testing
296ifeq ($(COVERAGE), 1)
297  CFLAGS  += --coverage
298  LDFLAGS += --coverage
299endif
300
301PROVE      ?= prove
302PROVE_OPTS ?= -j $(NPROCS)
303
304# Set libCEED backends for testing
305CEED_BACKENDS ?= /cpu/self
306export CEED_BACKENDS
307
308# Set number processes for testing
309NPROC_TEST ?= 1
310export NPROC_TEST
311
312# Set pool size for testing
313NPROC_POOL ?= 1
314export NPROC_POOL
315
316JUNIT_BATCH ?= ''
317
318run-% : $(OBJDIR)/%
319	@$(PYTHON) tests/junit.py --ceed-backends $(CEED_BACKENDS) --mode tap $(if $(SMARTREDIS_DIR),--smartredis_dir $(SMARTREDIS_DIR) ) $(if $(USE_TORCH),--has_torch $(USE_TORCH) )--nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) $(<:$(OBJDIR)/%=%)
320
321# The test and prove targets can be controlled via pattern searches. The default
322# is to run all tests and examples. Examples of finer grained control:
323#
324#   make prove search='t3'    # t3xx series tests
325#   make junit search='t ex'  # core tests and examples
326search    ?= navierstokes
327realsearch = $(search:%=%%)
328matched    = $(foreach pattern,$(realsearch),$(filter $(OBJDIR)/$(pattern),$(tests) $(examples)))
329
330# Test
331test    : $(matched:$(OBJDIR)/%=run-%)
332
333tst     : ;@$(MAKE) $(MFLAGS) V=$(V) test
334
335# Test with TAP output
336prove   : $(matched)
337	$(info Running unit tests)
338	$(info - Testing with libCEED backends: $(CEED_BACKENDS))
339	$(info - Testing on $(NPROC_TEST) processes)
340	$(PROVE) $(PROVE_OPTS) --exec '$(PYTHON) tests/junit.py --ceed-backends $(CEED_BACKENDS) --mode tap $(if $(SMARTREDIS_DIR),--smartredis_dir $(SMARTREDIS_DIR) ) $(if $(USE_TORCH),--has_torch $(USE_TORCH) )--nproc $(NPROC_TEST) --pool-size $(NPROC_POOL)' $(matched:$(OBJDIR)/%=%)
341
342prv     : ;@$(MAKE) $(MFLAGS) V=$(V) prove
343
344prove-all :
345	+$(MAKE) prove realsearch=%
346
347# Test with JUNIT output
348junit-% : $(OBJDIR)/%
349	@printf "  %10s %s\n" TEST $(<:$(OBJDIR)/%=%); $(PYTHON) tests/junit.py --junit-batch $(JUNIT_BATCH) --ceed-backends $(CEED_BACKENDS) $(if $(SMARTREDIS_DIR),--smartredis_dir $(SMARTREDIS_DIR) ) $(if $(USE_TORCH),--has_torch $(USE_TORCH) )--nproc $(NPROC_TEST) --pool-size $(NPROC_POOL) $(<:$(OBJDIR)/%=%)
350
351junit   : $(matched:$(OBJDIR)/%=junit-%)
352
353
354# Configure
355# "make configure" detects any variables passed on the command line or
356# previously set in config.mk, caching them in config.mk as simple
357# (:=) variables.  Variables set in config.mk or on the command line
358# take precedence over the defaults provided in the file.  Typical
359# usage:
360#
361#   make configure CC=/path/to/my/cc CUDA_DIR=/opt/cuda
362#   make
363#   make prove
364#
365# The values in the file can be updated by passing them on the command
366# line, e.g.,
367#
368#   make configure CC=/path/to/other/clang
369
370# All variables to consider for caching
371CONFIG_VARS = CEED_DIR PETSC_DIR PETSC_ARCH CCOPT CFLAGS CPPFLAGS AR ARFLAGS LDFLAGS LDLIBS SED USE_TORCH SMARTREDIS_DIR
372
373# $(call needs_save,CFLAGS) returns true (a nonempty string) if CFLAGS
374# was set on the command line or in config.mk (where it will appear as
375# a simple variable).
376needs_save = $(or $(filter command line,$(origin $(1))),$(filter simple,$(flavor $(1))))
377
378configure :
379	$(file > $(CONFIG))
380	$(foreach v,$(CONFIG_VARS),$(if $(call needs_save,$(v)),$(file >> $(CONFIG),$(v) := $($(v)))))
381	@echo "Configuration cached in $(CONFIG):"
382	@cat $(CONFIG)
383
384-include $(src.o:%.o=%.d)
385