# Wormlings makefile.
# ------------------------------------------------------------------------------
# $Revision: 166 $ $Date: 2005-03-04 14:13:01 +0100 (fr, 04 mar 2005) $
# ------------------------------------------------------------------------------
# Copyright 2000-2005 Daniel Schlyder.
# Distributed under the GNU General Public License; as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version. (See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl.html)



# ------------
# Instructions
# ------------------------------------------------------------------------------
#
# This makefile has been tested in Windows (MinGW) and Linux (GCC). Hopefully,
# it will also work using GCC in BeOS and QNX. Please let me know of any
# problems!
#
# Before using this makefile:
#
# * make sure Allegro 4.x (http://alleg.sourceforge.net/) is installed on your
#   system.
#
# * if target platform is Windows or BeOS, make sure the appropriate Allegro
#     fixicon tool is in PATH
#
# * note that dist and distsrc targets have further requirements (see below).
#
#
# Two environment variables control type of built executable:
#
# BUILD
# type of version to build: 'release' (default), 'debug', or 'profiling'
#
# STATICLINK
# whether to link with static (1) or dynamic (0, default) version of Allegro
# library
#
# So, for example, to build statically linked debug version using MinGW,
# you would type:
#
# $ make BUILD=debug STATICLINK=1
#
#
# Available targets:
#
# default:
# build executable
#
# dist:
# create binary distribution archive using Inno Setup 5
# (http://www.jrsoftware.org/isinfo.php)
# for Windows version, and tar/bzip2 for other versions
#
# distsrc:
# create source distribution archive using tar, bzip2 and dos2unix
# (from mingw-utils package.)
#
# version:
# alias for 'distsrc dist'
#
# clean:
# remove files created by make
#
# mostlyclean:
# as clean, but keep files you normally don't want to rebuild.
#
# distclean:
# prepare for distribution



# -------------
# Project files
# ------------------------------------------------------------------------------

appname        = wormlings
appversion     = 3.0
objects        = main.o graphics.o audio.o menu.o game.o utility.o \
                 latencyTimer.o
icon           = src/build/icon.bmp
dist_files     = readme.html LICENSE.txt data



# --------------
# Initialisation
# ------------------------------------------------------------------------------

# Make sure appversion value is sensible.
ifneq (,$(findstring agName,$(appversion)))
    appversion = wip
endif


# Detect target platform.
defined_macros := $(shell $(CXX) -E -dM src/build/get_defined_macros_helper.cpp)
ifeq (_WIN32,$(findstring _WIN32,$(defined_macros)))
    TARGET = windows
else
    ifeq (__BEOS__,$(findstring __BEOS__,$(defined_macros)))
        TARGET = beos
    else
        ifeq (__QNX__,$(findstring __QNX__,$(defined_macros)))
            TARGET = qnx
        else
            TARGET = unix
        endif
    endif
endif


# Build optimised version by default

ifeq (,$(findstring $(BUILD),release debug profiling))
    BUILD = release
endif


# Link against dynamic version of Allegro library by default

STATICLINK = 0


# Windows CLI sucks
ifeq (windows,$(TARGET))
    RM    = del /Q
    RMDIR = rd /S /Q
    SLASH = $(strip \)
else
    RM    = rm -f
    RMDIR = rm -rf
    SLASH = /
endif


# platform-independent compiler configuration

DEFS      := -DWORMLINGS_VERSION=\"$(appversion)\"
CXXFLAGS   = -Wall -W -march=i686
LDFLAGS    =
INCLUDES   = -Isrc/third-party/jpgalleg/include
LIBS       =
LIBS_BUILD = src/third-party/jpgalleg/lib/libjpgal.a

ifeq ($(BUILD), debug)
    CXXFLAGS += -g
    LDFLAGS  += -g
else
    DEFS     += -DNDEBUG
    CXXFLAGS += -O3 -ffast-math
    ifeq ($(BUILD), profiling)
        CXXFLAGS += -pg
        LDFLAGS  += -pg
    else
        CXXFLAGS += -fomit-frame-pointer
        LDFLAGS  += -s
    endif
endif

ifeq ($(STATICLINK), 1)
    DEFS += -DALLEGRO_STATICLINK
endif


# platform-dependent compiler configuration

ifeq ($(TARGET), windows)
    LDFLAGS += -Wl,--subsystem,windows
    ifeq ($(STATICLINK), 1)
        LIBS += -lalleg_s -lkernel32 -luser32 -lgdi32 -lcomdlg32 -lole32 \
                -ldinput -lddraw -ldxguid -lwinmm -ldsound
    else
        LIBS += -lalleg
    endif
    iconres = obj\icon.res
    exe := $(appname).exe
else
    ifeq ($(TARGET), beos)
        ifeq ($(STATICLINK), 1)
            LIBS += `allegro-config --static`
        else
            LIBS += `allegro-config --dynamic`
        endif
    else # QNX or Linux/Unix
        LIBS += `allegro-config --libs`
        ifeq ($(TARGET), qnx)
            CXXFLAGS += -fno-builtin
        endif
    endif
    exe := $(appname)
