IS_TOOLCHAIN_THERE := $(shell perl -e 'if (-e "/opt/cnstools/arm-linux-glibc-static-2009.q1_RC4/tools/bin/arm-cavm-linux-gnueabi-gcc") { print "yes\n"; } else { print "no\n"; }')
ifeq ($(IS_TOOLCHAIN_THERE), no)
    $(error Please install the arm-linux-glibc-static-2009.q1_RC4 toolchain)
endif

TMP := $(findstring arm-linux-glibc-static-2009.q1_RC4, $(PATH))
ifeq ($(TMP),)
    OLDPATH := $(PATH)
    PATH    := /opt/cnstools/arm-linux-glibc-static-2009.q1_RC4/tools/bin:/opt/cnstools/arm-linux-glibc-static-2009.q1_RC4/utils/bin:$(OLDPATH)
endif

CROSS_COMPILE := arm-cavm-linux-gnueabi-
CC            := $(CROSS_COMPILE)gcc
LD            := $(CROSS_COMPILE)ld
OBJDUMP       := $(CROSS_COMPILE)objdump
OBJCOPY       := $(CROSS_COMPILE)objcopy
CFLAGS        := -g -O0 -fno-strict-aliasing -fno-common -ffixed-r8 -DTEXT_BASE=0 -fno-builtin -ffreestanding -nostdinc -pipe -march=armv4 -mno-thumb-interwork -mabi=apcs-gnu -Wall -Werror -Wstrict-prototypes -fno-stack-protector
IMAGE         := ../cavm_bootloader.bin
OBJECTS       := start.o main.o image.o
TARGETS       := piggyback.elf piggyback.bin $(OBJECTS)

.PHONY:	all clean
all:	$(TARGETS)

clean:
	-rm -f $(TARGETS)

start.o:	start.S
	$(CC) $(CFLAGS) -D__ASSEMBLY__ -c start.S

image.o:	image.S $(IMAGE)
	$(CC) $(CFLAGS) -DIMAGE=\"$(IMAGE)\" -c image.S

piggyback.elf:	$(OBJECTS)
	$(LD) -Bstatic -T piggyback.lds  -Ttext 0 $(OBJECTS) -o piggyback.elf

piggyback.bin:	piggyback.elf
	$(OBJCOPY) --gap-fill=0xff -O binary piggyback.elf piggyback.bin
