# -*- Mode: Makefile -*-

# TODO: Find a way to point out directories (i.e. current dir, or root dir)
# TODO: Find the names of the icc windows library includes.. i.e. dlls/etc.
# TODO: Find icc equivalents for W64 (warn about 32 vs 64 bit incompatibility
# TODO: Add (linux) icc warning about windows porting issues.
# TODO: There is an icc option for short 1-liner diagnostics.. see if there is
# an opposite!  (i.e. long diagnostics which maybe will give us a stack trace
# for warnings)
# TODO: Learn how the hell to use idb.
#
# Default include makefile.
# Before (or after?) including this makefile, define the following variables
# 	INCLUDES = -I $(TOKUROOT)/.. -I . -I $(TOKUROOT)/... #Whatever include directories
# 		List of directories to include for search paths.
# 	TOKUROOT = absolute path of the root of this branch.
# 		TODO: find alternative way to do this.
# 	BINS     = base names for bins
# 	SUBDIRS  = Subdirs to make/clean
#
# On cygwin do:
#   make CYGWIN=cygwin check
#
# For default=debug (no optimization) do
#   make DEBUG=1

# For verbose output do
#   make VERBOSE=1
# For very verbose output do 
#   make VERBOSE=2

# For CIL do
#   make CIL=1

# For coverage do
#   make GCOV=1

# For profiling do
#   make PROF=1

# To make warnings be warnngs instead of errors do
#   make WERROR=

ifeq ($(DEBUG),)
    DEBUG = 0
endif

ifeq ($(HAVE_CILK),)
    HAVE_CILK = 0
endif

ifeq ($(VTUNE),1)
    LINK_MUST_BE_LAST = /link /fixed:no
else
    LINK_MUST_BE_LAST =
endif

.DELETE_ON_ERROR:

ifneq ($(GCOV),)
    GCOV_FLAGS = -fprofile-arcs -ftest-coverage -DGCOV
endif

ifneq ($(PROF),)
    PROF_FLAGS = -pg
endif

ifeq ($(FASTCHECKTIMEOUT),)
    FASTCHECKTIMEOUT = 30
endif

OPT_OPTFLAGS = -O3 -finline-functions
DBG_OPTFLAGS = -O0

SYSTEM = $(shell uname -s | tr [:upper:] [:lower:])
ARCH = $(shell uname -m | tr [:upper:] [:lower:])

ifeq ($(VERBOSE),2)
VERBVERBOSE=-v
MAYBEATSIGN= 
else ifeq ($(VERBOSE),1)
  VERBVERBOSE=
  MAYBEATSIGN=
 else
  VERBVERBOSE=-q
  MAYBEATSIGN=@
endif

CPPFLAGS = $(INCLUDEDIRS) -I$(PORTABILITY_HEADERS) -I$(TOKUROOT)toku_include $(EXTRA_INCLUDES)
ifeq ($(SYSTEM),freebsd)
CPPFLAGS += -DTOKU_ALLOW_DEPRECATED
endif
ifeq ($(SYSTEM),sunos)
CPPFLAGS += -DTOKU_ALLOW_DEPRECATED
endif

ifeq ($(CC),icc)
CPPFLAGS += -DUSING_ICC
ifeq ($(HAVE_CILK),1)
CPPFLAGS += -DHAVE_CILK
LCILKRTS += -lcilkrts
endif
endif

ifeq ($(TOKUDB_REVISION),)
TOKUDB_REVISION = 0
endif
CPPFLAGS += -DTOKUDB_REVISION=$(TOKUDB_REVISION)

#CFLAG default options
# Add -Wconversion #DISABLED for now.

ifeq ($(SYSTEM),freebsd)
WERROR    =
else
WERROR    = -Werror
endif

ifeq ($(GCCVERSION),)
GCCVERSION = $(shell $(CC) --version | head -1 | cut -f3 -d " ")
endif

