# This is the new standard for PDOS/386
# It produces an msvcrt.dll that allows you to run a
# subset of Win32 executables

# Note that you can switch from msvcrt to pdpcrt by
# doing a global change, so that you can take advantage
# of the pdp version doing ANSI escapes on Windows 10+
# and also switch to 64-bit long

# You should have run makek32.std in src first

# define OLDWIN if you want to target HX/Freewindows
# Since PDOS/386 doesn't use mscvrt.dll (it is instead
# built-in), and since Windows does use our msvcrt.dll
# and we instead need to bypass it, we may as well
# always build with OLDWIN

# So for example with microemacs, we need to run (in
# memacs) both makefile.std (that links with makefile.p32)
# for use on real Windows 10 and above (that supports
# ANSI escapes natively) - and we call this mew.exe.
# And we need to run makefile.dyn (which becomes me.exe)
# that uses msvcrt.dll for use on 32-bit Windows before
# Windows 10 in a DOSONLY environment with HX, like this:

# In C:\WINNT\system32\config.nt on a win2k system:
#  dosonly
#  dos=high, umb
#  device=%SystemRoot%\system32\himem.sys
#  device=%SystemRoot%\system32\ansi.sys /x
#  files=40
# (/x makes extended keys "independent")
# and then at a windows command prompt run command, then hxldr32

# In FreeDOS and DOS 4.0 and Freewindows you need to have
# an ANSI driver loaded and run hxldr32. And msvcrt.dll needs
# to be in the path
# So makefile.p32 should never need OLDWIN defined, as the
# static version (which won't work on PDOS/386 if OLDWIN is
# defined) is redundant

# Note that a separate version of Microemacs is available
# (ewin.exe) that uses traditional Windows screen manipulation
# (instead of ANSI). That won't work on PDOS/386 but should
# work elsewhere. For some reason it isn't working on
# Freedos+HX though. That's built by uemacs\nt\makefile.w32

# And although we shouldn't need it, there happens to be
# a statically-linked non-OLDWIN built with uemacs\mvs\makefile.w32
# known as epdos.exe that works on Win10+ and PDOS/386

CC=gccwin
#CC=gccwin_l64 -D__LONG64__
AR=xar
AS=pdas --oformat coff
LD=pdld
COPTS=-S -fno-common -fno-builtin -ansi -O2 -D__WIN32__ -D__PDPCLIB_DLL \
    -D__NOBIVA__ -I . -I ../src -DOLDWIN


ifeq "$(tgtchs)" "ebcdic"
COPTS:=$(COPTS) -funsigned-char
endif

ifeq "$(crt)" "ucrt"
COPTS:=$(COPTS) -D_UCRT -D_USE_32BIT_TIME_T
UCRT_COPTS:=$(UCRT_COPTS) -D_UCRT -D_USE_32BIT_TIME_T
DLLNAME=ucrtbase
# Ideal would be including the UCRTWRAP_OBJS
# in mixed import library but archivers do not support that,
# so next best option would be separate wrapper library.
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 winsupa.obj $(UCRTWRAP_OBJS)
  rm -f temp.lib
  $(AR) r temp.lib stdio.obj string.obj stdlib.obj winsupa.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 $(UCRTWRAP_OBJS)
  $(AR) s temp.lib
  $(LD) -s --implib-compat --no-insert-timestamp -o $(DLLNAME).dll --shared --out-implib $(DLLNAME).lib dllcrt.obj temp.lib ../src/kernel32.lib
  rm -f temp.lib
ifeq "$(crt)" "ucrt"
  rm -f $(UCRTWRAP_LIB)
  $(AR) r $(UCRTWRAP_LIB) $(UCRTWRAP_OBJS)
  $(AR) s $(UCRTWRAP_LIB)
endif
  $(LD) -s -nostdlib --no-insert-timestamp -o pdptest.exe w32start.obj pdptest.obj $(UCRTWRAP_LIB) $(DLLNAME).lib winsupa.obj setjmp.obj

w32start.obj: w32start.c
  $(CC) $(UCRT_COPTS) -S -O2 -D__WIN32__ -I . -I ../src -o $*.s $*.c
  $(AS) -o $*.obj $*.s
  rm -f $*.s

ucrtwrap.obj: ucrtwrap.c
  $(CC) $(UCRT_COPTS) -S -O2 -D__WIN32__ -I . -I ../src -o $*.s $*.c
  $(AS) -o $*.obj $*.s
  rm -f $*.s

pdptest.obj: pdptest.c
  $(CC) $(UCRT_COPTS) -S -O2 -D__WIN32__ -I . -I ../src -o $*.s $*.c
  $(AS) -o $*.obj $*.s
  rm -f $*.s

dllcrt.obj: dllcrt.c
  $(CC) $(COPTS) -D__EXPORT__ -DNEED_START -o $*.s $*.c
  $(AS) -o $*.obj $*.s
  rm -f $*.s

.c.obj:
  $(CC) $(COPTS) -o $*.s $<
  $(AS) -o $*.obj $*.s
  rm -f $*.s

.asm.obj:
  as86 -o $@ -f coff $<
#  sasm -o $@ -f coff $<

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