#
#  Shared makefile for building the Allegro library. Requires GNU make.
#
#  Available options:
#
#     Environment variables:
#        DEBUGMODE=1       selects a debug build.
#        PROFILEMODE=1     selects a profiling build.
#        WARNMODE=1        selects strict compiler warnings.
#        STATICLINK=1      use static linking (MSVC only).
#        PGCC=1            use PGCC compiler switches (djgpp, RSXNT, Mingw32).
#        CROSSCOMPILE=1    build djgpp version under Linux (djgpp only).
#        ALLEGRO_USE_C=1   use C drawing code instead of asm (djgpp only).
#
#     Targets: 
#        default:          build library version, docs, and programs.
#        all:              build all versions and programs.
#        lib:              build a single library only, no programs.
#        install:          install single library version.
#        installall:       install all library versions.
#        uninstall:        remove Allegro from system directories.
#        docs:             convert documentation from ._tx sources.
#        clean:            remove object files.
#        distclean:        remove objects and executables.
#        veryclean:        remove all generated files.
#        depend:           regenerate the dependency files.
#        compress:         compress the executable files (djgpp only).
#
#  The "clean", "distclean", "veryclean", and "uninstall" targets require
#  a Unix-style rm command to be installed, eg. from the djgpp fileutils
#  package.



# -------- read version information --------
include makefile.ver



# -------- check environment to see what type of library to build --------

ifdef DEBUGMODE

# -------- build a debugging library --------
VERSION = alld
SHORT_VERSION = ald
VERY_SHORT_VERSION = ad
DESCRIPTION = debugging

else
ifdef PROFILEMODE

# -------- build a profiling library --------
VERSION = allp
SHORT_VERSION = alp
VERY_SHORT_VERSION = ap
DESCRIPTION = profiling

else

# -------- build a release library --------
VERSION = alleg
SHORT_VERSION = all
VERY_SHORT_VERSION = al
DESCRIPTION = optimised

endif
endif



# -------- list of what files we need to build --------

include makefile.lst

COMMON_OBJECTS = $(basename $(notdir $(ALLEGRO_SRC_FILES)))
C_OBJECTS = $(basename $(notdir $(ALLEGRO_SRC_C_FILES)))
I386_OBJECTS = $(basename $(notdir $(ALLEGRO_SRC_I386_FILES)))



# -------- include platform-specific definitions --------

include $(MAKEFILE_INC)



# -------- list what objects and programs we need to build --------

VPATH += src demo docs examples setup tests tools tools/plugins

OBJECTS = $(addprefix $(OBJ_DIR)/, $(addsuffix $(OBJ), $(OBJECT_LIST)))

PROGRAMS += $(notdir $(ALLEGRO_LIB_EXES) $(ALLEGRO_DATAFILE_EXES)) \
	    examples makedoc

EXAMPLES = $(basename $(notdir $(ALLEGRO_EXAMPLE_FILES)))

EXAMPLE_FILES = $(addprefix examples/, $(addsuffix $(EXE), $(EXAMPLES)))

ifdef HTML
DOCS = docs/ahack.$(HTML) CHANGES docs/changes.$(HTML) \
       faq.txt docs/faq.$(HTML) help.txt docs/help.$(HTML) \
       AUTHORS THANKS docs/thanks.$(HTML) \
       allegro.txt docs/allegro.$(HTML) docs/allegro.rtf \
       docs/const.txt docs/const.$(HTML)
endif

ifdef INFO
DOCS += docs/allegro.$(TEXI) docs/allegro.$(INFO)
endif



# -------- top level boss targets like "default" and "all" --------

.PHONY: default all msg lib install installall uninstall docs \
	clean distclean veryclean depend $(PROGRAMS)

.PRECIOUS: $(OBJ_DIR)/%$(OBJ)

default: msg lib $(PROGRAMS) docs
	@echo The $(DESCRIPTION) $(PLATFORM) library has been compiled.
	@echo Run make install to complete the installation.

all:
	$(MAKE) msg lib
	$(MAKE) msg lib DEBUGMODE=1
	$(MAKE) msg lib PROFILEMODE=1
	@echo Compiling utilities, examples and test programs. Please wait...
	$(MAKE) $(PROGRAMS) docs
	@echo All $(PLATFORM) library versions have been compiled.
	@echo Run make installall to complete the installation.

installall:
	$(MAKE) msg lib install
	$(MAKE) msg lib install DEBUGMODE=1
	$(MAKE) msg lib install PROFILEMODE=1
	@echo All $(PLATFORM) library versions have been installed.

msg:
	@echo Compiling Allegro for $(PLATFORM), $(DESCRIPTION). Please wait...

docs: $(DOCS)

lib: $(LIB_NAME)

$(LIB_NAME): $(OBJECTS) $(RUNNER)
	$(MAKE_LIB)



# -------- rules for deleting the generated files --------

clean:
	-rm -fv $(CLEAN_FILES)

distclean: clean
	-rm -fv $(DISTCLEAN_FILES)

veryclean: distclean
	-rm -fv $(VERYCLEAN_FILES)



# -------- rules for converting the documentation --------

ifndef CROSSCOMPILE