# -Wno-deprecated is needed on gcc 4.4.{0,1,2,4} to make the #ident complaints go away.
# -Wno-strict-aliasing is needed on gcc 4.4.{1,2,4} to make certain gratuitous warnings go away.
# -Wno-unused-result is needed on gcc 4.4.1 to make warnings about ignoring the results of certain system calls go away.
# Gcc 4.4.1-4ubuntu9 cannot handle --combine, but 4.4.4 can.  But 4.5 cannot.
ifeq ($(GCCVERSION),4.6.1)
 GCC_VERSION_SPECIFIC = -Wno-deprecated -Wno-unused-but-set-variable -Wno-unused-but-set-parameter
else ifeq ($(GCCVERSION),4.6.0)
 GCC_VERSION_SPECIFIC = -Wno-deprecated -Wno-unused-but-set-variable -Wno-unused-but-set-parameter
else ifeq ($(GCCVERSION),4.4.4)
 GCC_VERSION_SPECIFIC = -Wno-deprecated 
else ifeq ($(GCCVERSION),4.4.2)
 GCC_VERSION_SPECIFIC = -Wno-deprecated 
else ifeq ($(GCCVERSION),4.4.1)
 GCC_VERSION_SPECIFIC = -Wno-deprecated -Wno-unused-result
 COMBINE := 0
else ifeq ($(GCCVERSION),4.4.0)
 GCC_VERSION_SPECIFIC = -Wno-deprecated
else ifeq ($(GCCVERSION),4.6.2)
 COMBINE := 0
else ifeq ($(GCCVERSION),4.6.3)
 COMBINE := 0
endif

WALL      = $(GCC_VERSION_SPECIFIC) -Wall -Wextra -Wcast-align -Wbad-function-cast -Wno-missing-noreturn -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations \
 -Wpointer-arith # don't do arithmetic on void*.

FORMAT    = -Wmissing-format-attribute #-Wformat=2 #Stronger printf warnings once logger.c cleaned up
ifeq ($(SYSTEM),sunos)
VISIBILITY=
else
VISIBILITY= -fvisibility=hidden
endif
FPICFLAGS = -fPIC
SHADOW = -Wshadow
SYMBOLS   = -g3 -ggdb3
PORTABILITY=
SKIP_WARNING=
COMBINE_C = -combine -c
ifeq ($(GCCVERSION),4.4.4)
 CFLAGS += -Wno-strict-aliasing 
else ifeq ($(GCCVERSION),4.4.2)
 CFLAGS += -Wno-strict-aliasing 
else ifeq ($(GCCVERSION),4.4.1)
 CFLAGS += -Wno-strict-aliasing 
else ifeq ($(GCCVERSION),4.4.0)
 CFLAGS += -Wno-strict-aliasing 
else ifeq ($(GCCVERSION),4.6.2)
 CFLAGS += -Wno-error=deprecated-declarations
 # Valgrind 3.6.1 annotations have unused but set variables.  Valgrind 3.7.0 fixed this problem.
 CFLAGS += -Wno-unused-but-set-variable
else ifeq ($(GCCVERSION),4.6.3)
 # Valgrind 3.6.1 annotations have unused but set variables.  Valgrind 3.7.0 fixed this problem.
 CFLAGS +=    -Wno-unused-but-set-variable
endif

LIBTOKUDB=tokudb
LIBTOKUPORTABILITY=tokuportability
LIBPORTABILITY_SO=$(TOKUROOT)lib/lib$(LIBTOKUPORTABILITY).$(SOEXT)
LIBPORTABILITY_A=$(TOKUROOT)lib/lib$(LIBTOKUPORTABILITY).$(AEXT)

PORTABILITY_HEADERS= $(TOKUROOT)$(SYSTEM)
ALWAYS_LINK= -lz -lpthread -ldl
ifeq ($(CC),icc)
# needed for things like intel_fast_memset(), intel_sse2_strlen()
ifeq ($(DEBUG),0)
    IRCSTATIC = -Bstatic
else
    IRCSTATIC =
