# COPYRIGHT(c) 1996 Bell Laboratories, Lucent Technologies
#
# this is the win32-specific main makefile for the SML/NJ runtime system
#

SHELL =
MAKEFILE = 	makefile.win32
MAKE =		nmake /F$(MAKEFILE)
CC =		cl
CPP =		cl /E /EP
LD_LIBS =
AS =		ml /Cx /Zd /Zi /Fr
AR =		lib
ARFLAGS =	
RANLIB =	lib

#
# dummy version
#
VERSION =	v-dummy

#
# target object
#
RUNTIME =	interactive-run

#
# source directories
#
ROOT_DIR =	..
OBJS_DIR =	$(ROOT_DIR)\objs
GC_DIR =	$(ROOT_DIR)\gc
INC_DIR =	$(ROOT_DIR)\include
KERN_DIR =	$(ROOT_DIR)\kernel
MACH_DIR =	$(ROOT_DIR)\mach-dep
MEM_DIR =	$(ROOT_DIR)\memory
MP_DIR =	$(ROOT_DIR)\mp
BC_DIR =	$(ROOT_DIR)\bytecode
CLIB_DIR =	$(ROOT_DIR)\c-libs
CONFIG_DIR =	$(ROOT_DIR)\config

CFLAGS = 	

INCLUDES =	/I$(OBJS_DIR) /I$(INC_DIR) /I$(MACH_DIR)
GC_INCLUDES =	$(INCLUDES) /I$(GC_DIR)
GEN_INCLUDES =	/I$(CONFIG_DIR) $(INCLUDES)

OBJS =		c-libraries.obj win32-raise-syserr.obj \
                ml-options.obj \
		new-boot.obj load-ml.obj run-ml.obj globals.obj ml-state.obj \
		error.obj timers.obj win32-timers.obj \
		qualify-name.obj swap-bytes.obj \
		signal-util.obj prim.obj \
		win32-util.obj win32-fault.obj win32-signal.obj \
		$(XOBJS)

#
# libraries of ML callable C functions
#
CLIBS =		$(XCLIBS) \
		$(CLIB_DIR)\win32\libwin32.lib \
		$(CLIB_DIR)\win32-io\libwin32-io.lib \
		$(CLIB_DIR)\win32-filesys\libwin32-filesys.lib \
		$(CLIB_DIR)\win32-process\libwin32-process.lib \
		$(CLIB_DIR)\smlnj-runtime\libsmlnj-runt.lib \
		$(CLIB_DIR)\smlnj-signals\libsmlnj-sig.lib \
		$(CLIB_DIR)\smlnj-sockets\libsmlnj-sock.lib \
		$(CLIB_DIR)\smlnj-prof\libsmlnj-prof.lib \
		$(CLIB_DIR)\smlnj-time\libsmlnj-time.lib \
		$(CLIB_DIR)\smlnj-date\libsmlnj-date.lib \
		$(CLIB_DIR)\smlnj-math\libsmlnj-math.lib

#
# The mem-win32 version is the default (and currently only) 
# memory subsystem for win32
#
LIBMEM_OBJS =	mem-win32.obj

#
# The SGI version is the default MP library
#
LIBMP_OBJS =	'OBJS=$$(SGI_OBJS)'

#
# the default GC library
#
GC_LIB =	libgc.lib

#
# The various libraries; note that the order matters, since the C functions
# call GC library routines, and the GC library uses the memory library.
#
ML_LIBS =	$(GC_DIR)\$(GC_LIB) \
		$(MEM_DIR)\libmem.lib

DEP_LIBS =	$(ML_LIBS) $(XLIBS)

ALL_LIBS =	$(CLIBS) $(DEP_LIBS)

clean:
	del /Q /F v-* run.* interactive-run \
		win32-sigtbl.c \
		gen-sizes.exe gen-offsets.exe gen-regmask.exe \
                gen-bc-instr-def.exe \
		gen-win32-signals.exe \
		gen-win32-sigtbl.exe \
		ml-sizes.h reg-mask.h mlstate-offsets.h bc-instr-def.h \
		system-signals.h \
		*.obj prim.s primops.s \
		*.sbr *.pdb
	cd $(MEM_DIR) 
	$(MAKE) MAKE="$(MAKE)" clean
	
	cd $(CLIB_DIR) 
	$(MAKE) MAKE="$(MAKE)" clean

	cd $(GC_DIR) 
	$(MAKE) MAKE="$(MAKE)" clean

	cd $(MP_DIR) 
	$(MAKE) MAKE="$(MAKE)" clean


