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

# You need to have run makefile.n64 in pdpclib first

# Note that we need to link with x64supa.obj because we
# need the routines to support cc64-compiled programs,
# and that is not in the library itself. But you will
# get an error at run-time, not link time, if you don't
# include it, due to cc64 limitations.

# If you are using Windows 2000 etc to target Win64 you will need to
# uncomment the copy of wingen.exe and build the windows
# version with makefile.std first and rename gen.exe to wingen.exe
# This is because if you use cc64 to build the gen.exe, you
# can't actually execute it on a 32-bit-only Windows 2000

# If you are using MacOS to target Win64, you will need to
# do the same thing, with targ=macarm before makefile.std
# and use the same name. And the commands are slightly
# different. We should probably use ifeq to select this
# instead of manual editing. But Rome wasn't built in a day.

# If you are building on Linux, you can do:
# gcc -o wingen.exe src/i386_gen.c
# and then ensure the cp wingen.exe gen.exe
# and chmod 755 gen.exe commands are executed

VPATH=src;src/hashtab;../pdld/src;../pdld/src/bytearray

PREPROCESSOR=pdcc -E
CPP=pdcc
ifeq "$(tools)" "mcc"
CC=mcc
CFLAGS=
else
CC=cc64
CFLAGS=-D__CC64__
endif
LD=pdld
LDFLAGS=-s -nostdlib --no-insert-timestamp --image-base 0x400000
AS=pdas --oformat coff --64
COPTS=$(CFLAGS) -I. -I./src -I./src/hashtab \
    -I../pdld/src -I../pdld/src/bytearray \
    -I../pdpclib \
    -D__WIN32__ -D__NOBIVA__ -D__64BIT__

OBJS=a_out.obj as.obj bytearray.obj cfi.obj coff.obj cond.obj cstr.obj elf.obj error.obj \
expr.obj frags.obj hashtab.obj libas.obj listing.obj load_line.obj process.obj sections.obj symbols.obj write.obj int64sup.obj \
i386_as.obj elf_bytearray.obj

TARGET=pdas.exe

all: clean generate $(TARGET)

$(TARGET): $(OBJS)
  $(LD) $(LDFLAGS) -o $(TARGET) ../pdpclib/w32start.obj $(OBJS) ../pdpclib/x64supa.obj ../pdpclib/msvcrt.lib

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

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

OBJS_GENERATE=i386_gen.obj i386_opc.i

generate: clean_generate $(OBJS_GENERATE)
  $(LD) $(LDFLAGS) -o gen.exe ../pdpclib/w32start.obj i386_gen.obj ../pdpclib/x64supa.obj ../pdpclib/msvcrt.lib
# For Windows 2000:
#  copy wingen.exe gen.exe
# For MacOS and Linux:
ifeq "$(docp)" "yes"
  cp wingen.exe gen.exe
  chmod 755 gen.exe
endif
  gen.exe i386_opc.i i386_tbl.h

clean_generate:
  rm -f $(OBJS_GENERATE) gen.exe i386_tbl.h

.tbl.i:
  $(PREPROCESSOR) $(COPTS) -o $@ $<
