# bdb addon makefile
# type make to build

ROOT := ..

LD := gcc

DEBUG_OBJ_DIR	:= $(ROOT)/Obj.Debug/AddOns
RELEASE_OBJ_DIR	:= $(ROOT)/Obj.NoDebug/AddOns

TARGET_DIR := $(ROOT)/AddOns

ifndef $(DEBUG)
	DEBUG   := 1
endif

ifeq ($(DEBUG), 1)
	DBG		:= -g
	OPT		:= -O0
	OBJ_BASE := $(ROOT)/Obj.Debug
	OBJ_DIR := $(DEBUG_OBJ_DIR)
else
	DBG		:= 
	OPT		:= -O1
	OBJ_BASE := $(ROOT)/Obj.NoDebug
	OBJ_DIR := $(RELEASE_OBJ_DIR)
endif

MAKE_OPTIONS = -j 2 --no-print-directory

# add individual addon names here

LIST = \
	DumpMessage \
	ShowBitmap \
#

ifneq ($(WHAT)x, x)

ifeq ($(WHAT), DumpMessage)
	SHORTCUT:=-D
endif

ifeq ($(WHAT), ShowBitmap)
	SHORTCUT:=-B
endif

define SRCS_LIST_TO_OBJS
    $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), $(basename $(notdir $(file))))))
endef

TARGET		= $(TARGET_DIR)/$(WHAT)$(SHORTCUT)
SRCS		= $(WHAT).c*
OBJS		= $(SRCS_LIST_TO_OBJS)

LDFLAGS 	= -nostart -lbe
INCLUDES	+= -I$(ROOT)/Sources

install: $(TARGET_DIR) $(OBJ_DIR) $(TARGET)

$(TARGET): $(OBJS)
	$(LD) -o $@ $(OBJS) $(LDFLAGS)
ifeq ($(DEBUG), 0)
	strip $@
endif
	mimeset -f $@

$(OBJ_DIR):
	@ mkdir -p $(OBJ_BASE)
	@ mkdir -p $@

$(TARGET_DIR):
	@ mkdir -p $@

$(OBJ_DIR)/%.o: %.cpp
	@ echo "-> "$(@F)
	@ $(CC) -c $< -o $@ $(INCLUDES) $(CFLAGS)

clean:
	rm -rf $(DEBUG_OBJ_DIR) $(RELEASE_OBJ_DIR)
	rm -rf $(TARGET_DIR)

else

install:
	@ for what in $(LIST); do \
		$(MAKE) WHAT=$$what $(MAKE_OPTIONS) $@; \
	done

.DEFAULT: FORCE
	@ for what in $(LIST); do \
		$(MAKE) WHAT=$$what $(MAKE_OPTIONS) $@; \
	done

endif
