#
#  Rules for building the Allegro library with Borland C++Builder (BCC32)
#  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 = BCC32
EXE = .exe
OBJ = .obj
HTML = html

PLATFORM_DIR = obj/bcc32



# -------- link as a DLL --------

OBJ_DIR = obj/bcc32/$(VERSION)
DLL_BASENAME = $(VERSION)$(LIBRARY_VERSION).dll
DLL_NAME = lib/bcc32/$(DLL_BASENAME)
IMPLIB_BASENAME = $(VERSION).lib
IMPLIB_NAME = lib/bcc32/$(IMPLIB_BASENAME)
LIB_NAME = $(IMPLIB_NAME)
EXPDEF_NAME = lib/bcc32/allegro.def



# -------- check that environment path variables are set --------

.PHONY: badwin badbcc32

ifeq ($(OS),Windows_NT)
   WINSYSDIR = $(SYSTEMROOT)
   ifeq ($(WINSYSDIR),)
      WINSYSDIR = $(SystemRoot)
   endif
   WINSUBDIR = system32
else
   WINSYSDIR = $(WINDIR)
   ifeq ($(WINSYSDIR),)
      WINSYSDIR = $(windir)
   endif
   WINSUBDIR = system
endif

ifneq ($(WINSYSDIR),)
   WINDIR_U = $(subst \,/,$(WINSYSDIR)/$(WINSUBDIR))
   WINDIR_D = $(subst /,\,$(WINSYSDIR)/$(WINSUBDIR))
else
badwin:
	@echo Your SYSTEMROOT or windir environment variable is not set!
endif

ifdef BCC32DIR
   BCC32DIR_U = $(subst \,/,$(BCC32DIR))
   BCC32DIR_D = $(subst /,\,$(BCC32DIR))
else
badbcc32:
	@echo You need to set BCC32DIR to the directory where Borland C++ Builder is located.
endif



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

.PHONY: _default

_default: default



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

ifdef WARNMODE
    WFLAGS = -w
else
    WFLAGS =
endif

OFLAGS = -O2 -OS

ifdef DEBUGMODE

# -------- debugging build --------
CFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -Od -q -R -v
SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
LFLAGS += -q -Gn -v

else
ifdef PROFILEMODE

# -------- profiling build --------
CFLAGS = $(WFLAGS) $(OFLAGS) -q -R -v
SFLAGS = $(WFLAGS)
LFLAGS += -q -Gn -v

else

# -------- optimised build --------
CFLAGS = $(WFLAGS) $(OFLAGS) -q
SFLAGS = $(WFLAGS)
LFLAGS += -x -q -Gn

DELETE_TDS_FILES = 1

endif
endif



# -------- list platform specific objects and programs --------

VPATH = tests/win tools/win

LIBRARIES = cw32 import32

PROGRAMS = dibgrab dibhello dibsound scrsave wfixicon

dibgrab: tests/win/dibgrab.exe
dibhello: tests/win/dibhello.exe
dibsound: tests/win/dibsound.exe
scrsave: tests/win/scrsave.scr
wfixicon: tools/win/wfixicon.exe

DISTCLEAN_FILES += $(addprefix tests/win/, dibgrab.exe dibhello.exe dibsound.exe scrsave.scr) tools/win/wfixicon.exe



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

ifdef UNIX_TOOLS

$(BCC32DIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME)
	cp lib/bcc32/$(IMPLIB_BASENAME) $(BCC32DIR_U)/lib

$(BCC32DIR_U)/include/allegro.h: include/allegro.h
	cp include/allegro.h $(BCC32DIR_U)/include

$(BCC32DIR_U)/include/winalleg.h: include/winalleg.h
	cp include/winalleg.h $(BCC32DIR_U)/include

$(BCC32DIR_U)/include/allegro:
	mkdir $(BCC32DIR_U)/include/allegro 

$(BCC32DIR_U)/include/allegro/%.h: include/allegro/%.h
	cp $< $@

$(BCC32DIR_U)/include/allegro/internal:
	mkdir $(BCC32DIR_U)/include/allegro/internal

$(BCC32DIR_U)/include/allegro/internal/%.h: include/allegro/internal/%.h
	cp $< $@

$(BCC32DIR_U)/include/allegro/inline:
	mkdir $(BCC32DIR_U)/include/allegro/inline

$(BCC32DIR_U)/include/allegro/inline/%.inl: include/allegro/inline/%.inl
	cp $< $@

$(BCC32DIR_U)/include/allegro/platform:
	mkdir $(BCC32DIR_U)/include/allegro/platform

$(BCC32DIR_U)/include/allegro/platform/%.h: include/allegro/platform/%.h
	cp $< $@

