# This is the new new standard (using cc64) for PDOS/x64
# It produces an msvcrt.dll that allows you to run a
# subset of Win64 executables

# You need to use makek32.n64 in src first

# Note that we export all symbols because cc64 isn't
# generating the required .drectve section. We may
# revisit this situation at a later date. If not by
# changing cc64 then perhaps allowing a definition file.

# Note that for development purposes, I think there is an
# "undocumented" option "-callback" which makes every
# function a callback so that you can see if the issue
# is you have failed to add a $callback to a function.

ifeq "$(tools)" "mcc"
CPP=pdcc
else
CPP=pdcc -D__CC64__
endif

ifeq "$(tools)" "mcc"
CC=mcc
else
CC=cc64
endif

AR=xar

ifeq "$(tools)" "mcc"
AS=sasm -f win64
GAS=pdas --oformat coff --64
else
AS=sasm -f win64 -DCC64
endif

#AS=pdas --oformat coff --64 --defsym PDAS=1 --defsym CC64=1 --no-pseudo-dot
#AS=ml64 -DCC64
LD=pdld
COPTS=-D__WIN32__ -D__64BIT__ -D__PDPCLIB_DLL -D__NOBIVA__ -DNOUNDMAIN -I . -I ../src

ifeq "$(crt)" "ucrt"
COPTS:=$(COPTS) -D_UCRT
UCRT_COPTS=-D_UCRT
DLLNAME=ucrtbase
UCRTWRAP_OBJS=ucrtwrap.obj
UCRTWRAP_LIB=ucrtwrap.lib
else
DLLNAME=msvcrt
endif

all: pdptest.exe

pdptest.exe: w32start.obj pdptest.obj stdio.obj string.obj stdlib.obj \
       start.obj time.obj errno.obj assert.obj signal.obj locale.obj \
       ctype.obj setjmp.obj math.obj dllcrt.obj x64supa.obj $(UCRTWRAP_OBJS)
  rm -f temp.lib
  $(AR) r temp.lib stdio.obj string.obj stdlib.obj x64supa.obj
  $(AR) r temp.lib start.obj time.obj errno.obj assert.obj signal.obj
  $(AR) r temp.lib locale.obj ctype.obj setjmp.obj math.obj
  $(AR) s temp.lib
ifeq "$(crt)" "ucrt"
  rm -f $(UCRTWRAP_LIB)
  $(AR) r $(UCRTWRAP_LIB) $(UCRTWRAP_OBJS)
  $(AR) s $(UCRTWRAP_LIB)
endif
ifeq "$(tools)" "mcc"
  $(LD) -s --implib-compat --no-insert-timestamp -o $(DLLNAME).dll --shared --out-implib $(DLLNAME).lib dllcrt.obj temp.lib $(UCRTWRAP_LIB) ../src/kernel32.lib
else
  $(LD) -s --implib-compat --export-all-symbols --no-insert-timestamp --image-base 0x400000 -o $(DLLNAME).dll --shared --out-implib $(DLLNAME).lib dllcrt.obj temp.lib $(UCRTWRAP_LIB) ../src/kernel32.lib
endif
  rm -f temp.lib
  $(LD) -s -nostdlib --no-insert-timestamp --image-base 0x400000 -o pdptest.exe w32start.obj pdptest.obj $(UCRTWRAP_LIB) $(DLLNAME).lib

ucrtwrap.obj: ucrtwrap.c
  $(CPP) -E -D__WIN32__ -D__CC64__ -D__64BIT__ -I . -I ../src $(UCRT_COPTS) -o $*.i $<
ifeq "$(tools)" "mcc"
  $(CC) -o:$*.s $*.i
else
  $(CC) -q -c -out:$@ $*.i
endif
  rm -f $*.i
ifeq "$(tools)" "mcc"
  $(GAS) -o $@ $*.s
  rm -f $*.s
endif

pdptest.obj: pdptest.c
  $(CPP) -E -D__WIN32__ -D__CC64__ -D__64BIT__ -I . -I ../src $(UCRT_COPTS) -o $*.i $<
ifeq "$(tools)" "mcc"
  $(CC) -o:$*.s $*.i
else
  $(CC) -q -c -out:$@ $*.i
endif
  rm -f $*.i
ifeq "$(tools)" "mcc"
  $(GAS) -o $@ $*.s
  rm -f $*.s
endif

dllcrt.obj: dllcrt.c
  $(CPP) -E $(COPTS) -D__EXPORT__ -DNEED_START -o $*.i $<
ifeq "$(tools)" "mcc"
  $(CC) -o:$*.s $*.i
else
  $(CC) -q -c -out:$@ $*.i
endif
  rm -f $*.i
ifeq "$(tools)" "mcc"
  $(GAS) -o $@ $*.s
  rm -f $*.s
endif

.c.obj:
  $(CPP) -E $(COPTS) -o $*.i $<
ifeq "$(tools)" "mcc"
  $(CC) -o:$*.s $*.i
else
  $(CC) -q -c -out:$@ $*.i
endif
  rm -f $*.i
ifeq "$(tools)" "mcc"
  $(GAS) -o $@ $*.s
  rm -f $*.s
endif

.asm.obj:
  $(AS) -o $@ $<
#  $(AS) -nologo -c -Fo$@ $<

clean:
  rm -f *.obj
  rm -f pdptest.exe
