# 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 32-bit uemacs.exe on Windows.
# makefile.p32 in PDPCLIB must be used before using this makefile
# so that a static link can be done to get ANSI screen I/O enabled
# instead of the default msvcrt.dll

# When building on Windows 10, change XHACK to HACK to avoid
# a hang at startup - cause unknown (looks like this might have
# been some virus false positive as the executable now works
# without the hack)


ifeq "$(targ)" "macarm"

# makefile.m64 needs to have been run in pdpclib first  
CC=clang -S -O2 -D__ARM__ -fno-builtin -D__MACOS__ \
    -D__64BIT__ -fno-stack-protector -D__USEBIVA__ \
    -Wno-error=implicit-function-declaration \
    -I../pdos/pdpclib \
    -mmacosx-version-min=10 \
    -D__LONG64__ \
    -DC90
AR=ar
AS=as -mmacosx-version-min=10
LD=clang -Wl,-e, -Wl,___pdpent -mmacosx-version-min=10
EXTRA2=../pdos/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 \
    -DC90 \
    -I../pdos/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=../pdos/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__ -DC90 \
    -I. -I../pdos/pdpclib
AR=ar
AS=as
# -m64 isn't always recognized
LD=gcc -static -s -nostdlib
EXTRA=../pdos/pdpclib/linstart.o
EXTRA2=../pdos/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__ -DC90 \
    -I. -I../pdos/pdpclib
AR=ar
AS=as
LD=gcc -s -static -m64 -nostdlib -Wl,-e -Wl,__pdpent
EXTRA=../pdos/pdpclib/linstart.o
EXTRA2=../pdos/pdpclib/pdplinux.a


else

CC=gccwin
CFLAGS=-O2 -ansi
COPTS=$(CFLAGS) -S -fno-common -D__WIN32__ -D__STATIC__ \
    -I. -I../pdos/pdpclib -DC90 -DXHACK
AS=pdas --oformat coff
AR=xar
LD=pdld --no-insert-timestamp -s
EXTRA=../pdos/pdpclib/p32start.obj
EXTRA2=../pdos/pdpclib/pdpwin32.lib ../pdos/src/kernel32.lib

endif



TARGET=uemacs.exe
TEMP_ARCHIVE=temp.lib
OBJS=		ansi.obj basic.obj bind.obj buffer.obj display.obj file.obj \
		fileio.obj line.obj lock.obj main.obj random.obj region.obj \
		search.obj spawn.obj termio.obj window.obj word.obj

all: clean $(TARGET)

$(TARGET): $(OBJS)
  $(AR) s $(TEMP_ARCHIVE)
  $(LD) -o $(TARGET) $(EXTRA) main.obj $(TEMP_ARCHIVE) $(EXTRA2)
  rm -f $(TEMP_ARCHIVE)

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

clean:
  rm -f *.obj
  rm -f $(TARGET)
  rm -f $(TEMP_ARCHIVE)
