# build/Common.bmake
#
# Global build system setup file for Borland Make/BCC
#
# $Id: Common.bmake,v 1.55 2020/04/30 01:56:59 deuce Exp $
#
#############################################################################
#																			#
# Common macro setup for Borland make										#
#																			#
# Common Build Macros REQUIRED:												#
#  SRC_ROOT			- *MUST* be set to the src dir							#
#																			#
# Common Build Macros Used:													#
#  DEBUG			- Create a debug build									#
#  RELEASE			- Create a release build								#
#					  (Mutually exclusive, if both are set, RELEASE 		#
#						is cleared)											#
#  DONT_CLOBBER_CC	- Do not change the default setting of CC				#
#  OBJPATH_SUFFIX	- Suffix appended to OBJPATH useful for compiling 		#
#					  different options of the same source file				#
#																			#
# Common Build Macros Defined:												#
#  DELETE			- Delete files (Preferrably verbose)					#
#  OBJODIR			- Object output dir										#
#  LIBODIR			- Library output dir									#
#  EXEODIR			- Executable output dir									#
#  SRCODIR			- Sourc output dir										#
#  DEBUG			- Set for debug builds									#
#  RELEASE			- Set for release builds								#
#					  One of DEBUG or RELEASE is always set!				#
#  QUIET			- Target command prefix to show/not show commands		#
#  					  (Toggled off by setting VERBOSE)						#
#  CFLAGS			- Common C and C++ compiler flags						#
#  CCFLAGS			- C specific compiler flags								#
#  CXXFLAGS			- C++ specific compiler flags							#
#  LDFLAGS			- Linker flags											#
#  CC				- C compiler											#
#  CXX				- C++ compiler											#
#  EXEFILE			- Executable file extension (Includes .)				#
#  OFILE			- Object file extension (Includes .)					#
#  SOFILE			- Shared object (DLL) file extension (Includes .)		#
#  LIBFILE			- Static library file extension (Include .)				#
#  LIBPREFIX		- Prefix to library filename							#
#  LIBS				- Library names	(Appropriate for dependencies)			#
#  LIB_LDFLAGS		- Libraries appropriate for link command-line usage		#
#  COMPILE_MSG		- Message saying a target is being compiled				#
#  DIRSEP			- The directory seperator this system likes most		#
#  VERSION			- Synchronet version number in MAJOR.MINOR format		#
#					  (Numeric ONLY)										#
#  OUTPUT			- Compiler flag specifying output filename				#
#  LOUTPUT			- Linker flag specifying output filename				#
#  XPDEV_SRC		- Path to xpdev											#
#  UIFC_SRC			- Path to uifc											#
#  CIOLIB_SRC		- Path to ciolib										#
#  SMBLIB_SRC		- Path to smblib										#
#  MT_CFLAGS		- CFLAGS for building MT objects						#
#  MT_LDFLAGS		- LDFLAGS for linking MT targets						#
#  UL_PRE           - Use Library prefix (*nix is -l)                       #
#  UL_SUF           - Use Library siffix (bcc is .lib)                      #
#																			#
# Common Targets Defined:													#
#  Implicit C and C++ targets												#
#  "clean" target															# 
#  Output directory targets													#
#																			#
#############################################################################

.autodepend
.cacheautodepend

# Macros
# Set VERSION
!ifndef VERSION
 VERSION	=	3.14
!endif

# Set DEBUG
!ifdef DEBUG
! ifdef RELEASE
!  undef RELEASE
! endif
!endif

!ifndef DEBUG
! ifndef RELEASE
  DEBUG	=	1
! endif
!endif

# VERBOSE/QUIET
!ifndef VERBOSE
 QUIET	=	@
!endif

!ifndef 3RDP_ROOT
 3RDP_ROOT = $(SRC_ROOT)/../3rdp
!endif

# OS-specific
DIRSEP	=	\	# This comment is necessary
EXEFILE	=	.exe
OFILE	=	.obj
SOFILE	=	.dll
LIBFILE	=	.lib
DELETE	=	del /q
IFNOTEXIST = if not exist $@
UL_PRE	=	
UL_SUF	=	$(LIBFILE)

# Compiler-specific
!ifdef msc	# Microsoft Visual C++
CC		=	cl
LD		=	link
CCPRE	=	msvc
OUTPUT	=	-Fo
LOUTPUT	=	-Fe
CFLAGS  =	-nologo -MTd
LDFLAGS	=	$(CFLAGS)
!ifdef DEBUG
 CFLAGS	=	$(CFLAGS) -Yd
!endif

!else		# Borland C++

CC		=	bcc32
CCPRE	=	bcc
LD		=	ilink32
OUTPUT	=	-o
LOUTPUT	=	$(OUTPUT)
CFLAGS	=	-q 
LDFLAGS	=	$(CFLAGS)
CFLAGS  =	$(CFLAGS) -M -X-
MT_CFLAGS = -WM 
MT_LDFLAGS = -WM 
!ifdef DEBUG
 CFLAGS	=	$(CFLAGS) -v
!endif
!endif
MKSHLIB =       $(CC) -WD

# Common compiler flags
!ifdef DEBUG
CFLAGS	=	$(CFLAGS) -Od -D_DEBUG 
!endif

