#-------------------------------------------------------
#
#	Generic Crossplatform Makefile
#
#	Author:	stephen beaulieu <hippo@be.com>
#
#	Questions, comments & concerns to <devsupport@be.com>
#
#	Copyright 1998 Be, Inc.  All Rights Reserved
#
#	Use the 'platform' and 'common' makefiles together
#	to build a standard application makefile.  Look in
#	the 'template' file for more information.
#--------------------------------------------------------

#--------------------------------------------------------
#	Common Project Definitions and Rules
#
#	Include this file at the end of your project
#	makefile to set up rules and symbols that are
#	common to all projects.
#
#	Input:
#		SRCS: List of source files in your project.
#		CFLAGS: Additional custom compiler flags.
#		LDFLAGS: Additional custom linker flags.
#		RSRCS: Specify the list of resource files.  The names listed will
#			automatically have PPC.rsrc or x86.rsrc added to the end
#			depending on the platform.
#		ADD_BESHLIBS: Specify any additional beos shared libraries needed.
#			These libraries should be specified by their actual runtime
#			name.  The makefile will add the appropriate path and any
#			extention necessary.  libbe.so and libroot.so are automatically
#			included.
#		ADD_SHLIBS: Specify additional non-beos shared libraries needed.
#			The path for these libraries is figured from the current
#			directory.  The make file will add any extention necessary.
#		ADD_BESTLIBS: Specify any additional beos static libraries needed.
#			These libraries should be specified by their actual runtime name.
#			The makefile will add the appropriate path and any extention
#			necessary.
#		ADD_STLIBS: Specify additional non-beos static libraries needed.
#			The path for these libraries is figured from the current
#			directory.  the make file will add any extention necessary.
#		SYSTEM_INCLUDES: Specify any additional system paths to check
#			for files or headers to include in the project.  These paths are
#			specified from /boot/system/headers most if not all headers under
#			this path are automatically included.  Add them specificly if you
#			run into problems.
#		SYSLOCAL_INCLUDES: Specify any additional system paths to check
#			for files or headers to include in the project.  These are
#			determined from the current directory.
#		LOCAL_INCLUDES: Specify any additional local paths to check for files
#			or headers to include in the project these are determined from
#			the current directory.
#
#	Output:
#		A lot of stuff.
#--------------------------------------------------------

ifndef DIST_DESTDIR
	DIST_FULLDESTDIR := $(DIST_TARGET)
else
	DIST_FULLDESTDIR := $(DIST_TARGET)/$(DIST_DESTDIR)
endif

ifeq ($(IMG_TYPE), APP)
	LDFLAGS		+=	-xma $(LDPLATFLAGS)
endif

ifeq ($(IMG_TYPE), SHARED)
	LDFLAGS		+=	-xms $(LDPLATFLAGS)
endif

ifeq ($(IMG_TYPE), STATIC)
	LDFLAGS		+=	-xml
endif

	CFLAGS		+= $(DEFINE_VERSION)

#	create the final list of libraries to include
	LIBS_TO_USE =	$(addsuffix $(LIB_EXTENTION), $(ADD_SHLIBS) $(DEFAULT_LIBS) $(ADD_BESHLIBS)) $(ADD_STLIBS) $(ADD_BESTLIBS)
	

#	additional common linker flags
ifneq ($(IMG_TYPE), STATIC)
	LDFLAGS	+= -L$(FULL_DIR) -L$(BELIBRARIES) -L$(MWLIBRARIES) $(LIBS_TO_USE)
endif
	
#	create the list of include paths
	INCLUDES = -i . $(addprefix -i ,$(LOCAL_INCLUDES)) -i- $(addprefix -i ,$(SYSLOCAL_INCLUDES)) $(addprefix -i $(BEHEADERS)/,$(SYSTEM_INCLUDES))

#	create the list of resources
	RSRCS_TO_USE :=	$(addsuffix _$(CPU).rsrc, $(RSRCS))

#	create the resource instruction
	ifeq ($(RSRCS), )
		DO_RSRCS :=
	else
		DO_RSRCS := $(ADDRES) $(RSRCS_TO_USE) $(TARGET)
	endif

# psuedo-function for converting a list of source files in SRCS variable
# to a corresponding list of object files in $(OBJ_DIR)/xxx.o
# The "function" strips off the src file suffix (.ccp or .c or whatever)
# and then strips of the directory name, leaving just the root file name.
# It then appends the .o suffix and prepends the $(OBJ_DIR)/ path
define SRCS_LIST_TO_OBJS
	$(foreach file, $(SRCS), $(addsuffix .o, $(addprefix $(OBJ_DIR)/$(shell dirname $(file))/, $(basename $(notdir $(file))))))
endef

#	$(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(foreach file, $(SRCS), $(basename $(notdir $(file))))))


#	specify the list of objects
	OBJS		:= $(SRCS_LIST_TO_OBJS)

#	specify the list of dependency files
	DEPS		:= $(OBJS:.o=.dep)
	
	DEPENDENCIES := $(OBJ_DIR)/$(IMG_NAME).Depends
	
