192186e02118d4d413520023eac3aca57a4e92d7
[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 DATA_FILE = $(PROGBASE).data
66 CC = emcc
67 AR = emar
68 RANLIB = emranlib
69 STRIP = true
70 FILE_PACKAGER = file_packager
71 endif
72
73 ifeq ($(shell uname -s),Darwin)         # compiling on Mac OS X
74 DEBUGGER = lldb --batch -o "run" -k "bt" -k "quit"
75 SANITIZING_FLAGS = -fsanitize=undefined
76 ifdef BUILD_DIST                        # distribution build
77 MAC_TARGET_VERSION_MIN = 10.7
78 EXTRA_FLAGS_MAC = -mmacosx-version-min=$(MAC_TARGET_VERSION_MIN)
79 EXTRA_CFLAGS = $(EXTRA_FLAGS_MAC)
80 EXTRA_LDFLAGS = $(EXTRA_FLAGS_MAC)
81 MACOSX_DEPLOYMENT_TARGET = $MAC_TARGET_VERSION_MIN
82 endif
83 endif
84
85
86 # -----------------------------------------------------------------------------
87 # configuring target
88 # -----------------------------------------------------------------------------
89
90 ifndef TARGET                           # auto-detect compiling for SDL2
91   SDL_VERSION := $(shell sdl2-config --version 2> /dev/null)
92   ifdef SDL_VERSION
93     TARGET = sdl2
94   else
95     $(error SDL2 library not found)
96   endif
97 endif
98
99 # $(info Using SDL version $(SDL_VERSION) [TARGET == $(TARGET)])
100
101 ifeq ($(TARGET),sdl2)                   # compiling for SDL2 target
102 ifeq ($(PLATFORM),emscripten)
103 SDL_LIBS = -s USE_SDL_IMAGE=2 -s USE_SDL_MIXER=2 -s USE_SDL_NET=2 -s USE_MODPLUG=1 -s USE_MPG123=1 -s USE_ZLIB=1
104 SDL_FMTS = -s SDL2_IMAGE_FORMATS='["bmp","png","pcx","xpm"]' -s SDL2_MIXER_FORMATS='["mod","mp3"]'
105 EXTRA_CFLAGS = $(SDL_LIBS)
106 EXTRA_LDFLAGS = $(SDL_FMTS) -s INITIAL_MEMORY=81920000 -s ALLOW_MEMORY_GROWTH=1 -s FORCE_FILESYSTEM -s NO_EXIT_RUNTIME=0 -s ASYNCIFY -O2 -lidbfs.js
107 DATA_DIRS = conf docs levels graphics sounds music
108 FILE_PACKAGER_ARGS = --preload $(DATA_DIRS) --js-output=$(DATA_FILE).js
109 else
110 SDL_LIBS = -lSDL2_image -lSDL2_mixer -lSDL2_net
111 endif
112
113 SYS_CFLAGS := -DTARGET_SDL2 $(shell sdl2-config --cflags)
114 SYS_LDFLAGS := $(SDL_LIBS) $(shell sdl2-config --libs) -lm -lz
115 endif
116
117
118 # -----------------------------------------------------------------------------
119 # configuring compile-time definitions
120 # -----------------------------------------------------------------------------
121
122 ifdef BASE_PATH                                 # path to read-only game data
123 CONFIG_BASE_PATH = -DBASE_PATH="\"$(BASE_PATH)\""
124 endif
125
126 CONFIG = $(CONFIG_BASE_PATH) $(JOYSTICK)
127
128 DEBUG = -DDEBUG -g
129
130 # ANALYZE = $(PROFILING_FLAGS)
131 # ANALYZE = $(SANITIZING_FLAGS)
132
133 # OPTIONS = $(DEBUG) -Wall                      # only for debugging purposes
134 # OPTIONS = $(DEBUG) -O2 -Wall                  # only for debugging purposes
135 # OPTIONS = $(DEBUG) -Wall                      # only for debugging purposes
136 OPTIONS = $(DEBUG) -Wall -Wstrict-prototypes -Wmissing-prototypes
137 # OPTIONS = $(DEBUG) -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes
138 # OPTIONS = $(DEBUG) -Wall -ansi -pedantic      # only for debugging purposes
139 # OPTIONS = -O2 -Wall -ansi -pedantic
140 # OPTIONS = -O2 -Wall
141 # OPTIONS = -O2
142
143 ifdef BUILD_TEST                        # test build
144 OPTIONS := $(OPTIONS) -DTESTING
145 endif
146
147 ifdef BUILD_DIST                        # distribution build
148 SYS_LDFLAGS := $(shell echo $(SYS_LDFLAGS) |    \
149                        sed -e "s%-rpath,[^ ]*%-rpath,'\$$ORIGIN/lib'%")
150 OPTIONS = -O2 -Wall
151 endif
152
153 CFLAGS = $(OPTIONS) $(ANALYZE) $(SYS_CFLAGS)  $(EXTRA_CFLAGS) $(CONFIG)
154 LDFLAGS =           $(ANALYZE) $(SYS_LDFLAGS) $(EXTRA_LDFLAGS)
155
156
157 SRCS =  main.c          \
158         conf_gfx.c      \
159         conf_snd.c      \
160         conf_mus.c      \
161         conf_hlp.c      \
162         init.c          \
163         config.c        \
164         events.c        \
165         tools.c         \
166         screens.c       \
167         game.c          \
168         editor.c        \
169         files.c         \
170         tape.c          \
171         anim.c          \
172         api.c           \
173         network.c       \
174         netserv.c
175
176 OBJS =  main.o          \
177         conf_gfx.o      \
178         conf_snd.o      \
179         conf_mus.o      \
180         conf_hlp.o      \
181         init.o          \
182         config.o        \
183         events.o        \
184         tools.o         \
185         screens.o       \
186         game.o          \
187         editor.o        \
188         files.o         \
189         tape.o          \
190         anim.o          \
191         api.o           \
192         network.o       \
193         netserv.o
194
195 CNFS =  conf_gfx.h      \
196         conf_snd.h      \
197         conf_mus.h      \
198         conf_chr.c      \
199         conf_chr.h      \
200         conf_cus.c      \
201         conf_cus.h      \
202         conf_grp.c      \
203         conf_grp.h      \
204         conf_emp.c      \
205         conf_emp.h      \
206         conf_e2g.c      \
207         conf_esg.c      \
208         conf_e2s.c      \
209         conf_fnt.c      \
210         conf_g2s.c      \
211         conf_g2m.c      \
212         conf_var.c      \
213         conf_act.c
214
215 CNFS_CMD = ../build-scripts/create_element_defs.pl
216
217 TIMESTAMP_FILE = conftime.h
218 TIMESTAMP_FORMAT = %Y-%m-%d %H:%M
219
220 # use SOURCE_DATE_EPOCH, or else last Git commit date, or else current date
221 SOURCE_DATE_EPOCH ?= $(shell test -d ../.git && test `git ls-files -m | wc -l` -eq 0 && git show -s --format=%ct || date +%s)
222
223 # get source date string from either GNU / Linux or BSD / Mac OS X style "date"
224 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)")
225
226 COMMIT_HASH_FILE = confhash.h
227 COMMIT_HASH_NONE = "[changed files]"
228
229 # get last Git commit hash, if no files were changed
230 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)")
231
232 LIBGAME_DIR = libgame
233 LIBGAME = $(LIBGAME_DIR)/libgame.a
234
235 GAME_EM_DIR = game_em
236 GAME_EM = $(GAME_EM_DIR)/game_em.a
237
238 GAME_SP_DIR = game_sp
239 GAME_SP = $(GAME_SP_DIR)/game_sp.a
240
241 GAME_MM_DIR = game_mm
242 GAME_MM = $(GAME_MM_DIR)/game_mm.a
243
244 RNDLIBS = $(LIBGAME) $(GAME_EM) $(GAME_SP) $(GAME_MM)
245 AUTOCONF = conf_gfx.h conf_snd.h conf_mus.h
246
247 ICONBASE = windows_icon
248 ICON_BASEPATH = ../Special/Icons/windows_icons
249
250 ifeq ($(PLATFORM_BASE),cross-win)
251 ICON_PATH = $(ICON_BASEPATH)/$(EDITION)
252 ICON = $(ICONBASE).o
253 endif
254
255 GRAPHICS_DIR = ../graphics
256
257
258 # -----------------------------------------------------------------------------
259 # build targets
260 # -----------------------------------------------------------------------------
261
262 all: $(AUTOCONF) libgame_dir game_em_dir game_sp_dir game_mm_dir $(PROGNAME) graphics_dir
263
264 $(PROGNAME): $(RNDLIBS) $(TIMESTAMP_FILE) $(COMMIT_HASH_FILE) $(OBJS) $(ICON)
265         $(CC) $(OBJS) $(ICON) $(RNDLIBS) $(LDFLAGS) -o $(PROGNAME)
266 ifdef BUILD_DIST
267         $(STRIP) $(PROGNAME)
268 endif
269 ifeq ($(PLATFORM),emscripten)
270         (cd .. ; $(FILE_PACKAGER) $(DATA_FILE) $(FILE_PACKAGER_ARGS))
271 endif
272
273 libgame_dir:
274         @$(MAKE) -C $(LIBGAME_DIR)
275 $(LIBGAME):
276         @$(MAKE) -C $(LIBGAME_DIR)
277
278 game_em_dir:
279         @$(MAKE) -C $(GAME_EM_DIR)
280 $(GAME_EM):
281         @$(MAKE) -C $(GAME_EM_DIR)
282
283 game_sp_dir:
284         @$(MAKE) -C $(GAME_SP_DIR)
285 $(GAME_SP):
286         @$(MAKE) -C $(GAME_SP_DIR)
287
288 game_mm_dir:
289         @$(MAKE) -C $(GAME_MM_DIR)
290 $(GAME_MM):
291         @$(MAKE) -C $(GAME_MM_DIR)
292
293 auto-conf:
294         @for i in $(CNFS); do                   \
295                 echo "$(CNFS_CMD) $$i > $$i";   \
296                 $(CNFS_CMD) $$i > $$i;          \
297         done
298
299 auto-conf-clean:
300         @for i in $(CNFS); do                   \
301                 echo "$(RM) $$i";               \
302                 $(RM) $$i;                      \
303         done
304
305 conf_gfx.h: conf_gfx.c $(CNFS_CMD)
306         @$(MAKE) auto-conf
307
308 conf_snd.h: conf_snd.c $(CNFS_CMD)
309         @$(MAKE) auto-conf
310
311 conf_mus.h: conf_mus.c $(CNFS_CMD)
312         @$(MAKE) auto-conf
313
314 conf-time:
315         @echo '#define SOURCE_DATE_STRING "$(SOURCE_DATE_STRING)"' \
316         > $(TIMESTAMP_FILE)
317
318 conf-hash:
319         @echo '#define SOURCE_HASH_STRING "$(SOURCE_HASH_STRING)"' \
320         > $(COMMIT_HASH_FILE)
321
322 config.o: config.c $(TIMESTAMP_FILE)
323
324 $(TIMESTAMP_FILE): $(SRCS) $(RNDLIBS)
325         @$(MAKE) conf-time
326
327 $(COMMIT_HASH_FILE): $(SRCS) $(RNDLIBS)
328         @$(MAKE) conf-hash
329
330 $(ICON):
331 #       $(CONVERT) $(ICON32X32) $(CONVERT_ICON_ARGS) $(ICONBASE).ico
332         $(CONVERT) $(ICON_PATH)/*.png $(CONVERT_ICON_ARGS) $(ICONBASE).ico
333         echo "$(ICONBASE) ICON $(ICONBASE).ico" | $(WINDRES) -o $(ICON)
334
335 .c.o:
336         $(CC) $(CFLAGS) -c $*.c
337
338 graphics_dir:
339         @test -f $(GRAPHICS_DIR)/Makefile && $(MAKE) -C $(GRAPHICS_DIR) || true
340
341 clean-obj:
342         $(MAKE) -C $(LIBGAME_DIR) clean
343         $(MAKE) -C $(GAME_EM_DIR) clean
344         $(MAKE) -C $(GAME_SP_DIR) clean
345         $(MAKE) -C $(GAME_MM_DIR) clean
346         $(RM) $(OBJS)
347         $(RM) $(RNDLIBS)
348
349 clean-ico:
350         $(RM) $(ICONBASE).ico
351         $(RM) $(ICONBASE).o
352
353 clean-bin:
354         $(RM) $(PROGNAME)
355         $(RM) $(PROGNAME).exe
356         $(RM) $(PROGNAME).js
357         $(RM) $(PROGNAME).wasm
358         $(RM) $(PROGNAME).data
359
360 clean: clean-obj clean-ico clean-bin
361
362 clean-git: clean auto-conf-clean
363         @$(MAKE) -C $(GRAPHICS_DIR) clean
364
365 dist-clean: clean-obj clean-ico
366
367
368 # -----------------------------------------------------------------------------
369 # run and test targets
370 # -----------------------------------------------------------------------------
371
372 run:
373         cd .. && ./$(PROGBASE) --verbose
374
375 gdb:
376         cd .. && $(DEBUGGER) $(PROGBASE)
377
378 valgrind:
379         cd .. && valgrind -v --leak-check=yes ./$(PROGBASE) 2> valgrind.out
380
381
382 # -----------------------------------------------------------------------------
383 # development only
384 # -----------------------------------------------------------------------------
385
386 tags:
387         $(ETAGS) *.[ch] $(LIBGAME_DIR)/*.[ch] $(GAME_EM_DIR)/*.[ch] $(GAME_SP_DIR)/*.[ch] $(GAME_MM_DIR)/*.[ch]
388
389 depend:
390         $(MAKE) -C $(LIBGAME_DIR) depend
391         $(MAKE) -C $(GAME_EM_DIR) depend
392         $(MAKE) -C $(GAME_SP_DIR) depend
393         $(MAKE) -C $(GAME_MM_DIR) depend
394         for i in $(SRCS); do $(CPP) $(CFLAGS) -M $$i; done > .depend
395
396 ifeq (.depend,$(wildcard .depend))
397 include .depend
398 endif