-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
305 lines (278 loc) · 9.24 KB
/
Makefile
File metadata and controls
305 lines (278 loc) · 9.24 KB
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
#### PROJECT SETTINGS ####
# Must be either executable, dynamic_library or static_library
# Defaults to executable if a wrong value is used!
BIN_TYPE = dynamic_library
# The name of the executable to be created
# The extension gets appended automatically based on the type
BIN_NAME := tgui
# The name of the documentation file
DOC_NAME := TGUI-doc
# Compiler used
CXX ?= g++
# Extension of source files used in the project
SRC_EXT = cpp
# Extension of header files used in the project
HEAD_EXT = hpp
# Path to the source directory, relative to the makefile
SRC_PATH = src
# Space-separated pkg-config libraries used by this project
LIBS =
# General compiler flags
COMPILE_FLAGS = -Wall -Wextra -Wpedantic -std=c++1z
# Additional release-specific flags
RCOMPILE_FLAGS = -D__NDEBUG__ -O2 -g0
# Additional debug-specific flags
DCOMPILE_FLAGS = -D__DEBUG__ -Og -g3
# Add additional include paths
INCLUDES = -I $(SRC_PATH)
# General linker settings
LINK_FLAGS =
# Additional release-specific linker settings
RLINK_FLAGS =
# Additional debug-specific linker settings
DLINK_FLAGS =
# Destination directory, like a jail or mounted system
DESTDIR = /
# Install path (bin/ or lib/ and include/ are appended automatically depending on the type)
INSTALL_PREFIX = usr/local
#### END PROJECT SETTINGS ####
# Generally should not need to edit below this line
# Obtains the OS type, either 'Darwin' (OS X) or 'Linux'
UNAME_S:=$(shell uname -s)
# Adds correct file extension
ifeq ($(OS), Windows_NT)
ifeq ($(BIN_TYPE), static_library)
BIN_NAME := $(BIN_NAME).lib
else ifeq ($(BIN_TYPE), dynamic_library)
BIN_NAME := $(BIN_NAME).dll
COMPILE_FLAGS += -fPIC
else
BIN_NAME := $(BIN_NAME).exe
BIN_TYPE := executable
endif
else
ifeq ($(BIN_TYPE), static_library)
BIN_NAME := lib$(BIN_NAME).a
else ifeq ($(BIN_TYPE), dynamic_library)
BIN_NAME := lib$(BIN_NAME).so
COMPILE_FLAGS += -fPIC
else
BIN_TYPE := executable
endif
endif
# Function used to check variables. Use on the command line:
# make print-VARNAME
# Useful for debugging and adding features
print-%: ; @echo $*=$($*)
# Shell used in this makefile
# bash is used for 'echo -en'
SHELL = /bin/bash
# Clear built-in rules
.SUFFIXES:
# Programs for installation
INSTALL = install
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644
# Append pkg-config specific libraries if need be
ifneq ($(LIBS),)
COMPILE_FLAGS += $(shell pkg-config --cflags $(LIBS))
LINK_FLAGS += $(shell pkg-config --libs $(LIBS))
endif
# Verbose option, to output compile and link commands
export V := true
export CMD_PREFIX := @
ifeq ($(V),true)
CMD_PREFIX :=
endif
# Combine compiler and linker flags
release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(RCOMPILE_FLAGS)
release: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(RLINK_FLAGS)
debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) $(DCOMPILE_FLAGS)
debug: export LDFLAGS := $(LDFLAGS) $(LINK_FLAGS) $(DLINK_FLAGS)
# Build and output paths
release: export BUILD_PATH := build/release
release: export BIN_PATH := bin/release
debug: export BUILD_PATH := build/debug
debug: export BIN_PATH := bin/debug
install: export BIN_PATH := bin/release
# Find all source files in the source directory, sorted by most
# recently modified
ifeq ($(UNAME_S),Darwin)
SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-)
else
SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' -printf '%T@\t%p\n' \
| sort -k 1nr | cut -f2-)
endif
# Find all header files in the source directory
HEADERS = $(shell (cd $(SRC_PATH); find . -name '*.$(HEAD_EXT)'))
# fallback in case the above fails
rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard,$d/,$2) \
$(filter $(subst *,%,$2), $d))
ifeq ($(SOURCES),)
SOURCES := $(call rwildcard, $(SRC_PATH), *.$(SRC_EXT))
endif
# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place
OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o)
# Set the dependency files that will be used to add header dependencies
DEPS = $(OBJECTS:.o=.d)
# Macros for timing compilation
ifeq ($(UNAME_S),Darwin)
CUR_TIME = awk 'BEGIN{srand(); print srand()}'
TIME_FILE = $(dir $@).$(notdir $@)_time
START_TIME = $(CUR_TIME) > $(TIME_FILE)
END_TIME = read st < $(TIME_FILE) ; \
$(RM) $(TIME_FILE) ; \
st=$$((`$(CUR_TIME)` - $$st)) ; \
echo $$st
else
TIME_FILE = $(dir $@).$(notdir $@)_time
START_TIME = date '+%s' > $(TIME_FILE)
END_TIME = read st < $(TIME_FILE) ; \
$(RM) $(TIME_FILE) ; \
st=$$((`date '+%s'` - $$st - 86400)) ; \
echo `date -u -d @$$st '+%H:%M:%S'`
endif
# Version macros
# Comment/remove this section to remove versioning
USE_VERSION := false
# If this isn't a git repo or the repo has no tags, git describe will return non-zero
ifeq ($(shell git describe > /dev/null 2>&1 ; echo $$?), 0)
USE_VERSION := true
VERSION := $(shell git describe --tags --long --dirty="-DEV-$(shell git diff 2> /dev/null | sha1sum | cut -c-7)" --always | \
sed 's/v\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)-\?.*-\([0-9]*\)-g\(.*\)/\1 \2 \3 \4 \5/g')
VERSION_MAJOR := $(word 1, $(VERSION))
VERSION_MINOR := $(word 2, $(VERSION))
VERSION_PATCH := $(word 3, $(VERSION))
VERSION_REVISION := $(word 4, $(VERSION))
VERSION_HASH := $(word 5, $(VERSION))
VERSION_STRING_SHORT := \
"$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION)"
VERSION_STRING := \
"$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH).$(VERSION_REVISION) ($(VERSION_HASH))"
override CXXFLAGS := $(CXXFLAGS) \
-D VERSION_MAJOR=$(VERSION_MAJOR) \
-D VERSION_MINOR=$(VERSION_MINOR) \
-D VERSION_PATCH=$(VERSION_PATCH) \
-D VERSION_REVISION=$(VERSION_REVISION) \
-D VERSION_HASH=\"$(VERSION_HASH)\" \
-D VERSION_STRING_SHORT=\"$(VERSION_STRING_SHORT)\" \
-D VERSION_STRING=\"$(VERSION_STRING)\"
endif
# Standard, non-optimized release build
.PHONY: release
release: dirs
ifeq ($(USE_VERSION), true)
@echo "Beginning release build v"$(VERSION_STRING)
else
@echo "Beginning release build"
endif
@$(START_TIME)
@$(MAKE) all --no-print-directory
@echo -n "Total build time: "
@$(END_TIME)
# Debug build for gdb debugging
.PHONY: debug
debug: dirs
ifeq ($(USE_VERSION), true)
@echo "Beginning debug build v"$(VERSION_STRING)
else
@echo "Beginning debug build"
endif
@$(START_TIME)
@$(MAKE) all --no-print-directory
@echo -n "Total build time: "
@$(END_TIME)
# Create the directories used in the build
.PHONY: dirs
dirs:
@echo "Creating directories"
@mkdir -p $(dir $(OBJECTS))
@mkdir -p $(BIN_PATH)
# Installs to the set path
.PHONY: install
install:
ifeq ($(BIN_TYPE), executable)
@echo "Installing to $(DESTDIR)$(INSTALL_PREFIX)/bin"
@$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/bin
else
@echo "Installing library to $(DESTDIR)$(INSTALL_PREFIX)/lib"
@$(INSTALL_PROGRAM) $(BIN_PATH)/$(BIN_NAME) $(DESTDIR)$(INSTALL_PREFIX)/lib
@echo "Installing headers to $(DESTDIR)$(INSTALL_PREFIX)/include"
@(cd $(SRC_PATH); for header in $(HEADERS); do dir="$(DESTDIR)$(INSTALL_PREFIX)/include/$$(dirname "$$header")"; mkdir -p "$$dir"; $(INSTALL_DATA) "$$header" "$$dir";done)
ifeq ($(BIN_TYPE), dynamic_library)
@ldconfig
endif
endif
# Uninstalls the program
.PHONY: uninstall
uninstall:
ifeq ($(BIN_TYPE), executable)
@echo "Removing $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)"
@$(RM) $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BIN_NAME)
else
@echo "Removing library $(DESTDIR)$(INSTALL_PREFIX)/lib/$(BIN_NAME)"
@$(RM) $(DESTDIR)$(INSTALL_PREFIX)/lib/$(BIN_NAME)
@echo "Removing headers from $(DESTDIR)$(INSTALL_PREFIX)/include"
@(cd $(DESTDIR)$(INSTALL_PREFIX)/include; $(RM) $(HEADERS))
ifeq ($(BIN_TYPE), dynamic_library)
@ldconfig
endif
endif
# Removes all build files
.PHONY: clean
clean:
@echo "Deleting $(BIN_NAME) symlink"
@$(RM) $(BIN_NAME)
@echo "Deleting directories"
@$(RM) -r build
@$(RM) -r bin
# Main rule, checks the executable and symlinks to the output
all: $(BIN_PATH)/$(BIN_NAME)
@echo "Making symlink: $(BIN_NAME) -> $<"
@$(RM) $(BIN_NAME)
@ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME)
# Link the executable
$(BIN_PATH)/$(BIN_NAME): $(OBJECTS)
@echo "Linking: $@"
@$(START_TIME)
ifeq ($(BIN_TYPE), executable)
$(CMD_PREFIX)$(CXX) $(OBJECTS) $(LDFLAGS) -o $@
else ifeq ($(BIN_TYPE), static_library)
$(CMD_PREFIX)$(AR) rcs $@ $(OBJECTS)
else ifeq ($(BIN_TYPE), dynamic_library)
$(CMD_PREFIX)$(CXX) $(OBJECTS) -shared $(LDFLAGS) -o $@
endif
@echo -en "\t Link time: "
@$(END_TIME)
# Add dependency files, if they exist
-include $(DEPS)
# Source file rules
# After the first compilation they will be joined with the rules from the
# dependency files to provide header dependencies
$(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT)
@echo "Compiling: $< -> $@"
@$(START_TIME)
$(CMD_PREFIX)$(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@
@echo -en "\t Compile time: "
@$(END_TIME)
# Generate Doxygen documentation
# Doesn't exits if doxygen or the Doxyfile doesn't exist
ifneq ("$(wildcard doc/Doxyfile)", "")
ifneq ("$(shell which doxygen)", "")
.PHONY: doc
doc:
@${RM} "$(DOC_NAME).*"
@doxygen doc/Doxyfile
@(cd doc/gen/html; zip -r9 ../../../"$(DOC_NAME).zip" *)
ifneq ("$(shell which pdflatex)", "")
@(cd doc/gen/latex; make)
@cp doc/gen/latex/refman.pdf "$(DOC_NAME).pdf"
endif
# Clear Doxygen documentation
.PHONY: cleandoc
cleandoc:
@echo "Deleting: $(DOC_NAME).*"
@${RM} -r doc/gen "$(DOC_NAME).*"
endif
endif