#	generate mapfiles for metrowerks.  
ifneq ($(IMG_TYPE), STATIC)
	MAP_FILE	:= $(TARGET).xMAP
	LDFLAGS		+= -map $(MAP_FILE)
	SYMBOL_FILE	:= $(TARGET).xSYM
	#LDFLAGS		+= -sym full -osym $(SYMBOL_FILE)
	#LDFLAGS		+= -sym full
endif

$(TARGET):	$(OBJ_DIR) $(OBJS) $(RSRCS_TO_USE) $(ADD_STLIBS)
		$(LD) -o $@ $(OBJS) $(LDFLAGS)
		$(DO_RSRCS)
		$(MIMESET) -f $@

$(DEPENDENCIES): $(OBJ_DIR) $(DEPS)
		cat $(DEPS) > $@

#	include dependency information if it is available

	DEP_EXISTS := $(shell [ -e $(DEPENDENCIES) ] && echo "YES")
	
ifeq ($(DEP_EXISTS), YES)
	include $(DEPENDENCIES)
endif

#--------------------------------------------------------
#	Rules for the whole system
#--------------------------------------------------------

#	rule to create the object file directory if needed
$(OBJ_DIR)::
	@mkdir --parents $(OBJ_DIR)

#	default rule for take xxx.c files on compile into $(OBJ_DIR)/xxx.o
$(OBJ_DIR)/%.o : %.c
	@mkdir --parents $(shell dirname $@)
	$(CC) -c $< $(INCLUDES) $(CPLATFLAGS) $(CFLAGS) -o $@

#	default rule for take xxx.cpp files on compile into $(OBJ_DIR)/xxx.o
$(OBJ_DIR)/%.o : %.cpp
	@mkdir --parents $(shell dirname $@)
	$(CC) -c $< $(INCLUDES) $(CPLATFLAGS) $(CFLAGS) -o $@

#	default rule for make xxx.c files into dependencies $(OBJ_DIR)/xxx.dep
$(OBJ_DIR)/%.dep : %.c
	@mkdir --parents $(shell dirname $@)
	$(CC) >$@ $(CPLATFLAGS) $(CFLAGS) -make -c $< $(INCLUDES) -o $(@:.dep=.o)
	@echo "" >>$@
	@sed -e 's:$(@:.dep=.o):$@:g' <$@ >$@.tmp
	@cat $@.tmp >>$@
	@rm $@.tmp
	
#	default rule for make xxx.cpp files into dependencies $(OBJ_DIR)/xxx.dep
$(OBJ_DIR)/%.dep : %.cpp
	@mkdir --parents $(shell dirname $@)
	$(CC) >$@ $(CPLATFLAGS) $(CFLAGS) -make -c $< $(INCLUDES) -o $(@:.dep=.o)
	@echo "" >>$@
	@sed -e 's:$(@:.dep=.o):$@:g' <$@ >$@.tmp
	@cat $@.tmp >>$@
	@rm $@.tmp

#	empty rule. Things that depend on this rule will always get triggered
FORCE:

#	The generic clean command. Delete everything in the object folder.
clean_project :: FORCE
	-rm -rf $(OBJ_DIR)
	-rm -f $(TARGET) $(TARGET).xMAP $(TARGET).xSYM $(TARGET).dbg
	rm -rf $(DIST_TARGET)
	rm -f $(DIST_TARGET).zip

#	The generic distribution command. Create distribution file and build
#	needed projects.
$(DIST_FULLDESTDIR)/$(TARGET_NAME) : $(TARGET)
	if [ "$(CLEANDIST)" = "yes" ]; then \
		rm -rf $(DIST_TARGET); \
		echo "Making directory: $(DIST_TARGET)"; \
		mkdir --parents $(DIST_TARGET); \
	fi
	for i in $(DIST_MAKES); do (				\
		echo "==> Distributing $$i";		\
		cd `dirname $$i`;					\
		make LOCAL_MAKE=1 DIST_TARGET="$(DIST_TARGET)" -f `basename $$i` dist;		\
	) done
	mkdir --parents $(DIST_FULLDESTDIR)
	$(COPY) $(TARGET) $(DIST_FULLDESTDIR)/$(TARGET_NAME); \
	mkdir --parents $(DIST_TARGET)/src/$(DIST_SRCDIR)
	for i in $(DIST_FILES); do \
		$(COPY) $$i $(DIST_TARGET)/$$i; \
	done
	for i in $(DIST_SRCS); do \
		$(COPY) $$i $(DIST_TARGET)/src/$(DIST_SRCDIR)/$$i; \
	done
	mkdir --parents $(DIST_TARGET)/src/makefiles
	for i in ../makefiles/*; do \
		$(COPY) $$i $(DIST_TARGET)/src/makefiles/$$i; \
	done

dist_project:: $(DIST_FULLDESTDIR)/$(TARGET_NAME)

#	remove just the application from the object folder
rmapp ::
	-rm -f $(TARGET)
