# This makefile is used to build the gateway ezsp uart application
#

all: gateway

SHELL = /bin/sh

OPTIONS=-Wcast-align -Wformat -Wimplicit -Wimplicit-int                 \
	-Wimplicit-function-declaration -Winline -Wlong-long -Wmain     \
	-Wnested-externs -Wno-import -Wparentheses -Wpointer-arith      \
	-Wredundant-decls -Wreturn-type -Wstrict-prototypes -Wswitch    \
	-Wunused \
	-g -O0 \
	-DGATEWAY_APP \
	-DEZSP_HOST \
	-DEZSP_UART \
	-DPLATFORM_HEADER=\"hal/micro/generic/compiler/gcc.h\"   \
	-DCONFIGURATION_HEADER=\"app/sensor-host/sensor-host-configuration.h\"


INCLUDES= -I../.. -I../../stack

CPPFLAGS = $(OPTIONS) $(INCLUDES)

SECURITY_FILES =\
	../util/security/trust-center.c	\
	../util/security/node.c


GATEWAY_FILES =                                 \
	../util/ezsp/ezsp.c                     \
	../util/ezsp/ezsp-callbacks.c           \
  ../util/ezsp/ezsp-enum-decode.c         \
	../util/ezsp/ezsp-frame-utilities.c     \
	../util/ezsp/ezsp-utils.c               \
	../util/ezsp/serial-interface-uart.c    \
        ../util/serial/linux-serial.c           \
        ../util/source-route-common.c	        \
        ../util/source-route-host.c	        \
	../ezsp-uart-host/ash-host.c            \
	../ezsp-uart-host/ash-host-io.c         \
	../ezsp-uart-host/ash-host-queues.c     \
	../ezsp-uart-host/ash-host-ui.c         \
        ../../hal/micro/generic/buzzer-stub.c   \
        ../../hal/micro/generic/led-stub.c      \
        ../../hal/micro/generic/micro-stub.c    \
	../../hal/micro/generic/ash-common.c    \
	../../hal/micro/generic/crc.c           \
	../../hal/micro/generic/random.c        \
	../../hal/micro/generic/system-timer.c  \
        ../sensor-host/common.c                 \
        ../sensor-host/sink.c							\
	$(SECURITY_FILES)

# This rule runs the C compiler to find the .h files referred to by a .c file
# and produces a 'make' dependency rule that encapsulates this.  We put the
# dependency rule is in a .d file that can be included back into the makefile.
# The sed command does two things:
#   - The C compiler produces a rule that ignores the path by which we found
#     the original .c file; sed adds that path back onto the name of the .c
#     file
#   - It makes the .d file dependent on the same things as the .o file, so
#     the .d file will be remade when any of those files change.
# One final note: the standard flag for producing dependencies is -M.  gcc
# has a -MM flag that leaves out references to system files like <stdio.h>.

%.d: %.c
	@set -e; $(CC) -MM $(CPPFLAGS) $<				\
                | sed 's^\($(*F)\)\.o[ :]*^$(@D)/\1.o $@ : ^g' > $@;	\
		echo 'created dependency file $@';			\
		[ -s $@ ] || rm -f $@

ifneq ($(MAKECMDGOALS),clean)
-include $(GATEWAY_FILES:.c=.d)
endif

gateway: $(GATEWAY_FILES:.c=.o)
	$(CC) -g $(OPTIONS) $^ -o gateway -static
	@set -e; echo ' '; echo '$@ build success'

clean:
	rm -f $(GATEWAY_FILES:.c=.o) $(GATEWAY_FILES:.c=.d)
	rm -f gateway gateway.exe
