1# GNU Makefile for vkQuake unix targets
2# You need the SDL2 library fully installed.
3# "make DEBUG=1" to build a debug client.
4# "make SDL_CONFIG=/path/to/sdl2-config" for unusual SDL2 installations.
5# "make DO_USERDIRS=1" to enable user directories support
6# "make VULKAN_SDK=/path/to/sdk" if it is not already in path
7
8# Enable/Disable user directories support
9DO_USERDIRS=0
10
11### Enable/Disable codecs for streaming music support
12USE_CODEC_WAVE=1
13USE_CODEC_FLAC=0
14USE_CODEC_MP3=1
15USE_CODEC_VORBIS=1
16USE_CODEC_OPUS=0
17# either mikmod, or xmp
18USE_CODEC_MIKMOD=0
19USE_CODEC_XMP=0
20USE_CODEC_UMX=0
21
22# which library to use for mp3 decoding: mad or mpg123
23MP3LIB=mad
24# which library to use for ogg decoding: vorbis or tremor
25VORBISLIB=vorbis
26
27# ---------------------------
28# Helper functions
29# ---------------------------
30
31check_gcc = $(shell if echo | $(CC) $(1) -Werror -S -o /dev/null -xc - > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi;)
32
33# ---------------------------
34
35HOST_OS := $(shell uname|sed -e s/_.*//|tr '[:upper:]' '[:lower:]')
36
37DEBUG   ?= 0
38
39# ---------------------------
40# build variables
41# ---------------------------
42
43CC ?= gcc
44LINKER = $(CC)
45
46STRIP ?= strip
47
48CPUFLAGS=
49DFLAGS ?=
50CFLAGS ?=
51CFLAGS += -Wall -Wno-trigraphs -Wno-unused-function -Werror -std=gnu99
52CFLAGS += $(CPUFLAGS)
53ifneq ($(DEBUG),0)
54DFLAGS += -D_DEBUG
55CFLAGS += -g
56do_strip=
57else
58DFLAGS += -DNDEBUG
59CFLAGS += -O3
60CFLAGS += $(call check_gcc,-fweb,)
61CFLAGS += $(call check_gcc,-frename-registers,)
62cmd_strip=$(STRIP) $(1)
63define do_strip
64	$(call cmd_strip,$(1));
65endef
66endif
67
68ifneq ($(VULKAN_SDK),)
69LDFLAGS += -L$(VULKAN_SDK)/lib
70CFLAGS += -I$(VULKAN_SDK)/include
71endif
72
73ifeq ($(DO_USERDIRS),1)
74CFLAGS += -DDO_USERDIRS=1
75endif
76
77SDL_CONFIG ?= sdl2-config
78SDL_CFLAGS := $(shell $(SDL_CONFIG) --cflags)
79SDL_LIBS   := $(shell $(SDL_CONFIG) --libs)
80
81ifeq ($(HOST_OS),sunos)
82NET_LIBS   :=-lsocket -lnsl -lresolv
83else
84NET_LIBS   :=
85endif
86
87ifneq ($(VORBISLIB),vorbis)
88ifneq ($(VORBISLIB),tremor)
89$(error Invalid VORBISLIB setting)
90endif
91endif
92ifneq ($(MP3LIB),mpg123)
93ifneq ($(MP3LIB),mad)
94$(error Invalid MP3LIB setting)
95endif
96endif
97ifeq ($(MP3LIB),mad)
98mp3_obj=snd_mp3
99lib_mp3dec=-lmad
100endif
101ifeq ($(MP3LIB),mpg123)
102mp3_obj=snd_mpg123
103lib_mp3dec=-lmpg123
104endif
105ifeq ($(VORBISLIB),vorbis)
106cpp_vorbisdec=
107lib_vorbisdec=-lvorbisfile -lvorbis -logg
108endif
109ifeq ($(VORBISLIB),tremor)
110cpp_vorbisdec=-DVORBIS_USE_TREMOR
111lib_vorbisdec=-lvorbisidec -logg
112endif
113
114CODECLIBS  :=
115ifeq ($(USE_CODEC_WAVE),1)
116CFLAGS+= -DUSE_CODEC_WAVE
117endif
118ifeq ($(USE_CODEC_FLAC),1)
119CFLAGS+= -DUSE_CODEC_FLAC
120CODECLIBS+= -lFLAC
121endif
122ifeq ($(USE_CODEC_OPUS),1)
123# opus and opusfile put their *.h under <includedir>/opus,
124# but they include the headers without the opus directory
125# prefix and rely on pkg-config. ewww...
126CFLAGS+= -DUSE_CODEC_OPUS
127CFLAGS+= $(shell pkg-config --cflags opusfile)
128CODECLIBS+= $(shell pkg-config --libs   opusfile)
129endif
130ifeq ($(USE_CODEC_VORBIS),1)
131CFLAGS+= -DUSE_CODEC_VORBIS $(cpp_vorbisdec)
132CODECLIBS+= $(lib_vorbisdec)
133endif
134ifeq ($(USE_CODEC_MP3),1)
135CFLAGS+= -DUSE_CODEC_MP3
136CODECLIBS+= $(lib_mp3dec)
137endif
138ifeq ($(USE_CODEC_MIKMOD),1)
139CFLAGS+= -DUSE_CODEC_MIKMOD
140CODECLIBS+= -lmikmod
141endif
142ifeq ($(USE_CODEC_XMP),1)
143CFLAGS+= -DUSE_CODEC_XMP
144CODECLIBS+= -lxmp
145endif
146ifeq ($(USE_CODEC_UMX),1)
147CFLAGS+= -DUSE_CODEC_UMX
148endif
149
150COMMON_LIBS:= -lm
151
152ifneq ($(HOST_OS),darwin)
153COMMON_LIBS+= -lvulkan
154CFLAGS+= -DLINUX
155else
156ifeq ($(shell test -d /opt/homebrew && echo true),true)
157COMMON_LIBS+= -L/opt/homebrew/lib
158CFLAGS+= -I/opt/homebrew/include
159endif
160COMMON_LIBS+= -lMoltenVK
161endif
162
163LIBS := $(COMMON_LIBS) $(NET_LIBS) $(CODECLIBS)
164
165# ---------------------------
166# targets
167# ---------------------------
168
169.PHONY:	clean debug release
170
171DEFAULT_TARGET := vkquake
172
173# ---------------------------
174# rules
175# ---------------------------
176
177%.o:	%.c
178	$(CC) $(DFLAGS) -c $(CFLAGS) $(SDL_CFLAGS) -o $@ $<
179%.o:	../Shaders/Compiled/%.c
180	$(CC) $(DFLAGS) -c $(CFLAGS) $(SDL_CFLAGS) -o $@ $^
181
182# ----------------------------------------------------------------------------
183# objects
184# ----------------------------------------------------------------------------
185
186MUSIC_OBJS:= bgmusic.o \
187	snd_codec.o \
188	snd_flac.o \
189	snd_wave.o \
190	snd_vorbis.o \
191	snd_opus.o \
192	$(mp3_obj).o \
193	snd_mp3tag.o \
194	snd_mikmod.o \
195	snd_xmp.o \
196	snd_umx.o
197COMOBJ_SND := snd_dma.o snd_mix.o snd_mem.o $(MUSIC_OBJS)
198SYSOBJ_SND := snd_sdl.o
199SYSOBJ_CDA := cd_sdl.o
200SYSOBJ_INPUT := in_sdl.o
201SYSOBJ_GL_VID:= gl_vidsdl.o
202SYSOBJ_NET := net_bsd.o net_udp.o
203SYSOBJ_SYS := pl_linux.o sys_sdl_unix.o
204SYSOBJ_MAIN:= main_sdl.o
205SYSOBJ_RES :=
206
207SHADER_OBJS = \
208	alias_frag.o \
209	alias_alphatest_frag.o \
210	alias_vert.o \
211	basic_alphatest_frag.o \
212	screen_effects_8bit_comp.o \
213	screen_effects_8bit_scale_comp.o \
214	screen_effects_8bit_scale_sops_comp.o \
215	screen_effects_10bit_comp.o \
216	screen_effects_10bit_scale_comp.o \
217	screen_effects_10bit_scale_sops_comp.o \
218	cs_tex_warp_comp.o \
219	basic_frag.o \
220	basic_notex_frag.o \
221	basic_vert.o \
222	sky_layer_frag.o \
223	sky_layer_vert.o \
224	sky_box_frag.o \
225	postprocess_frag.o \
226	postprocess_vert.o \
227	world_frag.o \
228	world_vert.o \
229	showtris_frag.o \
230	showtris_vert.o
231
232GLOBJS = \
233	$(SHADER_OBJS) \
234	gl_refrag.o \
235	gl_rlight.o \
236	gl_rmain.o \
237	gl_fog.o \
238	gl_rmisc.o \
239	r_part.o \
240	r_part_fte.o \
241	r_world.o \
242	gl_screen.o \
243	gl_sky.o \
244	gl_warp.o \
245	$(SYSOBJ_GL_VID) \
246	gl_draw.o \
247	image.o \
248	gl_texmgr.o \
249	gl_mesh.o \
250	gl_heap.o \
251	r_sprite.o \
252	r_alias.o \
253	r_brush.o \
254	gl_model.o
255
256OBJS := strlcat.o \
257	strlcpy.o \
258	$(GLOBJS) \
259	$(SYSOBJ_INPUT) \
260	$(COMOBJ_SND) \
261	$(SYSOBJ_SND) \
262	$(SYSOBJ_CDA) \
263	$(SYSOBJ_NET) \
264	net_dgrm.o \
265	net_loop.o \
266	net_main.o \
267	chase.o \
268	cl_demo.o \
269	cl_input.o \
270	cl_main.o \
271	cl_parse.o \
272	cl_tent.o \
273	console.o \
274	keys.o \
275	menu.o \
276	sbar.o \
277	view.o \
278	wad.o \
279	cmd.o \
280	common.o \
281	miniz.o \
282	crc.o \
283	cvar.o \
284	cfgfile.o \
285	host.o \
286	host_cmd.o \
287	mathlib.o \
288	mdfour.o \
289	pr_cmds.o \
290	pr_ext.o \
291	pr_edict.o \
292	pr_exec.o \
293	sv_main.o \
294	sv_move.o \
295	sv_phys.o \
296	sv_user.o \
297	world.o \
298	zone.o \
299	$(SYSOBJ_SYS) $(SYSOBJ_MAIN) $(SYSOBJ_RES)
300
301# ------------------------
302# Linux build rules
303# ------------------------
304
305vkquake:	$(OBJS)
306	$(LINKER) $(OBJS) $(LDFLAGS) $(LIBS) $(SDL_LIBS) -o $@
307	$(call do_strip,$@)
308
309image.o: lodepng.c lodepng.h stb_image_write.h
310
311release:	vkquake
312debug:
313	$(error Use "make DEBUG=1")
314
315clean:
316	rm -f $(shell find . \( -name '*~' -o -name '#*#' -o -name '*.o' -o -name '*.res' -o -name $(DEFAULT_TARGET) \) -print)
317
318prefix ?= /usr
319exec_prefix ?= $(prefix)
320bindir ?= $(exec_prefix)/games
321sbindir ?= $(exec_prefix)/sbin
322INSTALL ?= install
323INSTALL_PROGRAM ?= $(INSTALL)
324INSTALL_DATA ?= ${INSTALL} -m 644
325install: vkquake
326	$(INSTALL_PROGRAM) $(CURDIR)/vkquake $(DESTDIR)$(bindir)/vkquake
327