endif


# Add executable to binary distribution archive files list

dist_files := $(dist_files) $(exe)


# Fix path to object files

objects := $(addprefix obj/, $(objects))



# ------------
# Target rules
# -----------------------------------------------------------------------------

.PHONY: default dist distsrc version clean mostlyclean distclean

default: $(exe)



# link object(s) and add icon(s)

$(exe): $(objects) $(iconres) $(LIBS_BUILD)
	$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
ifeq ($(TARGET), beos)
	bfixicon $@ $(icon)
endif



# Compile source

obj/%.o: src/%.cpp
ifeq (,$(filter-out 2.%,$(shell $(CXX) -dumpversion)))
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
else
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDES) -MMD -MF $(basename $@).d -c $< -o $@
endif



# Generate dependency files automatically for users (on BeOS) stuck with GCC 2.x

ifeq (,$(filter-out 2.%,$(shell $(CXX) -dumpversion)))
obj/%.d: src/%.cpp
	@echo generating dependency file for $(*F).o
	@$(CXX) -MM $(INCLUDES) $< > obj/_temp.d
	@sed -e "s/^\(\w*\)\.o:/obj\/\1\.o obj\/\1\.d:/" obj/_temp.d > $@
endif



# create object file for Windows program icon

obj\icon.res: $(icon)
	wfixicon obj\icon.ico -ro $^



# Build JPGAlleg

src/third-party/jpgalleg/lib/libjpgal.a:
ifeq ($(TARGET),windows)
	cd src/third-party/jpgalleg & fix.bat mingw32
else
	chmod +x src/third-party/jpgalleg/fix.sh
    ifeq ($(TARGET),beos)
		cd src/third-party/jpgalleg ; ./fix.sh beos
    else
		cd src/third-party/jpgalleg ; ./fix.sh unix
    endif
endif
	make -Csrc/third-party/jpgalleg lib



# create binary distribution archive using Inno Setup 5
# (http://www.jrsoftware.org/isinfo.php)
# for Windows version, and tar/bzip2 for other versions

dist_archive_name := $(appname)-$(appversion).$(TARGET)
dist_temp_path := dist/$(appname)

dist: $(dist_files)
ifeq ($(TARGET), windows)
	iscc src/build/windows-setup/wormlings.iss
else
	mkdir $(dist_temp_path)
	cp -pR --parents $(dist_files) $(dist_temp_path)
	tar --create --directory=dist --file=dist/$(dist_archive_name).tar \
		$(appname) --exclude=.svn
	bzip2 -f dist/$(dist_archive_name).tar
	rm -rf $(dist_temp_path)
endif



# Create source distribution archive using tar/bzip2.

distsrc_top_dir_name := $(appname)-$(appversion)
distsrc_archive_basename := $(distsrc_top_dir_name)-source

distsrc: distclean
# Using tar to copy files so we can exclude .svn dirs.
	tar --create --file=dist/$(distsrc_archive_basename).tar . --exclude=.svn \
		--exclude=*.tar --exclude=*.bz2 --exclude=*.exe
	mkdir dist$(SLASH)$(distsrc_top_dir_name)
# Ignore errors cause we'll get a bunch in Windows when trying to change
# access and modification times for directories.
	-tar --extract --directory=dist/$(distsrc_top_dir_name) \
		--file=dist/$(distsrc_archive_basename).tar
	$(RM) dist$(SLASH)$(distsrc_archive_basename).tar
	dos2unix dist/$(distsrc_top_dir_name)/LICENSE.txt
	dos2unix dist/$(distsrc_top_dir_name)/Makefile
	dos2unix dist/$(distsrc_top_dir_name)/src/*.*pp
	tar --create --directory=dist --file=dist/$(distsrc_archive_basename).tar \
		$(distsrc_top_dir_name) --exclude=.svn
	$(RMDIR) dist$(SLASH)$(distsrc_top_dir_name)
	bzip2 -f dist/$(distsrc_archive_basename).tar



# Alias for 'distsrc dist'

version: distsrc dist



# Cleanup

mostlyclean:
	-$(RM) $(exe)
	-$(RM) obj$(SLASH)*.o
	-$(RM) obj$(SLASH)*.d
ifeq ($(TARGET),windows)
	-$(RM) $(iconres)
endif

clean: mostlyclean
	-make -Csrc/third-party/jpgalleg distclean

distclean: clean
	-$(RM) wormlings.cfg



# ------------
# Dependencies
# -----------------------------------------------------------------------------

ifeq ( , $(findstring $(MAKECMDGOALS), distsrc clean mostlyclean distclean))
    -include $(objects:.o=.d)
else
    ifneq ( , $(findstring $(MAKECMDGOALS), dist))
        -include $(objects:.o=.d)
    endif
endif
