# Makefile for Fujitsu FM3

CPUTYPE = cortex-m3 -mthumb
FORMAT = ihex

TARGET = webserver
CSRC = 
CSRC += ../../fm3_sample_ether/example/source/fm3_uart.c
CSRC += ../../fm3_sample_ether/example/source/fm3_ether.c
CSRC += ../../fm3_sample_ether/example/source/debug_printf.c
CSRC += ../../fm3_sample_ether/example/source/tinylib.c
CSRC += ../../fm3_sample_ether/example/source/vsnprintf.c
#CSRC += ../../fm3_sample_ether/example/source/arp.c
#CSRC += ../../fm3_sample_ether/example/source/icmp.c
CSRC += ../../fm3_sample_ether/example/source/fm3_exti.c
CSRC += ../../fm3_sample_ether/common/core_cm3.c
CSRC += ../../fm3_sample_ether/common/system_mb9bf61x.c

CSRC += ../src/93c46.c
CSRC += ../src/bt.c
CSRC += ../src/clock-arch.c
CSRC += ../src/led.c
CSRC += ../src/main.c
CSRC += ../src/sbrk.c
CSRC += ../src/tapdev.c
CSRC += ../src/uip_user_app.c

CSRC += ../../uip/apps/webserver/httpd.c
CSRC += ../../uip/apps/webserver/httpd-cgi.c
CSRC += ../../uip/apps/webserver/httpd-fs.c
CSRC += ../../uip/apps/webserver/http-strings.c
CSRC += ../../uip/uip/psock.c
CSRC += ../../uip/uip/timer.c
CSRC += ../../uip/uip/uip.c
CSRC += ../../uip/uip/uip_arp.c
CSRC += ../../uip/uip/uiplib.c

#ASRC = 
ASRC = ../../fm3_sample_ether/example/gcc/startup_fm3.s

# C flags.
CDEFS =
CDEFS += -DNULL=0
CDEFS += -DHTTPD_USER_APP=httpd_user_app

CINCS = ../../fm3_sample_ether/example/source
CINCS += ../../fm3_sample_ether/common
CINCS += ../src
CINCS += ../../uip/apps/webserver
CINCS += ../../uip/uip

CFLAGS = -g
CFLAGS += $(CDEFS)
CFLAGS += -O0
CFLAGS += $(patsubst %,-I%,$(CINCS))
CFLAGS += -w
CFLAGS += -DMB9BF618T
#CFLAGS += -Wall -Wstrict-prototypes

# Assembler flags.
ASFLAGS = -g
#ASFLAGS = -Wa

# Linker flags.
LINC = C:/PROGRA~1/CodeSourcery/SOURCE~1/arm-none-eabi/lib/thumb2
LINC += C:/PROGRA~1/CodeSourcery/SOURCE~1/lib/gcc/arm-none-eabi/4.5.2/thumb2
LDFLAGS = -lc -lm -lgcc 
LDFLAGS += -T ../../fm3_sample_ether/example/gcc/mb9bf618.ld
LDFLAGS += $(patsubst %,-L%,$(LINC))

# Define programs and commands.
#SHELL = sh
CC = arm-none-eabi-gcc
LD = arm-none-eabi-ld
AS = arm-none-eabi-as
OBJCOPY = arm-none-eabi-objcopy
OBJDUMP = arm-none-eabi-objdump
SIZE = arm-none-eabi-size
NM = arm-none-eabi-nm
REMOVE = cs-rm -f
#COPY = cp
#YACC = bison
#LEX = flex

# Define all object files.
OBJ = $(CSRC:.c=.o) $(ASRC:.s=.o) 

# Define all listing files.
LST = $(ASRC:.s=.lst) $(CSRC:.c=.lst)

# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mcpu=$(CPUTYPE) -I. $(CFLAGS)
ALL_ASFLAGS = -mcpu=$(CPUTYPE) -I. -x assembler-with-cpp $(ASFLAGS)

#all: gccversion build sizeafter
all: build

build: elf hex lss sym

elf: $(TARGET).elf
hex: $(TARGET).hex
lss: $(TARGET).lss 
sym: $(TARGET).sym

stdlib:	lib
	$(MAKE) -C lib

# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) -A $(TARGET).elf
sizebefore:
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi

sizeafter:
	@if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi

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

%.lss: %.elf
	$(OBJDUMP) -D $< > $@

%.sym: %.elf
	$(NM) -n $< > $@

%.elf : $(OBJ)
	$(LD) $(OBJ) -o $@ $(LDFLAGS) 

# Compile: create object files from C source files.
%.o : %.c
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 

# Compile: create assembler files from C source files.
%.s : %.c
	$(CC) -S $(ALL_CFLAGS) $< -o $@

# Assemble: create object files from assembler source files.
%.o : %.s
	$(CC) -c $(ALL_ASFLAGS) $< -o $@

distclean: clean
	$(MAKE) -C clean

clean:
	$(REMOVE) $(TARGET).hex
	$(REMOVE) $(TARGET).elf
	$(REMOVE) $(TARGET).map
	$(REMOVE) $(TARGET).obj
	$(REMOVE) $(TARGET).lss
	$(REMOVE) $(OBJ)
	$(REMOVE) $(LST)
