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