# This Makefile is partially based on "a completely generic Makefile",
# originally created by Justin Husted <husted@cs>
# 
# This Makefile is (c) Petr Baudis <pasky@ucw.cz>
# Last modified by: $Author: strigeus $
# On: $Date: 2004/03/11 19:15:06 $



##############################################################################
#
# Configuration
#

CC=gcc

# When calling the compiler, use these flags
# -g	debugging symbols
# -Wall	all warnings
# -s    automatic strip
#
# You may also want:
# -O	optimize or -O2 fully optimize (O's above 2 are not recommended)
# -pg	profile - generate profiling data.  See "man gprof" to use this.

# Release mode
#CFLAGS=-s -Wall -Wno-multichar `sdl-config --cflags` -O2 -fomit-frame-pointer -DUNIX -DWITH_SDL

# Debug mode
CFLAGS=-g -Wall -Wno-multichar `sdl-config --cflags` `libpng-config --cflags` -DZLIB_DLL -DUNIX -DWITH_SDL -DWITH_PNG -D_DEBUG

# this makes the mac run much faster. It contains at least one mac only flag
ifeq "$(VENDOR)" "apple"
CFLAGS=-g -Wall -Wno-multichar `sdl-config --cflags` `libpng-config --cflags` -DZLIB_DLL -DUNIX -DWITH_SDL -DWITH_PNG -D_DEBUG -O3 -fast -mcpu=7450 -fforce-mem -foptimize-sibling-calls -finline-functions -fno-branch-count-reg -fstrength-reduce 
endif

# this makes the mac file executable
ifeq "$(VENDOR)" "apple"
LDFLAGS=-bundle_loader executable;
endif

# And use these flags when building to an executable, to link in the right
# shared libraries
LIBS=`sdl-config --libs` -lz -lpng

ifeq "$(VENDOR)" "apple"
LIBS=`sdl-config --static-libs` -lz libpng12.a
endif



##############################################################################
#
# Targets
# (users do not want to modify anything below)
#


### Sources

ttd_SOURCES = \
	ai.c aircraft_cmd.c aircraft_gui.c airport_gui.c bridge_gui.c \
	clear_cmd.c command.c disaster_cmd.c dock_gui.c dummy_land.c economy.c \
	engine.c engine_gui.c extmidi.c fileio.c gfx.c graph_gui.c industry_cmd.c \
	industry_gui.c intro_gui.c landscape.c main_gui.c misc.c misc_cmd.c \
	misc_gui.c music_gui.c namegen.c network.c news_gui.c oldloader.c \
	order_cmd.c order_gui.c pathfind.c player_gui.c players.c rail_cmd.c \
	rail_gui.c road_cmd.c road_gui.c roadveh_cmd.c roadveh_gui.c saveload.c \
	sdl.c settings_gui.c ship_cmd.c ship_gui.c smallmap_gui.c sound.c \
	spritecache.c station_cmd.c station_gui.c strings.c subsidy_gui.c \
	texteff.c town_cmd.c town_gui.c train_cmd.c train_gui.c tree_cmd.c \
	ttd.c tunnelbridge_cmd.c unmovable_cmd.c vehicle.c viewport.c \
	water_cmd.c widget.c window.c minilzo.c unix.c screenshot.c settings.c

ttd_OBJS = $(ttd_SOURCES:%.c=%.o)

ttd_DEPS1 = $(foreach obj,$(ttd_OBJS),.deps/$(obj))
ttd_DEPS = $(ttd_DEPS1:%.o=%.P)

C_COMPILE = $(CC) $(CFLAGS)
C_BUILD = $(C_COMPILE) -c
C_LINK = $(C_COMPILE) $(LDFLAGS) -o



### Normal build rules

all: ttd

ttd: table/strings.h $(ttd_OBJS)
	$(C_LINK) $@ $(ttd_OBJS) $(LIBS)


strgen/strgen: strgen/strgen.c
	$(C_LINK) $@ $^

table/strings.h: lang/english.txt strgen/strgen
	strgen/strgen


clean:
	rm -rf .deps *~ $(ttd_OBJS) ttd strgen/strgen core table/strings.h lang/*.lng

.PHONY: clean



### Internal build rules

# This makes sure the .deps dir is always around.
DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)

# Introduce the dependencies
-include $(ttd_DEPS)

# This compiles the object file as well as silently updating its dependencies
# list at the same time. It is not an issue that they aren't around during the
# first compilation round as we just build everything at that time anyway,
# therefore we do not need to watch deps.
%.o: %.c
	@echo '$(C_BUILD) $<'; \
		$(C_BUILD) $< -Wp,-MD,.deps/$(*F).pp
	@-cp .deps/$(*F).pp .deps/$(*F).P; \
		tr ' ' '\012' < .deps/$(*F).pp \
		| sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \
		>> .deps/$(*F).P; \
	rm .deps/$(*F).pp
