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