#######################################################
# Makefile for InterPool
# Author: NTM
# Created: 10/09/04
#######################################################

# We now have an automatic dependancy generation system,
# you no longer call make depend.
# It builds .d files in the deps/ subdirectory (more details below).


#######################################################
# user-selectable compiler options
#######################################################

# architecture (for optimizations in gcc/icc).
# for possible choices, see gcc manpage.
# examples:
# for intel P4: -march=pentium4
# for AMD 64-bit (eg opteron): -march=k8
# for some architectures (eg SUN), use -mcpu instead of -march
# for example for an ultrasparc III you would use -mcpu=ultrasparc3
ARCH = #-march=core2


# Use profiling?
#PROF = -pg
PROF = 

# Use debugging? If not, optimize code
#DEBUG = -g
DEBUG = -O3

#######################################################
# environnement
#######################################################

# C compiler: gcc or icc?
CC = gcc

# shell
SHELL = /bin/sh

# perl interpreter
PERL = perl

# etags
ETAGS = etags

# expat dir
EXPATDIR = expat-2.0.0/

# expat static lib
EXPATLIB = libexpat.a

# compile flags
# for gcc:
CFLAGS = -Wall -W -pipe $(ARCH) $(PROF) $(DEBUG)
# for icc:
#CFLAGS = -Wall $(ARCH) $(PROF) $(DEBUG)


# suffixes to use in implicit rules (none)
.SUFFIXES:
# .SUFFIXES: .c .o


#######################################################
# file lists
#######################################################

# source files
SRCS = fonc.c pools.c signa.c varia.c myrand.c design.c \
       STD.c doSTD.c \
       solveSigSimple.c \
       distance.c conflicts.c \
       poolInVector.c closure.c unitClosures.c orderVectors.c \
       solvexpClosure.c solvexpNaive.c \
       sigsToVVs.c \
       signaIO.c \
       simulObservation.c \
       decodeObservedSig.c doDecoding.c \
       simulation.c doSimulations.c \
       validation.c doValidation.c


# common object files
COMMONOBJS = fonc.o pools.o signa.o varia.o myrand.o design.o STD.o \
             solveSigSimple.o

# objects common to simulation, validation, and decoding
SIMMVALOBJS = distance.o conflicts.o \
              poolInVector.o closure.o unitClosures.o orderVectors.o \
              solvexpClosure.o solvexpNaive.o \
              sigsToVVs.o \
              signaIO.o \
              simulObservation.o

# real decoding specific objects
DECOBJS = decodeObservedSig.o doDecoding.o

# simulation specific object files
SIMMOBJS = simulation.o doSimulations.o

# validation specific objects
VALOBJS = validation.o doValidation.o

# STD objects
STDOBJS = doSTD.o


#################################################################
# final targets
#################################################################

# default target: make all binaries
# for distribution, don't make TAGS or printSizes by default
all: ipoolDecoding ipoolSimulations ipoolValidation ipoolSTD # TAGS printSizes

# real decoding
ipoolDecoding: $(COMMONOBJS) $(SIMMVALOBJS) $(DECOBJS) $(EXPATLIB)
	$(CC) $(CFLAGS) -lm -o ipoolDecoding $(COMMONOBJS) $(SIMMVALOBJS) $(DECOBJS) $(EXPATLIB)

# simulator
ipoolSimulations: $(COMMONOBJS) $(SIMMVALOBJS) $(SIMMOBJS) $(EXPATLIB)
	$(CC) $(CFLAGS) -lm  -o ipoolSimulations $(COMMONOBJS) $(SIMMVALOBJS) $(SIMMOBJS) $(EXPATLIB)

# validation
ipoolValidation: $(COMMONOBJS) $(SIMMVALOBJS) $(VALOBJS) $(EXPATLIB)
	$(CC) $(CFLAGS) -lm  -o ipoolValidation $(COMMONOBJS) $(SIMMVALOBJS) $(VALOBJS) $(EXPATLIB)

# building STD design files
ipoolSTD: $(COMMONOBJS) $(STDOBJS)
	$(CC) $(CFLAGS) -lm -o ipoolSTD $(COMMONOBJS) $(STDOBJS)


# printSizes, tiny but useful for choosing MOT
printSizes: printSizes.c Makefile
	$(CC) $(CFLAGS) printSizes.c -o printSizes


# create TAGS file
TAGS: *.[ch]
	$(ETAGS) *.[ch]


# phony targets
.PHONY: clean distclean all

# remove object files
clean: 
	/bin/rm -f $(COMMONOBJS) $(SIMMVALOBJS) $(DECOBJS) $(SIMMOBJS) $(VALOBJS) $(STDOBJS) substractNegPools.bodyFull.c

# also remove binaries
distclean: clean
	/bin/rm -f ipoolDecoding ipoolSimulations ipoolValidation ipoolSTD printSizes TAGS $(EXPATLIB)
	cd $(EXPATDIR) ; make distclean


#################################################################
# compiling each source file
#################################################################

# pattern rule: making a .o from its .c
# following replaces the implicit predefined rule, which just has 
# an extra $(CPPFLAGS) which we don't use. It also makes every .o
# depend on this Makefile, so changing eg ARCH or DEBUG at the top
# of this file triggers a recompile of everything.
%.o: %.c Makefile
	$(CC) $(CFLAGS) -c $< -o $@

# making expat lib
$(EXPATLIB):
	cd $(EXPATDIR) ; ./configure ; make buildlib ; cp .libs/$(EXPATLIB) ../ 

# signaIO.c includes expat.h
signaIO.o: signaIO.c Makefile
	$(CC) $(CFLAGS) -I$(EXPATDIR)/lib/ -c $< -o $@


# special rule for making substractNegPools.bodyFull.c, using
# the script substractNegPools.body.make.pl
substractNegPools.bodyFull.c: substractNegPools.body.c substractNegPools.body.make.pl
	$(PERL)  substractNegPools.body.make.pl


#################################################################
# making the dependancy files
#################################################################
# each .c file has a corresponding .d file in the deps/ subdir.
# This .d file holds the dependancies for the .o.
# The .d is rebuilt automatically whenever the deps may have changed.

# the dependency files
# following only seems to work with gnu make, patsubst should be more standard
#DEPS := $(SRCS:%.c=deps/%.d)
DEPS := $(patsubst %.c,deps/%.d,$(SRCS))

# rule to build the .d files
# the output of gcc -MM is something like:
# $ gcc -MM pools.c
# pools.o: pools.c config.h types.h pools.h fonc.h
# the perl line then changes this into:
# pools.o deps/pools.d: pools.c config.h types.h pools.h fonc.h
# Therefore each .d depends on all the files that the corresponding
# .o depends on, and will be rebuilt when necessary.
# I think this can be useful for conditional includes, for example
# if pools.c has a block: 
#  #ifdef GMP
#  #include "gmp.h"
#  #endif
# and GMP is defined in config.h...

deps/%.d: %.c substractNegPools.bodyFull.c
	@echo rebuilding dependancy file $@
	@$(SHELL) -ec '$(CC) -MM $(CFLAGS) $< | $(PERL) -e '\''while(<>){s/^(.*)\.o:/$$1.o deps\/$$1.d:/g;print;}'\''> $@'

# include all dep files
include $(DEPS)

