####################################################################
# 								   #
# 	Makefile 						   #
# 	Written by Kim Sang-Cheol (ETRI), 2007			   #
# 	Des : Makefile for Nano OS and sensor applications   	   #
#								   #
# 	Usage :							   #
#	% make			(create xxx.rom)		   #
#								   #
#	To remove xxx.rom file 					   #
#	% make clean		(delete xxx.rom in apps dir)       #
#				   				   #
#	To generate kconf.h file				   #
#	% make menuconfig					   #
#							   	   #
####################################################################

	RM	= rm -f
	MKDIR	= mkdir -p 
	CP	= cp
	MV	= mv
	TOUCH	= touch
	AR	= ar

	OBJ	= $(SRC:.c=.o)
	NOS_HOME2 = $(subst /cygdrive/c/cygwin,,$(NOS_HOME))
	SRCDIR2 = $(subst /cygdrive/c/cygwin,,$(SRCDIR))
	RSRCDIR = $(subst $(NOS_HOME2),..,$(SRCDIR2))

ifeq ($(filter ..%,$(RSRCDIR)),)
        RSRCDIR = $(strip $(subst ../ ,../, $(filter ../%, $(subst /, ../ ,$(NOS_HOME2)))..$(SRCDIR2)))
endif   

## compiler and link flags
	INC	= -I. \
		  -I$(RSRCDIR) \
		  -I../nos/include \
		  -I../nos/kernel \
		  -I../nos/kernel/thread \
		  -I../nos/arch \
		  -I../nos/arch/$(MCU) \
		  -I../nos/arch/$(MCU)/mm \
		  -I../nos/platform/$(PLATFORM) \
		  -I../nos/platform/$(PLATFORM)/sensor \
		  -I../nos/drivers/rf \
		  -I../nos/net/mac \
		  -I../nos/net/mac/nano-mac \
		  -I../nos/net/routing \
		  -I../nos/net/routing/reno

	LIBNAME = libnos
	LIB	= -L.

	## For debug, add "-O0", "-DDEBUG" and "-gstabs" option in the CPFLAGS
	#CPFLAGS	= -DNOS -DATMEGA128 -g -O2 -std=gnu99 -Wall -Wstrict-prototypes -mmcu=$(MCU) 
	CPFLAGS	= -DNOS -D$(MCU) -O0 -std=gnu99 -Wall -Wstrict-prototypes -Wa,-ahlms=$(notdir $(<:.c=.lst)) -mmcu=$(MCU)
## for debug
#ifeq ($(CONFIG_DEBUG), y)
	CPFLAGS += -DDEBUG -gstabs
#endif
	ASFLAGS = -Wa,-ahlms=$(notdir $(<:.asm=.lst)),-gstabs -mmcu=$(MCU)
	# -ahlms : create listing
	# -gstabs : have the assembler create line number information
	MAP=$(RSRCDIR)/$(TRG).map
	LDFLAGS	= -mmcu=$(MCU) -Wl,-Map,$(MAP),--cref
	# --cref : add cross reference to map file

## 	for library 
	
	SRCSUBDIR = .	\
		  	$(RSRCDIR) \
			../nos/include \
			../nos/kernel \
			../nos/kernel/thread \
			../nos/arch \
			../nos/arch/$(MCU) \
			../nos/arch/$(MCU)/mm \
			../nos/platform/$(PLATFORM) \
			../nos/platform/$(PLATFORM)/sensor \
			../nos/drivers/rf \
			../nos/net/mac \
			../nos/net/mac/nano-mac \
			../nos/net/routing \
			../nos/net/routing/reno
		
	SRCSUBDIR2 = . \
			../nos/include \
			../nos/kernel \
			../nos/kernel/thread \
			../nos/arch/\
			../nos/arch/$(MCU) \
			../nos/arch/$(MCU)/mm \
			../nos/platform/$(PLATFORM) \
			../nos/platform/$(PLATFORM)/sensor \
			../nos/drivers/rf \
			../nos/net/mac \
			../nos/net/mac/nano-mac \
			../nos/net/routing \
			../nos/net/routing/reno
	
	LIBSRC = $(foreach dir,$(SRCSUBDIR),$(wildcard $(dir)/*.c)) 
	LIBOBJ = $(LIBSRC:.c=.o) $(ASRC:.asm=.o)
	LIBLST = $(notdir $(LIBSRC:.c=.lst))

	LIBSRC2 = $(foreach dir,$(SRCSUBDIR2),$(wildcard $(dir)/*.c)) 
	LIBOBJ2 = $(LIBSRC2:.c=.o) $(ASRC:.asm=.o)

##	for kconfig
	srctree=
	export srctree
	
## this defines the aims of the make process     
print :
	@echo "NOS_HOME2="$(NOS_HOME2)
	@echo "RSRCDIR="$(RSRCDIR)

all :	$(TRG).rom 
	$(RM) *.lst
	@echo ""
	@echo "### NOTE ###"
	@echo "- '$(TRG).rom' has been created."
	@echo "- To recompile it, 'make lib'."
	@echo "- For debugging, 'make debug'"

## create bin (ihex, srec) file from elf output file
$(TRG).rom : $(TRG).elf
	$(OBJCOPY) -O ihex $(RSRCDIR)/$< $(RSRCDIR)/$@
	$(OBJDUMP) -h -S $(RSRCDIR)/$< > $(RSRCDIR)/$(TRG).lst

## link: instructions to create elf output file from object files56
## Check if libnos.a was created
$(TRG).elf : $(LIBOBJ)
	$(CC) $(LIBOBJ) $(LIB) $(LDFLAGS) -o $(RSRCDIR)/$@

## compile to create object files
%o : %c $(SRCDIR)/kconf.h
	$(CC) -c $(CPFLAGS) $(INC) $< -o $@

%o : %asm
	$(CC) -c $(ASFLAGS) $(INC) -x assembler-with-cpp $< -o $@

## make instruction to delete created files
clean:
	$(RM) *.rom *.hex *.lst *.map *.a *.e2s *.o *.c *.elf
	$(RM) $(LIBOBJ)
	$(RM) -r DEBUG

lib : $(LIBOBJ2) 
	$(RM) *.lst
	$(AR) rscv $(RSRCDIR)/$(LIBNAME).a $(LIBOBJ2)
	

debug : clean lib $(TRG).rom
	$(MKDIR) DEBUG
	$(MV)	*.lst *.map DEBUG
	@echo ""
	@echo "### NOTE ###"
	@echo "- '$(TRG).rom' has been created."

# Definitions of MCU related variables and target
# this makefile defines
# CC, OBJCOPY, OBJDUMP, ISPEXE, "make burn" "make usb" "make lpt"
ifneq ($(MCU),)
include $(NOS_HOME)/nos/arch/$(MCU)/Makefile
endif