else

$(BCC32DIR_U)/lib/$(IMPLIB_BASENAME): $(IMPLIB_NAME)
	copy lib\bcc32\$(IMPLIB_BASENAME) $(BCC32DIR_D)\lib

$(BCC32DIR_U)/include/allegro.h: include/allegro.h
	copy include\allegro.h $(BCC32DIR_D)\include

$(BCC32DIR_U)/include/winalleg.h: include/winalleg.h
	copy include\winalleg.h $(BCC32DIR_D)\include

$(BCC32DIR_U)/include/allegro:
	md $(BCC32DIR_D)\include\allegro 

$(BCC32DIR_U)/include/allegro/%.h: include/allegro/%.h
	copy $(subst /,\,$< $@)

$(BCC32DIR_U)/include/allegro/internal:
	md $(BCC32DIR_D)\include\allegro\internal

$(BCC32DIR_U)/include/allegro/internal/%.h: include/allegro/internal/%.h
	copy $(subst /,\,$< $@)

$(BCC32DIR_U)/include/allegro/inline:
	md $(BCC32DIR_D)\include\allegro\inline

$(BCC32DIR_U)/include/allegro/inline/%.inl: include/allegro/inline/%.inl
	copy $(subst /,\,$< $@)

$(BCC32DIR_U)/include/allegro/platform:
	md $(BCC32DIR_D)\include\allegro\platform

$(BCC32DIR_U)/include/allegro/platform/%.h: include/allegro/platform/%.h
	copy $(subst /,\,$< $@)

endif # UNIX_TOOLS