# Required for Embarcadero C++Builder 2010
!if $(__MAKE__) >= 0x0540
	CCPRE	=	ecc6
	CFLAGS	=	$(CFLAGS) -D_WIN32_WINNT=0x502
!endif
CFLAGS	=	$(CFLAGS) -DHAS_STDINT_H 

# Debug or release build?
!ifdef DEBUG
BUILD	=	debug
!else
BUILD	=	release
!endif
!ifndef BUILDPATH
BUILDPATH	=	$(BUILD)
!endif

OBJODIR =	$(CCPRE).win32.obj.$(BUILDPATH)
MTOBJODIR =	$(CCPRE).win32.obj.$(BUILDPATH)_mt
LIBODIR =	$(CCPRE).win32.lib.$(BUILDPATH)
EXEODIR =	$(CCPRE).win32.exe.$(BUILDPATH)
SRCODIR =	$(CCPRE).win32.src.$(BUILDPATH)

# Paths
XPDEV_SRC	=	$(SRC_ROOT)$(DIRSEP)xpdev
CIOLIB_SRC	=	$(SRC_ROOT)$(DIRSEP)conio
SMBLIB_SRC	=	$(SRC_ROOT)$(DIRSEP)smblib
UIFC_SRC	=	$(SRC_ROOT)$(DIRSEP)uifc

# SDL ciolib enabled by default for the moment.
!ifndef WITHOUT_SDL
USE_SDL	=	1
!endif

# Pull in lib-specific flags
!include     $(XPDEV_SRC)$(DIRSEP)Common.make
!include     $(XPDEV_SRC)$(DIRSEP)Common.bmake
!include     $(SMBLIB_SRC)$(DIRSEP)Common.make
!include     $(CIOLIB_SRC)$(DIRSEP)Common.make
!include     $(CIOLIB_SRC)$(DIRSEP)Common.bmake
!include     $(UIFC_SRC)$(DIRSEP)Common.make

.path.c		=	.
.path.cpp	=	.

!include targets.mk
!include $(SRC_ROOT)/build/rules.mk
!include objects.mk		# defines $(OBJS)

.SUFFIXES
.SUFFIXES .cpp .c
# Implicit C Compile Rule
.c{$(OBJODIR)}$(OFILE):
	$(QUIET)$(CC) $(CFLAGS) $(CCFLAGS) -n$(OBJODIR) -c $< $(OUTPUT)$@

# Implicit C++ Compile Rule
.cpp{$(OBJODIR)}$(OFILE):
	$(QUIET)$(CC) $(CFLAGS) $(CXXFLAGS) -n$(OBJODIR) -c $< $(OUTPUT)$@

# Implicit MT C Compile Rule
.c{$(MTOBJODIR)}$(OFILE):
	$(QUIET)$(CC) $(CFLAGS) $(CCFLAGS) -n$(MTOBJODIR) $(MT_CFLAGS) -c $< $(OUTPUT)$@

# Implicit MT C++ Compile Rule
.cpp{$(MTOBJODIR)}$(OFILE):
	$(QUIET)$(CC) $(CFLAGS) $(CXXFLAGS) -n$(MTOBJODIR) $(MT_CFLAGS) -c $< $(OUTPUT)$@

$(XPDEV_LIB): xpdev
xpdev:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	set USE_SDL=$(USE_SDL)
	set USE_SDL_AUDIO=$(USE_SDL_AUDIO)
	cd $(XPDEV_SRC)
	$(MAKE) -$(MAKEFLAGS) lib
	exit
|

$(XPDEV-MT_LIB): xpdev-mt
xpdev-mt:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	set USE_SDL=$(USE_SDL)
	set USE_SDL_AUDIO=$(USE_SDL_AUDIO)
	cd $(XPDEV_SRC)
	$(MAKE) -$(MAKEFLAGS) mtlib
	exit
|

$(SMBLIB): smblib
smblib:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	cd $(SMBLIB_SRC)
	$(MAKE) -$(MAKEFLAGS) lib
	exit
|

$(CIOLIB-MT): ciolib-mt
ciolib-mt:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	set USE_SDL=$(USE_SDL)
	set USE_SDL_AUDIO=$(USE_SDL_AUDIO)
	set STATIC_SDL=$(STATIC_SDL)
	cd $(CIOLIB_SRC)
	$(MAKE) -$(MAKEFLAGS) mtlib
	exit
|

$(UIFCLIB): uifc
uifc:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	set USE_SDL=$(USE_SDL)
	set USE_SDL_AUDIO=$(USE_SDL_AUDIO)
	cd $(UIFC_SRC)
	$(MAKE) -$(MAKEFLAGS) lib
	exit
|

$(UIFCLIB-MT): uifc-mt
uifc-mt:
	$(COMSPEC) <<|
	set VERSION=$(VERSION)
	set DEBUG=$(DEBUG)
	set RELEASE=$(RELEASE)
	set VERBOSE=$(VERBOSE)
	set BUILDPATH=$(BUILDPATH)
	set USE_SDL=$(USE_SDL)
	set USE_SDL_AUDIO=$(USE_SDL_AUDIO)
	set STATIC_SDL=$(STATIC_SDL)
	cd $(UIFC_SRC)
	$(MAKE) -$(MAKEFLAGS) mtlib
	exit
|
