# *******************************************************************************
# * CMakeList.txt - tests CMake build file
# *
# * ScaleOut NamedCache C++ API Library
# *
# * Copyright (c) 2013-2016 by ScaleOut Software, Inc.
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# * http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# *******************************************************************************

# Define minimum CMake version
cmake_minimum_required(VERSION 2.6)

# Setup project directories
set(TEST_INCLUDE_DIR            "${CMAKE_CURRENT_SOURCE_DIR}")
set(TEST_SOURCE_DIR             "${CMAKE_CURRENT_SOURCE_DIR}")

set(GTEST_VERSION               "1.6.0")
set(GTEST_DIR                   "${TEST_DIR}/gtest-${GTEST_VERSION}")

set(CURRENT_TARGET_NAME         tests)

if(NOT UNIX)
	option(SOSS_TESTS_CHECK_MEMORY  "Run tests with memory leak detection." OFF)
endif(NOT UNIX)

# Build and configure gtest
# =============================================================================

# Build the gtest library
# FIX as per https://code.google.com/p/googletest/issues/detail?id=412
if(MSVC) # VS2012 doesn't correctly support tuples yet
	add_definitions( /D _VARIADIC_MAX=10 )
endif()

# Force the gtest library to use the shared run-time library
set(gtest_force_shared_crt ON CACHE BOOL
	"Use shared (DLL) run-time lib even when Google Test is built as static lib.")

# Add the gtest build target
add_subdirectory(${GTEST_DIR})

# Build and configure SossClient UnitTests
# =============================================================================

# Automatically scan the include directories for header files
file(GLOB INCLUDE_FILES
	"${TEST_INCLUDE_DIR}/*.h"
)

# Automatically scan the source directories for source files
file(GLOB SOURCE_FILES
	"${TEST_SOURCE_DIR}/*.cpp"
)

# Add include directories
include_directories("${TEST_INCLUDE_DIR}")

# Suppress VS warnings about std::swap_ranges() usage in boost::uuid:
if(MSVC)
	add_definitions( /D _SCL_SECURE_NO_WARNINGS )
endif(MSVC)

if(UNIX)
	add_definitions(-DUNIX)

	# Specify C++0x standard
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

	# Specifying input character set is required (only) for protobuf_query_tests
	# where we're specifically testing string contains with non-ascii characters.
	# The encoding in the source file is Latin-1 (windows default), aka ISO_8859-1.
	# If we don't specify, gcc will assume utf-8.

	if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
		# using Clang
		# Clang only supports utf-8, and does not have a -finput-charset argument
		# so just ignore the warning
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-source-encoding")
	elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
		# using GCC
		SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finput-charset=ISO_8859-1")
	endif()
endif(UNIX)

add_definitions(-DSOSS_TEST)

add_executable(${CURRENT_TARGET_NAME} ${SOURCE_FILES} ${INCLUDE_FILES})

# Configure UnitTest executable dependencies on SossClient
target_link_libraries(${CURRENT_TARGET_NAME} soss_client)
include_directories("${SOSS_CLIENT_INCLUDE_DIR}")

# Also include internal header files
include_directories("${SOSS_CLIENT_INCLUDE_DIR}\\internal")

# Configure UnitTest executable dependency on gtest
target_link_libraries(${CURRENT_TARGET_NAME} gtest)
include_directories(${GTEST_DIR}/include)

# Configure UnitTest Windows executable dependency on vld (Visual Leak Detector)
if(SOSS_TESTS_CHECK_MEMORY)
	find_package(Vld)
	if(Vld_FOUND)
		target_link_libraries(${CURRENT_TARGET_NAME}    ${VLD_LIB})
		include_directories(${VLD_INCLUDE_DIR})
		add_definitions(-DCHECK_MEMORY)
	endif(Vld_FOUND)
endif(SOSS_TESTS_CHECK_MEMORY)

# Add Boost dependency (only need include dirs, library is handled by soss_client)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

# Add soss_svccli dependency (only need include dirs, library is handled by soss_client)
find_package(SossSvcCli REQUIRED)
include_directories(${SOSS_SVCCLI_INCLUDE_DIR})

# Add protobuf dependency (only need include dirs, library is handled by soss_client)

# Attempt to find the source root from an environment variable to pass to the FindProtobuf module
if(NOT "$ENV{PROTOBUF_SRC_ROOT_FOLDER}" STREQUAL "")
	set(PROTOBUF_SRC_ROOT_FOLDER "$ENV{PROTOBUF_SRC_ROOT_FOLDER}" CACHE INTERNAL "Download location of the Protobuf source.")
endif(NOT "$ENV{PROTOBUF_SRC_ROOT_FOLDER}" STREQUAL "")

find_package(Protobuf)
if(PROTOBUF_FOUND)
	add_definitions(-DHAVE_PROTOBUF)
	include_directories(${PROTOBUF_INCLUDE_DIRS})
endif(PROTOBUF_FOUND)
