#
#  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.
#
#  Turbo Implib makes my job easy; no .DEF junk, heh
#
#  See makefile.all for a list of the available targets.


PLATFORM = Borland C++Builder
EXE = .exe
OBJ = .o
HTML = html

# BCC32 mimics mingw32 since it can't compile the LIBs either.

ifneq (,$(findstring /sh.exe,$(SHELL)))
  CP := command -c copy
  _S := /
  _Q := "
else
  CP := copy
  _S := $(subst \,/,/)
endif

OBJ_BASE = obj/bcc32
OBJ_BASE_D := $(subst /,\,$(OBJ_BASE)/)

    # -------- link as a DLL --------
    OBJ_DIR = $(OBJ_BASE)/$(VERSION)
    DLL_BASENAME = $(SHORT_VERSION)$(LIBRARY_VERSION).dll
    DLL_NAME = lib/bcc32/$(DLL_BASENAME)
    IMPLIB_BASENAME = $(VERSION).lib
    IMPLIB_NAME = lib/bcc32/$(IMPLIB_BASENAME)
    LIB_NAME = $(IMPLIB_NAME)

# -------- check that environment path variable is set --------
#
.PHONY: badwin

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

ifneq ($(WINSYSDIR),)
  WINDIR =$(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 -5

ifdef DEBUGMODE
    # -------- debugging build --------
    CFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS) -R -v -y
    SFLAGS = -DDEBUGMODE=$(DEBUGMODE) $(WFLAGS)
    LFLAGS += -v
else
ifdef PROFILEMODE
    # -------- profiling build --------
    CFLAGS = $(WFLAGS) $(OFLAGS) -R -v -y
    SFLAGS = $(WFLAGS)
    LFLAGS += -v
else
    # -------- optimised build --------
    CFLAGS = $(WFLAGS) $(OFLAGS)
    SFLAGS = $(WFLAGS)
    LFLAGS += -x
endif
endif



# -------- include files / directories --------
#

INCLUDE_FILES = $(addprefix include/,allegro.h winalleg.h          \
		$(addprefix allegro/,alwin.h aintern.h aintwin.h alconfig.h albcc32.h))

# -------- list which platform specific programs --------
#
VPATH = wintests

PROGRAMS = dibgrab dibhello scrsave

dibgrab: wintests/dibgrab.exe
dibhello: wintests/dibhello.exe
scrsave: wintests/scrsave.exe
#       copy wintests\scrsave.exe "$(WINDIR)\Allegro Screen Saver.scr"

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

ifdef prefix
  PREFIX  = $(prefix)
  BIN_DIR = $(PREFIX)\bin
else
  BIN_DIR = $(WINDIR)
  PREFIX := $(subst /,\,$(BCC32DIR))
endif

INC_DIR = $(PREFIX)\include
LIB_DIR = $(PREFIX)\lib

$(LIB_DIR)\$(IMPLIB_BASENAME): $(IMPLIB_NAME)
	copy lib\bcc32\$(IMPLIB_BASENAME) $(LIB_DIR)

$(INC_DIR)/%: include/%
	copy $(subst /,\,$< $@)

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

$(INC_DIR)\allegro:
	-md "$(INC_DIR)\allegro"

HEADERS = $(addprefix $(PREFIX)\,$(wildcard include\allegro/*.h))

INSTALL_FILES = $(LIB_DIR)\$(IMPLIB_BASENAME) \
		$(INC_DIR)/allegro.h \
		$(INC_DIR)/winalleg.h \
		$(INC_DIR)\allegro \
		$(HEADERS)

install: $(INSTALL_FILES)

UNINSTALL_FILES = \
      $(LIB_DIR)\alleg.lib $(LIB_DIR)\alld.lib $(LIB_DIR)\allp.lib \
      $(INC_DIR)\allegro.h $(INC_DIR)\winalleg.h $(INC_DIR)\allegro\*.h

define UNINSTALL_INCLUDES
	$(foreach ufile, $(UNINSTALL_FILES), del $(subst /,\\,$(ufile))
	)
endef

uninstall:
	$(UNINSTALL_INCLUDES)
	rd $(INC_DIR)\allegro
	@echo All gone!

#--------------------------------------------------------------------------------------------------
#
# Now, the interesting part . . .
#

# -------- use dynamic library --------

define MAKE_LIB
	command /c copy lib\\msvc\\allegro.def lib\\bcc32\\$(SHORT_VERSION)$(LIBRARY_VERSION).def
	lib /nologo /machine:ix86 /def:lib\\bcc32\\$(SHORT_VERSION)$(LIBRARY_VERSION).def /out:$(subst /,\\,$(IMPLIB_NAME).coff) /debugtype:cv /link50compat
	coff2omf $(subst /,\\,$(IMPLIB_NAME).coff $(IMPLIB_NAME))
endef

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

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

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

*/%.exe: $(OBJ_DIR)/%.o $(IMPLIB_NAME)
	tlink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\\,$<),$(subst /,\\,$@),,$(subst /,\\,$(LIB_NAME) $(LDLIBS)) cw32 import32,,

LINK_CONSOLE_DEPS = $(IMPLIB_NAME)

define LINK_CONSOLE
	tlink32 $(LFLAGS) -ap -Tpe c0x32 $(subst /,\\,$<),$(subst /,\\,$@),,$(subst /,\\,$(LIB_NAME) $(LDLIBS)) cw32 import32,,
endef

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

define GENERATE_PLUGINS_H
	copy tools\plugins\*.inc obj\bcc32\plugins.h
endef

define MAKE_PLUGIN_LIB
	$(foreach objfile, $(PLUGIN_OBJS), tlib $(subst /,\\,$(PLUGIN_LIB) +$(objfile))
	)
endef

define LINK_WITH_PLUGINS
	tlink32 $(LFLAGS) -aa -Tpe c0w32 $(subst /,\\,$<),$(subst /,\\,$@),,$(subst /,\\,$(LIB_NAME) $(LDLIBS) $(PLUGIN_LIB)) cw32 import32,,
endef

define LINK_CONSOLE_WITH_PLUGINS
	tlink32 $(LFLAGS) -ap -Tpe c0x32 $(subst /,\\,$<),$(subst /,\\,$@),,$(subst /,\\,$(LIB_NAME) $(LDLIBS) $(PLUGIN_LIB)) cw32 import32,,
endef



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

GCC2BCC = -D__BORLANDC__ -U__GNUC__ -UDJGPP -U__unix__

DEPEND_PARMS = $(GCC2BCC) -MM -MG -I. -I./include -DSCAN_DEPEND

depend:
	gcc $(DEPEND_PARMS) demo/*.c examples/*.c setup/*.c tests/*.c > _depend.tmp
	gcc $(DEPEND_PARMS) wintests/*.c tools/*.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
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/bcc32\/alleg\/\1/" _depend.tmp > $(OBJ_BASE)/alleg/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/bcc32\/alld\/\1/" _depend.tmp > $(OBJ_BASE)/alld/makefile.dep
	sed -e "s/^\([a-zA-Z0-9_]*\.o:\)/obj\/bcc32\/allp\/\1/" _depend.tmp > $(OBJ_BASE)/allp/makefile.dep
	rm _depend.tmp _depend1.tmp
