# makefile for secuential programs i.e. g++, gcc, etc
#         Author: Antonio Carrillo Ledesma 29/12/2010
#         antonio@mmc.geofisica.unam.mx
#         http://www.mmc.geofisica.unam.mx/acl/
#
# usage:
#   First automatic dependency calculation
#     $ make deps
#
#   Compile and link
#     $ make
#
#   Run
#     $ make run
#
#   Clean
#     $ make clean
#
#   Others
# 	target: gz - Store in tar.gz
# 	target: bz2 - Store in tar.bz2
# 	target: tar - Store in tar
# 	target: zip - Store in zip
#	target: ddd - Debuger with ddd
# 	target: val - Debuger with valgrind
# 	target: gpro - Analysis of speedup
# 	target: svnup - svn update
# 	target: svnst - svn status -verbose
# 	target: svnci - svn commit
# 	target: help - Display callable targets.




# Defining the sources
SOURCES = $(wildcard *.cpp)

# Parameters of executable
PARAMETERS = 

# Defining the neme of th executable
EXECUTABLE = test

# Defining the debug Flags
DEBUG = -g -pg -O0     # Debuger
#DEBUG += -Wall	     # Warning
#DEBUG += -O2	        # Optimization

# Defining the compiler Flags
CFLAGS = $(DEBUG)

# Defining the linker Flags
LDFLAGS = -lm $(DEBUG)

# Defining the compiler:
CC = g++



#######################################################################
# Defining the object files:
OBJECTS = $(SOURCES:.cpp=.o)

# Load the automatic dependency
DEPS = $(OBJECTS:.o=.d)


# target: all - The default rule - compiling our main program:
all: $(SOURCES)  $(EXECUTABLE) 
	echo all: make complete

$(EXECUTABLE): $(OBJECTS)
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@

# Tell make how to build .o files from .cpp files:
$(OBJECTS):
	$(CC) -MD -c $(CFLAGS) $(@:.o=.cpp) -o $@

#Now make sure that make rebuilds files if included headers change:
-include $(DEPS)




# target: deps - Automatic dependency calculation
deps: $(SOURCES)
	$(CC) -MD -E $(SOURCES) > /dev/null



# The .PHONY rule keeps make from doing something with a file named clean
.PHONY: clean

# target: clean - Removing the executable and the object files
clean:
	rm $(EXECUTABLE) $(OBJECTS) $(DEPS) gmon.out
	echo clean: make complete


# target: run - Run the application
run:
	./$(EXECUTABLE) $(PARAMETERS)


# target: gz - Store in tar.gz
gz:
	tar -zcvpf $(EXECUTABLE)-`date "+%F"`.tar.gz makefile *.?pp
	

# target: bz2 - Store in tar.bz2
bz2:
	tar -jcvpf $(EXECUTABLE)-`date "+%F"`.tar.bz2 makefile *.?pp


# target: tar - Store in tar
tar:
	tar -cvpf $(EXECUTABLE)-`date "+%F"`.tar makefile *.?pp


# target: zip - Store in zip
zip:
	zip  $(EXECUTABLE)-`date "+%F"`.zip makefile *.?pp


# target: ddd - Debuger with ddd, required DEBUG = -g
ddd:
	ddd  $(EXECUTABLE)


# target: val - Debuger with valgrind, required DEBUG = -g 
val:
	valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./$(EXECUTABLE) $(PARAMETERS)


# target: gpro - Analysis of speedup, required DEBUG = -g -pg
gpro:
	./$(EXECUTABLE) $(PARAMETERS)
	gprof -c -z $(EXECUTABLE)


# target: svnup - svn update
svnup:
	svn update


# target: svnst - svn status
svnst:
	svn status --verbose


# target: svnci - svn commit
svnci:
	svn ci -m "Update"


# target: help - Display callable targets.
help:
	egrep "^# target:" [Mm]akefile




#-------------------------------------------------------------------------------------------------------
# Algo de linux: http://www.mmc.geofisica.unam.mx/acl/linux/
#
#
# Antonio Carrillo Ledesma
# http://www.mmc.geofisica.unam.mx/acl/

