# Copyright 2005-2012 Intel Corporation.  All Rights Reserved.
#
# This file is part of Threading Building Blocks.
#
# Threading Building Blocks is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# Threading Building Blocks is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Threading Building Blocks; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As a special exception, you may use this file as part of a free software
# library without restriction.  Specifically, if other files instantiate
# templates or use macros or inline functions from this file, or you compile
# this file and link it with other files to produce an executable, this
# file does not by itself cause the resulting executable to be covered by
# the GNU General Public License.  This exception does not however
# invalidate any other reasons why the executable file might be covered by
# the GNU General Public License.

# default target
default_malloc: malloc malloc_test

tbb_root ?= $(TBBROOT)
BUILDING_PHASE=1
TEST_RESOURCE = $(MALLOC.RES)
include $(tbb_root)/build/common.inc
DEBUG_SUFFIX=$(findstring _debug,_$(cfg))

MALLOC_ROOT ?= $(tbb_root)/src/tbbmalloc
MALLOC_SOURCE_ROOT ?= $(MALLOC_ROOT)

VPATH = $(tbb_root)/src/tbb/$(ASSEMBLY_SOURCE) $(tbb_root)/src/tbb $(tbb_root)/src/test
VPATH += $(MALLOC_ROOT) $(MALLOC_SOURCE_ROOT)

CPLUS_FLAGS += $(if $(crosstest),$(DEFINE_KEY)__TBBMALLOC_NO_IMPLICIT_LINKAGE=1)

include $(tbb_root)/build/common_rules.inc

#------------------------------------------------------
# Define rules for making the TBBMalloc shared library.
#------------------------------------------------------

# Object files that make up TBBMalloc
MALLOC_CPLUS.OBJ = backend.$(OBJ) large_objects.$(OBJ) backref.$(OBJ)  tbbmalloc.$(OBJ)
MALLOC.OBJ := $(MALLOC_CPLUS.OBJ) $(MALLOC_ASM.OBJ) itt_notify_malloc.$(OBJ) frontend.$(OBJ)
PROXY.OBJ := proxy.$(OBJ) tbb_function_replacement.$(OBJ)
M_CPLUS_FLAGS := $(subst $(WARNING_KEY),,$(M_CPLUS_FLAGS)) $(DEFINE_KEY)__TBBMALLOC_BUILD=1
M_INCLUDES = $(INCLUDES) $(INCLUDE_KEY)$(MALLOC_ROOT) $(INCLUDE_KEY)$(MALLOC_SOURCE_ROOT)

# Suppress superfluous warnings for TBBmalloc compilation
$(MALLOC.OBJ): M_CPLUS_FLAGS += $(WARNING_SUPPRESS)

frontend.$(OBJ): frontend.cpp version_string.ver
	$(CPLUS) $(COMPILE_ONLY) $(M_CPLUS_FLAGS) $(PIC_KEY) $(M_INCLUDES) $(INCLUDE_KEY). $<

$(PROXY.OBJ): %.$(OBJ): %.cpp
	$(CPLUS) $(COMPILE_ONLY) $(CPLUS_FLAGS) $(PIC_KEY) $(DEFINE_KEY)__TBBMALLOC_BUILD=1 $(M_INCLUDES) $<

$(MALLOC_CPLUS.OBJ): %.$(OBJ): %.cpp
	$(CPLUS) $(COMPILE_ONLY) $(M_CPLUS_FLAGS) $(PIC_KEY) $(M_INCLUDES) $<

itt_notify_malloc.$(OBJ): itt_notify.cpp
	$(CPLUS) $(COMPILE_ONLY) $(M_CPLUS_FLAGS) $(PIC_KEY) $(OUTPUTOBJ_KEY)$@ $(INCLUDES) $<

MALLOC_LINK_FLAGS = $(LIB_LINK_FLAGS)
PROXY_LINK_FLAGS = $(LIB_LINK_FLAGS)

ifneq (,$(MALLOC.DEF))
tbbmalloc.def: $(MALLOC.DEF)
	$(CPLUS) $(PREPROC_ONLY) $< $(CPLUS_FLAGS) $(INCLUDES) > $@

MALLOC_LINK_FLAGS += $(EXPORT_KEY)tbbmalloc.def
$(MALLOC.DLL): tbbmalloc.def
endif

$(MALLOC.DLL): BUILDING_LIBRARY = $(MALLOC.DLL)
$(MALLOC.DLL): $(MALLOC.OBJ) $(MALLOC.RES) $(MALLOC_NO_VERSION.DLL)
	$(subst $(CPLUS),$(CONLY),$(LIB_LINK_CMD)) $(LIB_OUTPUT_KEY)$(MALLOC.DLL) $(MALLOC.OBJ) $(MALLOC.RES) $(LIB_LINK_LIBS) $(MALLOC_LINK_FLAGS)