MAKEDOC = docs/makedoc$(EXE)

else

MAKEDOC = docs/makedoc

docs/makedoc: docs/makedoc-linux.o
	PATH=$(NATIVEPATH) gcc -o $@ $<

docs/makedoc-linux.o: docs/makedoc.c
	PATH=$(NATIVEPATH) gcc -O2 -c -o $@ $<

endif

ifdef HTML

%.txt: docs/%._tx $(MAKEDOC)
	$(MAKEDOC) -ascii $@ $<

docs/%.$(HTML): docs/%._tx $(MAKEDOC)
	$(MAKEDOC) -$(HTML) $@ $<

docs/%.rtf: docs/%._tx $(MAKEDOC)
	$(MAKEDOC) -rtf $@ $<

docs/%.txt: docs/%._tx $(MAKEDOC)
	$(MAKEDOC) -ascii $@ $<

CHANGES: docs/changes._tx $(MAKEDOC)
	$(MAKEDOC) -ascii CHANGES docs/changes._tx

AUTHORS: docs/thanks._tx $(MAKEDOC)
	$(MAKEDOC) -part -ascii AUTHORS docs/thanks._tx

THANKS: docs/thanks._tx $(MAKEDOC)
	$(MAKEDOC) -part -ascii THANKS docs/thanks._tx

endif

ifdef INFO

docs/%.$(INFO): docs/%.$(TEXI)
	-makeinfo --no-split -o $@ $<

docs/%.$(TEXI): docs/%._tx $(MAKEDOC)
	$(MAKEDOC) -$(TEXI) $@ $<

endif



# -------- special rules for linking win32 console applications --------

ifdef LINK_CONSOLE

docs/makedoc$(EXE): $(OBJ_DIR)/makedoc$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

tools/colormap$(EXE): $(OBJ_DIR)/colormap$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

tools/exedat$(EXE): $(OBJ_DIR)/exedat$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

tools/pack$(EXE): $(OBJ_DIR)/pack$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

tools/rgbmap$(EXE): $(OBJ_DIR)/rgbmap$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

tools/textconv$(EXE): $(OBJ_DIR)/textconv$(OBJ) $(LINK_CONSOLE_DEPS)
	$(LINK_CONSOLE)

endif



# -------- special rules for linking the datafile plugins --------

PLUGIN_SOURCES = $(wildcard tools/plugins/*.c)
PLUGIN_OBJS = $(OBJ_DIR)/datedit$(OBJ) $(addprefix $(OBJ_DIR)/,$(addsuffix $(OBJ),$(basename $(notdir $(PLUGIN_SOURCES)))))
PLUGIN_SCRIPTS = $(wildcard tools/plugins/*.$(PLUGIN_SCR))

ifneq ($(PLUGIN_SCRIPTS),)
PLUGIN_DEPS += $(PLUGIN_SCRIPTS)
endif

$(PLUGINS_H): $(wildcard tools/plugins/*.inc)
	$(GENERATE_PLUGINS_H)

$(PLUGIN_LIB): $(PLUGIN_OBJS)
	$(MAKE_PLUGIN_LIB)

ifndef LINK_CONSOLE_WITH_PLUGINS
LINK_CONSOLE_WITH_PLUGINS = $(LINK_WITH_PLUGINS)
endif

tools/dat$(EXE): $(OBJ_DIR)/dat$(OBJ) $(PLUGIN_DEPS)
	$(LINK_CONSOLE_WITH_PLUGINS)

tools/dat2s$(EXE): $(OBJ_DIR)/dat2s$(OBJ) $(PLUGIN_DEPS)
	$(LINK_CONSOLE_WITH_PLUGINS)

tools/grabber$(EXE): $(OBJ_DIR)/grabber$(OBJ) $(PLUGIN_DEPS)
	$(LINK_WITH_PLUGINS)

tools/pat2dat$(EXE): $(OBJ_DIR)/pat2dat$(OBJ) $(PLUGIN_DEPS)
	$(LINK_CONSOLE_WITH_PLUGINS)



# -------- shortcuts for building various programs --------

demo: demo/demo$(EXE)
makedoc: docs/makedoc$(EXE)
keyconf: setup/keyconf$(EXE)
setup: setup/setup$(EXE)
afinfo: tests/afinfo$(EXE)
akaitest: tests/akaitest$(EXE)
digitest: tests/digitest$(EXE)
mathtest: tests/mathtest$(EXE)
miditest: tests/miditest$(EXE)
play: tests/play$(EXE)
playfli: tests/playfli$(EXE)
test: tests/test$(EXE)
vesainfo: tests/vesainfo$(EXE)
colormap: tools/colormap$(EXE)
dat: tools/dat$(EXE)
dat2s: tools/dat2s$(EXE)
exedat: tools/exedat$(EXE)
grabber: tools/grabber$(EXE)
pack: tools/pack$(EXE)
pat2dat: tools/pat2dat$(EXE)
rgbmap: tools/rgbmap$(EXE)
textconv: tools/textconv$(EXE)
examples: $(EXAMPLE_FILES)



# -------- automatic source dependencies --------

-include $(OBJ_DIR)/makefile.dep

