############  ENVIRONMENT VARIABLES  ##############
############# $Name:  $ ###############
############### $Revision: 1.60 $ #################
########## $Date: 2010/06/28 20:23:36 $ ###########
# These variables will control portions of the build process.
# For further configuration options, see ms/config/tekkotsu.xml, which
# specifies run-time configuration.

# Directory where the Tekkotsu framework resides
TEKKOTSU_ROOT ?= /afs/cs/academic/class/15494-s10/Tekkotsu

# Location where temporary object files will be stored
TEKKOTSU_BUILDDIR ?= $(TEKKOTSU_ROOT)/build
PROJECT_BUILDDIR ?= build
# or perhaps you would like one/both of these better:
# puts build directory in local tmp, but uses pathnames to avoid
# conflicts between multiple frameworks or multiple projects
#TEKKOTSU_BUILDDIR ?= $(shell echo /tmp/$(subst /,_,$(TEKKOTSU_ROOT))_build_$(USER) | sed "s/ /\\\ /g")
#PROJECT_BUILDDIR ?= $(shell echo /tmp/`pwd | tr / _`_build_$(USER) | sed "s/ /\\\ /g")

# What model of robot are you targeting?  Legal values:
#   Aibos: TGT_ERS210 TGT_ERS220 TGT_ERS2xx TGT_ERS7
#   Other platforms: TGT_CREATE TGT_HANDEYE TGT_CHIARA TGT_QBOTPLUS
TEKKOTSU_TARGET_MODEL ?= TGT_CHIARA

# What OS is this going to be running under?
# Choices are:
#   PLATFORM_APERIOS - the AIBO's OS
#   PLATFORM_LOCAL - the current desktop environment
# If you need to do platform specific stuff in your code, best
# to check #ifdef PLATFORM_APERIOS, and otherwise assume a UNIX-style
# environment.  Note that non-Aperios implies no OPEN-R headers.
#
# This default value selects PLATFORM_APERIOS if targeting an Aibo
# model, otherwise PLATFORM_LOCAL.
TEKKOTSU_TARGET_PLATFORM ?= $(if $(filter TGT_ERS%,$(TEKKOTSU_TARGET_MODEL)),PLATFORM_APERIOS,PLATFORM_LOCAL)

# This will trigger the project's Makefile to always attempt to make
# the framework as well.  This is useful if you are hacking the
# framework itself, and thus want any changes made there to be
# compiled automatically.
# default: ON (non-empty string) - any non-empty string is ON
TEKKOTSU_ALWAYS_BUILD ?= true

# What kind of intermediary linking to perform?
# SHARED/DYNAMIC library support is EXPERIMENTAL
# This setting will be ignored for PLATFORM_APERIOS
# To use a static library, try libtekkotsu.a
# To use a shared library, try libtekkotsu.$(if $(findstring Darwin,$(shell uname)),dylib,so)
LIBTEKKOTSU ?= libtekkotsu.$(if $(findstring Darwin,$(shell uname)),dylib,so)

# This will control the debug flags such as -g and -DDEBUG, disables optimization
# default: ON (specify compiler flags) for platform local, OFF (empty string) for others
TEKKOTSU_DEBUG ?= $(if $(findstring PLATFORM_LOCAL,$(TEKKOTSU_TARGET_PLATFORM)),-g -fno-inline -DDEBUG)

# Controls the type(s) of optimization performed, but
# only applied if TEKKOTSU_DEBUG is empty
TEKKOTSU_OPTIMIZE ?= $(if $(findstring PLATFORM_APERIOS,$(TEKKOTSU_TARGET_PLATFORM)), -O2 -DDEBUG -DOPENR_DEBUG, -Os -finline -funroll-loops -march=i686 -mmmx -msse -msse2 -msse3 -fomit-frame-pointer -mfpmath=sse -ftree-vectorize)

# This can be nice if you want to use 'more' to page through errors
# if they occur.  Otherwise, try using 'cat' instead.  Cygwin users
# may need to explicitly install the 'more' package
# This default value will test for the availability of 'more', and
# fall back to using 'cat'.
# The -r option is needed to display colors instead of control characters,
# and -E makes it exit when the end of log is reached (without user prompt)
# 'more' is used instead of 'less' because less doesn't leave the data on the screen after exiting
TEKKOTSU_LOGVIEW ?= $(shell if which more > /dev/null ; then echo "$(TEKKOTSU_ROOT)/tools/sbin/pager \$$retval"; else echo "cat" ; fi )

