# possible targets: all, mmseq, bam2hits, extract_transcripts
# for static compilation: make STATIC=1
# for debug: make DEBUG=1

SUFFIX = # set on command line for static compilation
MACROS = -DVERSION=$(shell echo `cat VERSION`)
ifdef DEBUG
  MACROS += -DDEBUG
endif

CC = gcc
CXX = g++
LIBS = -lgsl -lgslcblas 
LIBS_B2H = -lboost_regex -lbam -lz
OMP = -fopenmp
OPT = -O3

HPC_INCLUDE = -I /apps/boost/1.36.0/include -I /apps/gsl/1.8/include -L /apps/gsl/1.8/lib -I /home/et04/samtools
HPC_LIB = -L /apps/boost/1.36.0/lib -L /apps/gsl/1.8/lib -L /home/et04/samtools


ifeq ($(shell echo `uname -s`),Darwin)
#  CLEANSUFFIX = $(shell echo `cat VERSION`-`uname -s`-`ar -x /opt/local/lib/libgsl.a rng.o && file rng.o | sed -E 's/.*\ (.*)/\1/'`)
  CLEANSUFFIX = $(shell echo -`cat VERSION`-`uname -s`-`uname -m`)
endif
ifeq ($(shell echo `uname -s`),Linux)
  CLEANSUFFIX = $(shell echo -`cat VERSION`-`uname -s`-`uname -m`)
endif
ifdef STATIC
  SUFFIX = ${CLEANSUFFIX}
  ifeq ($(shell echo `uname -s`),Darwin)
    $(warning "Static compilation for Mac OS X")
    LIBS = /usr/local/lib/libgsl.a /usr/local/lib/libgslcblas.a
    LIBS_B2H = /usr/local/lib/libboost_regex-mt.a /usr/local/lib/libbam.a -lz
  endif
  ifeq ($(shell echo `uname -s`),Linux)
    $(warning "Static compilation for Linux")
    CXXFLAGS += -static
  endif
endif
  

.PHONY : all
all : mmseq bam2hits extract_transcripts

mmseq : sokal.o mmseq.cpp
	$(CXX) ${MACROS} $(CXXFLAGS) ${OMP} ${OPT} $^ -o $@${SUFFIX} ${LIBS}

bam2hits : fasta.o bam2hits.cpp
	${CXX} ${MACROS} $(CXXFLAGS) ${OMP} ${OPT} $^ -o $@${SUFFIX} ${LIBS_B2H}

extract_transcripts : fasta.o extract_transcripts.cpp
	${CXX} ${MACROS} $(CXXFLAGS) ${OPT} $^ -o $@${SUFFIX}

sokal.o : sokal.cc sokal.hh

fasta.o : fasta.c fasta.h

.PHONY : clean
clean :
	rm -f mmseq bam2hits extract_transcripts mmseq${CLEANSUFFIX} bam2hits${CLEANSUFFIX} extract_transcripts${CLEANSUFFIX} *.o
