#
#  Rules for building the Allegro library with djgpp. This file is included
#  by the primary makefile, and should not be used directly.
#
#  The "depend" target uses sed.
#
#  See makefile.all for a list of the available targets.



# -------- define some variables that the primary makefile will use --------

PLATFORM = djgpp
CC = gcc
EXE = .exe
OBJ = .o

PLATFORM_DIR = obj/djgpp

OBJ_DIR = obj/djgpp/$(VERSION)
LIB_NAME = lib/djgpp/lib$(VERSION).a

ifdef CROSSCOMPILE
   UNIX_TOOLS = 1
endif

ifneq (,$(findstring bash,$(SHELL)))
   UNIX_TOOLS = 1
endif



# -------- autodetect whether to use LFN --------

ifdef CROSSCOMPILE
   HTML = html
   INFO = info
   TEXI = texi
else
ifeq ($(LFN),y)
   HTML = html
   INFO = info
   TEXI = texi
else
   HTML = htm
   INFO = inf
   TEXI = txi
endif
endif



# -------- run some sanity checks on the djgpp installation --------

.PHONY: baddjgpp baddjdev badgcc badbnu badmake badtxi badpath badalleg



# -------- check that the DJGPP environment variable is set --------

ifndef DJDIR
baddjgpp:
	@echo Your DJGPP environment variable is not set correctly! It should
	@echo point to the djgpp.env file: see the djgpp readme.1st for details.
endif

DJDIR_U = $(subst \,/,$(DJDIR))
DJDIR_D = $(subst /,\,$(DJDIR))



ifndef CROSSCOMPILE

# -------- check that the djdev package is installed --------

ifeq ($(wildcard $(DJDIR_U)/bin/djasm.exe),)
baddjdev:
	@echo Missing djgpp package! You need to install djdev203.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif



# -------- check that the gcc package is installed --------

ifeq ($(wildcard $(DJDIR_U)/bin/gcc.exe),)
badgcc:
	@echo Missing djgpp package! You need to install gcc2952b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.

endif



# -------- check that the binutils package is installed --------

ifeq ($(wildcard $(DJDIR_U)/bin/ld.exe),)
badbnu:
	@echo Missing djgpp package! You need to install bnu210b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif



# -------- check that the make package is installed --------

ifeq ($(wildcard $(DJDIR_U)/bin/make.exe),)
badmake:
	@echo Missing djgpp package! You need to install mak3791b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory.
endif



# -------- check that the texinfo package is installed --------

ifeq ($(wildcard $(DJDIR_U)/bin/makeinfo.exe),)
badtxi:
	@echo Missing djgpp package! You need to install txi40b.zip (or whatever the
	@echo latest version is). Download this from wherever you got djgpp, and unzip
	@echo it into the root of your djgpp directory. If you do not need the Info
	@echo documentation, run make all to ignore this error.
endif



# -------- check that djgpp/bin is pathed --------

ifeq ($(wildcard $(addsuffix /djasm.exe,$(subst ;, ,$(PATH)))),)
badpath:
	@echo Your PATH is not set correctly! This must include the
	@echo djgpp bin directory: see the djgpp readme.1st for details.
endif



# -------- check that Allegro has a good directory structure --------

ifeq ($(wildcard src/allegro.c),)
badalleg:
	@echo Bad Allegro installation! You did not preserve the directory structure
	@echo while unzipping it: did you remember to use the -d flag with pkunzip?
endif

endif



# -------- give a sensible default target for make without any args --------

.PHONY: _default

_default: default



# -------- decide what compiler options to use --------

ifdef WARNMODE
   WFLAGS = -Wall -W -Wstrict-prototypes -Wno-unused -Werror
else
   WFLAGS = -Wall -Wno-unused
endif


ifdef TARGET_ARCH_COMPAT
   TARGET_ARCH = $(GCC_MTUNE)=$(TARGET_ARCH_COMPAT)
else
   ifdef TARGET_ARCH_EXCL
      TARGET_ARCH = -march=$(TARGET_ARCH_EXCL)
   else
      TARGET_ARCH = $(GCC_MTUNE)=i586
   endif
endif

ifndef TARGET_OPTS
   TARGET_OPTS = -O2 -funroll-loops -ffast-math
endif

OFLAGS = $(TARGET_ARCH) $(TARGET_OPTS)

CFLAGS = -DALLEGRO_LIB_BUILD



ifdef DEBUGMODE

# -------- debugging build --------

CFLAGS += -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -g
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
LFLAGS = -g

else
ifdef PROFILEMODE

# -------- profiling build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -pg
SFLAGS = $(WFLAGS)
LFLAGS = -pg

else

