21ca5f9534648dbb977c5a93c38c9191a11bb3fe
[rocksndiamonds.git] / src / Makefile
1 # =============================================================================
2 # Rocks'n'Diamonds - McDuffin Strikes Back!
3 # -----------------------------------------------------------------------------
4 # (c) 1995-2015 by Artsoft Entertainment
5 #                  Holger Schemel
6 #                  info@artsoft.org
7 #                  https://www.artsoft.org/
8 # -----------------------------------------------------------------------------
9 # src/Makefile
10 # =============================================================================
11
12 # -----------------------------------------------------------------------------
13 # configuration
14 # -----------------------------------------------------------------------------
15
16 .EXPORT_ALL_VARIABLES:
17
18 ifndef PLATFORM                         # unknown platform -- default to Unix
19 PLATFORM = unix
20 endif
21
22 PLATFORM_BASE = $(PLATFORM)
23
24 ifeq ($(PLATFORM),cross-win32)
25 PLATFORM_BASE = cross-win
26 endif
27 ifeq ($(PLATFORM),cross-win64)
28 PLATFORM_BASE = cross-win
29 endif
30
31 AR = ar
32 RANLIB = ranlib
33 ETAGS = etags
34 STRIP = strip
35 RM = rm -f
36
37 CONVERT = convert
38 WINDRES = windres
39
40 CONVERT_ICON_ARGS = -transparent black -background transparent
41
42 DEBUGGER = gdb -batch -ex "run" -ex "bt"
43
44 PROGBASE = rocksndiamonds
45 PROGNAME = ../$(PROGBASE)
46
47 EDITION ?= default
48
49
50 # -----------------------------------------------------------------------------
51 # configuring platform
52 # -----------------------------------------------------------------------------
53
54 ifeq ($(PLATFORM),unix)                 # compiling on Unix/Linux (generic)
55 PROFILING_FLAGS = -pg
56 endif
57
58 ifeq ($(PLATFORM_BASE),cross-win)       # cross-compiling to Windows
59 PROGNAME = ../$(PROGBASE).exe
60 EXTRA_LDFLAGS = -lshfolder -lwsock32
61 endif
62
63 ifeq ($(PLATFORM),emscripten)           # compiling with Emscripten
64 PROGNAME = ../$(PROGBASE).js
65 CC = emcc
66 AR = emar
67 RANLIB = emranlib
68 STRIP = true
69 endif
70
71 ifeq ($(shell uname -s),Darwin)         # compiling on Mac OS X
72 DEBUGGER = lldb --batch -o "run" -k "bt" -k "quit"
73 ifdef BUILD_DIST                        # distribution build
74 MAC_TARGET_VERSION_MIN = 10.7
75 EXTRA_FLAGS_MAC = -mmacosx-version-min=$(MAC_TARGET_VERSION_MIN)
76 EXTRA_CFLAGS = $(EXTRA_FLAGS_MAC)
77 EXTRA_LDFLAGS = $(EXTRA_FLAGS_MAC)
78 MACOSX_DEPLOYMENT_TARGET = $MAC_TARGET_VERSION_MIN
79 endif
80 endif
81
82
83 # -----------------------------------------------------------------------------
84 # configuring target
85 # -----------------------------------------------------------------------------
86
87 ifndef TARGET                           # auto-detect compiling for SDL2
88   SDL_VERSION := $(shell sdl2-config --version 2> /dev/null)
89   ifdef SDL_VERSION
90     TARGET = sdl2
91   else
92     $(error SDL2 library not found)
93   endif
94 endif
95
96 # $(info Using SDL version $(SDL_VERSION) [TARGET == $(TARGET)])
97
98 ifeq ($(TARGET),sdl2)                   # compiling for SDL2 target
99 ifeq ($(PLATFORM),emscripten)
100 SDL_LIBS = -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s USE_SDL_NET=2 -s USE_ZLIB=1
101 SDL_FMTS = -s SDL2_IMAGE_FORMATS='["bmp","png","pcx","xpm"]'
102 EXTRA_CFLAGS = $(SDL_LIBS)
103 EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=81920000 -s ALLOW_MEMORY_GROWTH=1 --preload-file ../graphics/ --preload-file ../sounds/ --preload-file ../levels/ --preload-file ../music/ -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2 -lidbfs.js
104 else
105 SDL_LIBS = -lSDL2_image -lSDL2_mixer -lSDL2_net
106 endif
107
108 SYS_CFLAGS := -DTARGET_SDL2 $(shell sdl2-config --cflags)
109 SYS_LDFLAGS := $(SDL_LIBS) $(shell sdl2-config --libs) -lm -lz
110 endif
111
112
113 # -----------------------------------------------------------------------------
114 # configuring compile-time definitions
115 # -----------------------------------------------------------------------------
116
117 ifdef BASE_PATH                                 # path to read-only game data
118 CONFIG_BASE_PATH = -DBASE_PATH="\"$(BASE_PATH)\""
119 endif
120
121 CONFIG = $(CONFIG_BASE_PATH) $(JOYSTICK)
122
123 DEBUG = -DDEBUG -g
124
125 # PROFILING = $(PROFILING_FLAGS)
126
127 # OPTIONS = $(DEBUG) -Wall                      # only for debugging purposes
128 # OPTIONS = $(DEBUG) -O2 -Wall                  # only for debugging purposes
129 # OPTIONS = $(DEBUG) -Wall                      # only for debugging purposes
130 OPTIONS = $(DEBUG) -Wall -Wstrict-prototypes -Wmissing-prototypes
131 # OPTIONS = $(DEBUG) -Wall -ansi -pedantic      # only for debugging purposes
132 # OPTIONS = -O2 -Wall -ansi -pedantic
133 # OPTIONS = -O2 -Wall
134 # OPTIONS = -O2
135
136 ifdef BUILD_DIST                        # distribution build
137 SYS_LDFLAGS := $(shell echo $(SYS_LDFLAGS) |    \
138                        sed -e "s%-rpath,[^ ]*%-rpath,'\$$ORIGIN/lib'%")
139 OPTIONS = -O2 -Wall
140 endif
141
142 CFLAGS = $(OPTIONS) $(SYS_CFLAGS)  $(EXTRA_CFLAGS) $(CONFIG)
143 LDFLAGS =           $(SYS_LDFLAGS) $(EXTRA_LDFLAGS)
144
145
146 SRCS =  main.c          \
147         conf_gfx.c      \
148         conf_snd.c      \
149         conf_mus.c      \
150         conf_hlp.c      \
151         init.c          \
152         config.c        \
153         events.c        \
154         tools.c         \
155         screens.c       \
156         game.c          \
157         editor.c        \
158         files.c         \
159         tape.c          \
160         anim.c          \
161         network.c       \
162         netserv.c
163
164 OBJS =  main.o          \
165         conf_gfx.o      \
166         conf_snd.o      \
167         conf_mus.o      \
168         conf_hlp.o      \
169         init.o          \
170         config.o        \
171         events.o        \
172         tools.o         \
173         screens.o       \
174         game.o          \
175         editor.o        \
176         files.o         \
177         tape.o          \
178         anim.o          \
179         network.o       \
180         netserv.o
181
182 CNFS =  conf_gfx.h      \
183         conf_snd.h      \
184         conf_mus.h      \
185         conf_chr.c      \
186         conf_chr.h      \
187         conf_cus.c      \
188         conf_cus.h      \
189         conf_grp.c      \
190         conf_grp.h      \
191         conf_e2g.c      \
192         conf_esg.c      \
193         conf_e2s.c      \
194         conf_fnt.c      \
195         conf_g2s.c      \
196         conf_g2m.c      \
197         conf_var.c      \
198         conf_act.c
199
200 CNFS_CMD = ../build-scripts/create_element_defs.pl
201
202 TIMESTAMP_FILE = conftime.h
203 TIMESTAMP_FORMAT = %Y-%m-%d %H:%M
204
205 # use SOURCE_DATE_EPOCH, or else last Git commit date, or else current date
206 SOURCE_DATE_EPOCH ?= $(shell test -d ../.git && test `git ls-files -m | wc -l` -eq 0 && git show -s --format=%ct || date +%s)
207
208 # get source date string from either GNU / Linux or BSD / Mac OS X style "date"
209 SOURCE_DATE_STRING := $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(TIMESTAMP_FORMAT)"  2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(TIMESTAMP_FORMAT)" 2>/dev/null || date -u "+$(TIMESTAMP_FORMAT)")
210
211 COMMIT_HASH_FILE = confhash.h
212 COMMIT_HASH_NONE = "[changed files]"
213
214 # get last Git commit hash, if no files were changed
215 SOURCE_HASH_STRING ?= $(shell test -d ../.git && test `git ls-files -m | wc -l` -eq 0 && git show -s --format=%h || echo "$(COMMIT_HASH_NONE)")
216
217 LIBGAME_DIR = libgame
218 LIBGAME = $(LIBGAME_DIR)/libgame.a
219
220 GAME_EM_DIR = game_em
221 GAME_EM = $(GAME_EM_DIR)/game_em.a
222
223 GAME_SP_DIR = game_sp
224 GAME_SP = $(GAME_SP_DIR)/game_sp.a
225
226 GAME_MM_DIR = game_mm
227 GAME_MM = $(GAME_MM_DIR)/game_mm.a
228
229 RNDLIBS = $(LIBGAME) $(GAME_EM) $(GAME_SP) $(GAME_MM)
230 AUTOCONF = conf_gfx.h conf_snd.h conf_mus.h
231
232 ICONBASE = windows_icon
233 ICON_BASEPATH = ../Special/Icons/windows_icons
234
235 ifeq ($(PLATFORM_BASE),cross-win)
236 ICON_PATH = $(ICON_BASEPATH)/$(EDITION)
237 ICON = $(ICONBASE).o
238 endif
239
240 GRAPHICS_DIR = ../graphics
241
242
243 # -----------------------------------------------------------------------------
244 # build targets
245 # -----------------------------------------------------------------------------
246
247 all: $(AUTOCONF) libgame_dir game_em_dir game_sp_dir game_mm_dir $(PROGNAME) graphics_dir
248
249 $(PROGNAME): $(RNDLIBS) $(TIMESTAMP_FILE) $(COMMIT_HASH_FILE) $(OBJS) $(ICON)
250         $(CC) $(PROFILING) $(OBJS) $(ICON) $(RNDLIBS) $(LDFLAGS) -o $(PROGNAME)
251 ifdef BUILD_DIST
252         $(STRIP) $(PROGNAME)
253 endif
254
255 libgame_dir:
256         @$(MAKE) -C $(LIBGAME_DIR)
257 $(LIBGAME):
258         @$(MAKE) -C $(LIBGAME_DIR)
259
260 game_em_dir:
261         @$(MAKE) -C $(GAME_EM_DIR)
262 $(GAME_EM):
263         @$(MAKE) -C $(GAME_EM_DIR)
264
265 game_sp_dir:
266         @$(MAKE) -C $(GAME_SP_DIR)
267 $(GAME_SP):
268         @$(MAKE) -C $(GAME_SP_DIR)
269
270 game_mm_dir:
271         @$(MAKE) -C $(GAME_MM_DIR)
272 $(GAME_MM):
273         @$(MAKE) -C $(GAME_MM_DIR)
274
275 auto-conf:
276         @for i in $(CNFS); do                   \
277                 echo "$(CNFS_CMD) $$i > $$i";   \
278                 $(CNFS_CMD) $$i > $$i;          \
279         done
280
281 auto-conf-clean:
282         @for i in $(CNFS); do                   \
283                 echo "$(RM) $$i";               \
284                 $(RM) $$i;                      \
285         done
286
287 conf_gfx.h: conf_gfx.c $(CNFS_CMD)
288         @$(MAKE) auto-conf
289
290 conf_snd.h: conf_snd.c $(CNFS_CMD)
291         @$(MAKE) auto-conf
292
293 conf_mus.h: conf_mus.c $(CNFS_CMD)
294         @$(MAKE) auto-conf
295
296 conf-time:
297         @echo '#define SOURCE_DATE_STRING "$(SOURCE_DATE_STRING)"' \
298         > $(TIMESTAMP_FILE)
299
300 conf-hash:
301         @echo '#define SOURCE_HASH_STRING "$(SOURCE_HASH_STRING)"' \
302         > $(COMMIT_HASH_FILE)
303
304 $(TIMESTAMP_FILE): $(SRCS) $(RNDLIBS)
305         @$(MAKE) conf-time
306
307 $(COMMIT_HASH_FILE): $(SRCS) $(RNDLIBS)
308         @$(MAKE) conf-hash
309
310 $(ICON):
311 #       $(CONVERT) $(ICON32X32) $(CONVERT_ICON_ARGS) $(ICONBASE).ico
312         $(CONVERT) $(ICON_PATH)/*.png $(CONVERT_ICON_ARGS) $(ICONBASE).ico
313         echo "$(ICONBASE) ICON $(ICONBASE).ico" | $(WINDRES) -o $(ICON)
314
315 .c.o:
316         $(CC) $(PROFILING) $(CFLAGS) -c $*.c
317
318 graphics_dir:
319         @test -f $(GRAPHICS_DIR)/Makefile && $(MAKE) -C $(GRAPHICS_DIR) || true
320
321 clean-obj:
322         $(MAKE) -C $(LIBGAME_DIR) clean
323         $(MAKE) -C $(GAME_EM_DIR) clean
324         $(MAKE) -C $(GAME_SP_DIR) clean
325         $(MAKE) -C $(GAME_MM_DIR) clean
326         $(RM) $(OBJS)
327         $(RM) $(RNDLIBS)
328
329 clean-ico:
330         $(RM) $(ICONBASE).ico
331         $(RM) $(ICONBASE).o
332
333 clean-bin:
334         $(RM) $(PROGNAME)
335         $(RM) $(PROGNAME).exe
336         $(RM) $(PROGNAME).js
337         $(RM) $(PROGNAME).wasm
338         $(RM) $(PROGNAME).data
339
340 clean: clean-obj clean-ico clean-bin
341
342 clean-git: clean auto-conf-clean
343         @$(MAKE) -C $(GRAPHICS_DIR) clean
344
345 dist-clean: clean-obj clean-ico
346
347
348 # -----------------------------------------------------------------------------
349 # run and test targets
350 # -----------------------------------------------------------------------------
351
352 run:
353         cd .. && ./$(PROGBASE) --verbose
354
355 gdb:
356         cd .. && $(DEBUGGER) $(PROGBASE)
357
358 valgrind:
359         cd .. && valgrind -v --leak-check=yes ./$(PROGBASE) 2> valgrind.out
360
361
362 # -----------------------------------------------------------------------------
363 # development only
364 # -----------------------------------------------------------------------------
365
366 tags:
367         $(ETAGS) *.[ch] $(LIBGAME_DIR)/*.[ch] $(GAME_EM_DIR)/*.[ch] $(GAME_SP_DIR)/*.[ch] $(GAME_MM_DIR)/*.[ch]
368
369 depend:
370         $(MAKE) -C $(LIBGAME_DIR) depend
371         $(MAKE) -C $(GAME_EM_DIR) depend
372         $(MAKE) -C $(GAME_SP_DIR) depend
373         $(MAKE) -C $(GAME_MM_DIR) depend
374         for i in $(SRCS); do $(CPP) $(CFLAGS) -M $$i; done > .depend
375
376 ifeq (.depend,$(wildcard .depend))
377 include .depend
378 endif