
##############################################################################
## GENERAL SETUP:
##############################################################################

DATE = $(shell date)
OS_NAME = $(shell uname -s)
OS_RELEASE = $(shell uname -r)
RELEASE = 1.0
USER = $(shell whoami)


## This part should be removed before distribution vvv ##

# User-specific configuration:
SKIPME = jbutler psc2 dnave
ifeq ($(filter-out $(SKIPME),$(USER)), $(USER))
ifndef SUBRUN
     junk := $(shell csh -c "if (!(-f MakeDepend))$(MAKE) CXX=$(CXX) SUBRUN=1 MakeDepend")
     junk := $(shell ./MakeDepend)
endif
endif

ifeq ($(USER),psc2)
     USER	= dnave
endif

## This part should be removed before distribution ^^^ ##

##############################################################################
## COMPILE-TIME CONFIGURATION:
##############################################################################

## These can be overridden from the `make' command line.  For example,
##
##   % make DEBUGGING=yes
##
## builds debugging executables.
##
##  In most cases, one must run `make clean' before changing the defaults
## below to ensure that executables are rebuilt correctly.  Generally, the
## compiler-specific makefiles sort out these options and turn on the right
## compiler flags to get the desired behavior.

# Which compiler to use; can be either `g++' or `cxx'.
COMPILER	= g++
#COMPILER	= cxx

# QUIET=yes causes the compiler to be less verbose about warnings.
QUIET		= no

# DEBUG=yes forces out-of-date executables to be built with symbolic
# debugging information.  Any other value is ignored.
DEBUG		= yes

# OPTIM=<compiler optimization> forces user-defined optimizations to be used,
# rather than the compiler-specific default.  
# If OPTIM=fast, then less optimization is done to speed compilation. 
# If OPTIM=none, then no optimization is used.  
# If unset, defaults are used.
OPTIM		=

# PROFILE=yes builds sampled-profiling (i.e. gprof) executables. Any other
# value is ignored.
PROFILE		= no

# FORCE_DEBUG=yes clears NDEBUG (i.e. turns on macros from <assert.h>).  Any
# other value is ignored.
FORCE_DEBUG	= no

# ASSERT=FAST turns on fast(er) assertion checking.  Any other value is
# ignored.
ASSERT		= FAST

# OPEN_MP=yes enables OpenMP directives.  This is currently only supported for
# CXX compilations.  Any other value is ignored.
OPEN_MP		= no

# Enables Posix multithreading.  This is currently only supported for CXX
# compilations, and partially for GCC v3.  This is enabled by default, since
# it has no effect in Gcc compilations.  Any other value is ignored.
PTHREADS	= yes


##############################################################################
## BASE SYSTEM SETUP:
##############################################################################

## These macros are used in Makefile and Makefile_auto to compile Spines.
## The compiler-specific makefile augments these macros before they are used
## used to build the system.  These are here to ensure that the macros are
## instantiated, and as a reminder that the macros exist.

# Location of Xerces XML parser library:
XERCES_INC	=
XERCES_LIB	=

# Base definitions:
SYS_DEFS	= \
  		  -DMAKE_DATE="$(DATE)" \
		  -DMAKE_OS_RELEASE="$(OS_RELEASE)" \
		  -DMAKE_RELEASE="$(RELEASE)" \
		  -DNEW_MAKEFILE

# XXX
ifeq ($(USER),dnave)
SYS_DEFS	+= -DNO_CHECK_EXECS
endif
# XXX


# Warning control:
SYS_WARN	=

# Optimization control:
SYS_OPT		=

# Language control (e.g. template instantiations):
SYS_LANG	=

# Where to find system-specific includes:
SYS_INCS	= -I. $(XERCES_INC)

# Linking control (e.g. to link templates):
SYS_LINK	=

# Required libraries:
SYS_LIBS	= -lm

# OpenMP macros:
ifeq ($(OPEN_MP),yes)
  OMP_FLAGS		=
  OMP_INCS		=
  OMP_DEFS		=

  OMP_OPTIONS		= \
			$(OMP_FLAGS) \
			$(OMP_INCS) \
			$(OMP_DEFS)

  OMP_LINK		=
  OMP_LIBS		=
