-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMakefile
executable file
·283 lines (195 loc) · 8.25 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
# This makefile builds the native SDL platform with the OpenGL renderer by
# default when just calling `make`. Targets for other platforms are present, but
# not part of the default build. The renderer can be specified separately.
# All PNG, WAV and JSON files in assets/ are converted/copied and all *.c files
# in the src/ directory are compiled. There's no need to specify the list of
# files separately.
# The tools needed to convert assets (PNG to QOI and WAV to QOA) are part of
# sdl/sokol/wasm targets.
# Options:
# RENDER = GL|SOFTWARE|METAL (default GL; Metal only for macOS)
# DEBUG = true|false (default false)
# USE_GL = true|false (default false; only relevant on linux)
# Examples:
# `make sdl` - native SDL platform
# `make sdl RENDER=SOFTWARE` - native SDL platform with the software renderer
# `make sdl RENDER=METAL` - native SDL platform with the Metal renderer
# `make sokol` - native SOKOL platform
# `make sokol RENDER=METAL` - native SOKOL platform with the Metal renderer
# `make sokol DEBUG=true` - native SOKOL platform with debug symbols
# `make wasm` - WASM (always with SOKOL platform)
# make sdl_release - create qop archive and append to sdl build
# make sokol_release - create qop archive and append to sokol build
# `make assets` - only convert assets
# `make qoaconv` - only build qoaconv (used to convert WAV to QOA)
# `make qoiconv` - only build qoiconv (used to convert PNG to QOI)
# `make qopconv` - only build qopconv (used to create asset archive)
# `make clean` - clean binaries
# `make clean_assets` - clean converted assets
# `make clean_all` - clean the whole build dir (assets and binaries)
CC ?= gcc
EMCC ?= emcc
UNAME_S := $(shell uname -s)
RENDER ?= GL
USE_GLX ?= false
DEBUG ?= false
USER_CFLAGS ?=
L_FLAGS ?= -lm
C_FLAGS ?= -std=gnu99 -Wall -Wstrict-prototypes -Wno-unused-variable $(USER_CFLAGS)
GAME_SRC := $(shell find src/ -type f -name '*.c')
BUILD_DIR ?= build
BUILD_DIR_WASM ?= build/wasm
OBJ_DIR = build/obj
ASSETS_SRC_DIR ?= assets
ASSETS_BUILD_DIR = $(BUILD_DIR)/$(ASSETS_SRC_DIR)
TARGET_SDL ?= $(BUILD_DIR)/game_sdl
TARGET_SOKOL ?= $(BUILD_DIR)/game_sokol
TARGET_WASM ?= $(BUILD_DIR_WASM)/game.js
QOICONV = high_impact/tools/qoiconv
QOACONV = high_impact/tools/qoaconv
QOPCONV = high_impact/tools/qopconv
# Use sdl as the default target when just calling `make`
.DEFAULT_GOAL := sdl
ifeq ($(DEBUG), true)
C_FLAGS := $(C_FLAGS) -g
else
C_FLAGS := $(C_FLAGS) -O3
endif
# Rendeder ---------------------------------------------------------------------
ifeq ($(RENDER), GL)
C_FLAGS := $(C_FLAGS) -DRENDER_GL
else ifeq ($(RENDER), METAL)
C_FLAGS := $(C_FLAGS) -DRENDER_METAL
else ifeq ($(RENDER), SOFTWARE)
C_FLAGS := $(C_FLAGS) -DRENDER_SOFTWARE
else
$(error Unknown RENDER backend)
endif
ifeq ($(GL_VERSION), GLES2)
C_FLAGS := $(C_FLAGS) -DUSE_GLES2
endif
# macOS ------------------------------------------------------------------------
ifeq ($(UNAME_S), Darwin)
BREW_HOME := $(shell brew --prefix)
C_FLAGS := $(C_FLAGS) -x objective-c -I/opt/homebrew/include -D_THREAD_SAFE -w
L_FLAGS := $(L_FLAGS) -L$(BREW_HOME)/lib -framework Foundation
L_FLAGS_SOKOL = -framework Cocoa -framework QuartzCore -framework AudioToolbox
ifeq ($(RENDER), GL)
L_FLAGS := $(L_FLAGS) -GLU -framework OpenGL
else ifeq ($(RENDER), METAL)
C_FLAGS := $(C_FLAGS) -fobjc-arc
L_FLAGS := $(L_FLAGS) -framework Metal
L_FLAGS_SOKOL := $(L_FLAGS_SOKOL) -framework MetalKit
endif
# Linux ------------------------------------------------------------------------
else ifeq ($(UNAME_S), Linux)
ifeq ($(RENDER), GL)
# Prefer modern GLVND instead of legacy X11-only GLX
ifeq ($(USE_GLX), true)
L_FLAGS := $(L_FLAGS) -lGL
else
L_FLAGS := $(L_FLAGS) -lOpenGL
endif
endif
L_FLAGS_SOKOL = -lX11 -lXcursor -pthread -lXi -ldl -lasound
# Windows MSYS -----------------------------------------------------------------
else ifeq ($(shell uname -o), Msys)
ifeq ($(RENDER), GL)
L_FLAGS := $(L_FLAGS) -lopengl32
endif
L_FLAGS_SOKOL = --pthread -lkernel32 -luser32 -lshell32 -lgdi32 -lole32
# Windows NON-MSYS -------------------------------------------------------------
else ifeq ($(OS), Windows_NT)
$(error TODO: FLAGS for windows have not been set up. Please modify this makefile and send a PR!)
else
$(error Unknown environment)
endif
# Asset tools ------------------------------------------------------------------
qoiconv: $(QOICONV)
$(QOICONV): high_impact/libs/qoiconv.c high_impact/libs/stb_image.h high_impact/libs/stb_image_write.h high_impact/libs/qoi.h
@mkdir -p $(@D)
$(CC) $(USER_CFLAGS) -std=c99 -O3 high_impact/libs/qoiconv.c -o $(QOICONV)
qoaconv: $(QOACONV)
$(QOACONV): high_impact/libs/qoaconv.c high_impact/libs/qoa.h
@mkdir -p $(@D)
$(CC) $(USER_CFLAGS) -std=c99 -O3 high_impact/libs/qoaconv.c -o $(QOACONV) -lm
qopconv: $(QOPCONV)
$(QOPCONV): high_impact/libs/qopconv.c high_impact/libs/qop.h
@mkdir -p $(@D)
$(CC) $(USER_CFLAGS) -std=c99 -O3 high_impact/libs/qopconv.c -o $(QOPCONV)
# Assets ----------------------------------------------------------------------
ASSETS_PNG := $(shell find $(ASSETS_SRC_DIR) -type f -name '*.png')
ASSETS_WAV := $(shell find $(ASSETS_SRC_DIR) -type f -name '*.wav')
ASSETS_JSON := $(shell find $(ASSETS_SRC_DIR) -type f -name '*.json')
ASSETS_ENC_QOI := $(ASSETS_PNG:assets/%.png=$(ASSETS_BUILD_DIR)/%.qoi)
ASSETS_ENC_QOA := $(ASSETS_WAV:assets/%.wav=$(ASSETS_BUILD_DIR)/%.qoa)
ASSETS_CP_JSON := $(ASSETS_JSON:assets/%.json=$(ASSETS_BUILD_DIR)/%.json)
assets: qoiconv qoaconv assets_build
assets_build: $(ASSETS_ENC_QOI) $(ASSETS_ENC_QOA) $(ASSETS_CP_JSON)
build/assets/%.qoi: assets/%.png
@mkdir -p $(@D)
$(QOICONV) $< $@
build/assets/%.qoa: assets/%.wav
@mkdir -p $(@D)
$(QOACONV) $< $@
build/assets/%.json: assets/%.json
@mkdir -p $(@D)
cp $< $@
assets_archive: qopconv assets
$(QOPCONV) -d $(BUILD_DIR) assets $(BUILD_DIR)/assets.qop
# Platform: Native/SDL ---------------------------------------------------------
SDL_C_FLAGS = $(shell sdl2-config --cflags)
SDL_L_FLAGS = $(shell sdl2-config --libs)
SDL_COMMON_OBJ = $(patsubst %.c, $(OBJ_DIR)/sdl/%.o, $(GAME_SRC))
SDL_COMMON_DEPS = $(patsubst %.c, $(OBJ_DIR)/sdl/%.d, $(GAME_SRC))
sdl: assets sdl_build
sdl_build: $(SDL_COMMON_OBJ)
mkdir -p $(BUILD_DIR)
$(CC) $^ -o $(TARGET_SDL) $(L_FLAGS) $(SDL_L_FLAGS)
$(OBJ_DIR)/sdl/%.o: %.c
mkdir -p $(dir $@)
$(CC) $(C_FLAGS) $(SDL_C_FLAGS) -DPLATFORM_SDL -MMD -MP -c $< -o $@
-include $(SDL_COMMON_DEPS)
sdl_release: sdl assets_archive
cat $(TARGET_SDL) $(BUILD_DIR)/assets.qop > $(TARGET_SDL)_release
chmod a+x $(TARGET_SDL)_release
# Platform: Native/SOKOL -------------------------------------------------------
SOKOL_COMMON_OBJ = $(patsubst %.c, $(OBJ_DIR)/sokol/%.o, $(GAME_SRC))
SOKOL_COMMON_DEPS = $(patsubst %.c, $(OBJ_DIR)/sokol/%.d, $(GAME_SRC))
sokol: assets sokol_build
sokol_build: $(SOKOL_COMMON_OBJ)
mkdir -p $(BUILD_DIR)
$(CC) $^ -o $(TARGET_SOKOL) $(L_FLAGS) $(L_FLAGS_SOKOL)
$(OBJ_DIR)/sokol/%.o: %.c
mkdir -p $(dir $@)
$(CC) $(C_FLAGS) -DPLATFORM_SOKOL -MMD -MP -c $< -o $@
-include $(SOKOL_COMMON_DEPS)
sokol_release: sokol assets_archive
cat $(TARGET_SOKOL) $(BUILD_DIR)/assets.qop > $(TARGET_SOKOL)_release
chmod a+x $(TARGET_SOKOL)_release
# Platform: WASM/Sokol ---------------------------------------------------------
COMMON_OBJ_WASM = $(patsubst %.c, $(OBJ_DIR)/wasm/%.o, $(GAME_SRC))
COMMON_DEPS_WASM = $(patsubst %.c, $(OBJ_DIR)/wasm/%.d, $(GAME_SRC))
wasm: assets wasm_build
wasm_build: $(COMMON_OBJ_WASM)
mkdir -p $(BUILD_DIR_WASM)
cp src/wasm_index.html $(BUILD_DIR_WASM)/game.html
$(EMCC) $^ -o $(TARGET_WASM) -lGL \
-s ALLOW_MEMORY_GROWTH=1 \
-s ENVIRONMENT=web \
-s MIN_WEBGL_VERSION=2 \
-s MAX_WEBGL_VERSION=2 \
-sEXPORTED_FUNCTIONS=_main,_input_set_button_state \
--preload-file $(ASSETS_BUILD_DIR)@$(ASSETS_SRC_DIR)
$(OBJ_DIR)/wasm/%.o: %.c
mkdir -p $(dir $@)
$(EMCC) $(C_FLAGS) -DPLATFORM_SOKOL -MMD -MP -c $< -o $@
-include $(COMMON_DEPS_WASM)
# Cleaing ----------------------------------------------------------------------
.PHONY: clean
clean:
$(RM) -rf $(OBJ_DIR) $(BUILD_DIR_WASM) $(TARGET_SDL) $(TARGET_SOKOL)
.PHONY: clean_assets
clean_assets:
$(RM) -rf $(ASSETS_BUILD_DIR)
clean_all: clean_assets clean