-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
206 lines (170 loc) · 5.91 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
# This file is part of prototype.
#
# (c) 2013 Nicolas Hillegeer <[email protected]>
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with the source code.
#
# $@ The name of the target file (the one before the colon)
# $< The name of the first (or only) prerequisite file (the first one after the colon)
# $^ The names of all the prerequisite files (space separated)
# $* The stem (the bit which matches the % wildcard in the rule definition.
#
# if ever in need of a cross-platform solution for making directories:
# http://stackoverflow.com/questions/99132/how-to-check-if-a-directory-exists-in-a-makefile
ENABLE_LUA ?= 1
# CC := /usr/local/bin/gcc-4.8
# CC := /usr/local/bin/gcc-4.9
CC := /usr/local/bin/clang
# CC := /usr/local/bin/clang-3.4
STD := -std=c11 -pedantic -fstrict-aliasing
SECURITY := -D_THREAD_SAFE -D_FORTIFY_SOURCE=2
WARN := -Wextra -Wcast-align -Wcast-qual \
-Wpointer-arith -Waggregate-return -Wunreachable-code \
-Wfloat-equal -Wformat=2 -Wredundant-decls \
-Wundef -Wdisabled-optimization -Wshadow \
-Wmissing-braces -Wstrict-aliasing=2 -Wstrict-overflow=5 \
-Wconversion
UNWARN := -Wno-unused-parameter -Wno-missing-field-initializers -Wno-missing-braces \
-Wno-strict-aliasing
DEBUG := -g3 -DDEBUG
OPT := -O2 -march=native -mtune=native -ftree-vectorize
CFLAGS ?= $(STD) $(SECURITY) $(WARN) $(UNWARN)
# END OF CONFIGURABLE PART
DEPS_PATH := ./deps
SDL_PATH := $(DEPS_PATH)/sdl2
LUA_PATH := $(DEPS_PATH)/lua
LIBS := $(SDL_PATH)/build/.libs/libSDL2.a $(SDL_PATH)/build/.libs/libSDL2main.a \
-lm
CC_VERSION := $(shell $(CC) --version | head -1 | cut -f1 -d' ')
ifeq ($(OS),Windows_NT)
CFLAGS += -DWIN32
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
CFLAGS += -DAMD64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
CFLAGS += -DIA32
endif
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
CFLAGS += -DLINUX
# On Linux, this allows GL/glext.h OpenGL to declare functions like
# glDebugMessageCallback(), eliminating undeclared function warnings. On
# other platforms, we should use use function pointers (e.g. GLEW), but it
# is simpler on Linux. See https://stackoverflow.com/a/17555881.
CFLAGS += -DGL_GLEXT_PROTOTYPES
LIBS += \
-ldl \
-lGL
endif
ifeq ($(UNAME_S),Darwin)
CFLAGS += -DOSX
LIBS += \
-liconv \
-framework Cocoa -framework Carbon -framework OpenGL \
-framework CoreAudio -framework AudioToolbox -framework AudioUnit \
-framework ForceFeedback -framework IOKit
endif
ifneq (,$(findstring clang,$(CC_VERSION)))
CFLAGS += -DCLANG
# -pthread is not necessary when using Clang on Darwin
ifneq ($(UNAME_S),Darwin)
CFLAGS += -pthread
endif
else
CFLAGS += -DGCC
CFLAGS += -pthread
# at least on OS X 10.7.5, the apple linker does not understand AVX, and gcc uses it natively
# fixed: http://mac-os-forge.2317878.n4.nabble.com/gcc-as-AVX-binutils-and-MacOS-X-10-7-td144472.html
ifeq ($(UNAME_S),Darwin)
# CFLAGS += -mno-avx
endif
# required for the math libraries (threedee and sse_mathfun.h)
CFLAGS += -mfpmath=sse
endif
UNAME_P := $(shell uname -m)
ifeq ($(UNAME_P),x86_64)
CFLAGS += -DAMD64
endif
ifneq ($(filter %86,$(UNAME_P)),)
CFLAGS += -DIA32
endif
ifneq ($(filter arm%,$(UNAME_P)),)
CFLAGS += -DARM
endif
endif
# clang doesn't perform link-time optimization without the gold linker, which as of yet
# is not the default, so for now I'm not bothering to make a wrapper script to make it
# use the gold linker. So don't perform LTO with clang for now. The slp vectorizer is cool though
ifneq (,$(findstring clang,$(CC_VERSION)))
OPT += -fslp-vectorize
else
OPT += -flto
endif
INCS := -I$(SDL_PATH)/include \
-I/usr/include/malloc \
-I/usr/X11R6/include \
-I./src \
-I./src/gfx \
-I./src/util
EXECUTABLE := prototype
SOURCE := src/main.c \
src/util.c \
src/zmalloc.c \
src/version.c \
src/stb_image.c \
src/texture.c \
src/gfx/shader.c \
src/gfx/model.c \
src/gfx/renderer.c \
src/gfx/drawlist.c \
src/gfx/perf.c \
src/scratch.c
DEPENDENCY_TARGETS := sdl2
ifeq ($(ENABLE_LUA), 1)
INCS += -I$(LUA_PATH)/src
LIBS += $(LUA_PATH)/src/libluajit.a
DEPENDENCY_TARGETS += lua
CFLAGS += -DHAVE_LUA
SOURCE += src/scripting.c
endif
OBJECTS=$(patsubst src%.c,build%.o, $(SOURCE))
all: debug
debug: CFLAGS += -O $(DEBUG)
debug: $(EXECUTABLE)
release: CFLAGS += $(DEBUG) $(OPT)
release: $(EXECUTABLE)
# main executable(s)
$(EXECUTABLE): $(OBJECTS)
install -d build
$(CC) -o $@ $^ $(LIBS) $(CFLAGS) -pagezero_size 10000 -image_base 100000000
deps:
-$(MAKE) -C $(DEPS_PATH) -j4 $(DEPENDENCY_TARGETS) CC=$(CC)
# tests
timer_query: CFLAGS += -O $(DEBUG)
timer_query: test/timer_query.c build/util.o build/zmalloc.o
$(CC) -o $@ $^ $(LIBS) $(CFLAGS) $(INCS) -pagezero_size 10000 -image_base 100000000
# object files
# stb_image doesn't conform to C11, so it provokes a lot of warnings, turn off
# warnings for this one file
build/stb_image.o: src/stb_image.c
$(CC) -c $< -o $@ $(filter-out $(WARN),$(CFLAGS)) -fno-strict-aliasing $(INCS)
# zmalloc.c needs -fno-strict-aliasing unfortunately, so we have a special rule for it
build/zmalloc.o: src/zmalloc.c
$(CC) -c $< -o $@ $(CFLAGS) -fno-strict-aliasing $(INCS)
# deps is an order-only dependency, because if we made it a phony target a
# normal dependency we'd always rebuild (https://stackoverflow.com/q/13852535).
# Ideally we'd only rebuild if either the *.c file changes or anything in deps
# changes. But deps is based on invoking another makefile. I thought about
# replacing this with a check on child-of-directory modification times, but this
# appears fraught (https://stackoverflow.com/a/16350249). If always-rebuilding
# ever happens again, run `make -d $TARGET` to debug. For now, we'll stay with
# this hack, and just manually make the deps when updating the deps.
build/%.o: src/%.c | deps
@mkdir -p $(dir $@)
$(CC) -c $< -o $@ $(CFLAGS) $(INCS)
clean:
rm -rf build/* || true
rm -f $(EXECUTABLE) || true
.PHONY: all debug release deps