endif

# Posix threads macros:
ifeq ($(PTHREADS),yes)
  PTHREAD_FLAGS		=
  PTHREAD_INCS		=
  PTHREAD_DEFS		= -DHAVE_PTHREADS

  PTHREAD_OPTIONS	= \
			$(PTHREAD_FLAGS) \
			$(PTHREAD_INCS) \
			$(PTHREAD_DEFS)

  PTHREAD_LINK	=
  PTHREAD_LIBS	=
endif

ifeq ($(OPTIM),fast)
  FAST		= yes
endif



##############################################################################
## COMPILATION:
##############################################################################

# Bring in the compiler-specific makefile:

ifeq ($(COMPILER),g++)
include Makefile_g++
endif

ifeq ($(COMPILER),cxx)
include Makefile_cxx
endif

# Setup final compilation options:

CPP_OPTIONS	= \
		$(SYS_WARN) \
		$(SYS_OPT) \
		$(SYS_DEFS) \
		$(SYS_LANG) \
		$(SYS_INCS) \
		$(OMP_OPTIONS) \
		$(PTHREAD_OPTIONS)

LINK_OPTIONS	= \
		$(SYS_WARN) \
		$(SYS_LINK) \
		$(OMP_LINK) \
		$(PTHREAD_LINK)

LINK_LIBS	= \
		$(SYS_LIBS) \
		$(OMP_LIBS) \
		$(PTHREAD_LIBS) \

CPPO		= $(LINK_OPTIONS)
CPPF		= $(CPP_OPTIONS) -c
CPPC		= $(CPP_OPTIONS) -c


##############################################################################
## COMPILATION TARGET SETUP:
##############################################################################

## The list of executables used by an Assemble run on real data, in rough
## order:

ASSEMBLY_EXECS = \
	Assemble \
	PackSource \
	RequiredDataCheck \
	CheckConfigurationFile \
	TraceArchiveParser \
	ConfigurationParser \
	PartitionInput \
	FetchAndTrim \
	FormPairings1 \
	FormPairings2 \
	FormPairings3 \
	BackupOriginalData \
	ExtractMetainfo \
	PreassembleInserts \
	ReadsToAligns1 \
	ReadsToAligns2 \
	ReadsToAligns3 \
	ReorderReads \
	TrimBadEnds \
	AnalyzeDepth1 \
	AnalyzeDepth2 \
	CorrectErrors \
	DiscardCrummyAlignments \
	LocateChimeras1 \
	LocateChimeras2 \
	LocateChimeras3 \
	LocateChimeras4 \
	LocateChimeras5 \
	BeforeFindSeeds \
	FindSeeds \
	ImproveScores \
	AlignsToLinks \
	SaveRawLinks \
	CleanAlignments \
	Layout \
	AllcontigsToPhrap \
	ToConsensus2 \
	ToConsensus3 \
	MergeContigsWithGaps \
	PostMergeClean \
	BackupMergedContigs \
	BriefGoodAligns \
	MergeSupercontigs \
	VerifySupercontigIntegrity \
	MakeFasta \
	ReportPairings \
	MakeAlewife \
	EvaluateConsensus \
	ReadUtilization \
	FinishOldReport \
	RefineConsensusAlignments \
	FinishNewReport \
	CreateAceFile

## A supplement to the above list, consisting of executables used by Assemble
## in a run on simulated data:

SIMULATION_EXECS = \
	CreateReadsFromTemplates \
	MakeTemplates \
	ParseRel

## The other executables which are always to be made:

