# 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.


# If you are cross-compiling, you will also need to uncomment
# the copy of wingen.exe to gen.exe and have renamed gen.exe
# to wingen.exe after building with makefile.std

# And you will need to use makefile.war instead of makefile.std
# in that case too.


ifeq "$(targ)" "arm"
#CC=gccprm
####CFLAGS=-O0
CFLAGS=-O0 -DNO_LONG_LONG -Dinline= -ansi -pedantic
AS=asprm
AR=arprm
COPTS=-S $(CFLAGS) -Wall -fno-common \
    -I. -I../pdpclib -D__WIN32__ -D__USEBIVA__ \
    -D__ARM__ -msoft-float -mapcs-32 -fno-leading-underscore
EXTRA=../pdpclib/msvcrt.lib ../pdpclib/armsupa.o ../pdpclib/fpfuncsa.o
START=../pdpclib/w32start.obj
LD=pdld
LDFLAGS=--no-insert-timestamp -s --stack ,300000


else ifeq "$(targ)" "winx64e"
# You need to have run makefile.weg first
# Because this is actually a PDOS-generic executable, not a Windows executable
AS=pdas --64 --oformat coff
#AS=as --64
CC=gcc

LD=pdlde --64 --no-insert-timestamp -nostdlib --oformat coff \
    --emit-relocs -e __crt0 --image-base 0x400000
COPTS=-S -O0 -mno-sse -fno-builtin -D__64BIT__ -m64 \
    -mno-red-zone \
    -fno-common -U__gnu_linux__ -fno-stack-protector \
    -D__USEBIVA__ -D__NOSPECBIVA__ \
    -nostdlib -nostdinc \
    --no-pie -fno-leading-underscore \
    -I../src -D__EBCDIC__ -funsigned-char \
    -fexec-charset=IBM1047 \
    -I . -I ../pdpclib \
    -U__MINGW32__ \
    -D__PDOSGEN__ \
    -I../generic \
    -DNO_LONG_LONG \
    -DPOS_EXTEND \
    -DHAVE_FOLDER
EXTRA=../pdpclib/x64supa.obj
EXTRA2=../src/pos2.obj ../pdpclib/string.obj
START=../pdpclib/pgastart.obj


else ifeq "$(targ)" "zpg"
# Note that you need to build the Windows version first
# to generate tables.c, then rename gen.exe to wingen.exe
# and then edit this makefile (search for wingen) as per
# instructions and build using makefile.zpg in PDPCLIB
# and then you can do pdmake targ=zpg -f makefile.std here
CC=gccmvs
####CFLAGS=-O0
CFLAGS=-O0 -DNO_LONG_LONG -Dinline= -ansi -pedantic
AS=as370 -mhlasm -mebcdic
AR=ar370
COPTS=-S $(CFLAGS) -Wall -fno-common -DPOS_EXTEND -U__MVS__ \
    -I. -I../pdpclib -D__MF32__ -D__NOBIVA__ -D__PDOSGEN__ \
    -I../generic -I../src -DHAVE_FOLDER
EXTRA2=../src/pos2.obj
START=../pdpclib/pgastart.o
LDFLAGS=--no-insert-timestamp -s --stack ,300000 --oformat mvs
LD=pdld



else
CC=gccwin
CFLAGS=-O2
####CFLAGS=-O2 -DNO_LONG_LONG -ansi -pedantic-errors
### The --stack ,30000 is needed to get around "__chkstk_ms" issue.
AS=pdas --oformat coff
COPTS=-S $(CFLAGS) -Wall -fno-common -I. -I../pdpclib -D__WIN32__ \
    -D__NOBIVA__ -DHAVE_FOLDER -D_MSC_VER -I../src
START=../pdpclib/w32start.obj
LDFLAGS=--no-insert-timestamp -s --stack ,300000
EXTRA=../pdpclib/msvcrt.lib ../src/kernel32.lib
LD=pdld
endif



OBJS=$(EXTRA2) crc32.obj deflate.obj fasc.obj huffman.obj hwzip.obj \
    implode.obj lz77.obj reduce.obj shrink.obj tables.obj zip.obj \
    folder.obj

TARGET=hwzip.exe

all: clean generate $(TARGET)

$(TARGET): $(OBJS)
  $(LD) $(LDFLAGS) -o $(TARGET) $(START) $(OBJS) $(EXTRA)

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

clean: clean_generate
  rm -f $(OBJS) $(TARGET)

OBJS_GENERATE=generate_tables.obj

generate: clean_generate $(OBJS_GENERATE)
ifeq "$(docp)" "yes"
else
# comment out this LD line if uncommenting the copy command
# to avoid build errors
  $(LD) $(LDFLAGS) -o gen.exe $(START) $(OBJS_GENERATE) ../pdpclib/msvcrt.lib
endif
#  copy wingen.exe gen.exe
ifeq "$(docp)" "yes"
  cp wingen.exe gen.exe
  chmod 755 gen.exe
endif
  gen.exe > tables.c

clean_generate:
  rm -f $(OBJS_GENERATE) gen.exe tables.c


