##===- unittests/Makefile.unittest -------------------------*- Makefile -*-===##
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
#
# This file is included by all of the unit test makefiles.
#
##===----------------------------------------------------------------------===##

ifndef MAKEFILE_UNITTEST_NO_INCLUDE_COMMON
include $(LEVEL)/Makefile.common
endif

# Clean up out-of-tree stray unittests for Lit not to pick one up.
.PHONY: cleanup-local
cleanup-local:
	-$(Verb) $(FIND) $(filter-out $(PARALLEL_DIRS), $(wildcard *)) -type f \
	  -path '*/$(BuildMode)/*Tests$(EXEEXT)' \
	  -exec rm -f '{}' \;

all:: cleanup-local
clean:: cleanup-local

# Set up variables for building a unit test.
ifdef TESTNAME

LLVMUnitTestExe = $(BuildMode)/$(TESTNAME)Tests$(EXEEXT)

# Note that these flags are duplicated when building GoogleTest itself in
# utils/unittest/googletest/Makefile; ensure that any changes are made to both.
CPP.Flags += -I$(LLVM_SRC_ROOT)/utils/unittest/googletest/include
CPP.Flags += $(NO_MISSING_FIELD_INITIALIZERS) $(NO_VARIADIC_MACROS)
CPP.Flags += -DGTEST_HAS_RTTI=0
# libstdc++'s TR1 <tuple> header depends on RTTI and uses C++'0x features not
# supported by Clang, so force googletest to use its own tuple implementation.
CPP.Flags += -DGTEST_USE_OWN_TR1_TUPLE

# Disable pthreads if LLVM was configured without them.
ifneq ($(HAVE_PTHREAD), 1)
  CPP.Flags += -DGTEST_HAS_PTHREAD=0
endif

TESTLIBS = -lgtest -lgtest_main

ifeq ($(ENABLE_SHARED), 1)
  ifneq (,$(RPATH))
    # Add the absolute path to the dynamic library.  This is ok because
    # we'll never install unittests.
    LD.Flags += $(RPATH) -Wl,$(SharedLibDir)
  endif
endif

$(LLVMUnitTestExe): $(ObjectsO) $(ProjLibsPaths) $(LLVMLibsPaths)
	$(Echo) Linking $(BuildMode) unit test $(TESTNAME) $(StripWarnMsg)
	$(Verb) $(Link) -o $@ $(TOOLLINKOPTS) $(ObjectsO) $(ProjLibsOptions) \
	$(TESTLIBS) $(LLVMLibsOptions) $(ExtraLibs) $(TOOLLINKOPTSB) $(LIBS)
	$(Echo) ======= Finished Linking $(BuildMode) Unit test $(TESTNAME) \
          $(StripWarnMsg)

all:: $(LLVMUnitTestExe)

unitcheck:: $(LLVMUnitTestExe)
	$(LLVMUnitTestExe)

endif