-
Notifications
You must be signed in to change notification settings - Fork 60
/
Makefile
339 lines (281 loc) · 11.1 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
# AABB.cc Makefile
# Copyright (c) 2016 Lester Hedges <lester.hedges+aabbcc@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################ INFO #######################################
# This Makefile can be used to build a CXX project 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 := aabb
# Upper case project name (for use in library header file).
project_upper := `echo $(project) | tr a-z A-Z`
# C++ compiler.
CXX := g++
# Installation path.
PREFIX := /usr/local
# Python version
PYTHON := 2.7
# External libraries.
LIBS :=
# Path for source files.
src_dir := src
# Path for demo code.
demo_dir := demos
# Path for object files.
obj_dir := obj
# Path for the library.
lib_dir := lib
# Path for the python wrapper.
python_dir := python
# Path for the header-only library.
header_only_dir := header-only
# Generate library target name.
library := $(lib_dir)/lib$(project).a
# Header only library file.
header_only_lib := $(header_only_dir)/$(project_upper).hpp
# 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 binary.
python_binary := $(shell which python$(PYTHON))
# SWIG binary.
swig_binary := $(shell which swig)
# 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.
headers := $(wildcard $(src_dir)/*.h)
sources := $(wildcard $(src_dir)/*.cc)
temp := $(patsubst %.cc,%.o,$(sources))
objects := $(subst $(src_dir),$(obj_dir),$(temp))
-include $(subst .o,.d,$(objects))
# Source files and executable names for demos.
demo_sources := $(wildcard $(demo_dir)/*.cc)
demos := $(patsubst %.cc,%,$(demo_sources))
# 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 " 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 (optmized)"
@echo " python --> build the python wrapper"
@echo " header-only --> create a header-only version of the library"
@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"
# 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 and swig binaries 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_binary)" = "" ] || [ "$(swig_binary)" = "" ] ; then \
echo "Python not found."; \
exit 1; \
else echo "Python found."; \
fi > $@
# Compile object files.
# Autodepenencies are handled using a recipe taken from
# http://scottmcpeak.com/autodepend/autodepend.html
$(obj_dir)/%.o: $(src_dir)/%.cc .compiler_flags
$(call colorecho, 2, "--> Building CXX object $*.o")
$(CXX) $(CXXFLAGS) -c -o $(obj_dir)/$*.o $(src_dir)/$*.cc
$(CXX) -MM $(CXXFLAGS) $(src_dir)/$*.cc > $*.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
# Build the library and demos.
.PHONY: build
build: $(obj_dir) $(library) $(demos) python
# Create output directory for object and dependency files.
$(obj_dir):
mkdir $(obj_dir)
# Build the static library.
$(library): $(objects)
$(call colorecho, 1, "--> Linking CXX static library $(library)")
mkdir -p $(lib_dir)
ar rcs $@ $(objects)
ranlib $@
# Compile demonstration code.
$(demos): %: %.cc $(library)
$(call colorecho, 1, "--> Linking CXX executable $@")
$(CXX) $(CXXFLAGS) $@.cc $(library) $(LIBS) $(LDFLAGS) -o $@
# Build the python wrapper.
.PHONY: python
python: .check_python $(python_dir)/aabb.i $(python_dir)/setup.py
$(call colorecho, 2, "--> Building Python wrapper")
cd $(python_dir) ;\
$(swig_binary) -builtin -c++ -python aabb.i ;\
$(python_binary) setup.py -q build_ext --inplace
# Create the header only library.
.PHONY: header-only
header-only: $(headers) $(sources)
mkdir -p $(header_only_dir)
head -n 499 src/AABB.h > $(header_only_lib)
echo >> $(header_only_lib)
tail +29 src/AABB.cc >> $(header_only_lib)
echo >> $(header_only_lib)
echo "#endif /* _AABB_H */" >> $(header_only_lib)
# Build documentation using Doxygen.
doc: $(headers) $(sources) $(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 Python wrapper to $(PREFIX)/lib/python$(PYTHON)")
$(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)/lib/python$(PYTHON)
$(install_cmd) -d $(iflags_exec) $(PREFIX)/include/$(project)
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/$(project)-demos
$(install_cmd) -d $(iflags_exec) $(PREFIX)/share/doc/$(project)
$(install_cmd) $(iflags) $(library) $(PREFIX)/lib
$(install_cmd) $(iflags) $(python_dir)/aabb.py $(PREFIX)/lib/python$(PYTHON)
$(install_cmd) $(iflags_exec) $(python_dir)/_aabb.so $(PREFIX)/lib/python$(PYTHON)
$(install_cmd) $(iflags) $(headers) $(PREFIX)/include/$(project)
$(install_cmd) $(iflags) $(demo_sources) $(PREFIX)/share/$(project)-demos
$(install_cmd) $(iflags_exec) $(demos) $(PREFIX)/share/$(project)-demos
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 Python wrapper from $(PREFIX)/lib/python$(PYTHON)")
$(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 -f $(PREFIX)/lib/python$(PYTHON)/aabb.py
rm -f $(PREFIX)/lib/python$(PYTHON)/_aabb.so
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)
# Clean up everything produced by make.
.PHONY: clobber
clobber:
$(call colorecho, 6, "--> Cleaning all output files")
rm -rf $(obj_dir)
rm -rf $(lib_dir)
rm -rf $(python_dir)/build
rm -rf $(python_dir)/_aabb.*
rm -rf $(python_dir)/aabb_wrap.cxx
rm -rf $(python_dir)/aabb.py*
rm -rf $(python_dir)/__pycache__
rm -rf $(header_only_dir)
rm -rf doc
rm -f $(demos)
rm -rf $(demo_dir)/*dSYM
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; \