endif
ALWAYS_LINK+=$(IRCSTATIC) -lirc -Bdynamic -lstdc++
endif
C99       = -std=c99
W64       = #-Wshorten-64-to-32
BINOUTPUT = -o
OOUTPUT   = -o
AROUTPUT  =#empty
BINSUF=#none
WRONGBINSUF=.exe
OEXT=o
WRONGOEXT=obj
AEXT=a
WRONGAEXT=lib
SOEXT=so
WRONGSOEXT=dll
AR=ar
DBG_ARFLAGS=cr
OPT_ARFLAGS=cr
LINK=-l
DEPEND_COMPILE += \
	$(TOKUROOT)include/db.h \
	$(TOKUROOT)toku_include/toku_os.h \
	$(TOKUROOT)toku_include/toku_portability.h \
	$(TOKUROOT)toku_include/rdtsc.h \
# keep this line so I can have a \ on the previous line
CCQUIET=

comma:=,
empty:=#
space:=$(empty) $(empty)
LINK_LPATH=$(patsubst %,-Wl$(comma)-rpath$(comma)%,$(RPATH_DIRS))
LINK_RPATH=$(patsubst %,-L%$(space),$(RPATH_DIRS))
DLINK_FILES_PREPROCESS_1=$(patsubst %.$(SOEXT),%,$(DLINK_FILES))
DLINK_FILES_PREPROCESS_2=$(patsubst lib%,%,$(notdir $(DLINK_FILES_PREPROCESS_1)))
LINK_DLINK_FILES=$(patsubst %,$(LINK)%,$(notdir $(DLINK_FILES_PREPROCESS_2)))

CRUNTIME=
DEPEND_LINK += \
	$(LIBPORTABILITY_SO) \
	$(LIBPORTABILITY_A) \
	$(LINK_FILES) \
# keep this line so I can have a \ on the previous line


ifeq ($(CYGWIN),)
    OS_CHOICE=$(SYSTEM)
    OS_NOTCHOICE=windows
else ifneq ($(CC),icc)
    OS_CHOICE=$(SYSTEM)
    OS_NOTCHOICE=windows
else
    OS_CHOICE=windows
    OS_NOTCHOICE=$(SYSTEM)
endif