UTILITY_EXECS = \
	AddSuperLengths \
	AlignByTransitivity \
	AlignReadsToConsensus \
	AllVsAll \
	Blast \
	CheckPairs \
	Clean \
	CleanAlignments \
	CmpSeq \
	CmpSeqPrint \
	CompareRuns \
	ContigifyUnplacedPartners \
	CvsBack \
	CvsClean \
	DumpAligns \
	DumpBacEnds \
	EnlargeContigs \
	EvaluateContigCenters \
	Fasta \
	FindChimeras \
	FindIdenticalReads \
	FindRepeats \
	FixReadLocations \
	GC \
	GenerateHumanReadableNobbits \
	LoadProject \
	Ls \
	MergeAssemblies \
	PackSource \
	PatchGaps \
	PositionMarkers \
	PrintAligns \
	PrintFiles \
	PrintNobbits \
	PrintReadMetaInfo \
	RecomputeGaps \
	Recover \
	ReportMemoryUsage \
	SelectAlign \
	SelectDoomedLinks \
	SelectFastb \
	SelectNamedReads \
	SelectNamedReadsOld \
	SelectQualb \
	SelectReads \
	ShowContigs \
	ShowPairings \
	ShowSuperLinks \
	ShrinkAssembly \
	SymmetrizeAligns \
	TraceContig \
	TraceLink \
	TranslateAssembly \
	ViewAlignments \
	Walk

EXECUTABLES = \
	$(ASSEMBLY_EXECS) \
	$(UTILITY_EXECS) \
	$(SIMULATION_EXECS)


# Standard targets:

all: assembler utilities simtools

assembler: $(ASSEMBLY_EXECS)
utilities: $(UTILITY_EXECS)
simtools:  $(SIMULATION_EXECS)

Makefile_auto: MakeDepend
	./MakeDepend

depend: MakeDepend
	./MakeDepend

clean:
	rm -f *.o $(EXECUTABLES) \
	     *.pixie *.third contigs.out my.permanent.log.file \
             core a.out Makefile.bak bsubin
	rm -rf cxx_repository

# Non-standard targets:

LayoutSRCS = Contig.h  Link.h Read.h FullGraph.h SparseGraph.h RunTime.h \
	MainArrays.h RawLink.h RawLink.cc RawGraph.h ActualLoc.h RunTime.cc \
	Assert.h Assert.cc FastIfstream.h FastIfstream.cc \
	Misc.h Templates.h ParsedArgs.cc BuildSuperContigs.cc \
	BuildSparseGraph.cc FullGraph.cc BuildContigs.cc LayoutMain.cc Contig.cc \
	MisplacedReads.cc BuildRawGraph.cc BuildFullGraph.cc  ReadActualLocs.cc \
	CleanRawGraph.cc Link.cc SparseGraph.cc ActualLoc.cc \
	Read.cc ReadsConnectingContigs.cc SuperContig.h ContigLink.h \
	ConvertReadLocationToActualLoc.cc PrintFullPerfect.cc \
	ReportResults.cc Heuristics.h BuildSuperContigs.cc SuperContig.cc \
	BuildContigEarMuff.cc BuildSuperContigActualLocs.cc EarLink.h EarLink.cc \
	Ear.h BuildSortedReads.cc InsertUnassignedReads.cc \
	Spgraph.h Spgraph.cc ComputeShortestPaths.cc \
	WrapUnassignedReadsIntoContigs.cc bcal.h SmithWat.h \
	BuildPerfectFullGraph.cc InsertSubsetReadsIntoContigs.cc \
	ContigsToPhrapInput.cc MakeReadLocations.cc ComparePlacementInContigs.cc \
	MakeReadLocations.h ComparePlacementInContigs.h PlaceEarlinksInGaps.cc \
	GapsToPhrapInput.cc DetectEarmuffConflicts.cc Ear.cc \
	DisplayGenomeAssembly.cc RemoveRepetitiveLinks.cc \
	FindPseudoSubsetReads.cc FindPseudoSubsetReads.h System.cc \
	MergePseudomaximalReads.cc ReadMetaInfoBroker.cc ReadMetaInfoBroker.h \
        Heuristics.cc ArrayTempls.h ReadLocation.h ReadLocation.cc \
	OutputFcns.h ReadActualLocs.h BuildContigs.h \
        BuildSuperContigs.h Basevector.cc Basevector.h \
	BuildContigEarMuff.h DetectEarmuffConflicts.h InteractiveSession.h InteractiveSession.cc \
	ConvertReadLocationToActualLoc.h MergeVerifiedReadPairs.cc \
	InsertAllMissedNeighborLinksInFullgraph.cc DismantleFullToRaw.cc ContigActualloc.h \
	ContigActualloc.cc Misc.cc LogoddRatios.h LogoddRatios.cc \
	LayoutSubroutines.h LayoutSubroutines.cc UseGoldenOverlaps.h UseGoldenOverlaps.cc \
	CountLinks.cc LinkStat.cc ReadPairing.h ReadPairing.cc ReadMetaInfo.h ReadMetaInfo.cc

