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

# Produces 64-bit EFI executable.
# Links with PDPCLIB created by makefile.es6.

CC=x86_64-w64-mingw32-gcc
CFLAGS=-O2
LD=pdld
LDFLAGS=--no-insert-timestamp -s
AS=pdas --64 --oformat coff
AR=xar

COPTS=$(CFLAGS) -S -mno-red-zone -D__64BIT__ -D__EFI__ -U__WIN32__ -Icclib -Icclib/include -Icpplib -Icpplib/include -I. -I../pdpclib -fno-builtin -fno-common -ansi -Wno-builtin-declaration-mismatch

all: clean pdcc.efi

pdcc.efi: main.obj c_ppout.obj inc_path.obj cpplib.lib cclib.lib
    $(LD) $(LDFLAGS) -s -subsystem 10 -e efimain -o pdcc.efi ../pdpclib/efistart.obj main.obj c_ppout.obj inc_path.obj cpplib.lib cclib.lib ../pdpclib/pdpefi.lib

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/asgen.obj cclib/lexer.obj cclib/diag.obj
    rm -f cclib.lib
    $(AR) r cclib.lib cclib/cclib.obj cclib/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.lib pdcc.efi
