# Produce MSDOS executable using Watcom C but with the
# assembler passed through as86 and linked with ld86
# Build with pdmake -f makefile.wca

# Change XXSZ4 to SZ4 (globally) to activate 32-bit size_t

# We define SEGHACK because as86, and more to the point, the
# a.out object file format itself, can't handle the "seg"
# keyword, so we need a compiler that doesn't generate that.
# In the meantime there is only a little bit of code that
# actually causes the generation of seg, and it isn't important,
# so we can just comment it out.

# There is also some assembler code that as86 doesn't seem
# to like, so we define AS86 for it too

CC=wcl
COPTS=-DSEGHACK -D__XXSZ4__ -oneatx -ecc -zp1 -q -w -c -I. -ml -zl -D__MSDOS__ -fpi87 -s -zdp -zu
# Adding -d2 allows you to get source code in the disassembly
# (wdis -l -s), but you need to switch off optimization (-oneatx)

AR=xar

#LD=ld86
LD=slink

pdptest.exe: dosstart.o pdptest.o stdio.o string.o stdlib.o \
       start.o time.o errno.o assert.o  locale.o \
       ctype.o setjmp.o math.o dossupc.o dossupa.o signal.o
  rm -f watcom.a
  $(AR) r watcom.a dosstart.o
  $(AR) r watcom.a stdio.o
  $(AR) r watcom.a string.o
  $(AR) r watcom.a stdlib.o
  $(AR) r watcom.a start.o
  $(AR) r watcom.a time.o
  $(AR) r watcom.a errno.o
  $(AR) r watcom.a assert.o
  $(AR) r watcom.a locale.o
  $(AR) r watcom.a ctype.o
  $(AR) r watcom.a setjmp.o
  $(AR) r watcom.a math.o
  $(AR) r watcom.a dossupc.o
  $(AR) r watcom.a dossupa.o
  $(AR) r watcom.a signal.o
  $(AR) s watcom.a
# To create a COM file, make it "Form dos com" and change the name to .com
# Stack size has no meaning for .com (the SP will be set by the OS, and the
# SS will be set to the CS/DS/ES). Also dosstart.obj needs to come first
# for a COM file, as the IP is always set to 0100H. Also it needs to be
# built with -mt and memmodel=tiny. If you build a .exe instead of a .com
# with all of that in place, the utility exe2bin, if available, can be
# used to convert a .exe into a .com if you are using a linker that
# can't directly produce a .com file.
# Because we are linking dosstart first, we are required to use the
# dosseg option, for some reason, because we need the stack to come last,
# so that the calculations in dosstart.asm are accurate with regard to
# releasing memory.
  $(LD) -e ___asmstart -N --oformat msdos-mz -o pdptest.exe dosstart.o pdptest.o watcom.a

.c.o:
  $(CC) $(COPTS) $<
  wdis -a -l=$*.s $*.obj
  rm $*.obj
  as86 -o $*.o $*.s
  rm $*.s

# Add -DMAKECOM to this if you want to produce a COM file
dosstart.o: dosstart.asm
#  ml -c -Dmemodel=large dosstart.asm
#  wasm -zcm -q -DWATCOM -D__XXSZ4__ -Dmemodel=large dosstart.asm
  as86 -o $@ -DAS86 -DWATCOM -D__XXSZ4__ -Dmemodel=large dosstart.asm

dossupa.o: dossupa.asm
#  ml -c -Dmemodel=large -DWATCOM dossupa.asm
#  wasm -zcm -q -DWATCOM -D__XXSZ4__ -Dmemodel=large dossupa.asm
  as86 -o $@ -DWATCOMC -D__XXSZ4__ -Dmemodel=large dossupa.asm
