# This is the makefile to build pdpclib using
# the cc64 compiler to produce a Linux ELF
# executable but with Win64 calling conventions
# for function calls (but not syscalls)

ifeq "$(tools)" "mcc"
CPP=pdcc
else
CPP=pdcc -D__CC64__ -Dconst= -D__CC64OS__
endif

ifeq "$(tools)" "mcc"
CC=mcc
else
CC=cc64
endif

CFLAGS=
COPTS=$(CFLAGS) -D__gnu_linux__ -I . -I../src \
    -D__64BIT__ -DNOUNDMAIN -D__W64SHELL__ \
    -DWINNEWMOD

AR=xar
#AR=arwin


ifeq "$(tools)" "mcc"
GAS=pdas --oformat coff --64
endif

AS=pdas --oformat coff --64


LD=pdld --no-insert-timestamp --oformat elf
#LD=ldwin

all: pdptest.exe

pdptest.exe: linstart.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 __memmgr.obj l64supb.obj
  rm -f pdpwin64.lib
  $(AR) r pdpwin64.lib linstart.obj stdio.obj string.obj stdlib.obj l64supb.obj
  $(AR) r pdpwin64.lib start.obj time.obj errno.obj assert.obj signal.obj
  $(AR) r pdpwin64.lib locale.obj ctype.obj setjmp.obj math.obj __memmgr.obj
  $(AR) s pdpwin64.lib
  $(LD) -s -e ___pdpstart --image-base 0x400000 -nostdlib -o pdptest.exe linstart.obj pdptest.obj pdpwin64.lib

.c.obj:
  $(CPP) -E $(COPTS) -o $*.i $<
ifeq "$(tools)" "mcc"
  $(CC) -o:$*.s $*.i
else
  $(CC) -q -c -out:$@ $*.i
endif
  rm -f $*.i
ifeq "$(tools)" "mcc"
  $(GAS) -o $@ $*.s
  rm -f $*.s
endif

.asm.obj:
  $(AS) --defsym LINUX=1 -o $@ $<
#  $(AS) --defsym STACKPARM=0 -o $@ $<

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