PRG            = uip
LDSCRIPT       = lpc2138-rom.ld
LDFLAGS        =  -Wl,-Map,$(PRG).map
DEBUG          = -g
DEFS           =
LIBS           =

CC             = arm-elf-gcc
OBJCOPY        = arm-elf-objcopy
OBJDUMP        = arm-elf-objdump
ARCH           = arm-elf-ar

CFLAGS=-Wall -I. $(DEBUG) -mcpu=arm7tdmi -nostartfiles -T$(LDSCRIPT) \
		-Wcast-align $(OPTIMIZE) $(DEFS) -I../uip -I. -I../apps/httpd -I../apps/resolv \
		-I../apps/webclient -I../apps/smtp  -I../apps/telnet -fpack-struct

%.o:
	$(CC) $(CFLAGS) -c $(<:.o=.c)

%.o : %.s makefile
	$(CC) -c $(ASFLAGS) $< -o $@

%.o : %.c makefile
	$(CC) -c $(CFLAGS) $< -o $@

$(PRG).elf: boot.o timer.o print.o enc28j60.o uip.o uip_arch.o tapdev.o httpd.o main.o cgi.o fs.o uip_arp.o Makefile
	$(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) boot.o timer.o print.o enc28j60.o uip.o uip_arch.o tapdev.o httpd.o main.o cgi.o fs.o uip_arp.o -o $@

tapdev.o: tapdev.c uipopt.h
boot.o: boot.s
timer.o: timer.c
print.o: print.c
enc28j60.o: enc28j60.c enc28j60.h
main.o: main.c ../uip/uip.h uipopt.h ../apps/httpd/httpd.h \
 tapdev.h
uip_arch.o: uip_arch.c ../uip/uip_arch.h ../uip/uip.h uipopt.h \
 ../apps/httpd/httpd.h 
uip.o: ../uip/uip.c ../uip/uip.h uipopt.h ../apps/httpd/httpd.h 

uip_arp.o: ../uip/uip_arp.c ../uip/uip_arp.h ../uip/uip.h uipopt.h \
 ../apps/httpd/httpd.h
	$(CC) -o uip_arp.o $(CFLAGS) -fpack-struct -c ../uip/uip_arp.c


cgi.o: ../apps/httpd/cgi.c ../uip/uip.h uipopt.h ../apps/smtp/smtp.h \
 ../apps/httpd/cgi.h ../apps/httpd/httpd.h ../apps/httpd/fs.h
fs.o: ../apps/httpd/fs.c ../uip/uip.h uipopt.h ../apps/smtp/smtp.h \
 ../apps/httpd/httpd.h ../apps/httpd/fs.h ../apps/httpd/fsdata.h \
 ../apps/httpd/fsdata.c
fsdata.o: ../apps/httpd/fsdata.c
httpd.o: ../apps/httpd/httpd.c ../uip/uip.h uipopt.h \
 ../apps/smtp/smtp.h ../apps/httpd/httpd.h ../apps/httpd/fs.h \
 ../apps/httpd/fsdata.h ../apps/httpd/cgi.h

clean:
	rm -rf *.o *.lst $(PRG).elf $(PRG).hex $(PRG).bin

hex:  $(PRG).hex

%.hex: %.elf
	$(OBJCOPY) -O ihex $< $@

bin: $(PRG).bin

%.bin: %.elf
	$(OBJCOPY) -O binary $< $@

run: $(PRG).hex
	jtag_flash $(PRG).hex

debug: $(PRG).hex
	jtag_flash $(PRG).hex N
	arm-elf-insight $(PRG).elf &
	jtag_stub

