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



ifeq "$(flavor)" "x64"
COPT2=-DTARGET_X64
TDIR=x64

else ifeq "$(flavor)" "i370"
COPT2=-DTARGET_I370
TDIR=i370
EXTRA3=tebc.obj

else ifeq "$(flavor)" "zarch"
COPT2=-DTARGET_ZARCH
TDIR=zarch
EXTRA3=tebc.obj

else
COPT2=-DTARGET_I386
TDIR=i386

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=xar
LD=pdld
LDFLAGS=-s --no-insert-timestamp
COPTS=-S $(CFLAGS) -fno-common -ansi -Icclib -Icclib/include -Icpplib \
    -Icpplib/include -I. -I../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 \
   -Icclib -Icclib/include -Icpplib \
   -Icpplib/include -I. -I../pdpclib $(COPT2) \
   -mmacosx-version-min=10 \
   -D__LONG64__
AR=ar
AS=as
LD=clang -Wl,-e, -Wl,__start
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 \
    -Icclib -Icclib/include -Icpplib \
    -Icpplib/include -I. -I../pdpclib $(COPT2)
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__ \
   -Icclib -Icclib/include -Icpplib \
   -Icpplib/include -I. -I../pdpclib $(COPT2)
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__ \
   -Icclib -Icclib/include -Icpplib \
   -Icpplib/include -I. -I../pdpclib $(COPT2)
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 ifeq "$(targ)" "gccia32"
# makefile.lnx needs to have been run in pdpclib first
CC=gcc -S -O2 -D__gnu_linux__ -nostdinc -nostdlib -fno-builtin \
   -fno-stack-protector --no-pie -D__NOBIVA__ \
   -m32 -fno-common \
   -Icclib -Icclib/include -Icpplib \
   -Icpplib/include -I. -I../pdpclib $(COPT2)
AR=ar
AS=as --32
LD=gcc -s -static -m32 -nostdlib
EXTRA1=../pdpclib/linstart.o ../pdpclib/pdplinux.a
EXTRA2=../pdpclib/pdplinux.a


else ifeq "$(targ)" "mingw64"
# links with PDPCLIB created with makefile.s64
CC=x86_64-w64-mingw32-gcc
CFLAGS=-O2
LD=pdld
LDFLAGS=-s --no-insert-timestamp
AS=pdas --64 --oformat coff
AR=xar
COPTS=-S $(CFLAGS) -fno-common -ansi -Icclib -Icclib/include -Icpplib \
    -Icpplib/include -I. -I../pdpclib -D__WIN32__ -D__NOBIVA__ $(COPT2) \
    -D__64BIT__
EXTRA1=../pdpclib/w32start.obj
EXTRA2=../pdpclib/msvcrt.lib ../pdpclib/x64supa.obj


else
# Produces Windows executable.
# Links with PDPCLIB created by makefile.std
CC=gccwin
CFLAGS=-O2
LD=pdld
LDFLAGS=-s --no-insert-timestamp
AS=pdas --oformat coff
AR=xar
COPTS=-S $(CFLAGS) -fno-common -ansi -pedantic-errors \
    -Icclib -Icclib/include -Icpplib \
    -Icpplib/include -I. -I../pdpclib -D__WIN32__ -D__NOBIVA__ $(COPT2)
EXTRA1=../pdpclib/w32start.obj
EXTRA2=../pdpclib/msvcrt.lib
endif




all: clean pdcc.exe

pdcc.exe: main.obj $(EXTRA3) c_ppout.obj inc_path.obj cpplib.lib cclib.lib
    $(LD) $(LDFLAGS) -o pdcc.exe $(EXTRA1) main.obj $(EXTRA3) c_ppout.obj inc_path.obj cpplib.lib cclib.lib $(EXTRA2)

cpplib.lib: cpplib/core.obj cpplib/directs.obj cpplib/errors.obj \
          cpplib/expr.obj cpplib/files.obj cpplib/chars.obj \
          cpplib/idents.obj cpplib/init.obj cpplib/lex.obj \
          cpplib/macro.obj cpplib/support.obj cpplib/symtab.obj \
          xmalloc.obj
    rm -f cpplib.lib
    $(AR) r cpplib.lib cpplib/core.obj cpplib/directs.obj cpplib/errors.obj
    $(AR) r cpplib.lib cpplib/expr.obj cpplib/files.obj cpplib/chars.obj
    $(AR) r cpplib.lib cpplib/idents.obj cpplib/init.obj cpplib/lex.obj
    $(AR) r cpplib.lib cpplib/macro.obj cpplib/support.obj cpplib/symtab.obj
    $(AR) r cpplib.lib xmalloc.obj
    $(AR) s cpplib.lib

cclib.lib: cclib/cclib.obj cclib/$(TDIR)/asgen.obj cclib/lexer.obj cclib/diag.obj \
         xmalloc.obj
    rm -f cclib.lib
    $(AR) r cclib.lib cclib/cclib.obj cclib/$(TDIR)/asgen.obj cclib/lexer.obj cclib/diag.obj
    $(AR) s cclib.lib

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

clean:
    rm -f *.obj cpplib/*.obj cpplib.lib cclib/*.obj cclib/$(TDIR)/*.obj cclib.lib pdcc.exe
