# Released to the public domain.
#
# Anyone and anything may copy, edit, publish,
# use, compile, sell and distribute this work
# and all its parts in any form for any purpose,
# commercial and non-commercial, without any restrictions,
# without complying with any conditions
# and by any means.

# This makefile builds pload.com for PDOS/386 on Windows,
# using Visual Studio 1.52 plus ex2bin (the PDOS version)

# To produce the 32-bit version, do this:
# pdmake -f makeio.vsc TARG=PDOS32
# and then run other make*.vsc instead of make*.v86 files

# The 16-bit just drop the TARG=
# Note that when targeting 16-bit, the 32-bit assembler is
# still assembled, but it is not referenced so not linked in.

# You must run makefile.msc in pdpclib before running this
# Or else makefile.m86 if using Microsoft C 6.0 or
# Microsoft C 5.1

# Note that it is necessary to add the "noe" linker option
# to stop the extended dictionary from being used and giving
# errors about duplicates. Not sure if there is a better
# way of doing that. Using a library reduces the number of
# object files needed to be specified on the link command,
# as we only need to specify the ones that are involved in
# the self-loading process.

# Note that we use the "old" model, because MASM 5.1 can't
# handle the NEWMODEL syntax in protinta.asm

VPATH=..\pdpclib

TARG=XPDOS32

# See pdpclib makefile.msc for explanation
# If you are using a newer version of masm, you will need
# to add OMF=-omf or change the below

OMF=
#OMF=-omf

# If you are using Microsoft C 6.0, it pollutes the
# namespace with edata and end (no underscore)
POLLUTE=-DPOLLUTE
#POLLUTE=

# Define this if you are using Microsoft C 6.0 which has
# AHSHIFT occupying space at the beginning of the executable
# which we need to compensate for in the startup assembler
#MSC6=-DMSC6
MSC6=

CC=cl

# Use -f to avoid an out of memory error
# If you use -O2 instead of -Ox, you get a smaller executable
# because -Gy seems to be in effect allowing the linker to
# strip unreferenced functions. But it has the side-effect of
# rearranging the order of object files so that they are not
# in the order required for self-loading to complete

# The out-of-memory error appears to be triggered by an attempt
# to use a command line greater than something like 106 characters
# in the command line (presumably after accounting for the
# command to be executed you hit the DOS limit for command lines,
# ie around 127).
# Adding -f apparently prevents the call to the optimizing compiler,
# but doubles the executable size, and presumably doesn't actually optimize.
# Using @ doesn't get around the limit

# The link command is also subject to the length limitation
# when run under MSDOS (or an MSDOS version of the executable
# is used, such as Microsoft C 7.0), so also needs to be
# under around 127. Some object code from pdpclib is put into
# this library for this reason. A response file would probably
# solve it differently.

# Ideally we would use these flags, but the command is too long
# when adding a -DPDOS32, so we trim it to just -O. This needs
# to be revisited. Also can't use -Ox because it enables intrinsics
# which are incompatible with PDPCLIB (which has size_t as an
# unsigned long for huge memory model). Also the length of the
# spawned command doesn't appear to be 1:1. ie deleting one
# character from a define, or even delete the whole word "nologo",
# doesn't allow more optimization options (which may be being split)

# Ol (loop optimizations) stops for (;;) ; working in MSC 5.1, so
# that is being disabled

#CFLAGS=-Ob1ceglnot
#CFLAGS=-O
CFLAGS=-Ont

COPTS=-X -c -nologo -I. -I..\pdpclib -D__SZ4__ -D__MSDOS__ \
  -D$(TARG) $(CFLAGS) -AH -Zl -Gs
# Note that we are hitting limits on command line size, so DISPLAYC is dropped
# But really, we should just activate that unconditionally
# This is set to huge:
AS=masm -D@model=6 -D@codesize=1 -D@datasize=1 -Mx -DNMOD2 -DMASM
#AS=ml -nologo -DNMOD2 -DNEWMODEL -DNEED_DISPLAYC
LINK=link
#LINK=link /nologo
LIB=lib
#LIB=lib /nologo

OBJS=support.obj bos.obj minifat.obj pdosload.obj \
    lldos.obj file.obj protint.obj pload.obj \
    ploadst.obj int13x.obj protinta.obj

all: clean pload.com

pload.com: $(OBJS)
  rm -f pload.tmp
  $(LIB) io ..\pdpclib\dossupa.obj+..\pdpclib\dossupc.obj,,
  $(LINK) /cp:1 /noe /noi /nod /map /dosseg ploadst.obj+pload.obj+int13x.obj,pload.tmp,pload.map,io.lib+..\pdpclib\msc.lib,,
  ex2bin -base=700 pload.tmp pload.com

.c.obj:
  $(CC) $(COPTS) $<
  $(LIB) io +$@,,

.asm.obj:
  $(AS) -c $(OMF) $(POLLUTE) -DMAKEPL -DMSC $(MSC6) -Dmemodel=huge $< ,,,,
  $(LIB) io +$@,,

clean:
  rm -f *.obj
  rm -f pload.com
  rm -f io.lib
  $(LIB) io ,