$(RUNTIME):	$(VERSION) main.obj $(OBJS) $(ALL_LIBS)
	$(CC) /Fe$(RUNTIME) $(CFLAGS) $(LDFLAGS) main.obj $(OBJS) $(ALL_LIBS) $(LD_LIBS)

$(VERSION):
		($(MAKE) MAKE="$(MAKE)" clean)
		echo "$(VERSION)" > $(VERSION)

#
# Sizes
#
ml-sizes.h:	gen-sizes.exe
	.\gen-sizes.exe

#
# kernel objects
#
main.obj:		$(KERN_DIR)\main.c \
		ml-sizes.h $(INC_DIR)\ml-base.h $(INC_DIR)\ml-options.h \
		$(INC_DIR)\ml-limits.h $(INC_DIR)\ml-objects.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\main.c

ml-options.obj:	$(KERN_DIR)\error.c \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-options.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\ml-options.c

error.obj:	$(KERN_DIR)\error.c $(INC_DIR)\ml-base.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\error.c

standalone.obj:	$(KERN_DIR)\standalone.c \
		ml-sizes.h $(INC_DIR)\ml-base.h $(INC_DIR)\ml-limits.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\standalone.c

new-boot.obj:	$(KERN_DIR)\new-boot.c \
		$(INC_DIR)\ml-osdep.h $(INC_DIR)\cache-flush.h \
		ml-sizes.h $(INC_DIR)\ml-base.h $(INC_DIR)\bin-file.h \
		$(INC_DIR)\ml-objects.h $(INC_DIR)\ml-globals.h $(INC_DIR)\gc.h \
		$(INC_DIR)\ml-limits.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\new-boot.c

load-ml.obj:	$(KERN_DIR)\load-ml.c \
		ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\load-ml.c

run-ml.obj:	$(KERN_DIR)\run-ml.c \
		ml-sizes.h reg-mask.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-values.h \
		$(INC_DIR)\vproc-state.h $(INC_DIR)\ml-roots.h \
		$(INC_DIR)\ml-state.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\run-ml.c

globals.obj:	$(KERN_DIR)\globals.c \
		ml-sizes.h $(INC_DIR)\ml-base.h $(INC_DIR)\ml-values.h $(INC_DIR)\ml-limits.h \
		$(INC_DIR)\c-globals-tbl.h $(INC_DIR)\machine-id.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\globals.c

ml-state.obj:	$(KERN_DIR)\ml-state.c \
		ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\tags.h \
		$(INC_DIR)\ml-values.h $(INC_DIR)\ml-roots.h \
		$(INC_DIR)\ml-state.h  $(INC_DIR)\vproc-state.h \
		$(INC_DIR)\ml-globals.h $(INC_DIR)\ml-timer.h $(INC_DIR)\gc.h \
		$(INC_DIR)\ml-limits.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\ml-state.c

timers.obj:	$(KERN_DIR)\timers.c \
		ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\vproc-state.h \
		$(INC_DIR)\ml-timer.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\timers.c

win32-timers.obj:	$(KERN_DIR)\win32-timers.c \
		ml-sizes.h \
		$(MACH_DIR)\win32-timers.h \
		$(INC_DIR)\ml-base.h \
		$(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h \
		$(INC_DIR)\ml-timer.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\win32-timers.c

qualify-name.obj:	$(KERN_DIR)\qualify-name.c \
		ml-sizes.h $(INC_DIR)\ml-base.h $(INC_DIR)\machine-id.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\qualify-name.c

swap-bytes.obj:	$(KERN_DIR)\swap-bytes.c \
		ml-sizes.h $(INC_DIR)\ml-base.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(KERN_DIR)\swap-bytes.c


#
# C libraries
#
c-libraries.obj:	$(CLIB_DIR)\c-libraries.c \
		ml-sizes.h $(INC_DIR)\ml-base.h \
		$(CLIB_DIR)\clib-list.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) /I$(CLIB_DIR) $(CLIB_DIR)\c-libraries.c

