# Genesis, the ColdC driver

# Borland can embed autodependency information inside object files - very nice
.AUTODEPEND

# The name of the program to invoke yacc
YACC = yacc
# Flags to YACC - bison would use -y -d
YFLAGS = -d

BCCROOT = $(MAKEDIR)\..
CC = $(BCCROOT)\bin\bcc32
LD = $(BCCROOT)\bin\ilink32
AR = $(BCCROOT)\bin\tlib

# Version info generated by version.bat
!include <version.cfg>
# Version info generated by systype.bat
!include <systype.cfg>

# The location of the Genesis distribution 
# Here we assume directory just below the current directory
DSTDIR = ..
# The distribution source directory
SD  = $(DSTDIR)\src
 
# Include directories 
BCCINCDIRS = -I$(BCCROOT)\include
COLDINCDIRS = -I$(SD)\include;$(SD)\modules
NDBMINCDIRS = -I$(DN)
INCDIRS = $(BCCINCDIRS) $(NDBMINCDIRS) $(COLDINCDIRS) 

# Compiler directives for debugging
!ifdef DEBUG
DEBUG_CFLAGS = -v -y -Od -r- 
!else
DEBUG_CFLAGS = -v- -O2 
!endif
!ifdef STATIC
STATIC_CFLAGS =
!else
STATIC_CFLAGS = -tWR
!endif

# Compiler directives
MACRO_CFLAGS = -DWIN32 -DWIN32_LEAN_AND_MEAN -D_NO_VCL  
CFLAGS = $(STATIC_CFLAGS) -tWC -q -w-pia $(DEBUG_CFLAGS) -$(PROCESSOR_LEVEL) $(MACRO_CFLAGS)

# Linker directives for debugging
!ifdef DEBUG
DEBUG_LFLAGS = -v
!else
DEBUG_LFLAGS =
!endif

# Linker directives
LFLAGS = -Tpe -ap -c -Gn -q $(DEBUG_LFLAGS) 

# Borland libraries
!ifdef STATIC
LIBS      = import32.lib cw32.lib ws2_32.lib
!else
LIBS      = import32.lib cw32i.lib ws2_32.lib
!endif
BCC32STARTUP = c0x32.obj

# ndbm parts
DN      = ndbm
NDBM_OBJ = $(DN)\ndbm.obj $(DN)\hash.obj $(DN)\hash_buf.obj $(DN)\hash_page.obj \
           $(DN)\hash_bigkey.obj $(DN)\hash_log2.obj $(DN)\hash_func.obj 
NDBM_LIB = $(DN)\ndbm.lib
GDBM_LIB = wingdbm.lib

# objects to be compiled
DD      = $(SD)\data
DATA_OBJ  = $(DD)\data.obj $(DD)\buffer.obj $(DD)\dict.obj $(DD)\ident.obj \
            $(DD)\list.obj $(DD)\object.obj $(DD)\string.obj \
	    $(DD)\handled_frob.obj $(DD)\quickhash.obj
DO      = $(SD)\ops
OPS_OBJ   = $(DO)\operators.obj $(DO)\buffer.obj $(DO)\error.obj $(DO)\list.obj \
            $(DO)\object.obj $(DO)\dict.obj $(DO)\string.obj $(DO)\data.obj $(DO)\sys.obj \
            $(DO)\misc.obj $(DO)\task.obj $(DO)\file.obj $(DO)\network.obj $(DO)\math.obj
DM      = $(SD)\modules
MOD_OBJ = $(DM)\cdc.obj $(DM)\cdc_buffer.obj $(DM)\cdc_dict.obj $(DM)\cdc_list.obj \
          $(DM)\cdc_misc.obj $(DM)\cdc_string.obj $(DM)\cdc_integer.obj $(DM)\web.obj \
          $(DM)\ext_math.obj
GRM_OBJ = $(SD)\grammar.obj
DB_OBJ   = $(SD)\cache.obj $(SD)\binarydb.obj $(SD)\dbpack.obj \
           $(SD)\decode.obj $(SD)\lookup.obj
IO_OBJ   = $(SD)\io.obj $(SD)\log.obj $(SD)\net.obj $(SD)\file.obj
MISC_OBJ = $(SD)\strutil.obj $(SD)\memory.obj $(SD)\sig.obj \
           $(SD)\util.obj $(SD)\regexp.obj $(SD)\defs.obj $(SD)\dns.obj