HEADERS = $(addprefix $(BCC32DIR_U)/,$(wildcard include/allegro/*.h))          \
          $(addprefix $(BCC32DIR_U)/,$(wildcard include/allegro/internal/*.h)) \
          $(addprefix $(BCC32DIR_U)/,$(wildcard include/allegro/inline/*.inl))

INSTALL_FILES = $(BCC32DIR_U)/lib/$(IMPLIB_BASENAME)              \
		$(BCC32DIR_U)/include/allegro.h                   \
		$(BCC32DIR_U)/include/allegro                     \
		$(BCC32DIR_U)/include/allegro/internal            \
		$(BCC32DIR_U)/include/allegro/inline              \
		$(BCC32DIR_U)/include/allegro/platform            \
		$(BCC32DIR_U)/include/winalleg.h                  \
		$(BCC32DIR_U)/include/allegro/platform/aintwin.h  \
		$(BCC32DIR_U)/include/allegro/platform/al386gcc.h \
		$(BCC32DIR_U)/include/allegro/platform/albcc32.h  \
		$(BCC32DIR_U)/include/allegro/platform/alplatf.h  \
		$(BCC32DIR_U)/include/allegro/platform/alwin.h    \
		$(HEADERS)

install: $(INSTALL_FILES)
ifneq ($(wildcard lib/msvc/$(DLL_BASENAME)),)
ifdef UNIX_TOOLS
	cp lib/msvc/$(DLL_BASENAME) $(WINDIR_U)
else
	copy lib\msvc\$(DLL_BASENAME) $(WINDIR_D)
endif
endif
	@echo The $(DESCRIPTION) $(PLATFORM) library has been installed.

UNINSTALL_FILES = $(BCC32DIR_U)/lib/alleg.lib                \
		  $(BCC32DIR_U)/lib/alld.lib                 \
		  $(BCC32DIR_U)/lib/allp.lib                 \
		  $(BCC32DIR_U)/include/allegro.h            \
		  $(BCC32DIR_U)/include/winalleg.h           \
		  $(BCC32DIR_U)/include/allegro/*.h          \
		  $(BCC32DIR_U)/include/allegro/internal/*.h \
		  $(BCC32DIR_U)/include/allegro/inline/*.inl \
		  $(BCC32DIR_U)/include/allegro/platform/*.h \
		  $(WINDIR_U)/alleg$(LIBRARY_VERSION).dll    \
		  $(WINDIR_U)/alld$(LIBRARY_VERSION).dll     \
		  $(WINDIR_U)/allp$(LIBRARY_VERSION).dll

uninstall:
ifdef UNIX_TOOLS
	-rm -fv $(UNINSTALL_FILES)
	-rmdir $(BCC32DIR_U)/include/allegro/platform
	-rmdir $(BCC32DIR_U)/include/allegro/inline
	-rmdir $(BCC32DIR_U)/include/allegro/internal
	-rmdir $(BCC32DIR_U)/include/allegro
else
   define RM_FILES
      $(foreach file, $(wildcard $(UNINSTALL_FILES)), del $(subst /,\,$(file))
      )
   endef
	-$(RM_FILES)
	-rd $(BCC32DIR_D)\include\allegro\platform
	-rd $(BCC32DIR_D)\include\allegro\inline
	-rd $(BCC32DIR_D)\include\allegro\internal
	-rd $(BCC32DIR_D)\include\allegro
endif
	@echo All gone!



# -------- link as a DLL (sort of ;-) --------

ifdef UNIX_TOOLS
   define MAKE_LIB
      echo LIBRARY     $(DLL_BASENAME) > _allegro.def
      cat _allegro.def $(EXPDEF_NAME) > _allegr2.def
      implib $(subst /,\,$(IMPLIB_NAME)) _allegr2.def
      rm _allegro.def _allegr2.def
   endef
else
   define MAKE_LIB
      echo LIBRARY     $(DLL_BASENAME) > _allegro.def
      copy _allegro.def + $(subst /,\,$(EXPDEF_NAME)) _allegr2.def
      implib $(subst /,\,$(IMPLIB_NAME)) _allegr2.def
      del _allegro.def
      del _allegr2.def
   endef
endif

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

$(OBJ_DIR)/%.obj: %.c
	bcc32 $(COMPILE_FLAGS) -W -I. -I./include -c -o$(subst /,\,$@) $(subst /,\,$<)

$(OBJ_DIR)/%.res: %.rc
	brcc32 -i $(BCC32DIR)\include -fo $(subst /,\,$@) $(subst /,\,$<)

tests/win/dibsound.exe: $(OBJ_DIR)/dibsound.obj $(OBJ_DIR)/dibsound.res $(IMPLIB_NAME)
	@echo $(subst /,\\,$(OBJ_DIR)/dibsound.res) > res.rsp
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$(OBJ_DIR)/dibsound.obj),tests\win\dibsound.exe,,$(subst /,\,$(LIB_NAME) $(LIBRARIES)),,@res.rsp
ifdef UNIX_TOOLS
	@rm res.rsp
   ifdef DELETE_TDS_FILES
	@rm tests/win/dibsound.tds
   endif
else
	@del res.rsp
   ifdef DELETE_TDS_FILES
	@del tests\win\dibsound.tds
   endif
endif

tests/win/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME)
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(LIBRARIES))
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
	@rm $(subst .exe,.tds,$@)
   else
	@del $(subst .exe,.tds,$(subst /,\,$@))
   endif
endif

tests/win/scrsave.scr: $(OBJ_DIR)/scrsave.obj $(OBJ_DIR)/scrsave.res $(IMPLIB_NAME)
	@echo $(subst /,\\,$(OBJ_DIR)/scrsave.res) > res.rsp
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$(OBJ_DIR)/scrsave.obj),tests\win\scrsave.scr,,$(subst /,\,$(LIB_NAME) $(LIBRARIES)),,@res.rsp
ifdef UNIX_TOOLS
	@rm res.rsp
   ifdef DELETE_TDS_FILES
	@rm tests/win/scrsave.tds
   endif
else
	@del res.rsp
   ifdef DELETE_TDS_FILES
	@del tests\win\scrsave.tds
   endif
endif

tools/win/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME)
	ilink32 $(LFLAGS) -ap -Tpe c0x32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(LIBRARIES))
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
	@rm $(subst .exe,.tds,$@)
   else
	@del $(subst .exe,.tds,$(subst /,\,$@))
   endif
endif

*/%.exe: $(OBJ_DIR)/%.obj $(IMPLIB_NAME)
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(LIBRARIES))
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
	@rm $(subst .exe,.tds,$@)
   else
	@del $(subst .exe,.tds,$(subst /,\,$@))
   endif
endif

LINK_CONSOLE_DEPS = $(IMPLIB_NAME)

define DO_LINK_CONSOLE
   ilink32 $(LFLAGS) -ap -Tpe c0x32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(LIBRARIES))
endef
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
      define LINK_CONSOLE
         $(DO_LINK_CONSOLE)
         @rm $(subst .exe,.tds,$@)
      endef
   else
      define LINK_CONSOLE
         $(DO_LINK_CONSOLE)
         @del $(subst .exe,.tds,$(subst /,\,$@))
      endef
   endif
else
   LINK_CONSOLE = $(DO_LINK_CONSOLE)
endif

PLUGIN_LIB = lib/bcc32/$(VERY_SHORT_VERSION)dat.lib
PLUGINS_H = obj/bcc32/plugins.h
PLUGIN_DEPS = $(LIB_NAME) $(PLUGIN_LIB)
PLUGIN_SCR = scx