ifneq (,$(MALLOCPROXY.DEF))
tbbmallocproxy.def: $(MALLOCPROXY.DEF)
	$(CPLUS) $(PREPROC_ONLY) $< $(CPLUS_FLAGS) $(INCLUDES) > $@

PROXY_LINK_FLAGS += $(EXPORT_KEY)tbbmallocproxy.def
$(MALLOCPROXY.DLL): tbbmallocproxy.def
endif

ifneq (,$(MALLOCPROXY.DLL))
$(MALLOCPROXY.DLL): BUILDING_LIBRARY = $(MALLOCPROXY.DLL)
$(MALLOCPROXY.DLL): $(PROXY.OBJ) $(MALLOCPROXY_NO_VERSION.DLL) $(MALLOC.DLL) $(MALLOC.RES)
	$(LIB_LINK_CMD) $(LIB_OUTPUT_KEY)$(MALLOCPROXY.DLL) $(PROXY.OBJ) $(MALLOC.RES) $(LIB_LINK_LIBS) $(LINK_MALLOC.LIB) $(PROXY_LINK_FLAGS)
endif

ifneq (,$(MALLOC_NO_VERSION.DLL))
$(MALLOC_NO_VERSION.DLL):
	echo "INPUT ($(MALLOC.DLL))" > $(MALLOC_NO_VERSION.DLL)
endif

ifneq (,$(MALLOCPROXY_NO_VERSION.DLL))
$(MALLOCPROXY_NO_VERSION.DLL):
	echo "INPUT ($(MALLOCPROXY.DLL))" > $(MALLOCPROXY_NO_VERSION.DLL)
endif

malloc: $(MALLOC.DLL) $(MALLOCPROXY.DLL)

malloc_dll: $(MALLOC.DLL)

malloc_proxy_dll: $(MALLOCPROXY.DLL)

.PHONY: malloc malloc_dll malloc_proxy_dll

#------------------------------------------------------
# End of rules for making the TBBMalloc shared library
#------------------------------------------------------

#------------------------------------------------------
# Define rules for making the TBBMalloc unit tests
#------------------------------------------------------

add_debug=$(basename $(1))_debug$(suffix $(1))
cross_suffix=$(if $(crosstest),$(if $(DEBUG_SUFFIX),$(subst _debug,,$(1)),$(call add_debug,$(1))),$(1))

MALLOC_MAIN_TESTS = test_ScalableAllocator.$(TEST_EXT) \
                    test_ScalableAllocator_STL.$(TEST_EXT) \
                    test_malloc_compliance.$(TEST_EXT) \
                    test_malloc_regression.$(TEST_EXT) \
                    test_malloc_init_shutdown.$(TEST_EXT) \
                    test_malloc_pools.$(TEST_EXT)
MALLOC_OVERLOAD_TESTS =  test_malloc_overload.$(TEST_EXT) test_malloc_overload_proxy.$(TEST_EXT) test_malloc_atexit.$(TEST_EXT)

MALLOC_LIB = $(call cross_suffix,$(MALLOC.LIB))
MALLOC_PROXY_LIB = $(call cross_suffix,$(MALLOCPROXY.LIB))

ifeq (windows.gcc,$(tbb_os).$(compiler))
test_malloc_overload.$(TEST_EXT): LIBS += $(MALLOC_PROXY_LIB)
endif

test_malloc_overload.$(TEST_EXT): test_malloc_overload.cpp
	$(CPLUS) $(OUTPUT_KEY)$@ $(subst /MT,/MD,$(M_CPLUS_FLAGS)) $(M_INCLUDES) $< $(LIBDL) $(LIBS) $(LINK_FLAGS)
test_malloc_overload_proxy.$(TEST_EXT): test_malloc_overload.cpp $(MALLOC_PROXY_LIB)
	$(CPLUS) $(OUTPUT_KEY)$@ $(subst /MT,/MD,$(M_CPLUS_FLAGS)) $(M_INCLUDES) $< $(LIBDL) $(MALLOC_PROXY_LIB) $(LIBS) $(LINK_FLAGS)

test_malloc_whitebox.$(TEST_EXT): test_malloc_whitebox.cpp $(MALLOC_ASM.OBJ) version_string.ver
	$(CPLUS) $(OUTPUT_KEY)$@ $(M_CPLUS_FLAGS) $(M_INCLUDES) $(INCLUDE_KEY). $< $(MALLOC_ASM.OBJ) $(LIBS) $(LIBDL) $(LINK_FLAGS)

