#
# common targets
#

STATIC_OBJS=
SHARED_OBJS=
DEBUG_OBJS=
PROFILE_OBJS=
REENTRENT_OBJS=

#ifeq ($(SPEED),fast)
#.PHONY: $(STATIC_LIB) $(SHARED_LIB) $(DEBUG_LIB) $(PROFILE_LIB) \
#	$(LIBALIAS)
#endif

lib::
	@if [ -d $(STATIC_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(STATIC_DIR)/$(SUBDIR); \
	  $(MKDIR) $(STATIC_DIR)/$(SUBDIR); \
	fi
	@if [ -d $(SHARED_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(SHARED_DIR)/$(SUBDIR); \
	  $(MKDIR) $(SHARED_DIR)/$(SUBDIR); \
	fi
	@if [ -d $(DEBUG_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(DEBUG_DIR)/$(SUBDIR); \
	  $(MKDIR) $(DEBUG_DIR)/$(SUBDIR); \
	fi
	@if [ -d $(PROFILE_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(PROFILE_DIR)/$(SUBDIR); \
	  $(MKDIR) $(PROFILE_DIR)/$(SUBDIR); \
	fi
	@if [ -d $(CHECKER_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(CHECKER_DIR)/$(SUBDIR); \
	  $(MKDIR) $(CHECKER_DIR)/$(SUBDIR); \
	fi
ifeq ($(REENTRENT),true)
	@if [ -d $(REENTRENT_DIR)/$(SUBDIR) ]; then \
	  true; else \
	  $(RM) -f $(REENTRENT_DIR)/$(SUBDIR); \
	  $(MKDIR) $(REENTRENT_DIR)/$(SUBDIR); \
	fi
endif

ifeq ($(STATIC),true)

STATIC_OBJS:= $(addprefix $(STATIC_DIR)/$(SUBDIR)/, $(OBJS))

ifeq ($(SPEED),fast)
#.PHONY: $(STATIC_LIB)

lib:: $(STATIC_OBJS) $(STATIC_LIB)

$(STATIC_LIB): $(STATIC_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(STATIC_LIB)($(STATIC_OBJS))
endif
endif

ifeq ($(SHARED),true)
SHARED_OBJS:= $(addprefix $(SHARED_DIR)/$(SUBDIR)/, $(OBJS))

ifeq ($(ELF),true)
lib:: $(SHARED_OBJS)
else
ifeq ($(SPEED),fast)
#.PHONY: $(SHARED_LIB)

lib:: $(SHARED_OBJS) $(SHARED_LIB)

$(SHARED_LIB): $(SHARED_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(SHARED_LIB)($(SHARED_OBJS))
endif
endif
endif

ifeq ($(DEBUG),true)
DEBUG_OBJS:= $(addprefix $(DEBUG_DIR)/$(SUBDIR)/, $(OBJS))

ifeq ($(SPEED),fast)
#.PHONY: $(DEBUG_LIB)

lib:: $(DEBUG_OBJS) $(DEBUG_LIB)

$(DEBUG_LIB): $(DEBUG_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(DEBUG_LIB)($(DEBUG_OBJS))
endif
endif

ifeq ($(PROFILE),true)
#.PHONY: $(PROFILE_LIB)

PROFILE_OBJS:= $(addprefix $(PROFILE_DIR)/$(SUBDIR)/, $(OBJS))

ifeq ($(SPEED),fast)
lib:: $(PROFILE_OBJS) $(PROFILE_LIB)

$(PROFILE_LIB): $(PROFILE_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(PROFILE_LIB)($(PROFILE_OBJS))
endif
endif

ifeq ($(CHECKER),true)
#.PHONY: $(CHECKER_LIB)

CHECKER_OBJS:= $(addprefix $(CHECKER_DIR)/$(SUBDIR)/, $(OBJS))

ifeq ($(SPEED),fast)
lib:: $(CHECKER_OBJS) $(CHECKER_LIB)

$(CHECKER_LIB): $(CHECKER_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(CHECKER_LIB)($(CHECKER_OBJS))
endif
endif

ifeq ($(REENTRENT),true)
REENTRENT_OBJS:= $(addprefix $(REENTRENT_DIR)/$(SUBDIR)/, $(OBJS_R))

ifeq ($(SPEED),fast)
#.PHONY: $(REENTRENT_LIB)

lib:: $(REENTRENT_OBJS) $(REENTRENT_LIB)

$(REENTRENT_LIB): $(REENTRENT_OBJS)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(REENTRENT_LIB)($(REENTRENT_OBJS))
endif
endif

ifneq ($(ELF),true)
ifneq ($(ALIASES),)
ifeq ($(SPEED),fast)
#.PHONY: $(LIBALIAS)

lib:: $(ALIASES) $(LIBALIAS)

$(LIBALIAS): $(ALIASES)
	$(AR) $(ARFLAGS) $@ $^
else
lib:: $(LIBALIAS)($(ALIASES))
endif
endif
endif

obj: $(OBJS)

asm: $(ASMS)

realclean::
	$(RM) -f .depend core *.o *.a *.s *.i tmp_make foo *.orig
#	$(MAKE) subdir TARGET=realclean

clean::
	$(RM) -f core *.o *.a *.s *.i tmp_make foo
#	$(MAKE) subdir TARGET=clean

#.depend: $(SRCS)
depend:: $(SRCS)
	$(CC) $(CFLAGS) -M $^ | \
		sed -e 's,^[ 	]*\(.*\.o\)[ 	]*:,$(STATIC_DIR)/$(SUBDIR)/\1 $(SHARED_DIR)/$(SUBDIR)/\1 $(DEBUG_DIR)/$(SUBDIR)/\1 $(PROFILE_DIR)/$(SUBDIR)/\1 $(CHECKER_DIR)/$(SUBDIR)/\1 $(REENTRENT_DIR)/$(SUBDIR)/\1:,' > .depend
#	$(MAKE) subdir TARGET=depend

ifneq ($(DIRS),)
lib realclean clean depend::
	@set -e; for i in $(DIRS); do \
	  echo making $@ in $$i; \
	  $(MAKE) -C $$i $@; \
	done;
endif

#
# include a dependency file if one exists
#
ifeq (.depend,$(wildcard .depend))
include .depend
endif