ifdef UNIX_TOOLS
   define GENERATE_PLUGINS_H
      cat tools/plugins/*.inc > obj/bcc32/plugins.h
   endef
else
   define GENERATE_PLUGINS_H
      copy tools\plugins\*.inc obj\bcc32\plugins.h
   endef
endif

ifdef UNIX_TOOLS
   define MAKE_PLUGIN_LIB
      $(foreach objfile, $(PLUGIN_OBJS), tlib $(subst /,\,$(PLUGIN_LIB) + $(objfile))
      )
      @rm $(subst .lib,.BAK,$(PLUGIN_LIB))
   endef
else
   define MAKE_PLUGIN_LIB
      $(foreach objfile, $(PLUGIN_OBJS), tlib $(subst /,\,$(PLUGIN_LIB) + $(objfile))
      )
      @del $(subst .lib,.BAK,$(subst /,\,$(PLUGIN_LIB)))
   endef
endif

define DO_LINK_WITH_PLUGINS
   ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(PLUGIN_LIB) $(LIBRARIES))
endef
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
      define LINK_WITH_PLUGINS
         $(DO_LINK_WITH_PLUGINS)
         @rm $(subst .exe,.tds,$@)
      endef
   else
      define LINK_WITH_PLUGINS
         $(DO_LINK_WITH_PLUGINS)
         @del $(subst .exe,.tds,$(subst /,\,$@))
      endef
   endif
else
   LINK_WITH_PLUGINS = $(DO_LINK_WITH_PLUGINS)
endif

define DO_LINK_CONSOLE_WITH_PLUGINS
   ilink32 $(LFLAGS) -ap -Tpe c0x32 $(subst /,\,$<),$(subst /,\,$@),,$(subst /,\,$(LIB_NAME) $(PLUGIN_LIB) $(LIBRARIES))
endef
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
      define LINK_CONSOLE_WITH_PLUGINS
         $(DO_LINK_CONSOLE_WITH_PLUGINS)
         @rm $(subst .exe,.tds,$@)
      endef
   else
      define LINK_CONSOLE_WITH_PLUGINS
         $(DO_LINK_CONSOLE_WITH_PLUGINS)
         @del $(subst .exe,.tds,$(subst /,\,$@))
      endef
   endif
else
   LINK_CONSOLE_WITH_PLUGINS = $(DO_LINK_CONSOLE_WITH_PLUGINS)
endif



# -------- demo program iconification --------

.PHONY: fixdemo

$(OBJ_DIR)/demo.res: demo/demo.dat tools/win/wfixicon.exe
	tools/win/wfixicon.exe $(OBJ_DIR)/demo.ico -ro -d demo/demo.dat SHIP3 GAME_PAL

fixdemo: $(OBJ_DIR)/demo.obj $(OBJ_DIR)/demo.res $(IMPLIB_NAME)
	@echo $(subst /,\\,$(OBJ_DIR)/demo.res) > res.rsp
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$(OBJ_DIR)/demo.obj),demo\demo.exe,,$(subst /,\,$(LIB_NAME) $(LIBRARIES)),,@res.rsp
ifdef UNIX_TOOLS
	@rm res.rsp
   ifdef DELETE_TDS_FILES
	@rm demo/demo.tds
   endif
else
	@del res.rsp
   ifdef DELETE_TDS_FILES
	@del demo\demo.tds
   endif
endif



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

DEPEND_PARMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_BCC32

depend:
	gcc $(DEPEND_PARMS) demo/*.c examples/*.c setup/*.c tests/*.c > _depend.tmp
	gcc $(DEPEND_PARMS) tests/win/*.c tools/*.c tools/win/*.c tools/plugins/*.c >> _depend.tmp
	sed -e 's/\\/\//g' -e 's/\/$$/\\/' -e "s/^[a-zA-Z0-9_\/]*\///" -e "s/ \.\// /g" _depend.tmp > _depend1.tmp
	sed -e "s/asmdef\.o/asmdef\.exe/" _depend1.tmp > _depend.tmp
ifdef UNIX_TOOLS
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/alleg\/\1\.obj:/" _depend.tmp > obj/bcc32/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/alld\/\1\.obj:/" _depend.tmp > obj/bcc32/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/allp\/\1\.obj:/" _depend.tmp > obj/bcc32/allp/makefile.dep
	rm _depend.tmp _depend1.tmp
else
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/alleg\/\1\.obj:/" _depend.tmp > obj\bcc32\alleg\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/alld\/\1\.obj:/" _depend.tmp > obj\bcc32\alld\makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\)\.o:/obj\/bcc32\/allp\/\1\.obj:/" _depend.tmp > obj\bcc32\allp\makefile.dep
	del _depend.tmp
	del _depend1.tmp
endif
