#
# Copyright (c) Microsoft Corporation. All rights reserved. 
# 
# You may only use this code if you agree to the terms of the Windows Research Kernel Source Code License agreement (see License.txt).
# If you do not agree to the terms, do not use the code.
# 

space = $(rundir)\SPACE
bootstrap = $(rundir)\Bootstrap

build: $(bootstrap).exe $(space).exe

# boottarget
bootstrapbase=0x71000000
bootstrapentry=bootspacecpu

OBJ = obj$(targ)


!if "$(targ)" == "i386"
asobjs =
!else
asobjs =
!endif

bootobjs = \
	$(OBJ)\Bootstrap.obj	\
	$(OBJ)\SystemLib.obj

ccarchobjs=
			
ccobjs= \
	$(OBJ)\CPU.obj			\
	$(OBJ)\Debug.obj		\
	$(OBJ)\Emulation.obj	\
	$(OBJ)\Interrupt.obj	\
	$(OBJ)\IO.obj			\
	$(OBJ)\LoadKernel.obj	\
	$(OBJ)\Memory.obj		\
	$(OBJ)\Portal.obj		\
	$(OBJ)\Processor.obj	\
	$(OBJ)\SPACE.obj		\
	$(OBJ)\Trap.obj			\
	$(OBJ)\Util.obj

!include DEVICES\makefile.DEVICES

spaceobjs  = $(asobjs) $(ccarchobjs) $(ccobjs) $(deviceobjs)

spacesrcs  = $(targ)\Bootstrap.asm CPU.c Debug.c Emulation.c Interrupt.c IO.c LoadKernel.c Memory.c Portal.c Processor.c SPACE.c Trap.c Util.c

!if !defined(wrk) || !defined(subdir) || !defined(rundir) || !defined(targ) || ("$(targ)" != "i386" && "$(targ)" != "amd64")
!ERROR Run subdir makefiles from $(MAKEDIR)\.. -- e.g. make $(subdir).build
!endif

!if "$(targ)" == "i386"
targdefs  = -D_X86_=1 -Di386=1 -DSTD_CALL -DFPO=0
targaopts = -safeseh -coff -Zm
targcopts = -Gm- -GX- -G7 -Ze -Gi- -QIfdiv- -Oxs -Oy- -Z7
targlopts =	
machine   = x86
archml    = ml
!else
targdefs  = -D_WIN64 -D_AMD64_ -DAMD64
targaopts = 
targcopts = -Wp64 -Oxt -EHs-c- -Oxt -Gs12288
targlopts =	-IGNORE:4108,4088,4218,4218,4235
machine   = amd64
archml    = ml64
!endif

pub 	  = $(wrk)\public\sdk\inc

incs 	  = $(incs) -I$(targ) -I. -I..\INC -I$(pub) -I$(pub)\crt

defs 	  = $(defs) $(targdefs) -D_$(targarch) -D_$(ARCH)_ -D$(ARCH) -D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -DWIN32_LEAN_AND_MEAN=1

aopts	  = -Cx -Zi -Zd $(targaopts)
copts 	  = -Zl -Zp8 -Gy -cbstring -W3 -WX -GR- -GF -GS -GL- $(targcopts)
 
AS		  = $(archml).exe -nologo
AFLAGS	  = $(aopts) $(incs) -Fo$(OBJ)\ $(defs) $(specialaflags) -Fl$(bootstrap).lst

CC		  = cl.exe -nologo
CFLAGS 	  = $(copts) $(incs) -Fo$(OBJ)\ $(defs) $(specialcflags)


linklibpath = -LIBPATH:LIB\$(targ)
linkignore  = -IGNORE:4087,4001,4010,4037,4039,4065,4070,4078,4087,4089,4221,4198
buildlibs   =  bufferoverflowu.lib kernel32.lib libcmt.lib ntdll.lib

LINKFLAGS = $(linkignore) -WX -NODEFAULTLIB -machine:$(machine) -debug -debugtype:cv,fixup
LINK	  = link.exe -nologo

linkspacenames     = -out:$(space).exe     -map:$(space).map     -pdb:$(space).pdb 
linkbootstrapnames = -out:$(bootstrap).exe -map:$(bootstrap).map -pdb:$(bootstrap).pdb 

$(space).exe: $(spaceobjs)
	$(LINK) $(LINKFLAGS) $** $(linkspacenames) $(linklibpath) $(buildlibs)


$(bootstrap).exe: $(bootobjs)
	$(LINK) $(LINKFLAGS) $** $(linkbootstrapnames) /SUBSYSTEM:NATIVE /BASE:$(bootstrapbase) /ENTRY:$(bootstrapentry)

# assembly files
{$(targ)\}.asm{$(OBJ)\}.obj::
	$(AS) $(AFLAGS) -c $<

# arch-specific C files
{$(targ)\}.c{$(OBJ)\}.obj::
	$(CC) $(CFLAGS) -c $<

# C files
.c{$(OBJ)\}.obj::
	$(CC) $(CFLAGS) -c $<

# C DEVICES files
{DEVICES\}.c{$(OBJ)\}.obj::
	$(CC) $(CFLAGS) -c $<

.c.i:
	@$(CC) $(CFLAGS) /P $?

$(targ)\Bootstrap.asm:	..\INC\TrapValues.inc makefile.SPACE
SystemLib.c: 			..\INC\SystemLib.h makefile.SPACE
$(spacesrcs):			Defs.h Defs-crt.h ..\INC\SpaceOps.h Config.h TrapValues.h Devices.h Debug.h makefile.SPACE


# clean pseudo targets

clean: clean0

clean0:
	-del $(bootobjs) $(spaceobjs) $(extraobjs)

