# Checking variable (you should define these as global)
ifeq ($(strip $(WRS_BASE_DIR)),)
$(error You first need to define WRS_BASE_DIR variable)
#WRS_BASE_DIR=/home/neub/Work/7S/WR/wr-switch-sw/build
endif
ifeq ($(strip $(CROSS_COMPILE)),)
$(warning You might define CROSS_COMPILE variable)
endif
ifeq ($(strip $(WR_INSTALL_ROOT)),)
WR_INSTALL_ROOT=/tftpboot/rootfs-test/wr/
endif

# Standard stanza for cross-compilation (courtesy of the linux makefile)
AS              = $(CROSS_COMPILE)as
LD              = $(CROSS_COMPILE)ld
CC              = $(CROSS_COMPILE)gcc
AR              = $(CROSS_COMPILE)ar
NM              = $(CROSS_COMPILE)nm
STRIP           = $(CROSS_COMPILE)strip
OBJCOPY         = $(CROSS_COMPILE)objcopy
OBJDUMP         = $(CROSS_COMPILE)objdump

TARGETS :=  wrs-iftool

# Calculate endianness before everything, for my ptpd-wrappers.h to have it
CFLAGS += $(shell ./check-endian $(CC))

# Flags for standalone compilation
TOPDIR=$(shell /bin/pwd)
CFLAGS =  -Wall -ggdb  -O2 -DDEBUG \
		-I$(LINUX)/include \
		-I$(LINUX)/arch/arm/mach-at91/include \
		-I${WRS_BASE_DIR}/../userspace/ptp-noposix/libptpnetif \
		-I${WRS_BASE_DIR}/../userspace/wrsw_hal \
		-I${WRS_BASE_DIR}/../userspace/wrsw_rtud \
		-I${WRS_BASE_DIR}/../userspace/mini-rpc \
		-I${WRS_BASE_DIR}/../userspace/ptp-noposix/PTPWRd \
		-I${WRS_BASE_DIR}/../userspace/ptp-noposix/libposix \
		-I${WRS_BASE_DIR}/../userspace/include  
	
	
LDFLAGS =  -L$(WR_INSTALL_ROOT)/lib \
		-L${WRS_BASE_DIR}/../userspace/mini-rpc \
		-L${WRS_BASE_DIR}/../userspace/ptp-noposix \
		-L${WRS_BASE_DIR}/../userspace/libswitchhw \
		-lminipc -lptpnetif -lswitchhw	

CFLAGSII= -g3 -O0 -lm  -w   -Wall

# The main objects are all from the ptp directory
all: $(TARGETS)

# the binary is just a collection of object files.
# However, we need a freestanding version, so we build two binaries:
# one is gnu/linux-based (well, "posix") and the other is freestanding.
# The "ptpd.o" object is used to run "nm" on it and drive patches
$(TARGETS) : $(TARGETS).o $(CORELIBS) 
	$(CC) $(CFLAGS) $< $(LDFLAGS) -o $@

install: $(TARGETS)
	cp $(TARGETS) $(WR_INSTALL_ROOT)/bin

# This is a target for me: print all headers included by this tree
printh:
	grep -h '^#include' $(OBJS:.o=.c) $$(find . -name \*.h) | \
		sort | uniq -c

printhf:
	grep '^#include' $(OBJS:.o=.c) $$(find . -name \*.h) | \
		sort -k 2


# clean and so on.
clean:
	rm -f $(TARGETS) *.o *.a */*.o */dep/*.o *~ */*~ */dep/*~ 
# even if there are files with these names, ignore them
.PHONY: all check libs clean distclean printh printhf