test_malloc_lib_unload.$(TEST_EXT): test_malloc_lib_unload.cpp test_malloc_lib_unload_dll.$(DLL)
	$(CPLUS) $(OUTPUT_KEY)$@ $(M_CPLUS_FLAGS) $(M_INCLUDES) $< $(LIBS) test_malloc_lib_unload_dll.$(LIBEXT)  $(LIBDL) $(LINK_FLAGS)

# TODO: use generic rules
test_malloc_used_by_lib.$(TEST_EXT): test_malloc_used_by_lib.cpp test_malloc_used_by_lib.$(DLL)
	$(CPLUS) $(OUTPUT_KEY)$@ $(M_CPLUS_FLAGS) $(M_INCLUDES) $< $(LIBS) $(LIBDL) $(LINK_FLAGS)

test_malloc_used_by_lib.$(DLL): test_malloc_used_by_lib.cpp
	$(CPLUS) $(OUTPUT_KEY)$@ $(subst /MT,/LD,$(M_CPLUS_FLAGS)) $(PIC_KEY) $(M_INCLUDES) $(DEFINE_KEY)_USRDLL $< $(MALLOC_LIB) $(LINK_FLAGS) $(DYLIB_KEY)

$(MALLOC_MAIN_TESTS): %.$(TEST_EXT): %.$(OBJ) $(MALLOC_LIB)
	$(CPLUS) $(OUTPUT_KEY)$@ $(CPLUS_FLAGS) $< $(MALLOC_LIB) $(LIBS) $(LINK_FLAGS)

MALLOC_C_TESTS = test_malloc_pure_c.$(TEST_EXT)

$(MALLOC_C_TESTS): %.$(TEST_EXT): %.$(OBJ) $(MALLOC_LIB)
	$(CPLUS) $(OUTPUT_KEY)$@ $(CPLUS_FLAGS) $^ $(LIBS) $(LINK_FLAGS)

# Rules for generating a test DLL
%_dll.$(DLL): %_dll.$(OBJ)
	$(LIB_LINK_CMD) $(OUTPUT_KEY)$@ $(CPLUS_FLAGS) $(PIC_KEY) $< $(LIBS) $(LINK_FLAGS) $(DYLIB_KEY)
.PRECIOUS: %_dll.$(OBJ)

test_malloc_atexit.$(TEST_EXT): test_malloc_atexit.$(OBJ) test_malloc_atexit_dll.$(DLL)
	$(CPLUS) $(OUTPUT_KEY)$@ $(CPLUS_FLAGS) $< $(MALLOC_PROXY_LIB) $(MALLOC_LIB) test_malloc_atexit_dll.$(LIBEXT) $(LIBS) $(LINK_FLAGS)

MALLOC_TESTS = $(MALLOC_MAIN_TESTS) $(MALLOC_C_TESTS) test_malloc_whitebox.$(TEST_EXT) test_malloc_used_by_lib.$(TEST_EXT)
ifneq (,$(MALLOCPROXY.DLL))
MALLOC_TESTS += $(MALLOC_OVERLOAD_TESTS) test_malloc_lib_unload.$(TEST_EXT)
endif

# run_cmd is usually empty
malloc_test: $(call cross_suffix,$(MALLOC.DLL)) malloc_test_no_depends

malloc_test_no_depends: $(TEST_PREREQUISITE) $(MALLOC_TESTS)
	$(run_cmd) ./test_malloc_pools.$(TEST_EXT) $(args) 1:4
ifneq (,$(MALLOCPROXY.DLL))
	$(run_cmd) ./test_malloc_atexit.$(TEST_EXT) $(args)
	$(run_cmd) ./test_malloc_lib_unload.$(TEST_EXT) $(args)
	$(run_cmd) $(TEST_LAUNCHER) -l $(call cross_suffix,$(MALLOCPROXY.DLL)) test_malloc_overload.$(TEST_EXT) $(args)
	$(run_cmd) $(TEST_LAUNCHER) test_malloc_overload_proxy.$(TEST_EXT) $(args)
endif
	$(run_cmd) ./test_malloc_used_by_lib.$(TEST_EXT)
	$(run_cmd) ./test_malloc_whitebox.$(TEST_EXT) $(args) 1:4
	$(run_cmd) $(TEST_LAUNCHER) test_malloc_compliance.$(TEST_EXT) $(args) 1:4
	$(run_cmd) ./test_ScalableAllocator.$(TEST_EXT) $(args)
	$(run_cmd) ./test_ScalableAllocator_STL.$(TEST_EXT) $(args)
	$(run_cmd) ./test_malloc_regression.$(TEST_EXT) $(args)
	$(run_cmd) ./test_malloc_init_shutdown.$(TEST_EXT) $(args)
	$(run_cmd) ./test_malloc_pure_c.$(TEST_EXT) $(args)

#------------------------------------------------------
# End of rules for making the TBBMalloc unit tests
#------------------------------------------------------

# Include automatically generated dependences
-include *.d
