ifeq "$(crt)" "ucrt"
CRTCOPTS=-D_UCRT -D_USE_32BIT_TIME_T
CRTEXTRA=../pdpclib/ucrtwrap.lib ../pdpclib/ucrtbase.lib
else
CRTEXTRA=../pdpclib/msvcrt.lib
endif

ifeq "$(targ)" "zpg"
# Produces z/PDOS-generic executable.
# Links with PDPCLIB created by makefile.zpg
CC=gccmvs
CFLAGS=-Os
AS=as370 -mhlasm -mebcdic
AR=ar370
LD=pdld
LDFLAGS=-s --no-insert-timestamp -nostdlib
COPTS=-S $(CFLAGS) -fno-common -ansi \
    -I. -I../pdos/pdpclib \
    -U__MVS__ -D__MF32__ -D__PDOSGEN__ -D__NOBIVA__ \
    -I../generic -I../src $(COPT2)
EXTRA1=--oformat mvs ../pdpclib/pgastart.o


else ifeq "$(targ)" "macarm"
# makefile.m64 needs to have been run in pdpclib first
# with tools=mac altld=pdld
CC=clang -S -O2 -D__ARM__ -fno-builtin -D__MACOS__ \
    -D__64BIT__ -fno-stack-protector -D__USEBIVA__ \
    -Wno-error=implicit-function-declaration \
    -mmacosx-version-min=10 \
    -D__LONG64__ \
    -I../pdpclib
AR=ar
AS=as -mmacosx-version-min=10
LD=clang -Wl,-e, -Wl,___pdpent -mmacosx-version-min=10
EXTRA2=../pdpclib/pdpmacos.a


else ifeq "$(targ)" "macamd64"
# makefile.mx6 needs to have been run in pdpclib first
CC=clang -arch x86_64 -mmacosx-version-min=11 \
    -S -O2 -fno-builtin -D__MACOS__ \
    -D__64BIT__ -D__LONG64__ \
    -fno-stack-protector  -D__gnu_linux__  -D__USEBIVA__ \
    -nostdinc \
    -I../pdpclib
AR=ar
AS=as -arch x86_64 -mmacosx-version-min=11
LD=clang -arch x86_64 -Wl,-no_uuid -Wl,-no_adhoc_codesign \
    -Wl,-e, -Wl,__start \
    -mmacosx-version-min=11
EXTRA2=../pdpclib/pdpmacos.a


else ifeq "$(targ)" "gccarm64"
# makefile.a64 needs to have been run in pdpclib first
CC=gcc -S -O2 -D__ARM__ -nostdinc -nostdlib -fno-builtin \
   -D__64BIT__ -fno-stack-protector --no-pie -D__USEBIVA__ \
   -D__LONG64__ \
   -I../pdpclib
AR=ar
AS=as
# -m64 isn't always recognized
LD=gcc -static -s -nostdlib
EXTRA1=../pdpclib/linstart.o
EXTRA2=../pdpclib/pdplinux.a


else ifeq "$(targ)" "gccamd64"
# makefile.l64 needs to have been run in pdpclib first
CC=gcc -S -O2 -D__gnu_linux__ -nostdinc -nostdlib -fno-builtin \
    -D__64BIT__ -fno-stack-protector --no-pie -D__USEBIVA__ \
    -m64 -fno-common -D__LONG64__ \
    -I../pdpclib
AR=ar
AS=as
LD=gcc -s -static -m64 -nostdlib -Wl,-e -Wl,__pdpent
EXTRA1=../pdpclib/linstart.o ../pdpclib/pdplinux.a
EXTRA2=../pdpclib/pdplinux.a


else
AS=pdas --oformat coff
CC=gccwin
LD=pdld --no-insert-timestamp -nostdlib
COPTS=-S -O2 -fno-common -ansi -I. -I../pdpclib -D__WIN32__ -D__NOBIVA__ $(CRTCOPTS)
EXTRA1=../pdpclib/w32start.obj
EXTRA2=$(CRTEXTRA)
endif


COBJ=append.obj ar.obj conv.obj delete.obj display.obj extract.obj \
    lib.obj ranlib.obj replace.obj report.obj

all: clean xar.exe

xar.exe: $(COBJ)
  $(LD) $(LDFLAGS) -o xar.exe $(EXTRA1) $(COBJ) $(EXTRA2)

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

clean:
  rm -f *.o xar.exe
