ifeq ($(origin MORPHEO_TOPLEVEL), undefined)
	$(error "variable MORPHEO_TOPLEVEL is undefined");
else
	include $(MORPHEO_TOPLEVEL)/Makefile.tools
	include $(MORPHEO_TOPLEVEL)/Makefile.flags
endif

#-----[ Directory ]----------------------------------------
DIR_LIB				= $(MORPHEO_PREFIX)/lib/$(MORPHEO_TYPE)
DIR_ENVIRONMENT			= .
DIR_CACHE			= Cache
DIR_DATA			= Data
DIR_ENDIANNESS			= Endianness
DIR_QUEUE			= Queue
DIR_RAMLOCK			= RamLock
DIR_SIM2OS			= Sim2OS
DIR_TTY   			= TTY

include $(DIR_CACHE)/Makefile.defs
include $(DIR_DATA)/Makefile.defs
include $(DIR_ENDIANNESS)/Makefile.defs
include $(DIR_QUEUE)/Makefile.defs
include $(DIR_RAMLOCK)/Makefile.defs
include $(DIR_SIM2OS)/Makefile.defs
include $(DIR_TTY)/Makefile.defs

#-----[ Variable ]-----------------------------------------

ENTITY                          := Environment
DIR_SRC				 = $(DIR_ENVIRONMENT)/src

include                          $(DIR_ENVIRONMENT)/Makefile.defs

LIBRARY_NAME			:= $(ENTITY)
LIBRARY_FILE			:= $(DIR_LIB)/lib$(LIBRARY_NAME).a

Environment_OBJECTS_DEPS	:= 	$(OBJECTS) \
					$(Cache_OBJECTS_DEPS) \
					$(Data_OBJECTS_DEPS) \
					$(Endianness_OBJECTS_DEPS) \
					$(Queue_OBJECTS_DEPS) \
					$(RamLock_OBJECTS_DEPS) \
					$(Sim2OS_OBJECTS_DEPS) \
					$(TTY_OBJECTS_DEPS)

#-----[ Rules ]--------------------------------------------

all				: all_parallel

all_step			:
				@\
				$(MAKE) lib_cache;      \
				$(MAKE) lib_data;       \
				$(MAKE) lib_endianness; \
				$(MAKE) lib_queue;      \
				$(MAKE) lib_ramlock;    \
				$(MAKE) lib_sim2os;     \
				$(MAKE) lib_tty;        \
				$(MAKE) $(LIBRARY_FILE);

all_parallel			: 	lib_cache      \
					lib_data       \
					lib_endianness \
					lib_queue      \
					lib_ramlock    \
					lib_sim2os     \
					lib_tty
				@\
				$(MAKE) $(LIBRARY_FILE);

lib_cache			:
				@\
				$(MAKE) --directory=$(DIR_CACHE)      only;
lib_data			:
				@\
				$(MAKE) --directory=$(DIR_DATA)       only;
lib_endianness			:
				@\
				$(MAKE) --directory=$(DIR_ENDIANNESS) only;
lib_queue			:
				@\
				$(MAKE) --directory=$(DIR_QUEUE)      only;
lib_ramlock			:
				@\
				$(MAKE) --directory=$(DIR_RAMLOCK)    only;
lib_sim2os			:
				@\
				$(MAKE) --directory=$(DIR_SIM2OS)     only;
lib_tty				:
				@\
				$(MAKE) --directory=$(DIR_TTY)        only;

$(LIBRARY_FILE)			: $(DIR_LIB) $(Environment_OBJECTS_DEPS)
				@\
				$(ECHO) "Archive            : $@";\
				$(AR)  -r $@ $(Environment_OBJECTS_DEPS);\
				$(RANLIB) $@;

$(DIR_LIB)			:
				@\
				$(ECHO) "Create directory   : $@";\
				$(MKDIR) $@

exe_all				:
				@\
				$(MAKE) --directory=$(DIR_CACHE)      clean; \
				$(MAKE) --directory=$(DIR_CACHE)      exe; \
				$(MAKE) --directory=$(DIR_DATA)       clean; \
				$(MAKE) --directory=$(DIR_DATA)       exe; \
				$(MAKE) --directory=$(DIR_ENDIANNESS) clean; \
				$(MAKE) --directory=$(DIR_ENDIANNESS) exe; \
				$(MAKE) --directory=$(DIR_QUEUE)      clean; \
				$(MAKE) --directory=$(DIR_QUEUE)      exe; \
				$(MAKE) --directory=$(DIR_RAMLOCK)    clean; \
				$(MAKE) --directory=$(DIR_RAMLOCK)    exe; \
				$(MAKE) --directory=$(DIR_SIM2OS)     clean; \
				$(MAKE) --directory=$(DIR_SIM2OS)     exe; \
				$(MAKE) --directory=$(DIR_TTY)        clean; \
				$(MAKE) --directory=$(DIR_TTY)        exe; \
				$(MAKE) clean; \
				$(MAKE) exe;

clean				:
				@\
				$(MAKE) --directory=$(DIR_CACHE)      clean; \
				$(MAKE) --directory=$(DIR_DATA)       clean; \
				$(MAKE) --directory=$(DIR_ENDIANNESS) clean; \
				$(MAKE) --directory=$(DIR_QUEUE)      clean; \
				$(MAKE) --directory=$(DIR_RAMLOCK)    clean; \
				$(MAKE) --directory=$(DIR_SIM2OS)     clean; \
				$(MAKE) --directory=$(DIR_TTY)        clean; \
				$(MAKE) environment_clean; \
				$(RM) tty_*;

clean_all			: clean
				@\
				$(RM) $(DIR_LIB);

help				:
				@$(MAKE) environment_help

include                         Makefile.Environment
