-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
367 lines (307 loc) · 12.4 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
# VMMC Makefile
#
# Author : Lester. O. Hedges
# Email : [email protected]
# Date : April 15th 2015
################################# INFO ########################################
# This Makefile can be used to build the VMMC library along with its demos
# and documentation. For detailed information on using the Makefile run make
# without a target, i.e. simply run make at your command prompt.
#
# Makefile style adapted from http://clarkgrubb.com/make-file-style-guide
# Conventions:
# - Environment and Makefile variables are in upper case, user
# defined variables are in lower case.
# - Variables are declared using the immediate assignment operator :=
############################### MACROS ########################################
define colorecho
@if hash tput 2> /dev/null; then \
if [[ -t 1 ]]; then \
tput setaf $1; \
echo $2; \
tput sgr0; \
else \
echo $2; \
fi \
else \
echo $2; \
fi
endef
define boldcolorecho
@if hash tput 2> /dev/null; then \
if [[ -t 1 ]]; then \
tput bold; \
tput setaf $1; \
echo $2; \
tput sgr0; \
else \
echo $2; \
fi \
else \
echo $2; \
fi
endef
############################## VARIABLES ######################################
# Set shell to bash.
SHELL := bash
# Suppress display of executed commands.
.SILENT:
# Default goal will print the help message.
.DEFAULT_GOAL := help
# Project name.
project := vmmc
# C++ compiler.
CXX := g++
# Installation path.
PREFIX := /usr/local
# Python version.
PYTHON := 3.7
# Path for source files.
src_dir := src
# Path for demo code.
demo_dir := demos
# Path for object files.
obj_dir := obj
# Path for demo object files.
demo_obj_dir := $(demo_dir)/obj
# Path for the library.
lib_dir := lib
# Path for the demonstration library.
demo_lib_dir := $(demo_dir)/lib
# Library header file.
library_header := $(src_dir)/VMMC.h
# Demo header file.
demo_library_header := $(demo_dir)/src/Demo.h
# Generate library target name.
library := $(lib_dir)/lib$(project).a
# Generate demo library target name.
demo_library := $(demo_lib_dir)/libdemo.a
# Install command.
install_cmd := install
# Install flags for executables.
iflags_exec := -m 0755
# Install flags for non-executable files.
iflags := -m 0644
# Git commit information.
commit := $(shell git describe --abbrev=4 --dirty --always --tags 2> /dev/null)
# Git branch information.
branch := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null)
# Python header file.
python_header := $(shell locate Python.h | grep $(PYTHON) | head -n 1 | awk -F "/Python.h" '{print $$1}')
# Python library.
python_library := $(shell locate libpython$(PYTHON) | head -n 1)
# C++ compiler flags for development build.
cxxflags_devel := -O0 -std=c++11 -g -Wall -Isrc -DCOMMIT=\"$(commit)\" -DBRANCH=\"$(branch)\" $(OPTFLAGS)
# C++ compiler flags for release build.
cxxflags_release := -O3 -std=c++11 -DNDEBUG -Isrc -DCOMMIT=\"$(commit)\" -DBRANCH=\"$(branch)\" $(OPTFLAGS)
# Default to release build.
CXXFLAGS := $(cxxflags_release)
# The C++ header, source, object, and dependency files.
vmmc_headers := $(wildcard $(src_dir)/*.h)
vmmc_sources := $(wildcard $(src_dir)/*.cpp)
temp := $(patsubst %.cpp,%.o,$(vmmc_sources))
objects := $(subst $(src_dir),$(obj_dir),$(temp))
-include $(subst .o,.d,$(objects))
# Source files and executable names for demos.
demo_files := $(wildcard $(demo_dir)/*.cpp)
demos := $(patsubst %.cpp,%,$(demo_files))
demo_headers := $(wildcard $(demo_dir)/src/*.h)
demo_headers := $(filter-out $(demo_library_header), $(demo_headers))
demo_sources := $(wildcard $(demo_dir)/src/*.cpp)
temp := $(patsubst %.cpp,%.o,$(demo_sources))
demo_objects := $(subst $(demo_dir)/src,$(demo_obj_dir),$(temp))
# Source files and executable names for Python demos.
python_demo_files := $(wildcard $(demo_dir)/python/*.cpp)
python_demos := $(patsubst %.cpp,%,$(python_demo_files))
python_sources := $(wildcard $(demo_dir)/python/demo/*.py)
# Doxygen files.
dox_files := $(wildcard dox/*.dox)
############################### TARGETS #######################################
# Print help message.
.PHONY: help
help:
$(call boldcolorecho, 4, "About")
@echo " This Makefile can be used to build the $(project) library along with its"
@echo " associated demos and documentation."
@echo
$(call boldcolorecho, 4, "Targets")
@echo " help --> print this help message"
@echo " build --> build library and demos (default=release)"
@echo " devel --> build using development compiler flags (debug)"
@echo " release --> build using release compiler flags (optimized)"
@echo " doc --> generate source code documentation with doxygen"
@echo " clean --> remove object and dependency files"
@echo " clobber --> remove all files generated by make"
@echo " install --> install library, demos, and documentation"
@echo " uninstall --> uninstall library, demos, and documentation"
@echo
$(call boldcolorecho, 4, "Tips")
@echo " To set a different installation path run"
@echo " make PREFIX=path install"
@echo
@echo " Additional CXXFLAGS can be passed using OPTFLAGS, e.g."
@echo " make OPTFLAGS=-Wall devel"
@echo
@echo " To compile an optimised version for pure isotropic systems"
@echo " make OPTFLAGS=-DISOTROPIC release"
@echo
@echo " Targets can be chained together, e.g."
@echo " make release doc"
# Set development compilation flags and build.
devel: CXXFLAGS := $(cxxflags_devel)
devel: build
# Set release compilation flags and build.
release: CXXFLAGS := $(cxxflags_release)
release: build
# Print compiler flags.
devel release:
$(call colorecho, 5, "--> CXXFLAGS: $(CXXFLAGS)")
# Save compiler flags to file if they differ from previous build.
# This target ensures that all object files are recompiled if the flags change.
.PHONY: force
.compiler_flags: force
@echo "$(CXXFLAGS)" | cmp -s - $@ || echo "$(CXXFLAGS)" > $@
# Check that Python header file and library are present.
# This target ensures that all Python demos are recompiled if the .check_python file changes.
.PHONY: force
.check_python: force
@echo "Python found." | cmp -s - $@ || \
if [ "$(python_header)" = "" ] || [ "$(python_library)" = "" ] ; then \
echo "Python not found."; \
exit 1; \
else echo "Python found."; \
fi > $@
# Compile VMMC object files.
# Autodepenencies are handled using a recipe taken from
# http://scottmcpeak.com/autodepend/autodepend.html
$(obj_dir)/%.o: $(src_dir)/%.cpp .compiler_flags
$(call colorecho, 2, "--> Building CXX object $*.o")
$(CXX) $(CXXFLAGS) -c -o $(obj_dir)/$*.o $(src_dir)/$*.cpp
$(CXX) -MM $(CXXFLAGS) $(src_dir)/$*.cpp > $*.d
@mv -f $*.d $*.d.tmp
@sed -e 's|.*:|$(obj_dir)/$*.o:|' < $*.d.tmp > $(obj_dir)/$*.d
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(obj_dir)/$*.d
@rm -f $*.d.tmp
# Compile demo object files.
# Autodepenencies are handled using a recipe taken from
# http://scottmcpeak.com/autodepend/autodepend.html
$(demo_obj_dir)/%.o: $(demo_dir)/src/%.cpp .compiler_flags
$(call colorecho, 2, "--> Building CXX object $*.o")
$(CXX) $(CXXFLAGS) -c -o $(demo_obj_dir)/$*.o $(demo_dir)/src/$*.cpp
$(CXX) -MM $(CXXFLAGS) $(demo_dir)/src/$*.cpp > $*.d
@mv -f $*.d $*.d.tmp
@sed -e 's|.*:|$(obj_dir)/$*.o:|' < $*.d.tmp > $(demo_obj_dir)/$*.d
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(demo_obj_dir)/$*.d
@rm -f $*.d.tmp
# Build the library and demos.
.PHONY: build
build: $(obj_dir) $(demo_obj_dir) $(library) $(demo_library_header) $(demo_library) $(demos) $(python_demos)
# Create output directory for object and dependency files.
$(obj_dir):
mkdir $(obj_dir)
# Create output directory for demo object and dependency files.
$(demo_obj_dir):
mkdir $(demo_obj_dir)
# Create demo library header file.
$(demo_library_header): $(demo_headers)
$(call colorecho, 4, "--> Generating CXX library header $(demo_library_header)")
@echo -e "#ifndef _DEMO_H\n#define _DEMO_H\n" > $(demo_library_header)
@for i in $(demo_headers); \
do h=`echo $$i | cut -d '/' -f 3`; \
echo "#include \"$$h\""; \
done | sort -g >> $(demo_library_header)
@echo -e "\n#endif" >> $(demo_library_header)
# Build the static library.
$(library): $(objects)
$(call colorecho, 1, "--> Linking CXX static library $(library)")
mkdir -p $(lib_dir)
ar rcs $@ $(objects)
ranlib $@
# Build the static demo library.
$(demo_library): $(demo_objects)
$(call colorecho, 1, "--> Linking CXX static library $(demo_library)")
mkdir -p $(demo_lib_dir)
ar rcs $@ $(demo_objects)
ranlib $@
# Compile demonstration code.
$(demos): %: %.cpp $(demo_library_header) $(library) $(demo_library) $(demo_objects)
$(call colorecho, 1, "--> Linking CXX executable $@")
-$(CXX) $(CXXFLAGS) -Wfatal-errors -I$(demo_dir)/src [email protected] $(library) $(demo_library) $(LIBS) $(LDFLAGS) -o $@
# Compile C++ Python API demonstration code.
$(python_demos): $(python_demo_files) $(python_sources) $(library) .check_python .compiler_flags
$(call colorecho, 1, "--> Linking CXX executable $@")
-$(CXX) $(CXXFLAGS) -Wfatal-errors -I$(python_header) [email protected] $(library) $(python_library) $(LIBS) $(LDFLAGS) -o $@
# Build documentation using Doxygen.
doc: $(headers) $(vmmc_headers) $(dox_files)
$(call colorecho, 4, "--> Generating CXX source documentation with Doxygen")
doxygen dox/Doxyfile
# Install the library and demos.
.PHONY: install
install: build doc
$(call colorecho, 3, "--> Installing CXX static library $(library) to $(PREFIX)/lib")
$(call colorecho, 3, "--> Installing CXX demos $(demos) to $(PREFIX)/share/$(project)-demos")
$(call colorecho, 3, "--> Installing CXX Doxygen documentation to $(PREFIX)/share/doc/$(project)")
$(install_cmd) -d $(iflags_exec) $(PREFIX)/lib
$(install_cmd) -d $(iflags_exec) $(PREFIX)/include/$(project)
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/$(project)-demos
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/$(project)-demos/python
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/$(project)-demos/python/demo
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/doc/$(project)
$(install_cmd) $(iflags) $(library) $(PREFIX)/lib
$(install_cmd) $(iflags) $(library_header) $(PREFIX)/include/$(project)
$(install_cmd) $(iflags_exec) $(demos) $(PREFIX)/share/$(project)-demos
$(install_cmd) $(iflags_exec) $(python_demos) $(PREFIX)/share/$(project)-demos/python
$(install_cmd) $(iflags) $(python_sources) $(PREFIX)/share/$(project)-demos/python/demo
cp -r doc/html $(PREFIX)/share/doc/$(project)
# Uninstall the library and demos.
.PHONY: uninstall
uninstall:
$(call colorecho, 3, "--> Uninstalling CXX static library $(library) from $(PREFIX)/lib")
$(call colorecho, 3, "--> Uninstalling CXX demos $(demos) from $(PREFIX)/share/$(project)-demos")
$(call colorecho, 3, "--> Uninstalling CXX Doxygen documentation from $(PREFIX)/share/doc/$(project)")
rm -f $(PREFIX)/$(library)
rm -rf $(PREFIX)/include/$(project)
rm -rf $(PREFIX)/share/$(project)-demos
rm -rf $(PREFIX)/share/doc/$(project)
# Clean up object and dependecy files.
.PHONY: clean
clean:
$(call colorecho, 6, "--> Cleaning CXX object and dependency files")
rm -rf $(obj_dir)
rm -rf $(demo_obj_dir)
# Clean up everything produced by make.
.PHONY: clobber
clobber:
$(call colorecho, 6, "--> Cleaning all output files")
rm -rf $(obj_dir)
rm -rf $(demo_obj_dir)
rm -rf $(lib_dir)
rm -rf $(demo_lib_dir)
rm -rf doc
rm -f $(demos)
rm -f $(demo_library_header)
rm -f $(python_demos)
rm -rf $(demo_dir)/*dSYM
rm -rf $(demo_dir)/python/*dSYM
rm -f $(demo_dir)/python/demo/*.pyc
rm -f .compiler_flags
rm -f .check_python
.PHONY: sandwich
sandwich:
if [ "$$(id -u)" != "0" ]; then \
echo " What? Make it yourself." ;\
else \
echo " ____" ;\
echo " .----------' '-." ;\
echo " / . ' . \\" ;\
echo " / ' . /|" ;\
echo " / . \ /" ;\
echo " / ' . . . || |" ;\
echo " /.___________ ' / //" ;\
echo " |._ '------'| /|" ;\
echo " '.............______.-' /" ;\
echo " |-. | /" ;\
echo " \`\"\"\"\"\"\"\"\"\"\"\"\"\"-.....-'" ;\
fi; \