0456b906a58798e4a7c5728b791ce757abe66e54
[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 ../conf/ --preload-file ../docs/ --preload-file ../levels/ --preload-file ../graphics/ --preload-file ../sounds/ --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_TEST                        # test build
137 OPTIONS := $(OPTIONS) -DTESTING
138 endif
139
140 ifdef BUILD_DIST                        # distribution build
141 SYS_LDFLAGS := $(shell echo $(SYS_LDFLAGS) |    \
142                        sed -e "s%-rpath,[^ ]*%-rpath,'\$$ORIGIN/lib'%")
143 OPTIONS = -O2 -Wall
144 endif
145
146 CFLAGS = $(OPTIONS) $(SYS_CFLAGS)  $(EXTRA_CFLAGS) $(CONFIG)
147 LDFLAGS =           $(SYS_LDFLAGS) $(EXTRA_LDFLAGS)
148
149
150 SRCS =  main.c          \
151         conf_gfx.c      \
152         conf_snd.c      \
153         conf_mus.c      \
154         conf_hlp.c      \
155         init.c          \
156         config.c        \
157         events.c        \
158         tools.c         \
159         screens.c       \
160         game.c          \
161         editor.c        \
162         files.c         \
163         tape.c          \
164         anim.c          \
165         network.c       \
166         netserv.c
167
168 OBJS =  main.o          \
169         conf_gfx.o      \
170         conf_snd.o      \
171         conf_mus.o      \
172         conf_hlp.o      \
173         init.o          \
174         config.o        \
175         events.o        \
176         tools.o         \
177         screens.o       \
178         game.o          \
179         editor.o        \
180         files.o         \
181         tape.o          \
182         anim.o          \
183         network.o       \
184         netserv.o
185
186 CNFS =  conf_gfx.h      \
187         conf_snd.h      \
188         conf_mus.h      \
189         conf_chr.c      \
190         conf_chr.h      \
191         conf_cus.c      \
192         conf_cus.h      \
193         conf_grp.c      \
194         conf_grp.h      \
195         conf_e2g.c      \
196         conf_esg.c      \
197         conf_e2s.c      \
198         conf_fnt.c      \
199         conf_g2s.c      \
200         conf_g2m.c      \
201         conf_var.c      \
202         conf_act.c
203
204 CNFS_CMD = ../build-scripts/create_element_defs.pl
205
206 TIMESTAMP_FILE = conftime.h
207 TIMESTAMP_FORMAT = %Y-%m-%d %H:%M
208
209 # use SOURCE_DATE_EPOCH, or else last Git commit date, or else current date
210 SOURCE_DATE_EPOCH ?= $(shell test -d ../.git && test `git ls-files -m | wc -l` -eq 0 && git show -s --format=%ct || date +%s)
211
212 # get source date string from either GNU / Linux or BSD / Mac OS X style "date"
213 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)")
214
215 COMMIT_HASH_FILE = confhash.h
216 COMMIT_HASH_NONE = "[changed files]"
217
218 # get last Git commit hash, if no files were changed
219 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)")
220
221 LIBGAME_DIR = libgame
222 LIBGAME = $(LIBGAME_DIR)/libgame.a
223
224 GAME_EM_DIR = game_em
225 GAME_EM = $(GAME_EM_DIR)/game_em.a
226
227 GAME_SP_DIR = game_sp
228 GAME_SP = $(GAME_SP_DIR)/game_sp.a
229
230 GAME_MM_DIR = game_mm
231 GAME_MM = $(GAME_MM_DIR)/game_mm.a
232
233 RNDLIBS = $(LIBGAME) $(GAME_EM) $(GAME_SP) $(GAME_MM)
234 AUTOCONF = conf_gfx.h conf_snd.h conf_mus.h
235
236 ICONBASE = windows_icon
237 ICON_BASEPATH = ../Special/Icons/windows_icons
238
239 ifeq ($(PLATFORM_BASE),cross-win)
240 ICON_PATH = $(ICON_BASEPATH)/$(EDITION)
241 ICON = $(ICONBASE).o
242 endif
243
244 GRAPHICS_DIR = ../graphics
245
246
247 # -----------------------------------------------------------------------------
248 # build targets
249 # -----------------------------------------------------------------------------
250
251 all: $(AUTOCONF) libgame_dir game_em_dir game_sp_dir game_mm_dir $(PROGNAME) graphics_dir
252
253 $(PROGNAME): $(RNDLIBS) $(TIMESTAMP_FILE) $(COMMIT_HASH_FILE) $(OBJS) $(ICON)
254         $(CC) $(PROFILING) $(OBJS) $(ICON) $(RNDLIBS) $(LDFLAGS) -o $(PROGNAME)
255 ifdef BUILD_DIST
256         $(STRIP) $(PROGNAME)
257 endif
258
259 libgame_dir:
260         @$(MAKE) -C $(LIBGAME_DIR)
261 $(LIBGAME):
262         @$(MAKE) -C $(LIBGAME_DIR)
263
264 game_em_dir:
265         @$(MAKE) -C $(GAME_EM_DIR)
266 $(GAME_EM):
267         @$(MAKE) -C $(GAME_EM_DIR)
268
269 game_sp_dir:
270         @$(MAKE) -C $(GAME_SP_DIR)
271 $(GAME_SP):
272         @$(MAKE) -C $(GAME_SP_DIR)
273
274 game_mm_dir:
275         @$(MAKE) -C $(GAME_MM_DIR)
276 $(GAME_MM):
277         @$(MAKE) -C $(GAME_MM_DIR)
278
279 auto-conf:
280         @for i in $(CNFS); do                   \
281                 echo "$(CNFS_CMD) $$i > $$i";   \
282                 $(CNFS_CMD) $$i > $$i;          \
283         done
284
285 auto-conf-clean:
286         @for i in $(CNFS); do                   \
287                 echo "$(RM) $$i";               \
288                 $(RM) $$i;                      \
289         done
290
291 conf_gfx.h: conf_gfx.c $(CNFS_CMD)
292         @$(MAKE) auto-conf
293
294 conf_snd.h: conf_snd.c $(CNFS_CMD)
295         @$(MAKE) auto-conf
296
297 conf_mus.h: conf_mus.c $(CNFS_CMD)
298         @$(MAKE) auto-conf
299
300 conf-time:
301         @echo '#define SOURCE_DATE_STRING "$(SOURCE_DATE_STRING)"' \
302         > $(TIMESTAMP_FILE)
303
304 conf-hash:
305         @echo '#define SOURCE_HASH_STRING "$(SOURCE_HASH_STRING)"' \
306         > $(COMMIT_HASH_FILE)
307
308 $(TIMESTAMP_FILE): $(SRCS) $(RNDLIBS)
309         @$(MAKE) conf-time
310
311 $(COMMIT_HASH_FILE): $(SRCS) $(RNDLIBS)
312         @$(MAKE) conf-hash
313
314 $(ICON):
315 #       $(CONVERT) $(ICON32X32) $(CONVERT_ICON_ARGS) $(ICONBASE).ico
316         $(CONVERT) $(ICON_PATH)/*.png $(CONVERT_ICON_ARGS) $(ICONBASE).ico
317         echo "$(ICONBASE) ICON $(ICONBASE).ico" | $(WINDRES) -o $(ICON)
318
319 .c.o:
320         $(CC) $(PROFILING) $(CFLAGS) -c $*.c
321
322 graphics_dir:
323         @test -f $(GRAPHICS_DIR)/Makefile && $(MAKE) -C $(GRAPHICS_DIR) || true
324
325 clean-obj:
326         $(MAKE) -C $(LIBGAME_DIR) clean
327         $(MAKE) -C $(GAME_EM_DIR) clean
328         $(MAKE) -C $(GAME_SP_DIR) clean
329         $(MAKE) -C $(GAME_MM_DIR) clean
330         $(RM) $(OBJS)
331         $(RM) $(RNDLIBS)
332
333 clean-ico:
334         $(RM) $(ICONBASE).ico
335         $(RM) $(ICONBASE).o
336
337 clean-bin:
338         $(RM) $(PROGNAME)
339         $(RM) $(PROGNAME).exe
340         $(RM) $(PROGNAME).js
341         $(RM) $(PROGNAME).wasm
342         $(RM) $(PROGNAME).data
343
344 clean: clean-obj clean-ico clean-bin
345
346 clean-git: clean auto-conf-clean
347         @$(MAKE) -C $(GRAPHICS_DIR) clean
348
349 dist-clean: clean-obj clean-ico
350
351
352 # -----------------------------------------------------------------------------
353 # run and test targets
354 # -----------------------------------------------------------------------------
355
356 run:
357         cd .. && ./$(PROGBASE) --verbose
358
359 gdb:
360         cd .. && $(DEBUGGER) $(PROGBASE)
361
362 valgrind:
363         cd .. && valgrind -v --leak-check=yes ./$(PROGBASE) 2> valgrind.out
364
365
366 # -----------------------------------------------------------------------------
367 # development only
368 # -----------------------------------------------------------------------------
369
370 tags:
371         $(ETAGS) *.[ch] $(LIBGAME_DIR)/*.[ch] $(GAME_EM_DIR)/*.[ch] $(GAME_SP_DIR)/*.[ch] $(GAME_MM_DIR)/*.[ch]
372
373 depend:
374         $(MAKE) -C $(LIBGAME_DIR) depend
375         $(MAKE) -C $(GAME_EM_DIR) depend
376         $(MAKE) -C $(GAME_SP_DIR) depend
377         $(MAKE) -C $(GAME_MM_DIR) depend
378         for i in $(SRCS); do $(CPP) $(CFLAGS) -M $$i; done > .depend
379
380 ifeq (.depend,$(wildcard .depend))
381 include .depend
382 endif