#
#  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
GCC = gcc
EXE = .exe
OBJ = .obj
HTML = html

PLATFORM_DIR = obj/bcc32

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



# -------- 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 Your BCC32DIR environment variable is not set!
	@echo See the docs/build/bcc32.txt file!
endif

NULLSTRING :=
SPACE := $(NULLSTRING) # special magic to get an isolated space character

ifneq ($(findstring $(SPACE),$(BCC32DIR)),)
badspaces:
	@echo There are spaces in your BCC32DIR environment variable:
	@echo please change it to the 8.3 short filename version,
	@echo or move your compiler to a different directory.
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 = -w-8060
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 dxwindow scrsave wfixicon

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



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

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


ifdef UNIX_TOOLS

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

$(WINDIR_U)/$(DLL_BASENAME): lib/msvc/$(DLL_BASENAME)
	cp lib/msvc/$(DLL_BASENAME) $(WINDIR_U)

else

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

$(WINDIR_U)/$(DLL_BASENAME): lib/msvc/$(DLL_BASENAME)
	copy lib\msvc\$(DLL_BASENAME) $(WINDIR_D)

endif # UNIX_TOOLS


HEADERS = $(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/astdint.h  \
          $(BCC32DIR_U)/include/allegro/platform/alwin.h

INSTALL_FILES = $(BCC32DIR_U)/lib/$(IMPLIB_BASENAME)

ifneq ($(wildcard lib/msvc/$(DLL_BASENAME)),)
   INSTALL_FILES += $(WINDIR_U)/$(DLL_BASENAME)
endif

INSTALL_FILES += $(HEADERS)

install: generic-install
	@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              \
                  $(WINDIR_U)/alleg$(LIBRARY_VERSION).dll \
                  $(WINDIR_U)/alld$(LIBRARY_VERSION).dll  \
                  $(WINDIR_U)/allp$(LIBRARY_VERSION).dll  \
                  $(HEADERS)

uninstall: generic-uninstall
	@echo All gone!



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

.PHONY: compress

compress:
ifdef UPX_BIN
	$(UPX_BIN) demo/*.exe examples/*.exe setup/*.exe tests/*.exe tools/*.exe lib/mingw32/all*.dll
else
	@echo No executable compressor specified! You must set the environment variable
	@echo UPX_BIN to point to upx.exe.
endif



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

TEST_CPP = @echo ...integrated

include makefile.tst



# -------- 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)/%.obj: %.cpp
	bcc32 $(COMPILE_FLAGS) -W -I. -I./include -c -o$(subst /,\,$@) $(subst /,\,$<)

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

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

define DO_LINK_WITHOUT_LIB
   $(foreach objfile, $^, echo $(subst /,\\,$(objfile)) + >> obj.rsp
   )
   ilink32 $(LFLAGS) -ap -Tpe c0x32 @obj.rsp,$(subst /,\,$@),,$(LIBRARIES)
   del obj.rsp
endef
ifdef DELETE_TDS_FILES
   ifdef UNIX_TOOLS
      define LINK_WITHOUT_LIB
         $(DO_LINK_WITHOUT_LIB)
         @rm $(subst .exe,.tds,$@)
      endef
   else
      define LINK_WITHOUT_LIB
         $(DO_LINK_WITHOUT_LIB)
         @del $(subst .exe,.tds,$(subst /,\,$@))
      endef
   endif
else
   LINK_WITHOUT_LIB = $(DO_LINK_WITHOUT_LIB)
endif

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

ifdef UNIX_TOOLS
   define GENERATE_PLUGINS_H
      cat tools/plugins/*.inc > obj/bcc32/plugins.h
   endef
else
   define GENERATE_PLUGINS_H
      copy /B 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



# -------- link demo --------

demo/demo.exe: $(OBJ_DIR)/demo.obj $(OBJ_DIR)/demo.res
	ilink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\,$(OBJECTS_DEMO)),demo\demo.exe,,$(subst /,\,$(LIB_NAME) $(LIBRARIES))


# -------- 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_PARAMS = -MM -MG -I. -I./include -DSCAN_DEPEND -DALLEGRO_BCC32

depend:
	$(GCC) $(DEPEND_PARAMS) demo/*.c docs/src/makedoc/*.c examples/*.c setup/*.c tests/*.c > _depend.tmp
	$(GCC) $(DEPEND_PARAMS) tests/win/*.c tools/*.c tools/win/*.c tools/plugins/*.c >> _depend.tmp
	$(GCC) $(DEPEND_PARAMS) -x c tests/*.cpp >> _depend.tmp
	sed -e "s/^[a-zA-Z0-9_\/]*\///" _depend.tmp > _depend2.tmp
	sed -e "s/asmdef\.o/asmdef\.exe/" _depend2.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 _depend2.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 _depend2.tmp
endif
