# Makefile for CX2100psu util

# Default Target    ############

# standard default target
DEFAULT_TARGET = binaries
BUILD_TYPE = release

INSTALL_PATH = /bin/


# ==============================================================================
# Primary Targets
# ---------------
# depend:          creates/updates dependencies for all source files (*.c)
# upload:          generates all binaries and uploads it to the test machine
# install:         generates all binaries and installs it to the $(INSTALL_PATH) folder
# binaries:        generates binaries
# doc:             generates documentation under doxygen/
# check:           generates test binaries and runs them
# clean:           remove generated source, object and binaries files

# Remote machine
# --------------
# executing upload, uploads the binaries and support files to a remote machine using
# scp.
# Note: site is not a mount point, so the files will only be loaded temporarily
#       (until next boot)

REMOTE_USER = root
REMOTE_IP = 172.25.0.201
REMOTE_PATH = /bin/
REMOTE_PORT = 22

# optionally include Make.local to allow overriding of the default target on a user basis
# note Make.local is not subversioned
-include Make.local


# Note: Make.local needs to be before this
all: $(DEFAULT_TARGET)



# ==============================================================================
# Environment Variables
#

# command shell
SHELL = /bin/sh

# base directory of files (gets us back to "workspace" directory)
BASE = ../../

# compiler prefix
CROSS_COMPILE = $(BASE)bin/CX2020/bin/i686-kinetic-linux-gnu-

# where we find our library files
SYSROOT = $(BASE)svn/linuxBuild/rootfs/CX2020/staging

# compiler
CC = $(CROSS_COMPILE)gcc --sysroot=$(SYSROOT)


# ==============================================================================
# System Paths and Build Dependancies
#

# paths to library files
LIBPATHS = 

# project includes
INCLUDES =

# project libraries
LIBRARIES =


# ==============================================================================
# Build context
#
# compiler flags,
# for further info see: http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_2.html
#  -g             = produces debug information
#  -c             = compile but don't link, output object files
#  -S             = compile as assembler code
#  -E             = stop after pre-processing stage, before compiling
#  -Wall          = show most warnings (but there are more)
#  --sysroot=dir  = use dir as the logical root directory for headers and libraries
#  -o             = output file, NOTE: don't use this one in the flag variables below
#  -D NDEBUG      = #define NDEBUG (ie no debug, = no assertions etc)

SOURCEDIR = 

$(SOURCEDIR):
	mkdir -p $(SOURCEDIR)

BUILDDIR = build/

$(BUILDDIR):
	mkdir -p $(BUILDDIR)

OUTPUTDIR = bin/

$(OUTPUTDIR):
	mkdir -p $(OUTPUTDIR)

	

# common flags
CFLAGS =  $(INCLUDES)
LDFLAGS = $(LIBPATHS) $(LIBRARIES)

# Release mode
R_DEFINES = -D NDEBUG -rdynamic
# compiler flags
# set to optimization level 2
R_CFLAGS = $(R_DEFINES) -c -Wall -O2
# linker flags
R_LFLAGS = $(R_DEFINES) -Wall

# Debug mode
D_DEFINES = -D DEBUG -rdynamic
# compiler flags
# set to no optimization
D_CFLAGS = $(D_DEFINES) -std=gnu99 -g -c -Wall -O0
# linker flags
D_LFLAGS = $(D_DEFINES) -std=gnu99 -g -Wall

# whats the build mode?
ifeq ($(BUILD_TYPE), debug)
  CFLAGS += $(D_CFLAGS)
  LFLAGS += $(D_LFLAGS)
else
  CFLAGS += $(R_CFLAGS)
  LFLAGS += $(R_LFLAGS)
endif


# ==============================================================================
# Remote copy params
#
SCP_PREFIX = scp -P $(REMOTE_PORT)
SCP_SUFFIX = $(REMOTE_USER)@$(REMOTE_IP):$(REMOTE_PATH)
RSH        = ssh $(REMOTE_USER)@$(REMOTE_IP) -p $(REMOTE_PORT)



# ==============================================================================
# Output generation
#

# runtimes

$(BUILDDIR)cx2100psu.o:	$(SOURCEDIR)cx2100psu.c $(SOURCEDIR)i2c-dev.h
	$(CC) $(CFLAGS) $< -o $@

	
$(OUTPUTDIR)cx2100psu:	$(BUILDDIR)cx2100psu.o
	$(CC) $(LDFLAGS) $(COMMON_OBJECTS) $^ -o  $@ -Wl


binaries:	$(SOURCEDIR) $(BUILDDIR) $(OUTPUTDIR) $(OUTPUTDIR)cx2100psu


compile:  binaries


install:	binaries
	cp $(OUTPUTDIR)cx2100psu $(INSTALL_PATH)cx2100psu
	chmod 755 $(INSTALL_PATH)cx2100psu


upload:	binaries
	$(SCP_PREFIX) $(OUTPUTDIR)cx2100psu $(SCP_SUFFIX)cx2100psu
	$(RSH) chmod 755 $(REMOTE_PATH)cx2100psu


bin_clean:
	rm -f $(OUTPUTDIR)*


# ==============================================================================
# docs
#

DOXYGENDIR = ./doxygen/

dox_clean:
	rm -rf $(DOXYGENDIR)html
	rm -rf $(DOXYGENDIR)latex

dox:
	doxygen $(DOXYGENDIR)doxygen_config

doc: dox_clean dox

doc_run: doc
	iceweasel $(DOXYGENDIR)html/index.html



# ==============================================================================
# output clean
#

clean:	bin_clean
	rm -rf $(BUILDDIR)*.o $(BUILDDIR)*.s




.PHONY:	all clean binaries install upload compile bin_clean dox_clean dox doc doc_run