# -------- optimised build --------
CFLAGS += $(WFLAGS) $(OFLAGS) -fomit-frame-pointer
SFLAGS = $(WFLAGS)

ifndef SYMBOLMODE
   LFLAGS = -s
endif

endif
endif



# -------- list which platform specific objects to include --------

VPATH = src/dos src/misc 

ifdef ALLEGRO_USE_C

# ------ build a C-only version ------

VPATH += src/c
MY_OBJECTS = $(C_OBJECTS) cmiscs
CFLAGS += -DALLEGRO_NO_ASM

else

# ------ build the normal asm version ------

VPATH += src/i386
MY_OBJECTS = $(I386_OBJECTS)

endif # ALLEGRO_USE_C

OBJECT_LIST = $(COMMON_OBJECTS) $(MY_OBJECTS) \
	      $(basename $(notdir $(ALLEGRO_SRC_DOS_FILES))) \
	      djirq djirqs



# -------- rules for installing and removing the library files --------

INSTALLDIR = $(DJDIR_U)
LIBDIR = lib
INCDIR = include

ifdef UNIX_TOOLS

$(DJDIR_U)/lib/lib$(VERSION).a: $(LIB_NAME) $(DJDIR_U)/lib
	cp lib/djgpp/lib$(VERSION).a $(DJDIR_U)/lib

$(DJDIR_U)/info/allegro.$(INFO): docs/info/allegro.$(INFO)
	-cp docs/info/allegro.$(INFO) $(DJDIR_U)/info
	-install-info $(DJDIR_U)/info/allegro.$(INFO) $(DJDIR_U)/info/dir

else

$(DJDIR_U)/lib/lib$(VERSION).a: $(LIB_NAME)
	copy lib\djgpp\lib$(VERSION).a $(DJDIR_D)\lib

$(DJDIR_U)/info/allegro.$(INFO): docs/info/allegro.$(INFO)
	-copy docs\info\allegro.$(INFO) $(DJDIR_D)\info
	-install-info $(DJDIR_U)/info/allegro.$(INFO) $(DJDIR_U)/info/dir

endif


HEADERS = $(DJDIR_U)/include/allegro/platform/aintdos.h  \
          $(DJDIR_U)/include/allegro/platform/al386gcc.h \
          $(DJDIR_U)/include/allegro/platform/aldjgpp.h  \
          $(DJDIR_U)/include/allegro/platform/alplatf.h  \
          $(DJDIR_U)/include/allegro/platform/astdint.h  \
          $(DJDIR_U)/include/allegro/platform/aldos.h

INSTALL_FILES = $(DJDIR_U)/lib/lib$(VERSION).a  \
                $(DJDIR_U)/info/allegro.$(INFO) \
                $(HEADERS)

install: generic-install
	@echo The $(DESCRIPTION) $(PLATFORM) library has been installed.

UNINSTALL_FILES = $(DJDIR_U)/lib/liballeg.a       \
                  $(DJDIR_U)/lib/liballd.a        \
                  $(DJDIR_U)/lib/liballp.a        \
                  $(DJDIR_U)/info/allegro.$(INFO) \
                  $(HEADERS)

uninstall: generic-uninstall
	-install-info --delete $(DJDIR_U)/info/allegro.$(INFO) $(DJDIR_U)/info/dir
	@echo All gone!



# -------- helper function for compressing the executables --------

ifneq ($(wildcard $(DJDIR_U)/bin/upx.exe),)
   DJP = $(DJDIR_U)/bin/upx.exe
else
ifneq ($(wildcard $(DJDIR_U)/bin/djp.exe),)
   DJP = $(DJDIR_U)/bin/djp.exe -s
endif
endif

.PHONY: compress

