###################################################
#
# Makefile for mothur
#
###################################################

#
# Macros
#
# 64BIT_VERSION - set to no if you are using a 32bit arch.
# OPTIMIZE - yes will increase speed of executable.
# USEREADLINE - link with readline libraries.  Must have readline installed. Windows set to no.
# USEBOOST - link with boost libraries. Must install boost. Allows the make.contigs command to read .gz files.
# BOOST_LIBRARY_DIR - location of boost libraries
# BOOST_INCLUDE_DIR - location of boost include files
# MOTHUR_FILES - default location for mothur to look for input files at runtime. Most often used for reference files.

64BIT_VERSION ?= yes
USEREADLINE ?= no
USEBOOST ?= no
RELEASE_DATE = "\"09/01/2017\""
VERSION = "\"1.40.0\""

# Optimize to level 3:
		CXXFLAGS += -O3

#CPP_11
	CXXFLAGS += -std=c++11

ifeq  ($(strip $(64BIT_VERSION)),yes)
		#if you using cygwin to build Windows the following line
		CXX = x86_64-w64-mingw32-g++
		CC = x86_64-w64-mingw32-g++
		TARGET_ARCH += -m64 -static
		CXXFLAGS += -DBIT_VERSION
endif

CXXFLAGS += -DRELEASE_DATE=${RELEASE_DATE} -DVERSION=${VERSION}

# INCLUDE directories for mothur
		VPATH=source/calculators:source/chimera:source/classifier:source/clearcut:source/commands:source/communitytype:source/datastructures:source/metastats:source/randomforest:source/read:source/svm
		skipUchime := source/uchime_src/
		subdirs :=  $(sort $(dir $(filter-out  $(skipUchime), $(wildcard source/*/))))
		subDirIncludes = $(patsubst %, -I %, $(subdirs))
		subDirLinking =  $(patsubst %, -L%, $(subdirs))
		CXXFLAGS += -I. $(subDirIncludes)
		LDFLAGS += $(subDirLinking)

#
# Get the list of all .cpp files, rename to .o files
#
		OBJECTS=$(patsubst %.cpp,%.o,$(wildcard $(addsuffix *.cpp,$(subdirs))))
		OBJECTS+=$(patsubst %.c,%.o,$(wildcard $(addsuffix *.c,$(subdirs))))
		OBJECTS+=$(patsubst %.cpp,%.o,$(wildcard *.cpp))
		OBJECTS+=$(patsubst %.c,%.o,$(wildcard *.c))

mothur : $(OBJECTS) uchime
		$(CXX) $(LDFLAGS) $(TARGET_ARCH) -o $@ $(OBJECTS) $(LIBS)
		strip mothur


uchime:
		cd source/uchime_src && ./mk && mv uchime ../../ && cd ..


install : mothur


%.o : %.c %.h
		$(COMPILE.c) $(OUTPUT_OPTION) $<
%.o : %.cpp %.h
		$(COMPILE.cpp) $(OUTPUT_OPTION) $<
%.o : %.cpp %.hpp
		$(COMPILE.cpp) $(OUTPUT_OPTION) $<



clean :
		@rm -f $(OBJECTS)
		@rm -f uchime