########## AIBO-Specific Section ##########
#

# Directory where the OPEN-R SDK was installed, required for building Aibo executables
# See http://www.tekkotsu.org/openr-install.html
OPENRSDK_ROOT ?= /usr/local/OPEN_R_SDK

# Location where the memstick will be mounted (AIBO only)
# You may wish to override this as an environment variable
# instead of changing it here, as the former will allow you to run
# tools/sbin/mntmem and tools/sbin/umntmem manually.
ifeq ($(findstring Darwin,$(shell uname)),Darwin)
#   For OS X users, the following script assumes the highest numbered
#   device is your memory stick.  Feel free to replace this with the actual
#   mount point (e.g. /Volumes/Untitled) if this doesn't work.
MEMSTICK_ROOT ?= $(shell $(TEKKOTSU_ROOT)/tools/sbin/osx_find_memstick)
else
#   Cygwin users will probably want /cygdrive/e, and anyone else
#   will need to check /etc/mtab (we'll assume /mnt/memstick)
MEMSTICK_ROOT ?= $(if $(findstring CYGWIN,$(shell uname)),/cygdrive/e,/mnt/memstick)
endif

# Note: if your memory stick drivers use uppercase, you'll need to
# set 'FILENAME_CASE' to 'upper'.
FILENAME_CASE ?= lower

# this will delete files from the memory stick that aren't also in
# your build image - except files at the root level of the memstick,
# like memstick.ind
# default: OFF (empty string) - any non-empty string is ON
STRICT_MEMSTICK_IMAGE ?=


########## QWERK-Specific section ##########
#
# Directory where ICE is installed, required for Qwerk support (TGT_QBOTPLUS)
# See http://www.zeroc.com/ice.html
ICE_ROOT ?= $(if $(filter TGT_QWERK TGT_QBOTPLUS,$(TEKKOTSU_TARGET_PLATFORM)),/usr/local/Ice)


########## FLYCAP-Specific section ##########
#
# Directory where FlyCapture include files are installed,
# required for Pt. Grey camera support
# See http://www.ptgrey.com, libflycapture 2.0 for more details
FLYCAP_ROOT ?= $(shell if [ -d "/usr/include/flycapture" ] ; then echo "/usr/include/flycapture"; fi)

##########  TOOL SELECTION VARIABLES  #############
# You probably don't really want to edit these unless you've
# got an "interesting" system configuration
TEKKOTSU_FILTERSYSWARN?=$(TEKKOTSU_ROOT)/tools/filtersyswarn/filtersyswarn
TEKKOTSU_COLORFILT?=$(TEKKOTSU_ROOT)/tools/sbin/colorfilt

ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
  CC=$(OPENRSDK_ROOT)/bin/mipsel-linux-gcc
  CXX=$(OPENRSDK_ROOT)/bin/mipsel-linux-g++
  LD=$(OPENRSDK_ROOT)/bin/mipsel-linux-ld
  STRIP=$(OPENRSDK_ROOT)/bin/mipsel-linux-strip
  NM=$(OPENRSDK_ROOT)/bin/mipsel-linux-nm
  MKBIN=$(OPENRSDK_ROOT)/OPEN_R/bin/mkbin
  MKBINFLAGS=-p $(OPENRSDK_ROOT) -L$(TEKKOTSU_ROOT)/aperios/lib
  AR=$(OPENRSDK_ROOT)/bin/mipsel-linux-ar rcs
  AR2=touch
  ifeq ($(TEKKOTSU_FILTERSYSWARN),cat)
    FILTERSYSWARN=$(TEKKOTSU_FILTERSYSWARN)
  else
    FILTERSYSWARN=$(TEKKOTSU_FILTERSYSWARN) $(OPENRSDK_ROOT)
  endif
  STUBGEN=$(OPENRSDK_ROOT)/OPEN_R/bin/stubgen2
  LIBTEKKOTSU=libtekkotsu.a
else
  CC?=gcc
  CXX?=g++
  LD=ld
  STRIP=strip
  NM=nm
  AR=ar rcs
  AR2=touch
  ifeq ($(TEKKOTSU_FILTERSYSWARN),cat)
    FILTERSYSWARN=$(TEKKOTSU_FILTERSYSWARN)
  else
    FILTERSYSWARN=$(TEKKOTSU_FILTERSYSWARN) "/usr/.*/?include/|/opt/local/include|/sw/include/$(if $(ICE_ROOT),|$(ICE_ROOT))"
  endif
