-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmakefile
249 lines (174 loc) · 7.09 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
# Source http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
CONFIG = Debug
Debug_CXX_FLAGS := -Og -g
Release_CXX_FLAGS := -O3
CONFIG_CXX_FLAGS := $($(CONFIG)_CXX_FLAGS)
# Determine OS (Linux, Darwin, ...)
CURRENT_OS := $(shell uname 2>/dev/null || echo Unknown)
TARGET_OS ?= $(CURRENT_OS)
Windows_RUN_PREFIX := wine
RUN_PREFIX := $($(TARGET_OS)_RUN_PREFIX)
Windows_EXE_SUFFIX := .exe
EXE_SUFFIX := $($(TARGET_OS)_EXE_SUFFIX)
ROOTBUILDDIR := .build
BUILDDIRPREFIX := $(ROOTBUILDDIR)/$(CONFIG)_Linux_
## Default and top-level targets ##
.DEFAULT_GOAL := nas2d
.PHONY: all
all: nas2d test test-graphics
## NAS2D project ##
SRCDIR := NAS2D
BINDIR := lib
INTDIR := $(BUILDDIRPREFIX)nas2d/intermediate
OUTPUT := $(BINDIR)/libnas2d.a
SRCS := $(shell find $(SRCDIR) -name '*.cpp')
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(INTDIR)/%.o,$(SRCS))
Linux_OpenGL_LIBS := -lGLEW -lGL
Darwin_OpenGL_LIBS := -lGLEW -framework OpenGL
Windows_OpenGL_LIBS := -lglew32 -lopengl32
OpenGL_LIBS := $($(TARGET_OS)_OpenGL_LIBS)
SDL_CONFIG_CFLAGS := $(shell type sdl2-config >/dev/null 2>&1 && sdl2-config --cflags)
SDL_CONFIG_LIBS := $(shell type sdl2-config >/dev/null 2>&1 && sdl2-config --libs)
CPPFLAGS := $(CPPFLAGS_EXTRA)
CXXFLAGS_WARN := -Wall -Wextra -Wpedantic -Wzero-as-null-pointer-constant -Wnull-dereference -Wold-style-cast -Wcast-qual -Wcast-align -Wdouble-promotion -Wshadow -Wnon-virtual-dtor -Woverloaded-virtual -Wmissing-declarations -Wmissing-include-dirs -Winvalid-pch -Wmissing-format-attribute -Wredundant-decls -Wformat=2 $(WARN_EXTRA)
CXXFLAGS := $(CXXFLAGS_EXTRA) $(CONFIG_CXX_FLAGS) -std=c++20 $(CXXFLAGS_WARN) $(SDL_CONFIG_CFLAGS)
LDFLAGS := $(LDFLAGS_EXTRA)
LDLIBS := $(LDLIBS_EXTRA) -lstdc++ -lSDL2_image -lSDL2_mixer -lSDL2_ttf $(SDL_CONFIG_LIBS) $(OpenGL_LIBS)
PROJECT_FLAGS = $(CPPFLAGS) $(CXXFLAGS)
.PHONY: nas2d
nas2d: $(OUTPUT)
$(OUTPUT): $(OBJS)
$(OBJS): $(INTDIR)/%.o : $(SRCDIR)/%.cpp $(INTDIR)/%.dep
include $(wildcard $(patsubst %.o,%.dep,$(OBJS)))
## Unit Test project ##
TESTDIR := test
TESTINTDIR := $(BUILDDIRPREFIX)test/intermediate
TESTOUTPUT := $(BUILDDIRPREFIX)test/test$(EXE_SUFFIX)
TESTSRCS := $(shell find $(TESTDIR) -name '*.cpp')
TESTOBJS := $(patsubst $(TESTDIR)/%.cpp,$(TESTINTDIR)/%.o,$(TESTSRCS))
TESTCPPFLAGS := $(CPPFLAGS) -I./
TESTLDFLAGS := $(LDFLAGS)
TESTLIBS := -lgmock_main -lgmock -lgtest -lpthread $(LDLIBS)
TESTPROJECT_FLAGS = $(TESTCPPFLAGS) $(CXXFLAGS)
TESTPROJECT_LINKFLAGS = $(TESTLDFLAGS) $(TESTLIBS)
.PHONY: test
test: $(TESTOUTPUT)
$(TESTOUTPUT): PROJECT_LINKFLAGS = $(TESTPROJECT_LINKFLAGS)
$(TESTOUTPUT): $(TESTOBJS) $(OUTPUT)
$(TESTOBJS): PROJECT_FLAGS = $(TESTPROJECT_FLAGS)
$(TESTOBJS): $(TESTINTDIR)/%.o : $(TESTDIR)/%.cpp $(TESTINTDIR)/%.dep
include $(wildcard $(patsubst %.o,%.dep,$(TESTOBJS)))
.PHONY: check
check: | test
cd test && $(RUN_PREFIX) ../$(TESTOUTPUT) $(GTEST_OPTIONS)
## Graphics test project ##
TESTGRAPHICSDIR := test-graphics
TESTGRAPHICSINTDIR := $(BUILDDIRPREFIX)testGraphics/intermediate
TESTGRAPHICSOUTPUT := $(BUILDDIRPREFIX)testGraphics/testGraphics$(EXE_SUFFIX)
TESTGRAPHICSSRCS := $(shell find $(TESTGRAPHICSDIR) -name '*.cpp')
TESTGRAPHICSOBJS := $(patsubst $(TESTGRAPHICSDIR)/%.cpp,$(TESTGRAPHICSINTDIR)/%.o,$(TESTGRAPHICSSRCS))
# The `-Umain` needs to come after `sdl2-config` flags in `CXXFLAGS`
TESTGRAPHICSPROJECT_FLAGS = $(TESTCPPFLAGS) $(CXXFLAGS) -Umain
TESTGRAPHICSPROJECT_LINKFLAGS = $(TESTLDFLAGS) $(LDLIBS)
.PHONY: test-graphics
test-graphics: $(TESTGRAPHICSOUTPUT)
$(TESTGRAPHICSOUTPUT): PROJECT_LINKFLAGS = $(TESTGRAPHICSPROJECT_LINKFLAGS)
$(TESTGRAPHICSOUTPUT): $(TESTGRAPHICSOBJS) $(OUTPUT)
$(TESTGRAPHICSOBJS): PROJECT_FLAGS = $(TESTGRAPHICSPROJECT_FLAGS)
$(TESTGRAPHICSOBJS): $(TESTGRAPHICSINTDIR)/%.o : $(TESTGRAPHICSDIR)/%.cpp $(TESTGRAPHICSINTDIR)/%.dep
include $(wildcard $(patsubst %.o,%.dep,$(TESTGRAPHICSOBJS)))
.PHONY: run-test-graphics
run-test-graphics: | test-graphics
cd test-graphics/ && ../$(TESTGRAPHICSOUTPUT) ; cd ..
## Compile rules ##
DEPFLAGS = -MMD -MP
COMPILE.cpp = $(CXX) $(DEPFLAGS) $(PROJECT_FLAGS) $(TARGET_ARCH) -c
POSTCOMPILE = @mv -f $(@:.o=.d) $(@:.o=.dep) && touch $@
%:
@mkdir -p "${@D}"
$(CXX) $^ $(PROJECT_LINKFLAGS) -o $@
lib%.a:
@mkdir -p "${@D}"
ar rcs $@ $^
%.o:
@mkdir -p "${@D}"
$(COMPILE.cpp) $(OUTPUT_OPTION) $<
$(POSTCOMPILE)
%.dep: ;
.PRECIOUS: %.dep
## Clean ##
.PHONY: clean clean-all
clean:
-rm --force --recursive $(INTDIR)
clean-all: | clean
-rm --force --recursive $(ROOTBUILDDIR)
-rm --force $(OUTPUT)
-rm --force --dir $(BINDIR)
## Package ##
PACKAGEDIR := $(ROOTBUILDDIR)/package
VERSION = $(shell git describe --tags --dirty)
PLATFORM = $(TARGET_OS).x64
PACKAGE_NAME = $(PACKAGEDIR)/nas2d-$(VERSION)-$(PLATFORM)-$(CONFIG).tar.gz
Darwin_TAR_RENAME_FLAG := -s '!^$(SRCDIR)/!include/\0!'
Linux_TAR_RENAME_FLAG := --transform='s/^$(SRCDIR)/include\/\0/'
TAR_RENAME_FLAG := $($(CURRENT_OS)_TAR_RENAME_FLAG)
.PHONY: package
package: $(PACKAGE_NAME)
$(PACKAGE_NAME): $(OUTPUT) $(shell find $(SRCDIR) -name '*.h')
@mkdir -p "${@D}"
# Package an "include/" folder containing all header files, plus the library file
find $(SRCDIR) -name '*.h' | tar -czf $(PACKAGE_NAME) $(TAR_RENAME_FLAG) -T - $(OUTPUT)
## Linting ##
.PHONY: show-warnings
show-warnings:
@$(MAKE) clean > /dev/null
$(MAKE) --output-sync all CXX=clang++ CXXFLAGS_WARN=-Weverything 2>&1 >/dev/null | grep -o "\[-W.*\]" | sort | uniq
@$(MAKE) clean > /dev/null
.PHONY: lint
lint: cppcheck cppclean
.PHONY: cppcheck
cppcheck:
cppcheck --quiet "$(SRCDIR)"
.PHONY: cppclean
cppclean:
cppclean "$(SRCDIR)" --exclude="NAS2D.h" --exclude="Xml"
.PHONY: format
format:
clang-format --version
find NAS2D/ -not -path 'NAS2D/Xml/*' -regex '.*\.\(cpp\|h\)' | xargs clang-format -i
### Linux development package dependencies ###
# This section contains install rules to aid setup and compiling on Linux.
# Only a few common Linux distributions are covered. Other distributions
# should be similar.
RELEASE_FILES = $(wildcard /etc/*-release)
RELEASE_FILE_TAGS = $(filter-out os lsb,$(patsubst /etc/%-release,%,$(RELEASE_FILES)))
RELEASE_SETTING_NAME = $(shell '$(SHELL)' -c '[ -f /etc/os-release ] && . /etc/os-release && echo $${ID}')
LINUX_DISTRIBUTION = $(or $(RELEASE_SETTING_NAME),$(RELEASE_FILE_TAGS),Unknown)
DISTRIBUTION = $(subst Darwin,darwin,$(subst Linux,$(LINUX_DISTRIBUTION),$(CURRENT_OS)))
.PHONY: install-dependencies
install-dependencies:
@echo
@echo "Detected distribution: $(DISTRIBUTION)"
@echo
$(MAKE) "install-dependencies-$(DISTRIBUTION)"
## Ubuntu ##
.PHONY: install-dependencies-ubuntu
install-dependencies-ubuntu:
apt --yes install libsdl2-dev libsdl2-mixer-dev libsdl2-image-dev libsdl2-ttf-dev libglew-dev
## Arch Linux ##
.PHONY: install-dependencies-arch
install-dependencies-arch:
pacman --sync --refresh sdl2 sdl2_mixer sdl2_image sdl2_ttf glew
## MacOS ##
.PHONY: install-dependencies-darwin
install-dependencies-darwin:
xargs brew install < BrewDeps.txt
xargs brew link < BrewDeps.txt
#### Docker related build rules ####
include dockerBuildEnv/makefile
#### CircleCI related build rules ####
.PHONY: circleci-validate circleci-build
circleci-validate:
circleci config validate
circleci-build:
circleci build