win32-raise-syserr.obj:	$(CLIB_DIR)\win32-raise-syserr.c \
		ml-sizes.h \
		$(INC_DIR)\ml-base.h \
                $(INC_DIR)\ml-state.h $(INC_DIR)\ml-roots.h \
		$(INC_DIR)\ml-objects.h $(INC_DIR)\ml-globals.h \
		$(CLIB_DIR)\ml-c.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(CLIB_DIR)\win32-raise-syserr.c


#
# Machine dependent objects
#

win32-fault.obj:	$(MACH_DIR)\win32-fault.c $(MACH_DIR)\win32-fault.h \
		$(MACH_DIR)\signal-sysdep.h \
		system-signals.h \
		ml-sizes.h \
		$(INC_DIR)\ml-base.h \
		$(INC_DIR)\vproc-state.h $(INC_DIR)\ml-roots.h \
		$(INC_DIR)\ml-state.h $(INC_DIR)\ml-globals.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(MACH_DIR)\win32-fault.c

win32-util.obj:	$(MACH_DIR)\win32-util.c
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(MACH_DIR)\win32-util.c

signal-util.obj:	$(MACH_DIR)\signal-util.c \
		ml-sizes.h system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-signals.h \
		$(INC_DIR)\vproc-state.h $(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h \
		$(INC_DIR)\ml-limits.h $(INC_DIR)\ml-objects.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(MACH_DIR)\signal-util.c

win32-signal.obj:	$(MACH_DIR)\win32-signal.c win32-sigtbl.c \
		ml-sizes.h \
		$(INC_DIR)\ml-base.h \
		$(INC_DIR)\ml-signals.h $(INC_DIR)\vproc-state.h $(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h \
		$(INC_DIR)\ml-limits.h $(INC_DIR)\ml-objects.h \
		$(INC_DIR)\ml-globals.h \
		$(MACH_DIR)\signal-sysdep.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(MACH_DIR)\win32-signal.c

system-signals.h:	gen-win32-signals.exe
	.\gen-win32-signals.exe

win32-sigtbl.c:	gen-win32-sigtbl.exe
	.\gen-win32-sigtbl.exe

reg-mask.h:	gen-regmask.exe
	.\gen-regmask.exe

prim.obj:	$(MACH_DIR)\$(TARGET).prim.masm \
		$(MACH_DIR)\x86-masm.h \
		ml-sizes.h reg-mask.h $(INC_DIR)\ml-base.h $(INC_DIR)\tags.h \
		$(INC_DIR)\asm-base.h \
		mlstate-offsets.h
	$(CPP) /D_ASM_ $(DEFS) /I$(BC_DIR) /I$(MACH_DIR) $(INCLUDES) /Tc$(MACH_DIR)\$(TARGET).prim.masm > prim.s
	$(AS) /c prim.s
	

$(MACH_DIR)\BYTECODE.prim.asm: bc-instr-def.h $(BC_DIR)\bc-instrs.h $(BC_DIR)\bc.h


#
#

mlstate-offsets.h:	gen-offsets.exe ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\vproc-state.h \
		$(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h
	.\gen-offsets.exe


#
# bytecode interpreter
#
interp.obj:	$(BC_DIR)\interp.c \
		ml-sizes.h reg-mask.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-values.h \
		$(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h \
		$(INC_DIR)\tags.h $(INC_DIR)\bibop.h \
		$(GC_DIR)\arena-id.h \
		$(BC_DIR)\bc.h $(BC_DIR)\bc-trace.h $(BC_DIR)\memory-trace.h
	$(CC) /c $(CFLAGS) $(DEFS) $(GC_INCLUDES) $(BC_DIR)\interp.c

print-bc.obj:	$(BC_DIR)\print-bc.c \
		ml-sizes.h $(INC_DIR)\ml-base.h \
		$(BC_DIR)\bc.h $(BC_DIR)\bc-trace.h $(BC_DIR)\print-bc.h
	$(CC) /c $(CFLAGS) $(DEFS) $(INCLUDES) $(BC_DIR)\print-bc.c

memory-trace.obj:	$(BC_DIR)\memory-trace.c \
		ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h $(INC_DIR)\cntr.h \
		$(INC_DIR)\bibop.h \
		$(GC_DIR)\heap.h $(GC_DIR)\arena-id.h \
		$(BC_DIR)\memory-trace.h
	$(CC) /c $(CFLAGS) $(DEFS) $(GC_INCLUDES) $(BC_DIR)\memory-trace.c

primops.obj:	$(BC_DIR)\primops.asm \
		$(INC_DIR)\asm-base.h
	$(CPP) /D_ASM_ $(DEFS) $(INCLUDES) $(BC_DIR)\primops.asm > primops.s
	$(AS) -o primops.obj primops.s

bc-instr-def.h:	gen-bc-instr-def.exe \
		$(BC_DIR)\bc.h $(BC_DIR)\print-bc.h
	.\gen-bc-instr-def.exe


#
# arguments to recursive make
#
MK_ARGS =		VERSION="$(VERSION)" \
			MAKE="$(MAKE)" \
			CC="$(CC)" CFLAGS="$(CFLAGS)" DEFS="$(DEFS)" \
			AR="$(AR)" ARFLAGS="$(ARFLAGS)" \
			RANLIB="$(RANLIB)" \
			INCLUDES="$(GC_INCLUDES) /I..\bytecode"

#
# memory management library
#
$(MEM_DIR)\libmem.lib:	FORCE
	cd $(MEM_DIR)
	$(MAKE) OBJS="$(LIBMEM_OBJS)" $(MK_ARGS) libmem.lib
	cd $(MAKEDIR)

#
# C functions library
#
$(CLIB_DIR)\libcfuns.lib:	FORCE
	cd $(CLIB_DIR) 
	$(MAKE) $(MK_ARGS) libcfuns.lib
	cd $(MAKEDIR)

#
# GC and heap I/O library
#
$(GC_DIR)\$(GC_LIB):	FORCE
	cd $(GC_DIR)
	$(MAKE) CHECK_HEAP="$(CHECK_HEAP)" $(MK_ARGS) $(GC_LIB)
	cd $(MAKEDIR)

#
# multiprocessor library (optional)
#
$(MP_DIR)\libmp.lib:	FORCE
	cd $(MP_DIR)
	$(MAKE) $(LIBMP_OBJS) $(MK_ARGS) libmp.lib
	cd $(MAKEDIR)

#
# to force recursive makes
#
FORCE:


#
# Configuration tools
#
gen-sizes.exe:	$(CONFIG_DIR)\gen-sizes.c gen-common.obj $(CONFIG_DIR)\gen.h \
		$(INC_DIR)\ml-base.h
	$(CC) $(DEFS) $(GEN_INCLUDES) /Fegen-sizes.exe $(CONFIG_DIR)\gen-sizes.c gen-common.obj

gen-regmask.exe:	$(CONFIG_DIR)\gen-regmask.c gen-common.obj $(CONFIG_DIR)\gen.h
	$(CC) $(DEFS) $(GEN_INCLUDES) /Fegen-regmask.exe $(CONFIG_DIR)\gen-regmask.c gen-common.obj

gen-offsets.exe:	$(CONFIG_DIR)\gen-offsets.c gen-common.obj $(CONFIG_DIR)\gen.h \
		ml-sizes.h \
                system-signals.h \
		$(INC_DIR)\ml-base.h $(INC_DIR)\vproc-state.h \
		$(INC_DIR)\ml-roots.h $(INC_DIR)\ml-state.h
	$(CC) $(DEFS) $(GEN_INCLUDES) /Fegen-offsets $(CONFIG_DIR)\gen-offsets.c gen-common.obj

gen-bc-instr-def.exe:	$(CONFIG_DIR)\gen-bc-instr-def.c \
		gen-common.obj $(CONFIG_DIR)\gen.h \
		$(BC_DIR)\bc.h $(BC_DIR)\print-bc.h
	$(CC) $(GEN_INCLUDES) /I$(BC_DIR) /Fegen-bc-instr-def.exe $(CONFIG_DIR)\gen-bc-instr-def.c gen-common.obj

gen-win32-signals.exe:	$(CONFIG_DIR)\gen-win32-signals.c $(CONFIG_DIR)\win32-sigtab.h \
		gen-common.obj \
		$(CONFIG_DIR)\gen.h
	$(CC) $(GEN_INCLUDES) $(DEFS) /Fegen-win32-signals.exe $(CONFIG_DIR)\gen-win32-signals.c gen-common.obj

gen-win32-sigtbl.exe:	$(CONFIG_DIR)\gen-win32-sigtbl.c $(CONFIG_DIR)\win32-sigtab.h \
		gen-common.obj \
		$(CONFIG_DIR)\gen.h
	$(CC) $(CFLAGS) $(GEN_INCLUDES) $(DEFS) /Fegen-win32-sigtbl.exe $(CONFIG_DIR)\gen-win32-sigtbl.c gen-common.obj

gen-common.obj:	$(CONFIG_DIR)\gen-common.c $(CONFIG_DIR)\gen.h
	$(CC) /c $(GEN_INCLUDES) $(CONFIG_DIR)\gen-common.c

#
# Make rules for the C libraries
#

# include directories for the library sub-directories
#
LIB_OBJS_DIR =	..\..\objs
LIB_INC_DIR =	..\..\include
LIB_MACH_DIR =  ..\..\mach-dep
LIB_INCLUDES =	/I$(LIB_OBJS_DIR) /I$(LIB_INC_DIR) /I$(LIB_MACH_DIR) /I..

#
# arguments to recursive make
#
LIB_MK_ARGS =		VERSION="$(VERSION)" \
			MAKE="$(MAKE)" \
			CC="$(CC)" CFLAGS="$(CFLAGS)" DEFS="$(DEFS)" \
			AR="$(AR)" ARFLAGS="$(ARFLAGS)" \
			RANLIB="$(RANLIB)" \
			INCLUDES="$(LIB_INCLUDES)"

$(CLIB_DIR)\win32\libwin32.lib: FORCE
	cd $(CLIB_DIR)\win32
	$(MAKE) $(LIB_MK_ARGS) libwin32.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\win32-io\libwin32-io.lib: FORCE
	cd $(CLIB_DIR)\win32-io
	$(MAKE) $(LIB_MK_ARGS) libwin32-io.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\win32-filesys\libwin32-filesys.lib: FORCE
	cd $(CLIB_DIR)\win32-filesys
	$(MAKE) $(LIB_MK_ARGS) libwin32-filesys.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\win32-process\libwin32-process.lib: FORCE
	cd $(CLIB_DIR)\win32-process
	$(MAKE) $(LIB_MK_ARGS) libwin32-process.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-runtime\libsmlnj-runt.lib: FORCE
	cd $(CLIB_DIR)\smlnj-runtime 
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-runt.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-signals\libsmlnj-sig.lib: FORCE
	cd $(CLIB_DIR)\smlnj-signals
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-sig.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-sockets\libsmlnj-sock.lib: FORCE
	cd $(CLIB_DIR)\smlnj-sockets
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-sock.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-prof\libsmlnj-prof.lib: FORCE
	cd $(CLIB_DIR)\smlnj-prof
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-prof.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-time\libsmlnj-time.lib: FORCE
	cd $(CLIB_DIR)\smlnj-time
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-time.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-date\libsmlnj-date.lib: FORCE
	cd $(CLIB_DIR)\smlnj-date
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-date.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-math\libsmlnj-math.lib: FORCE
	cd $(CLIB_DIR)\smlnj-math
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-math.lib
	cd $(MAKEDIR)

$(CLIB_DIR)\smlnj-ccalls\libsmlnj-ccalls.lib: FORCE
	cd $(CLIB_DIR)\smlnj-ccalls 
	$(MAKE) $(LIB_MK_ARGS) libsmlnj-ccalls.lib
	cd $(MAKEDIR)

#$(CLIB_DIR)\smlnj-mp\libsmlnj-mp.lib: FORCE
#	cd $(CLIB_DIR)\smlnj-mp 
#	$(MAKE) $(LIB_MK_ARGS) libsmlnj-mp.lib
#	cd $(MAKEDIR)


