include				$(MORPHEO_TOPLEVEL)/Makefile.tools

#-----[ Directory ]---------------------------------------------------------------
DIRECTORY_OBJ			= obj
DIRECTORY_BIN			= bin

EXE				= soft

#-----[ Files ]-------------------------------------------------------------------
OBJECTS				= $(OBJECTS_SYS) $(OBJECTS_ASM) $(OBJECTS_C)

#-----[ To the compilation ]------------------------------------------------------
OPTIMIZE			= -O3 -fomit-frame-pointer -fdelayed-branch -mror -mcmov -msext -mhard-mul -msoft-div -msoft-float
#Option :
# -fomit-frame-pointer 			: n'utilise pas le pointeur de frame
# -fdelayed-branch   			: utilise les delayed slot
# ON		OFF
# -mror   	-mno-ror              	: utilisation de l'instruction l.ror
# -mcmov   	-mno-cmov       	: utilisation de l'instruction l.cmov
# -msext	-mno-sext		: utilisation des instructions l.extXX
# -mhard-mul	-msoft-mul	        : utilisation du multiplieur materiel
# -mhard-div	-msoft-div              : utilisation du diviseur materiel
# -mhard-float	-msoft-float      	: utilisation du flottant materiel
# -msibcall     -mno-sibcall            : Enable sibcall optimization
# -mlogue	-mno-logue 		: Schedule pro/epi-logue.
# -maj		-mno-aj     		: Use aligned jumps.

# Tools
CC				= $(OR1K_BIN)/or32-elf-gcc
AS				= $(OR1K_BIN)/or32-elf-as
LD				= $(OR1K_BIN)/or32-elf-ld
OBJDUMP				= $(OR1K_BIN)/or32-elf-objdump
NM           			= $(OR1K_BIN)/or32-elf-nm

# Tools's option
CC_OPT				= $(INCLUDE) $(OPTIMIZE) -Wlong-long
# -DHAVE_LIBC
AS_OPT				=
LD_OPT				= -T$(DIRECTORY_LDSCRIPT)/or32.ld $(LIBRARY)  
OBJDUMP_OPT			= -D
NM_OPT				= -n

#-----[ Rules ]-------------------------------------------------------------------
.PRECIOUS			: $(DIRECTORY_BIN)/%.x.txt $(DIRECTORY_BIN)/%.x $(DIRECTORY_OBJ)/%.o

all 				: test_env $(DIRECTORY_BIN)/$(EXE).x.txt

$(DIRECTORY_BIN)/%.x.txt	: $(DIRECTORY_BIN)/%.x
				@\
                                $(ECHO) "List symbols       : $*";		\
				$(NM)      $(NM_OPT)      $^ > $@.nm;		\
				$(ECHO) "Display info       : $*";		\
				$(OBJDUMP) $(OBJDUMP_OPT) $^ > $@;

$(DIRECTORY_BIN)/%.x		: $(OBJECTS)
				@						\
				$(ECHO) "Linkage            : $*";		\
				$(LD) -o $@ $^ $(LD_OPT);

$(DIRECTORY_OBJ)/%.o		: $(DIRECTORY_ASM)/%.s	
				@						\
                                $(ECHO) "Compilation        : $*";		\
				$(CC) $(CC_OPT)     -o $@   -c $^ ;		\
				$(CC) $(CC_OPT)  -S -o $@.s -c $^ ;
#				@$(AS) $(AS_OPT) $^ -o $@

$(DIRECTORY_OBJ)/%.o		: $(DIRECTORY_SYS)/%.s	
				@						\
                                $(ECHO) "Compilation        : $*";		\
				$(CC) $(CC_OPT)     -o $@   -c $^ ;		\
				$(CC) $(CC_OPT)  -S -o $@.s -c $^ ;
#				@$(AS) $(AS_OPT) $^ -o $@

$(DIRECTORY_OBJ)/%.o		: $(DIRECTORY_C)/%.c
				@						\
                                $(ECHO) "Compilation        : $*";		\
				$(CC) $(CC_OPT)     -o $@   -c $^ ;		\
				$(CC) $(CC_OPT)  -S -o $@.s -c $^ ;

#-----[ Environement ]------------------------------------------------------------
test_env			:
# ifeq ($(origin TOOLS), undefined)
#				$(error "variable TOOLS is undefined");
# endif
# ifeq ($(origin SOFT), undefined)
#				$(error "variable SOFT  is undefined");
# endif
				@$(MKDIR) $(DIRECTORY_OBJ) $(DIRECTORY_BIN)

#-----[ Maintenance ]-------------------------------------------------------------
clean 				:
				@\
				$(ECHO) "Delete     temporary files              "`$(PWD)`;		\
				$(RM) $(DIRECTORY_OBJ) $(DIRECTORY_BIN);						\
				$(MAKE) clean_rec DIRECTORY_CLEAN=.;

clean_rec			:
				@\
				$(ECHO) "Delete     temporary files in directory $(DIRECTORY_CLEAN)";		\
				$(RM) -f $(DIRECTORY_CLEAN)/*~;						\
				for files in `$(LS) $(DIRECTORY_CLEAN)`; do					\
				    	if $(TEST) -d $(DIRECTORY_CLEAN)/$$files; 				\
					then 								\
						$(MAKE) clean_rec DIRECTORY_CLEAN=$(DIRECTORY_CLEAN)/$$files;	\
					fi; 								\
				done;

#-----[ Help ]--------------------------------------------------------------------
help				:
				@\
				$(ECHO) "";\
				$(ECHO) "List of directive   : ";\
				$(ECHO) " * make             : Compile the software";\
				$(ECHO) " * make clean       : Erase all files generates";\
				$(ECHO) "";