endif
COLORFILT=$(TEKKOTSU_COLORFILT)

ifneq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
	ifeq ($(findstring Darwin,$(shell uname)),Darwin)
		ISMACOSX=true
		TEST_MACOS_MAJOR:=$(shell sw_vers -productVersion  | cut -f 1 -d .)
		TEST_MACOS_MINOR:=$(shell sw_vers -productVersion  | cut -f 2 -d .)
	else
		# building these as 32 bit in case we want to copy binaries to robot
		ifeq ($(TEKKOTSU_TARGET_MODEL),TGT_CHIARA2)
			CC:=$(CC) -m32
			CXX:=$(CXX) -m32
		endif
		
		ifeq ($(TEKKOTSU_TARGET_MODEL),TGT_CHIARA)
			CC:=$(CC) -m32
			CXX:=$(CXX) -m32
		endif

		ifeq ($(TEKKOTSU_TARGET_MODEL),TGT_CREATE)
			CC:=$(CC) -m32
			CXX:=$(CXX) -m32
		endif

		ifeq ($(TEKKOTSU_TARGET_MODEL),TGT_CALLIOPE)
			CC:=$(CC) -m32
			CXX:=$(CXX) -m32
		endif
	endif
endif

ifeq ($(TEKKOTSU_TARGET_MODEL),TGT_HANDEYE)
	CC:=$(CC) -m32
	CXX:=$(CXX) -m32
endif

# Determine if precompiled header support is available...
# This can speed up the compilation process, but is currently only
# supported by the 3.4 branch of gcc, or 3.3 of the gcc from Apple
ifeq ($(shell $(CXX) -v > /dev/null 2>&1 || echo "not found"),)
TEST_CXX_MAJOR:=$(shell $(CXX) -v 2>&1 | sed -n 's/^.* version \([0-9]*\)\.\([0-9]*\).*/\1/p')
TEST_CXX_MINOR:=$(shell $(CXX) -v 2>&1 | sed -n 's/^.* version \([0-9]*\)\.\([0-9]*\).*/\2/p')
#Apple added support for precompiled headers early in the 3.x branch
TEST_CXX_APPLE:=$(findstring Apple,$(shell $(CXX) --version))
#Cygming apparently removes support for precompiled headers in the 3.x branch
TEST_CXX_CYGMING:=$(findstring cygming,$(shell $(CXX) --version))
TEKKOTSU_PCH ?= $(shell if [ "$(TEST_CXX_MAJOR)" -ge 4 -o "$(TEST_CXX_MAJOR)" -ge 3 -a -z "$(TEST_CXX_CYGMING)" -a \( "$(TEST_CXX_MINOR)" -ge 4 -o -n "$(TEST_CXX_APPLE)" \) ] ; then echo "common.h"; fi)
# 'cache' result:
TEKKOTSU_PCH:=$(TEKKOTSU_PCH)
endif

#These will be the actual build directories used for the current target
TK_LIB_BD:=$(TEKKOTSU_BUILDDIR)/$(TEKKOTSU_TARGET_PLATFORM)
TK_BD?=$(TEKKOTSU_BUILDDIR)/$(TEKKOTSU_TARGET_PLATFORM)/$(TEKKOTSU_TARGET_MODEL)
PROJ_BD?=$(PROJECT_BUILDDIR)/$(TEKKOTSU_TARGET_PLATFORM)/$(TEKKOTSU_TARGET_MODEL)

#For debugging:
#test:=$(shell echo "TEKKOTSU_BUILDDIR: $(TEKKOTSU_BUILDDIR)" > /dev/tty)
#test:=$(shell echo "PROJECT_BUILDDIR: $(PROJECT_BUILDDIR)" > /dev/tty)
#test:=$(shell echo "TEST_CXX_MAJOR: $(TEST_CXX_MAJOR)" > /dev/tty)
#test:=$(shell echo "TEST_CXX_MINOR: $(TEST_CXX_MINOR)" > /dev/tty)
#test:=$(shell echo "TEST_CXX_APPLE: $(TEST_CXX_APPLE)" > /dev/tty)
#test:=$(shell echo "TEKKOTSU_PCH: $(TEKKOTSU_PCH)" > /dev/tty)
#$(error that's all folks)
