-
Notifications
You must be signed in to change notification settings - Fork 4
/
Makefile
518 lines (374 loc) · 14.3 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# @file Makefile
# @brief `make` instructions for building and testing Triumvirate.
# @authors Mike S Wang (https://github.com/MikeSWang)
#
# ========================================================================
# Configuration
# ========================================================================
REPONAME := Triumvirate
PKGNAME := triumvirate
PROGNAME := triumvirate
LIBNAME := trv
SCM_VER_SCHEME ?= no-guess-dev
SCM_LOC_SCHEME ?= node-and-date
# ------------------------------------------------------------------------
# Preamble
# ------------------------------------------------------------------------
# Escape characters
COMMA := ,
# ------------------------------------------------------------------------
# Directories
# ------------------------------------------------------------------------
# Repository root
DIR_ROOT := $(shell pwd)
# Package, build, test and distribution directories
DIR_PKG := ${DIR_ROOT}/src/${PKGNAME}
DIR_BUILD := ${DIR_ROOT}/build
DIR_TESTS := ${DIR_ROOT}/tests
DIR_DIST := ${DIR_ROOT}/dist
# Package subdirectories
DIR_PKG_INCLUDE := ${DIR_PKG}/include
DIR_PKG_SRC := ${DIR_PKG}/src
DIR_PKG_SRCPROG := ${DIR_PKG}/main
# Build subdirectories
DIR_BUILDOBJ := ${DIR_BUILD}/obj
DIR_BUILDLIB := ${DIR_BUILD}/lib
DIR_BUILDBIN := ${DIR_BUILD}/bin
# Test subdirectories
DIR_TESTBUILD := ${DIR_TESTS}/test_build
DIR_TESTOUT := ${DIR_TESTS}/test_output
# ------------------------------------------------------------------------
# Options
# ------------------------------------------------------------------------
# Extract the '-j' or '--jobs' option (possibly empty).
# Note the space added after ``${MAKEFLAGS}``.
PATTERN_JOBS = "\-j[[:digit:][:space:]]*[^a-z[:punct:]]"
MAKEFLAGS_JOBS = $(shell echo "${MAKEFLAGS} " | grep -Eo ${PATTERN_JOBS})
# ------------------------------------------------------------------------
# Compilation
# ------------------------------------------------------------------------
# -- OS ------------------------------------------------------------------
OS := $(shell uname -s)
# -- Compiler ------------------------------------------------------------
# Assume explicitly GCC compiler by default. [adapt]
ifeq (${OS}, Linux)
CXX ?= g++
else ifeq (${OS}, Darwin)
# Use GCC compiler from Homebrew (brew formula 'gcc').
# The compiler binary may have suffix '-<version>';
# check the version number with ``brew info gcc``.
CXX ?= $(shell find $(brew --prefix gcc)/bin -type f -name 'g++*')
# Use LLVM compiler from Homebrew (brew formula 'llvm').
# CXX ?= $(shell brew --prefix llvm)/bin/clang++
else # OS
CXX ?= g++
endif # OS
# Assume default archiver. [adapt]
AR ?= ar
ARFLAGS ?= -rcsv
# Assume default remover. [adapt]
RM ?= rm -f
# -- Dependencies --------------------------------------------------------
DEPS := gsl fftw3
# Dependencies are searched for by `pkg-config`. Ensure the set-up of
# `pkg-config` matches that of the dependencies (e.g. both are installed
# by Conda in the same Conda environment).
DEP_INCLUDES := $(shell pkg-config --silence-errors --cflags-only-I ${DEPS})
DEP_CXXFLAGS := $(shell pkg-config --silence-errors --cflags-only-other ${DEPS})
DEP_LDFLAGS := $(shell pkg-config --silence-errors --libs-only-other --libs-only-L ${DEPS})
DEP_LDLIBS := $(shell pkg-config --silence-errors --libs-only-l ${DEPS})
# -- Dependencies (test) -------------------------------------------------
DEPS_TEST := gtest
DEP_TEST_INCLUDES := $(shell pkg-config --silence-errors --cflags-only-I ${DEPS_TEST})
DEP_TEST_CXXFLAGS := $(shell pkg-config --silence-errors --cflags-only-other ${DEPS_TEST})
DEP_TEST_LDFLAGS := $(shell pkg-config --silence-errors --libs-only-other --libs-only-L ${DEPS_TEST})
DEP_TEST_LDLIBS := $(shell pkg-config --silence-errors --libs-only-l ${DEPS_TEST})
# -- Options -------------------------------------------------------------
INCLUDES += -I${DIR_PKG_INCLUDE} ${DEP_INCLUDES}
CPPFLAGS += -MMD -MP
CXXFLAGS += -std=c++17 -Wall -O3 ${DEP_CXXFLAGS}
LDFLAGS += \
$(addprefix -Wl${COMMA}-rpath${COMMA},$(patsubst -L%,%,${DEP_LDFLAGS})) \
${DEP_LDFLAGS}
LDLIBS += $(if ${DEP_LDLIBS},${DEP_LDLIBS},-lgsl -lgslcblas -lfftw3 -lm)
PIPOPTS ?= --user
# -- Options (test) ------------------------------------------------------
INCLUDES_TEST = ${INCLUDES} ${DEP_TEST_INCLUDES}
CXXFLAGS_TEST = ${CXXFLAGS} ${DEP_TEST_CXXFLAGS}
LDFLAGS_TEST = -L${DIR_BUILDLIB} ${LDFLAGS} \
$(addprefix -Wl${COMMA}-rpath${COMMA},$(patsubst -L%,%,${DEP_TEST_LDFLAGS})) \
${DEP_TEST_LDFLAGS}
LDLIBS_TEST = -l${LIBNAME} ${LDLIBS} \
$(if ${DEP_TEST_LDLIBS},${DEP_TEST_LDLIBS},-lgtest -lpthread)
# -- Environment ---------------------------------------------------------
# NERSC computer cluster: an example environment [adapt]
ifdef NERSC_HOST
# GSL library
ifdef GSL_ROOT
INCLUDES += -I${GSL_ROOT}/include
LDFLAGS += -L${GSL_ROOT}/lib
endif # GSL_ROOT
# FFTW library
ifdef FFTW_ROOT
INCLUDES += -I${FFTW_INC}
LDFLAGS += -L${FFTW_DIR}
endif # FFTW_ROOT
# GTEST library
ifdef GTEST_ROOT
INCLUDES_TEST += -I${GTEST_ROOT}/include
LDFLAGS_TEST += -Wl,-rpath,${GTEST_ROOT}/lib -L${GTEST_ROOT}/lib
endif # GTEST_ROOT
endif # NERSC_HOST
# DiRAC computer cluster [adapt]
ifdef DIRAC_HOST
# GTEST library
ifdef GTEST_ROOT
INCLUDES_TEST += -I${GTEST_ROOT}/include
LDFLAGS_TEST += -Wl,-rpath,${GTEST_ROOT}/lib -L${GTEST_ROOT}/lib
endif # GTEST_ROOT
endif # DIRAC_HOST
# -- Customisation -------------------------------------------------------
# OpenMP: enabled with ``useomp=(true|1)``; disabled otherwise
ifdef useomp
# Assume GCC implementation by default. [adapt]
ifeq ($(strip ${useomp}), $(filter $(strip ${useomp}), true 1))
ifeq (${OS}, Linux)
# Use GCC implementation.
CXXFLAGS_OMP ?= -fopenmp
LDFLAGS_OMP ?= -fopenmp
# LDLIBS_OMP ?= -lgomp
# Use Intel implementation.
# CXXFLAGS_OMP ?= -qopenmp
# LDFLAGS_OMP ?= -qopenmp
# # LDLIBS_OMP ?= -liomp5
else ifeq (${OS}, Darwin)
# Use GCC implementation.
CXXFLAGS_OMP ?= -fopenmp
LDFLAGS_OMP ?= -fopenmp
# LDLIBS_OMP ?= -lgomp
# Use LLVM implementation from Homebrew (brew formula 'libomp').
# CXXFLAGS_OMP ?= -I$(shell brew --prefix libomp)/include -Xpreprocessor -fopenmp
# LDFLAGS_OMP ?= -L$(shell brew --prefix libomp)/lib
# LDLIBS_OMP ?= -lomp
else # OS
CXXFLAGS_OMP ?= -fopenmp
LDFLAGS_OMP ?= -fopenmp
endif # OS
CPPFLAGS += -DTRV_USE_OMP -DTRV_USE_FFTWOMP
CXXFLAGS += ${CXXFLAGS_OMP}
LDFLAGS += ${LDFLAGS_OMP}
LDLIBS += -lfftw3_omp ${LDLIBS_OMP}
CPPFLAGS_TEST +=
CXXFLAGS_TEST += ${CXXFLAGS_OMP}
LDFLAGS_TEST += ${LDFLAGS_OMP}
LDLIBS_TEST += ${LDLIBS_OMP}
WOMP := with
else # useomp!=(true|1)
# NOTE: Use `undefine` for make>=3.82.
unexport useomp
WOMP := without
endif # useomp==(true|1)
else # !useomp
WOMP := without
endif # useomp
# Visual enhancements: enabled with `uselogo=(true|1)`; disabled otherwise
ifdef uselogo
ifeq ($(strip ${uselogo}), $(filter $(strip ${uselogo}), true 1))
CPPFLAGS += -DTRV_USE_LOGO
endif # uselogo==(true|1)
endif # uselogo
# Profiler flags: enabled with `useprof=(true|1)`; disabled otherwise
ifdef useprof
ifeq ($(strip ${useprof}), $(filter $(strip ${useprof}), true 1))
# Linaro MAP profiler
CXXFLAGS += -g1 -O3 -fno-inline -fno-optimize-sibling-calls
endif # useprof==(true|1)
endif # useprof
# Parameter debugging: enabled with `dbgpars=(true|1)`; disabled otherwise
ifdef dbgpars
ifeq ($(strip ${dbgpars}), $(filter $(strip ${dbgpars}), true 1))
CPPFLAGS += -DDBG_MODE -DDBG_PARS
endif # dbgpars==(true|1)
endif # dbgpars
# Add options: e.g. {-DTRV_USE_LEGACY_CODE, -DDBG_MODE, -DDBG_FLAG_NOAC, ...}.
# -- Parsing -------------------------------------------------------------
# Python: export build options as environmental variables.
export PY_CXX=${CXX}
export PY_INCLUDES=${INCLUDES}
export PY_CXXFLAGS=${CXXFLAGS}
export PY_LDFLAGS=${LDFLAGS}
ifndef useomp
export PY_NO_OMP
else # useomp
export PY_CXXFLAGS_OMP=${CXXFLAGS_OMP}
export PY_LDFLAGS_OMP=${LDFLAGS_OMP}
endif # !useomp
export PY_BUILD_PARALLEL=${MAKEFLAGS_JOBS}
export PY_SCM_VER_SCHEME=${SCM_VER_SCHEME}
export PY_SCM_LOC_SCHEME=${SCM_LOC_SCHEME}
# C++: strip whitespace.
CPPFLAGS := $(strip ${CPPFLAGS}) $(strip ${INCLUDES})
CXXFLAGS := $(strip ${CXXFLAGS})
LDFLAGS := $(strip ${LDFLAGS})
LDLIBS := $(strip ${LDLIBS})
CPPFLAGS_TEST := $(strip ${CPPFLAGS_TEST}) $(strip ${INCLUDES_TEST})
CXXFLAGS_TEST := $(strip ${CXXFLAGS_TEST})
LDFLAGS_TEST := $(strip ${LDFLAGS_TEST})
LDLIBS_TEST := $(strip ${LDLIBS_TEST})
# ========================================================================
# Recipes
# ========================================================================
# ------------------------------------------------------------------------
# Building
# ------------------------------------------------------------------------
SRCS := $(wildcard ${DIR_PKG_SRC}/*.cpp)
OBJS := $(SRCS:${DIR_PKG_SRC}/%.cpp=${DIR_BUILDOBJ}/%.o)
DEPS := $(OBJS:.o=.d)
PROGSRC := ${DIR_PKG_SRCPROG}/${PROGNAME}.cpp
PROGOBJ := ${DIR_BUILDOBJ}/${PROGNAME}.o
PROGEXE := ${DIR_BUILDBIN}/${PROGNAME}
PROGLIB := ${DIR_BUILDLIB}/lib${LIBNAME}.a
# -- Installation --------------------------------------------------------
.PHONY: install cppinstall pyinstall cpplibinstall cppappbuild \
uninstall cppuninstall pyuninstall
install: cppinstall pyinstall
cppinstall: cppinstall_ cpplibinstall cppappbuild
cppinstall_:
@echo "Installing Triumvirate C++ library/program..."
cpplibinstall: library
cppappbuild: executable
pyinstall:
@echo "Installing Triumvirate Python package ${WOMP} OpenMP (in pip dev mode)..."
python -m pip install ${PIPOPTS} --editable . -vvv
uninstall: cppuninstall pyuninstall
cppuninstall:
@echo "Uninstalling Triumvirate C++ library/program..."
@echo " removing builds..."
@find ${DIR_BUILD} -mindepth 1 -maxdepth 1 ! -name ".gitignore" -exec rm -r {} +
pyuninstall:
@echo "Uninstalling Triumvirate Python package (in pip mode)..."
python -m pip uninstall -y ${PKGNAME}
# -- Components ----------------------------------------------------------
.PHONY: executable library objects_
executable: ${PROGEXE}
${PROGEXE}: $(OBJS) ${PROGOBJ}
@echo "Compiling Triumvirate C++ program ${WOMP} OpenMP..."
@if [ ! -d ${DIR_BUILDBIN} ]; then \
echo " making bin subdirectory in build directory..."; \
mkdir -p ${DIR_BUILDBIN}; \
fi
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS) $(LDLIBS)
library: ${PROGLIB}
${PROGLIB}: $(OBJS)
@echo "Creating Triumvirate C++ library ${WOMP} OpenMP..."
@if [ ! -d ${DIR_BUILDLIB} ]; then \
echo " making lib subdirectory in build directory..."; \
mkdir -p ${DIR_BUILDLIB}; \
fi
$(AR) $(ARFLAGS) $@ $^
objects_:
@echo "Creating Triumvirate C++ object files..."
@if [ ! -d ${DIR_BUILDOBJ} ]; then \
echo " making obj subdirectory in build directory..."; \
mkdir -p ${DIR_BUILDOBJ}; \
fi
${PROGOBJ}: ${PROGSRC}
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
$(OBJS): ${DIR_BUILDOBJ}/%.o: ${DIR_PKG_SRC}/%.cpp | objects_
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
-include $(DEPS)
# -- Configuration -------------------------------------------------------
.PHONY: checkopts
checkopts:
@echo "Checking options parsed by Makefile..."
@echo " MAKEFLAGS=${MAKEFLAGS}"
@echo " MAKEFLAGS_JOBS=${MAKEFLAGS_JOBS}"
@echo " SCM_VER_SCHEME=${SCM_VER_SCHEME}"
@echo " SCM_LOC_SCHEME=${SCM_LOC_SCHEME}"
@echo " CXX=${CXX}"
@echo " INCLUDES=${INCLUDES}"
@echo " CPPFLAGS=${CPPFLAGS}"
@echo " CXXFLAGS=${CXXFLAGS}"
@echo " LDFLAGS=${LDFLAGS}"
@echo " LDLIBS=${LDLIBS}"
@echo " CXXFLAGS_OMP=${CXXFLAGS_OMP}"
@echo " LDFLAGS_OMP=${LDFLAGS_OMP}"
@echo " LDLIBS_OMP=${LDLIBS_OMP}"
@echo " AR=${AR}"
@echo " ARFLAGS=${ARFLAGS}"
@echo " RM=${RM}"
@echo " PIPOPTS=${PIPOPTS}"
# ------------------------------------------------------------------------
# Testing
# ------------------------------------------------------------------------
TEST_SRCS := $(wildcard ${DIR_TESTS}/*.cpp)
TEST_EXES := $(TEST_SRCS:${DIR_TESTS}/%.cpp=${DIR_TESTBUILD}/%)
.PHONY: test cpptest cpptest_ pytest
test: cpptest pytest
cpptest: cpptest_ library ${TEST_EXES}
@echo " running tests..."
@sh -c ${TEST_EXES}
cpptest_:
@echo "Performing Triumvirate C++ tests..."
@if [ ! -d ${DIR_TESTBUILD} ]; then \
echo " making build subdirectory in test directory..."; \
mkdir -p ${DIR_TESTBUILD}; \
fi
@echo " compiling tests..."
${TEST_EXES}: ${TEST_SRCS}
$(CXX) $(CPPFLAGS_TEST) $(CXXFLAGS_TEST) $< -o $@ $(LDFLAGS_TEST) $(LDLIBS_TEST)
pytest:
@echo "Peforming Triumvirate Python tests..."
@if [ ! -d ${DIR_TESTOUT} ]; then \
echo " making output subdirectory in test directory..."; \
mkdir -p ${DIR_TESTOUT}; \
fi
@echo " running tests..."
pytest -vvv
# ------------------------------------------------------------------------
# Cleaning
# ------------------------------------------------------------------------
.PHONY: clean buildclean testclean distclean runclean cppclean pyclean
clean: buildclean testclean distclean runclean
buildclean: cppclean pyclean
cppclean:
@echo "Cleaning up Triumvirate C++ build..."
@echo " removing builds..."
@find ${DIR_BUILD} -mindepth 1 -maxdepth 1 ! -name ".gitignore" -exec rm -r {} +
pyclean:
@echo "Cleaning up Triumvirate Python build..."
@echo " removing Cythonised C/C++ scripts..."
@find ${DIR_PKG} -maxdepth 1 -name "*.cpp" -exec rm {} +
@echo " removing Cythonised extensions..."
@find ${DIR_PKG} -maxdepth 1 -name "*.so" -exec rm {} +
@echo " removing eggs..."
@find . -type d -name ".eggs" -exec rm -r {} +
@echo " removing compiled bytecode..."
@find . -type d -name "__pycache__" -exec rm -r {} +
@echo " removing Jupyter notebook checkpoints..."
@find . -type d -name ".ipynb_checkpoints" -exec rm -r {} +
testclean:
@echo "Cleaning up Triumvirate tests..."
@echo " removing test builds and outputs..."
@$(RM) -r ${DIR_TESTBUILD}/* ${DIR_TESTOUT}/*
@echo " removing pytest cache..."
@find . -type d -name ".pytest_cache" -exec rm -r {} +
@echo " removing compiled bytecode..."
@find . -type d -name "__pycache__" -exec rm -r {} +
@echo " removing core dumps..."
@$(RM) -r core
distclean:
@echo "Cleaning up Triumvirate distributions..."
@echo " removing distribution outputs..."
@$(RM) -r ${DIR_DIST}/
@echo " removing wheels..."
@find . -type d -name "wheelhouse" -exec rm -r {} +
@echo " removing eggs..."
@find . -name "*.eggs" -exec rm -r {} +
@find . -name "*.egg-info" -exec rm -r {} +
runclean:
@echo "Cleaning up Triumvirate runs..."
@echo " removing compiled bytecode..."
@find . -type d -name "__pycache__" -exec rm -r {} +
@echo " removing core dumps..."
@$(RM) -r core