# GNU Makefile for DJP execompressor

CC = gcc
CFLAGS  = -O2 -fomit-frame-pointer 
#CFLAGS  = -g
CFLAGS += -Wall -W -Wwrite-strings -Wno-unused -Ilzo/include 
LDFLAGS = -s
AFLAGS = -x assembler-with-cpp

# cpp for use with djasm
DJCPP = cpp -Wall -P -C -traditional

STUBS = djpstub.h startup.h stub.h

.PHONY: all stubs clean

all: djp.exe djpstub.exe

stubs: $(STUBS)

clean:
	-$(RM) *.o *.cof *.as_
	-$(RM) $(STUBS) djpstub.inc stub.asm
	-$(RM) djp.exe djpstub.exe makeh.exe

djp.exe : djp.o lzo/liblzo.a
	gcc $(LDFLAGS) $^ -o $@

startup.cof : startup.s
	gcc $(AFLAGS) -c $< -o startup.o
	ld --oformat coff-go32 -s startup.o -o $@

djpstub.cof : djpstub.s
	gcc $(AFLAGS) -c $< -o djpstub.o
	ld --oformat coff-go32 -s djpstub.o -o $@

makeh.exe : makeh.o 
	gcc  -s $< -o $@

startup.h : makeh.exe startup.cof
	makeh.exe 0

djpstub.inc : makeh.exe djpstub.cof
	makeh.exe 1

djpstub.h : djpstub.asm djpstub.inc
	$(DJCPP) -DDJP $< djpstub.as_
	djasm djpstub.as_ $@

djpstub.exe : djpstub.asm djpstub.inc
	$(DJCPP) -DDJP $< djpstub.as_
	djasm djpstub.as_ $@

stub.asm : djpstub.asm
	$(DJCPP) $< $@

stub.h : stub.asm
	djasm $< $@

lzo/liblzo.a:
	$(MAKE) target=djgpp2 -C lzo lib

# dependencies
djp.o: djp.c $(STUBS)