CRYPT_OBJ = $(SD)\shs.obj $(SD)\crypt.obj
PCODE_OBJ = $(SD)\codegen.obj $(SD)\execute.obj $(SD)\opcodes.obj \
            $(SD)\token.obj $(SD)\native.obj
        
COMMON_OBJ = $(DATA_OBJ) $(DB_OBJ) $(IO_OBJ) $(MISC_OBJ) \
             $(PCODE_OBJ) $(OPS_OBJ) $(MOD_OBJ) $(CRYPT_OBJ)
DRIVER_OBJ = $(GRM_OBJ) $(COMMON_OBJ) $(SD)\genesis.obj 
COMPILER_OBJ = $(GRM_OBJ) $(COMMON_OBJ) $(SD)\coldcc.obj $(SD)\textdb.obj 
PARSER_SRC = $(SD)\grammar.c $(SD)\include\parse.h

# The final targets
DRIVER = genesis.exe
COMPILER = coldcc.exe

# default make rule
all: $(GRM_OBJ) $(NDBM_LIB) $(COMMON_OBJ) $(DRIVER) $(COMPILER) 

# Rule to link genesis driver
$(DRIVER): $(DRIVER_OBJ)
  @echo Linking driver...
  $(LD) $(LFLAGS) $(BCC32STARTUP) $(DRIVER_OBJ), $<,, $(LIBS) $(NDBM_LIB)

# Rule to link coldcc compiler
$(COMPILER): $(COMPILER_OBJ)
  @echo Linking compile...
  $(LD) $(LFLAGS) $(BCC32STARTUP) $(COMPILER_OBJ), $<,, $(LIBS) $(NDBM_LIB)

# Rule for compiling grammar
$(PARSER_SRC): $(SD)\grammar.y
  @echo Generating parser source...
  @$(YACC) $(YFLAGS) $(SD)\grammar.y
  @move y.tab.c $(SD)\grammar.c
  @move y.tab.h $(SD)\include\parse.h

# Clean up the products of compilation and linking
clean:
  @echo Cleaning...
  -@del $(PARSER_SRC) $(DRIVER_OBJ) $(COMPILER_OBJ) 2>NUL
  -@del $(NDBM_LIB) $(NDBM_OBJ) 2>NUL
  -@del $(DRIVER) $(COMPILER) 2>NUL
  -@del *.tds *.tmp 2>NUL
  -@del y.tab.* 2>NUL

# Clean up the products of the configure.bat script
cleanconfig:
  @echo Cleaning configuration...
  -@del version.cfg systype.cfg version.h systype.h 2>NUL
  
# Install the genesis driver and coldcc compiler to the supplied directory  
install: $(DRIVER) $(COMPILER) 
  @echo Installing...
!ifdef INSTDIR
  -@if not exist $(INSTDIR) echo Installation directory [$(INSTDIR)] does not exist.
  @&copy $** $(INSTDIR) 2>NUL 1>NUL
  @for /f %f in ('dir ..\bin /b /a-d') do @copy ..\bin\%f $(INSTDIR) 2>NUL 1>NUL
!else
  @echo Installation directory not defined use - make -DINSTDIR=C:\mydir install
!endif

# Install the genesis driver and coldcc compiler to the supplied directory  
test: $(DRIVER) $(COMPILER) 
  @runtests.bat

# Rule for building ndbm library
$(NDBM_LIB): $(NDBM_OBJ)
  @echo "making ndbm library..."
  -@del $(NDBM_LIB) 2>NUL
  $(AR) /C $(NDBM_LIB) @&&!
+ $(**: = &^
+ )
!

# Implicit rule for all C compiles
.c.obj:
  $(CC) $(CFLAGS) $(INCDIRS) -c -n$(@D) $<
  
# Some overrides for specific files follow

# regexp.h is a borland C++ header so we switch the order of includes for this file - yuck
$(SD)\regexp.obj: $(SD)\regexp.c
  $(CC) $(CFLAGS) $(COLDINCDIRS) $(BCCINCDIRS) -c -n$(@D) $(SD)\regexp.c

# list.h is a borland C++ header so we switch the order of includes for this file - yuck
$(DM)\ext_math.obj: $(DM)\ext_math.c
  $(CC) $(CFLAGS) $(COLDINCDIRS) $(BCCINCDIRS) -c -n$(@D) $(DM)\ext_math.c