LayoutOBJS = BuildSparseGraph.o  FullGraph.o ReadMetaInfoBroker.o ReadLocation.o \
	BuildSortedReads.o BuildPerfectFullGraph.o Assert.o RawLink.o RunTime.o \
	PrintFullPerfect.o BuildContigEarMuff.o FastIfstream.o \
	BuildContigs.o LayoutMain.o Contig.o MisplacedReads.o \
	BuildSuperContigActualLocs.o SuperContig.o BuildSuperContigs.o EarLink.o  \
	BuildRawGraph.o common.o BuildFullGraph.o  ReadActualLocs.o \
	ReportResults.o ConvertReadLocationToActualLoc.o \
	CleanRawGraph.o Link.o SparseGraph.o ActualLoc.o \
	Kclock.o GetHost.o String.o System.o \
	Read.o ReadsConnectingContigs.o InsertUnassignedReads.o \
	Splink.o Spgraph.o ComputeShortestPaths.o WrapUnassignedReadsIntoContigs.o \
	InsertSubsetReadsIntoContigs.o Basevector.o \
	ContigsToPhrapInput.o MakeReadLocations.o ComparePlacementInContigs.o \
        PlaceEarlinksInGaps.o ParsedArgs.o Crash.o \
	GapsToPhrapInput.o DetectEarmuffConflicts.o Ear.o \
        DisplayGenomeAssembly.o InsertAllMissedNeighborLinksInFullgraph.o \
	FindPseudoSubsetReads.o MergePseudomaximalReads.o \
        Heuristics.o InteractiveSession.o MergeVerifiedReadPairs.o \
	ContigActualloc.o Misc.o LogoddRatios.o \
        LayoutSubroutines.o UseGoldenOverlaps.o Vec.o CountLinks.o LinkStat.o \
        ReadPairing.o ReadMetaInfo.o PrintMemStats.o Feudal.o $(PROCBUF_OBJ)

Layout:  $(LayoutOBJS) $(LayoutSRCS)
	$(CPLUSPLUS) $(CPPO) -o Layout $(LayoutOBJS) $(LINK_LIBS)


ifeq ($(COMPILER),cxx)
MakeDepend: procbuf_local.o
endif

MakeDepend: MakeDepend.o String.o System.o Assert.o Crash.o
	$(CPLUSPLUS) $(LINK_OPTIONS) -o MakeDepend \
	  MakeDepend.o String.o System.o Assert.o Crash.o $(LINK_LIBS)

Random.o: Random.cc
	cp Random.cc Random.c
	$(CC) -c Random.c -w -Drandom=randomx -Dsrandom=srandomx -o Random.o
	rm Random.c

Alignment.o: Alignment.cc
	$(CPLUSPLUS) $(CPPC_SAFE) $*.cc

LayoutMain.o: LayoutMain.cc 
	$(CPLUSPLUS) $(CPPF) $*.cc 

procbuf_local.o: procbuf_local.cc procbuf_local.h
String.o: String.cc String.h Assert.h Types.h
System.o: System.cc System.h String.h Types.h

# How to compile C++ files:
.cc.o:
	$(CPLUSPLUS) $(CPP_OPTIONS) -c $*.cc



##############################################################################
## ADDITIONAL TARGETS:
##############################################################################

## Prepping Spines for download:
## For example:
##
##    % make download DOWNLOAD_DIR=<destination>
##
## This will create three tarred, gzipped directories: the binaries, the
## source, and a valid data directory with sample project.