DEPEND_COMPILE += $(wildcard $(TOKUROOT)$(OS_CHOICE)/*.h)

SUPPRESSIONS=no

#Tools
ifeq ($(VALGRIND),)
    VALGRIND=valgrind
endif
VGRIND_BASE=$(VALGRIND) --quiet --error-exitcode=1 \
       --suppressions=$(TOKUROOT)ft/valgrind.suppressions \
       --suppressions=$(TOKUROOT)src/tests/bdb.suppressions \
       --gen-suppressions=$(SUPPRESSIONS) --num-callers=20
ifeq ($(DB_ATTACH),1)
	VGRIND_BASE+=--db-attach=yes
endif
ifeq ($(TRACK_ORIGINS),1)
	VGRIND_BASE+=--track-origins=yes
endif
VGRIND=$(VGRIND_BASE) --leak-check=full --show-reachable=yes 
ifeq ($(VGRIND),)
VGRIND_NOMEM=
else
VGRIND_NOMEM=$(VGRIND_BASE)
endif

HGRIND=$(VALGRIND) --quiet --tool=helgrind --error-exitcode=1
DRD=$(VALGRIND) --quiet --tool=drd --error-exitcode=1

# When debugging, try: valgrind --show-reachable=yes --leak-check=full ./brt-test
# When debugging, try: valgrind --show-reachable=yes --leak-check=full ./brt-test

ifeq ($(WINDBG),)
    WINDBG=0
endif

ifeq ($(CC),icc)
    #icc only:
    OPT_OPTFLAGS = -O3 -ip -ipo1
    DBG_OPTFLAGS = -O0
    COMBINE_C = -ipo-c
    FORMAT= #No argument for extra format warnings.
    WALL  = -Wall -Wcheck # '-Wextra' becomes '-Wcheck' in icc
    SYMBOLS= -g -debug all # deprecated -inline-debug-info
    PORTABILITY=-diag-enable port-win
    AR=xiar
    DISABLE_WARNING=
    ifneq ($(CYGWIN),)
	#Cygwin
        ICC_NOWARN=-Qdiag-disable:
        DISABLE_WARNING +=869# Don't complain about unused variables (since we defined __attribute__ to be nothing.)
        DISABLE_WARNING +=593# Don't complain about unused variables (since we defined __attribute__ to be nothing.)
        DISABLE_WARNING +=11000# Disable message about multi-file optimization
        DISABLE_WARNING +=11000# Disable message about single-file optimization
        DISABLE_WARNING +=11005# Disable message about creating object file
        DISABLE_WARNING +=188# Disable message about 0 used for enum
        DISABLE_WARNING +=1011# Disable message about missing return with an abort
    else
	#Linux
        ICC_NOWARN=-diag-disable #Need the space
    endif
    #DISABLE_WARNING +=111# Unreachable code (i.e. assert(0) )
    #DISABLE_WARNING +=271# Remove warnings about trailing comma being nonstandard
    #DISABLE_WARNING +=424# Remove warnings about extra ';' being ignored
    #DISABLE_WARNING +=810# Remove warnings about losing precision
    DISABLE_WARNING +=94# Allow arrays of length 0
    #DISABLE_WARNING +=118# Allow void functions to return void functions
    #DISABLE_WARNING +=177# Do not complain about static variables that are not used.
    #DISABLE_WARNING +=188# Do not complain about enumerated type mixed with another type.
    DISABLE_WARNING +=589# Do not complain about goto into a block that skips initializing variables.  GCC catches the actual uninitialized variables.
    #DISABLE_WARNING +=981# Do not complain about "operands are evaluated in unspecified order".  This seems to be generated whenever more than one argument to a function or operand is computed by function call.
    #DISABLE_WARNING +=1324# Do not complain about rdtsc clobbering its registers more than once.
    #DISABLE_WARNING +=1786# Do not complain about 'read/write/other standards' being deprecated
    DISABLE_WARNING +=2259# Do not complain about 'non-pointer conversion from int to u_int8_t (and other small types) may lose significant bits'.  This is a useless warning because it produces too many false positives.
    DISABLE_WARNING +=11000 11001 11006# Do not remark about multi-file optimizations, single-file optimizations, and object temp files.
    DISABLE_WARNING += 11003# Don't complain if some file was not compiled with -ipo
    SKIP_WARNING = $(ICC_NOWARN)$(shell sed 's/ /,/g' <<< "$(DISABLE_WARNING)" | sed 's/,//')
endif

ifneq ($(CC),icc)
ifeq ($(ARCH),i686)
ARCHFLAGS = -march=$(ARCH)
endif
endif

ifneq ($(CYGWIN),)
    #Cygwin (Windows) Must override some settings
    BINSUF=.exe
    WRONGBINSUF=#empty
    ALWAYS_LINK=/usr/lib/libz.a
    SRCPATH_DIRS = `find $(TOKUROOT) -type d | grep -v '/\.[^.]' | grep -F -v "$(TOKUROOT)$(OS_NOTCHOICE)" | tr '\n' ';'` 
    ifeq ($(WINDBG),1)
	VGRIND = windbg -hd -y "$(SRCPATH_DIRS)" -srcpath "$(SRCPATH_DIRS)" #No Valgrind in cygwin #-G is quit at exit, but does not return ERROR on error!!! and quits on error to quickly
    else
	VGRIND =
    endif
    HGRIND   =#No Hgrind in cygwin
    FPICFLAGS=#FPIC is default and not allowed as an option.
    VISIBILITY=#Not supported
    ifeq ($(CC),icc)
	#Cygwin icc only
	ifeq ($(CRUNTIME),)
	ifeq ($(DEBUG),0)
	    CRUNTIME=MT
	else
	    CRUNTIME=MTd
	endif
	endif
        ALWAYS_LINK=$(TOKUROOT)windows/lib/$(CRUNTIME)/zlib.lib Ws2_32.lib psapi.lib PowrProf.lib
        LINK=#Empty
        BINOUTPUT=-Fe
        OOUTPUT=-Fo
        AROUTPUT=/out:
        OEXT=obj
        WRONGOEXT=o
        AEXT=lib
        WRONGAEXT=a
        SOEXT=dll
        WRONGSOEXT=so
        C99       = -Qstd=c99 -Qvc9
        OPT_OPTFLAGS = -Ox -Qip -Qipo1
        DBG_OPTFLAGS = -Od
        COMBINE_C = -Qipo-c
	WERROR = -WX # Windows icc version of -Werror
	WERROR += -Qdiag-error:167,266,810,592,147 # Workarounds for -WX not being complete on windows icc
        SYMBOLS= -Zi -debug:all -Qinline-debug-info
        PORTABILITY=
        PORTABILITY_HEADERS = $(TOKUROOT)windows
        AR=xilib
        DBG_AROPT=-qnoipo
        OPT_AROPT=-qipo
        DBG_ARFLAGS=$(DBG_AROPT) -lib /VERBOSE /WX /NOLOGO
        OPT_ARFLAGS=$(OPT_AROPT) -lib /VERBOSE /WX /NOLOGO
        CCQUIET=-nologo
        LINK_DLINK_FILES=$(patsubst %.$(SOEXT),%.$(AEXT),$(DLINK_FILES))
        LINK_RPATH=
        LINK_LPATH=
        DEPEND_LINK += $(LINK_DLINK_FILES)
        W64 = -Wport #-Wp64 
    else
        #Cygwin gcc only
        FORMAT = -Wno-format
    endif
endif

ifeq ($(DEBUG),0)
    OPTFLAGS = $(OPT_OPTFLAGS)
    ARFLAGS  = $(OPT_ARFLAGS)
else
    OPTFLAGS = $(DBG_OPTFLAGS)
    ARFLAGS  = $(DBG_ARFLAGS)
endif

LZMA_H = $(TOKUROOT)include/lzma.h
ifeq ($(CC),icc)
 ifeq ($(DEBUG),1)
  LZMA_A = $(TOKUROOT)lib/liblzma_icc_dbg.$(AEXT)
 else
  LZMA_A = $(TOKUROOT)lib/liblzma_icc_opt.$(AEXT)
 endif
else
 ifeq ($(DEBUG),1)
  LZMA_A = $(TOKUROOT)lib/liblzma_gcc_dbg.$(AEXT)
 else
  LZMA_A = $(TOKUROOT)lib/liblzma_gcc_opt.$(AEXT)
 endif
endif

CFLAGS += $(WALL) $(W64) $(WERROR) $(FORMAT) $(VISIBILITY) $(FPICFLAGS) $(SHADOW) $(ARCHFLAGS)
CFLAGS += $(OPTFLAGS) $(GCOV_FLAGS) $(PROF_FLAGS)
CFLAGS += $(SYMBOLS) $(SKIP_WARNING) $(C99) $(CCQUIET)

LDFLAGS_NOLIB = $(OPTFLAGS) $(SYMBOLS) $(GCOV_FLAGS) $(PROF_FLAGS)
LDFLAGS = $(LDFLAGS_NOLIB) $(LINK_LPATH) $(LINK_FILES) $(ALWAYS_LINK) $(LINK_DLINK_FILES) $(LINK_RPATH)

ifneq ($(CRUNTIME),)
LDFLAGS += -$(CRUNTIME)
CFLAGS += -$(CRUNTIME)
endif

# Need XOPEN_SOURCE=600 to get strtoll()
ifeq ($(SYSTEM),linux)
CPPFLAGS+=-D_SVID_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_XOPEN_SOURCE=600
endif
ifeq ($(SYSTEM),sunos)
CPPFLAGS+=-D_SVID_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_XOPEN_SOURCE=600
endif

#TODO: Fix this up if we want to keep using CIL.
#ifeq ($(CIL),1)
#	CC=../../cil/cil-1.3.6/bin/cilly --merge --keepmerged
#endif

# When debugging, try: valgrind --show-reachable=yes --leak-check=full ./brt-test

ifneq ($(CYGWIN),)
    CSCOPE=mlcscope
else
    CSCOPE=cscope
endif

.PHONY: tags
tags: $(TOKUROOT)cscope.out $(TOKUROOT)TAGS

ifneq ($(TOKUROOT),./)
TAGS: $(TOKUROOT)*/*.[ch] $(TOKUROOT)*/*/*.[ch] $(TOKUROOT)*/*/*/*.[ch]
	etags $(TOKUROOT)*/*.[ch] $(TOKUROOT)*/*/*.[ch] $(TOKUROOT)*/*/*/*.[ch]
endif

$(TOKUROOT)TAGS: $(TOKUROOT)*/*.[ch] $(TOKUROOT)*/*/*.[ch] $(TOKUROOT)*/*/*/*.[ch]
	cd $(TOKUROOT) && etags */*.[ch] */*/*.[ch] */*/*/*.[ch]

$(TOKUROOT)cscope.files: $(TOKUROOT)*/*.[ch] $(TOKUROOT)*/*/*.[ch] $(TOKUROOT)*/*/*/*.[ch]
	cd $(TOKUROOT) && (echo */*.[ch] */*/*.[ch] */*/*/*.[ch] | tr " " "\n") > $(notdir $@)

$(TOKUROOT)cscope.in.out $(TOKUROOT)cscope.po.out: $(TOKUROOT)cscope.out;

$(TOKUROOT)cscope.out: $(TOKUROOT)cscope.files $(TOKUROOT)*/*.[ch] $(TOKUROOT)*/*/*.[ch] $(TOKUROOT)*/*/*/*.[ch]
	cd $(TOKUROOT) && $(CSCOPE) -qb

.PHONY: clean clean-default %.dir.clean
clean: clean-default

%.dir.clean:
	cd $* && $(MAKE) clean

clean-default:
	rm -f $(BINS) *.$(AEXT) *.$(SOEXT) *.$(OEXT)
	rm -f *.bb *.bbg *.da *.gcov *.gcno *.gcda
	rm -f *.exe *.obj *.pdb *.ilk
	rm -f *.exe.manifest *.dll.manifest
	rm -rf *.bundle


#Prevent using the wrong extensions/target types (Otherwise prereqs get confused).
%$(WRONGBINSUF): %.c
	echo "Wrong target type: $@ should be $*$(BINSUF)" && false
%$(WRONGBINSUF): %.$(WRONGOEXT)
	echo "Wrong target type: $@ should be $*$(BINSUF)" && false
%.$(WRONGOEXT): %.c
	echo "Wrong target type: $@ should be $*.$(OEXT)" && false
%.$(WRONGAEXT):
	echo "Wrong target type: $@ should be $*.$(AEXT)" && false
%.$(WRONGSOEXT):
	echo "Wrong target type: $@ should be $*.$(SOEXT)" && false

ifeq ($(SKIP_LOCKTREERULE),)
LOCKTREE         = $(TOKUROOT)src/lock_tree/locktree.$(AEXT)
LOCKTREE_BUNDLE  = $(TOKUROOT)src/lock_tree/locktree.bundle
LOCKTREE_LINEAR  = $(TOKUROOT)src/lock_tree/locktree_linear.$(AEXT)
LOCKTREE_TLOG    = $(TOKUROOT)src/lock_tree/locktree_tlog.$(AEXT)
LOCKTREE_LOG     = $(TOKUROOT)src/lock_tree/locktree_log.$(AEXT)
$(LOCKTREE) $(LOCKTREE_LINEAR) $(LOCKTREE_TLOG) $(LOCKTREE_LOG) $(LOCKTREE_BUNDLE): $(@D)*.[ch]
	cd $(@D) && $(MAKE) $(@F)
$(LOCKTREE) $(LOCKTREE_LINEAR) $(LOCKTREE_TLOG) $(LOCKTREE_LOG) $(LOCKTREE_BUNDLE): $(LIBPORTABILITY_SO)
endif

ifeq ($(SKIP_RANGETREERULE),)
RANGETREE        = $(TOKUROOT)src/range_tree/rangetree.$(AEXT)
RANGETREE_BUNDLE = $(TOKUROOT)src/range_tree/rangetree.bundle
RANGETREE_LINEAR = $(TOKUROOT)src/range_tree/rangetree_linear.$(AEXT)
RANGETREE_TLOG   = $(TOKUROOT)src/range_tree/rangetree_tlog.$(AEXT)
RANGETREE_LOG    = $(TOKUROOT)src/range_tree/rangetree_log.$(AEXT)
$(RANGETREE) $(RANGETREE_LINEAR) $(RANGETREE_TLOG) $(RANGETREE_LOG) $(RANGETREE_BUNDLE): $(@D)*.[ch]
	cd $(@D) && $(MAKE) $(@F)
$(RANGETREE) $(RANGETREE_LINEAR) $(RANGETREE_TLOG) $(RANGETREE_LOG) $(RANGETREE_BUNDLE): $(LIBPORTABILITY_SO)
endif

ifeq ($(SKIP_FTRULE),)
LOG_HEADER       = $(TOKUROOT)ft/log_header.h
$(LOG_HEADER): $(TOKUROOT)ft/logformat.c
	cd $(@D) && $(MAKE) $(@F)
endif

ifeq ($(SKIP_YDBRULE),)
IPO_YDB       = $(TOKUROOT)src/ipo_libtokudb.$(AEXT)
NOIPO_YDB     = $(TOKUROOT)src/static_libtokudb.$(AEXT)
$(NOIPO_YDB) $(IPO_YDB): $(@D)*.[ch]
	cd $(@D) && $(MAKE) $(@F)
ifeq  ($(DEBUG),0)
    WIN_YDB   = $(IPO_YDB)
else
    WIN_YDB   = $(NOIPO_YDB)
endif
endif

ifeq ($(OS_CHOICE),windows)
PTHREAD_LIB=$(TOKUROOT)lib/pthreadVC2.dll
PTHREAD_LOCAL=$(notdir $(PTHREAD_LIB))

$(PTHREAD_LOCAL): $(PTHREAD_LIB)
	cp -u $< $@
else
PTHREAD_LOCAL=
endif


BIN_FROM_C_FLAGS      =$(CFLAGS) $(CPPFLAGS)    $(LDFLAGS)       $(LDLIBS) $(BINOUTPUT)$@
BIN_FROM_C_FLAGS_NOLIB=$(CFLAGS) $(CPPFLAGS)    $(LDFLAGS_NOLIB) $(BINOUTPUT)$@
%$(BINSUF):%.c       $(DEPEND_COMPILE) $(DEPEND_LINK)
	$(CC) $< $(BIN_FROM_C_FLAGS) $(LINK_MUST_BE_LAST)

BIN_FROM_O_FLAGS      =$(CFLAGS) $(CPPFLAGS)    $(LDFLAGS)       $(LDLIBS) $(BINOUTPUT)$@
BIN_FROM_O_FLAGS_NOLIB=$(CFLAGS) $(CPPFLAGS)    $(LDFLAGS_NOLIB) $(BINOUTPUT)$@
%$(BINSUF):%.$(OEXT) $(DEPEND_COMPILE) $(DEPEND_LINK)
	$(CC) $< $(BIN_FROM_O_FLAGS) $(LINK_MUST_BE_LAST)

%.$(OEXT):%.c        $(DEPEND_COMPILE)
	$(CC) $< -c $(CPPFLAGS) $(CFLAGS) $(OOUTPUT)$@

CXXFLAGS=$(filter-out -Wbad-function-cast -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -std=c99,$(CFLAGS)) $(CPPFLAGS)

%.$(AEXT):
	$(AR) $(ARFLAGS) $(AROUTPUT)$@ $(filter %.$(OEXT),$^) $(patsubst %.bundle, %.bundle/*.$(OEXT), $(filter-out %.$(OEXT),$^))
ifeq ($(AEXT),a)
%.bundle: %.$(AEXT)
	rm -f $@/*.$(OEXT) && mkdir -p $@ && cd $@ && $(AR) x ../$(<F)
	touch $@
else
#Windows lib cannot extract everything.
#Do a for loop and extract one at a time.
%.bundle: %.$(AEXT)
	mkdir -p $@ && cd $@ && for ofile in `lib /list ../$(<F) |dos2unix |grep -F obj`; do lib /extract:$$ofile ../$(<F); done
	touch $@
endif


ifeq ($(SOEXT),so)
EXPORTMAPFILE=export.map
EXPORTMAP = -Wl,--version-script=$(EXPORTMAPFILE)
SHARED=-shared
ifeq ($(CC),icc)
SHARED += -static-intel -nodefaultlibs
endif
endif
ifeq ($(SOEXT),dll)
    ifeq ($(DEBUG),0)
        SHARED=/LD
    else
        SHARED=/LDd
    endif
EXPORTMAPFILE=export.def
EXPORTMAP=/link /def:$(EXPORTMAPFILE)
endif

SO_FLAGS=$(SHARED) $(BIN_FROM_O_FLAGS) $(EXPORTMAP)

TOKULINKER = $(CC)

%.$(SOEXT): $(EXPORTMAPFILE)
	$(TOKULINKER) $(SO_FLAGS)

#Testing tools
ifeq ($(SUMMARIZE),1)
SUMMARIZE_CMD = ;if test $$? = 0; then printf "%-60sPASS\n" $(HERE)/$@; else printf "%-60sFAIL\n" $(HERE)/$@ ; test 0 = 1; fi
SUMMARIZE_SHOULD_FAIL= ;if test $$? = 0; then printf "%-60sXFAIL\n" $(HERE)/$@; else printf "%-60sXPASS\n" $(HERE)/$@ ; test 0 = 1; fi
INVERTER=;test $$? -ne 0
else
SUMMARIZE_CMD =
endif

#target for flymake-mode
.PHONY: check-syntax
check-syntax: $(DEPEND_COMPILE)
	$(CC) $(CPPFLAGS) $(CFLAGS) -fno-ident -fsyntax-only $(CHK_SOURCES)

#Auto change variables from raw to include .exe if necessary.
BINS = $(patsubst %,%$(BINSUF),$(BINS_RAW))
OBJS = $(patsubst %,%.$(OEXT),$(OBJS_RAW))
REGRESSION_TESTS = $(patsubst %,%$(BINSUF),$(REGRESSION_TESTS_RAW))

APPVERIFIER_TESTS=exceptions handles heaps locks memory threadpool tls dangerousapis dirtystacks filepaths inputoutput
APPVERIFIER_TESTS_NONDEFAULT=heaps.backward:TRUE heaps.protect:TRUE
ENABLE_APPVERIFIER_FOR=appverif -enable $(APPVERIFIER_TESTS) -with $(APPVERIFIER_TESTS_NONDEFAULT) -for
DISABLE_APPVERIFIER_FOR=appverif -disable \* -for
%.appverifyenable:
	$(ENABLE_APPVERIFIER_FOR) $*.exe
%.appverifydisable:
	$(DISABLE_APPVERIFIER_FOR) $*.exe

.PHONY:%.appverifyenable %.appverifydisable %.appverifymaybeenable %.appverifymaybedisable

EXE_FILES = $(wildcard *.exe)
appverifyenable: $(patsubst %.exe,%.appverifyenable,$(EXE_FILES));
appverifydisable: $(patsubst %.exe,%.appverifydisable,$(EXE_FILES));