compress: $(PROGRAMS)
    ifdef DJP
	$(DJP) demo/*.exe examples/*.exe setup/*.exe tests/*.exe tools/*.exe
    else
	@echo No executable compressor found! This target requires either the
	@echo DJP or UPX utilities to be installed in your djgpp bin directory.
    endif


# -------- test capabilities --------

TEST_CPP = $(CC) -fno-exceptions -c src/misc/test.cpp -o $(PLATFORM_DIR)/test.o

include makefile.tst



# -------- finally, we get to the fun part... --------

define MAKE_LIB
   ar rs $(LIB_NAME) $(OBJECTS)
endef

COMPILE_FLAGS = $(subst src/,-DALLEGRO_SRC ,$(findstring src/, $<))$(CFLAGS)

$(OBJ_DIR)/%.o: %.c
	$(CC) $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.cpp
	$(CC) -fno-exceptions $(COMPILE_FLAGS) -I. -I./include -o $@ -c $<

$(OBJ_DIR)/%.o: %.s
	$(CC) $(SFLAGS) -I. -I./include -x assembler-with-cpp -o $@ -c $<

ifndef CROSSCOMPILE

obj/djgpp/setupdat.s: setup/setup.dat tools/dat2s.exe
	tools/dat2s.exe -p setup -o obj/djgpp/setupdat.s setup/setup.dat

obj/djgpp/setupdat.o: obj/djgpp/setupdat.s
	$(CC) -o obj/djgpp/setupdat.o -c obj/djgpp/setupdat.s

$(OBJ_DIR)/setup.o: setup/setup.c
	$(CC) $(COMPILE_FLAGS) -DSETUP_USE_COMPILED_DATAFILES -I. -I./include -o $(OBJ_DIR)/setup.o -c setup/setup.c

setup/setup.exe: $(OBJ_DIR)/setup.o obj/djgpp/setupdat.o $(LIB_NAME)
	$(CC) $(LFLAGS) -o setup/setup.exe $(OBJ_DIR)/setup.o obj/djgpp/setupdat.o $(LIB_NAME)

endif

demo/demo.exe: $(OBJECTS_DEMO)
	$(CC) $(LFLAGS) -o $@ $(OBJECTS_DEMO) $(LIB_NAME)

*/%.exe: $(OBJ_DIR)/%.o $(LIB_NAME)
	$(CC) $(LFLAGS) -o $@ $< $(LIB_NAME)

ifdef CROSSCOMPILE

obj/djgpp/asmdef.s: src/i386/asmdef.c include/*.h include/allegro/*.h obj/djgpp/asmcapa.h
	$(CC) $(WFLAGS) -I. -I./include -S -o obj/djgpp/asmdef.s src/i386/asmdef.c

obj/djgpp/asmdef.inc: obj/djgpp/asmdef.s
	if [ ! -x misc/asmdef.sh ]; then chmod a+x misc/asmdef.sh; fi
	misc/asmdef.sh obj/djgpp/asmdef.s obj/djgpp/asmdef.inc

else

obj/djgpp/asmdef.inc: obj/djgpp/asmdef.exe
	obj/djgpp/asmdef.exe obj/djgpp/asmdef.inc

obj/djgpp/asmdef.exe: src/i386/asmdef.c include/*.h include/allegro/*.h obj/djgpp/asmcapa.h
	$(CC) -O $(WFLAGS) -I. -I./include -o obj/djgpp/asmdef.exe src/i386/asmdef.c

endif

define LINK_WITHOUT_LIB
   $(CC) $(LFLAGS) -o $@ $^
endef

PLUGIN_LIB = lib/djgpp/lib$(VERY_SHORT_VERSION)dat.a
PLUGIN_DEPS = $(LIB_NAME) $(PLUGIN_LIB)
PLUGIN_SCR = scr

ifdef UNIX_TOOLS
   define GENERATE_PLUGINS_H
      cat tools/plugins/*.inc > $(PLUGINS_H)
   endef
else
   define GENERATE_PLUGINS_H
      copy /B tools\plugins\*.inc $(subst /,\,$(PLUGINS_H))
   endef
endif

define MAKE_PLUGIN_LIB
   ar rs $(PLUGIN_LIB) $(PLUGIN_OBJS)
endef

define LINK_WITH_PLUGINS
   $(CC) $(LFLAGS) -o $@ $< $(strip $(PLUGIN_LIB) $(addprefix @,$(PLUGIN_SCRIPTS)) $(LIB_NAME))
endef



# -------- generate automatic dependencies --------

DEPEND_PARAMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_DJGPP

depend:
	$(CC) $(DEPEND_PARAMS) src/*.c src/dos/*.c src/c/*.c src/i386/*.c src/misc/*.c demo/*.c > _depend.tmp
	$(CC) $(DEPEND_PARAMS) docs/src/makedoc/*.c examples/*.c setup/*.c tests/*.c tools/*.c tools/plugins/*.c >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x c tests/*.cpp >> _depend.tmp
	$(CC) $(DEPEND_PARAMS) -x assembler-with-cpp src/i386/*.s src/c/*.s src/dos/*.s src/misc/*.s >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
ifdef UNIX_TOOLS
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/alleg\/\1/" _depend2.tmp > obj/djgpp/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/alld\/\1/" _depend2.tmp > obj/djgpp/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/allp\/\1/" _depend2.tmp > obj/djgpp/allp/makefile.dep
	rm _depend.tmp _depend2.tmp
else
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/alleg\/\1/" _depend2.tmp > obj\djgpp\alleg\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/alld\/\1/" _depend2.tmp > obj\djgpp\alld\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/djgpp\/allp\/\1/" _depend2.tmp > obj\djgpp\allp\makefile.dep
	del _depend.tmp
	del _depend2.tmp
endif
