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