# common definition
include			Makefile.morpheo

#-----[ Directory ]---------------------------------------------------------------

DIR_LOCAL    		= .
DIR_LOCAL_C		= $(DIR_LOCAL)/src/c
DIR_LOCAL_ASM		= $(DIR_LOCAL)/src/asm
DIR_LOCAL_SYS		= $(DIR_LOCAL)/src/sys
DIR_LOCAL_INC       	= $(DIR_LOCAL)/src/c/include

DIR_GLOBAL     		= $(SOFT)/Global.or32
DIR_GLOBAL_C		= $(DIR_GLOBAL)/src/c
DIR_GLOBAL_ASM		= $(DIR_GLOBAL)/src/asm
DIR_GLOBAL_SYS		= $(DIR_GLOBAL)/src/sys
DIR_GLOBAL_INC       	= $(DIR_GLOBAL)/include

DIR_INC			= -I$(DIR_LOCAL_INC) -I$(DIR_GLOBAL_INC)
DIR_OBJ			= obj
DIR_BIN			= bin

EXE			= soft

#-----[ Files ]-------------------------------------------------------------------
OBJ_ASM			= $(patsubst $(DIR_LOCAL_ASM)/%.s,$(DIR_OBJ)/%.o,$(wildcard $(DIR_LOCAL_ASM)/*.s))	\
			  $(patsubst $(DIR_GLOBAL_ASM)/%.s,$(DIR_OBJ)/%.o,$(wildcard $(DIR_GLOBAL_ASM)/*.s))
OBJ_SYS			= $(patsubst $(DIR_LOCAL_SYS)/%.s,$(DIR_OBJ)/%.o,$(wildcard $(DIR_LOCAL_SYS)/*.s))	\
			  $(patsubst $(DIR_GLOBAL_SYS)/%.s,$(DIR_OBJ)/%.o,$(wildcard $(DIR_GLOBAL_SYS)/*.s))
OBJ_C           	= $(patsubst $(DIR_LOCAL_C)/%.c,$(DIR_OBJ)/%.o,$(wildcard $(DIR_LOCAL_C)/*.c))		\
			  $(patsubst $(DIR_GLOBAL_C)/%.c,$(DIR_OBJ)/%.o,$(wildcard $(DIR_GLOBAL_C)/*.c))

OBJ			= $(OBJ_SYS) $(OBJ_ASM) $(OBJ_C)

#-----[ Rules ]-------------------------------------------------------------------
.PRECIOUS		: $(DIR_BIN)/%.x $(DIR_OBJ)/%.o $(DIR_OBJ)/%.a

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

$(DIR_BIN)/%.x.txt	: $(DIR_BIN)/%.x
			@$(ECHO) "List symbols        of $*"
			@$(NM)      $(NM_OPT)      $^ > $@.nm
			@$(ECHO) "Display information of $*"
			@$(OBJDUMP) $(OBJDUMP_OPT) $^ > $@

$(DIR_BIN)/%.x		: $(OBJ_SYS) $(OBJ) 
			@$(ECHO) "Linkage             of $*"
			@$(LD) -o $@ $^ $(LD_OPT)

$(DIR_OBJ)/%.o		: $(DIR_LOCAL_ASM)/%.s	
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -S -o $@.s -c $^ 
#			@$(AS) $(AS_OPT) $^ -o $@

$(DIR_OBJ)/%.o		: $(DIR_LOCAL_SYS)/%.s	
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -S -o $@.s -c $^ 
#			@$(AS) $(AS_OPT) $^ -o $@

$(DIR_OBJ)/%.o		: $(DIR_LOCAL_C)/%.c
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -S -o $@.s -c $^ 

$(DIR_OBJ)/%.o		: $(DIR_GLOBAL_ASM)/%.s	
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -S -o $@.s -c $^ 
#			@$(AS) $(AS_OPT) $^ -o $@

$(DIR_OBJ)/%.o		: $(DIR_GLOBAL_SYS)/%.s	
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -S -o $@.s -c $^ 
#			@$(AS) $(AS_OPT) $^ -o $@

$(DIR_OBJ)/%.o		: $(DIR_GLOBAL_C)/%.c
			@$(ECHO) "Compile             of $*"
			@$(CC) $(CC_OPT) $(DIR_INC)    -o $@   -c $^ 
			@$(CC) $(CC_OPT) $(DIR_INC) -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) $(DIR_OBJ) $(DIR_BIN)

#-----[ Maintenance ]-------------------------------------------------------------
clean 			:
			@$(ECHO) "Delete     temporary files              "`$(PWD)`
			@$(RM) $(DIR_OBJ) $(DIR_BIN)
			@$(MAKE) clean_rec DIR_CLEAN=.

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

#-----[ Help ]--------------------------------------------------------------------
help			:
			@$(ECHO) ""
			@$(ECHO) "List of directive   : "
			@$(ECHO) " * make             : Compile the software"
			@$(ECHO) " * make clean       : Erase all files generates"
			@$(ECHO) " * make bench_list  : View all benchmark"
			@$(ECHO) " * make bench_exec  : Print the file \"$(FILE_BENCH)\" - it's the Workload"
			@$(ECHO) " * make bench       : Compile the Benchmark Suite"
			@$(ECHO) ""