ifndef $(DOWNLOAD_DIR)
  DOWNLOAD_DIR	= for_download
endif

BIN_DL_DIR	= $(DOWNLOAD_DIR)/Spines_bin
SRC_DL_DIR	= $(DOWNLOAD_DIR)/Spines_src
DATA_DL_DIR	= $(DOWNLOAD_DIR)/Spines_data

download: bin_download src_download data_download

bin_download: $(EXECUTABLES) ViewAlignments.cc Time Manual.html LICENSE.txt lib$(XERCES_LIB).so LICENSE.xerces.txt
	rm -rf $(BIN_DL_DIR)
	mkdir -p $(BIN_DL_DIR)
	cp $(EXECUTABLES) Time $(BIN_DL_DIR)
	cp Manual.html $(BIN_DL_DIR)
	cp LICENSE.txt $(BIN_DL_DIR)
	cp ViewAlignments.cc $(BIN_DL_DIR)
	cp lib$(XERCES_LIB).so LICENSE.xerces.txt $(BIN_DL_DIR)
	( cd $(DOWNLOAD_DIR); \
	  tar -cvf Spines_bin.tar Spines_bin; \
	  rm -rf Spines_bin; \
	  gzip -vf Spines_bin.tar )

src_download:
	rm -rf $(SRC_DL_DIR)
	mkdir -p $(SRC_DL_DIR)
	find . \( -name "*.c" -o \
	          -name "*.cc" -o \
	          -name "*.cpp" -o \
	          -name "*.h" -o \
	          -name "*.hh" -o \
		  -name "*.hpp" \) -print | cpio -pd $(SRC_DL_DIR)
	cp Makefile $(SRC_DL_DIR)
	cp LICENSE.txt $(SRC_DL_DIR)
	cp LICENSE.xerces.txt $(SRC_DL_DIR)/xerces_include
	( cd $(DOWNLOAD_DIR); \
	  tar -cvf Spines_src.tar --exclude=CVS Spines_src; \
	  rm -rf Spines_src; \
	  gzip -vf Spines_src.tar )

data_download:
	( cd $(DOWNLOAD_DIR); \
	  rm -rf Spines_data; \
	  cvs checkout Spines_data; \
	  tar -cvf Spines_data.tar --exclude=CVS Spines_data; \
	  rm -rf Spines_data; \
	  gzip -vf Spines_data.tar )

kecho:
	@echo $(command) 

gcov_clean:
	rm -f *.bb *.bbg *.da *.gcov

diff: PrintFiles
	@./kcomp `PrintFiles`


## Additional targets available with CXX:

PIXIES =  ReadsToAligns.pixie Layout.pixie LoadProject.pixie \
	AlignReadsToConsensus.pixie FetchAndTrim.pixie


THIRDS =  ReadsToAligns.third Layout.third LoadProject.third \
	AlignReadsToConsensus.third FetchAndTrim.third

pixie: $(PIXIES)

third: $(THIRDS)


ReadsToAligns.pixie:  ReadsToAligns
	 atom -tool pixie ReadsToAligns
LoadProject.pixie:  LoadProject
	 atom -tool pixie LoadProject
AlignReadsToConsensus.pixie:  AlignReadsToConsensus
	 atom -tool pixie AlignReadsToConsensus
FetchAndTrim.pixie:  FetchAndTrim
	 atom -tool pixie FetchAndTrim
Layout.pixie:  Layout
	 atom -tool pixie Layout

ReadsToAligns.third:  ReadsToAligns
	 atom -tool third ReadsToAligns
LoadProject.third:  LoadProject
	 atom -tool third LoadProject
AlignReadsToConsensus.third:  AlignReadsToConsensus
	 atom -tool third AlignReadsToConsensus
FetchAndTrim.third:  FetchAndTrim
	 atom -tool third FetchAndTrim
Layout.third:  Layout
	 atom -tool third Layout

# Import automatic dependencies here:
include Makefile_auto
