# *******************************************************************************
# * CMakeList.txt - soss_client 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)

# Include directories specified at top-level

set(CURRENT_TARGET_NAME soss_client)

if(UNIX)
	add_definitions(-DUNIX)
	add_definitions(-DSOSS_UNIX)
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif(UNIX)

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

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

# Internal header files
file(GLOB INTERNAL_INCLUDE_FILES
	"${SOSS_CLIENT_INCLUDE_DIR}/${CURRENT_TARGET_NAME}/internal/*.h"
)
SOURCE_GROUP("Header Files\\internal"
	FILES ${INTERNAL_INCLUDE_FILES}
)

# Internal source files
file(GLOB INTERNAL_SOURCE_FILES
	"${SOSS_CLIENT_SRC_DIR}/internal/*.cpp"
	"${SOSS_CLIENT_SRC_DIR}/internal/*.c"
)
SOURCE_GROUP("Source Files\\internal"
	FILES ${INTERNAL_SOURCE_FILES}
)

# Add include directories
include_directories("${SOSS_CLIENT_INCLUDE_DIR}"
					"${SOSS_CLIENT_INCLUDE_DIR}\\internal")

add_library(${CURRENT_TARGET_NAME} ${SOURCE_FILES} ${INCLUDE_FILES} ${INTERNAL_SOURCE_FILES} ${INTERNAL_INCLUDE_FILES})

# Add Boost dependency
set(Boost_USE_MULTITHREADED ON)
if(MSVC)
	# MSVC all but requires usage of static Boost libraries
	set(Boost_USE_STATIC_LIBS   ON)
endif(MSVC)

# Optional boost libraries
find_package(Boost COMPONENTS chrono)
if(${Boost_FOUND})
	target_link_libraries(${CURRENT_TARGET_NAME}    ${Boost_LIBRARIES})
endif(${Boost_FOUND})

# Required boost libraries
find_package(Boost REQUIRED COMPONENTS thread date_time system)
target_link_libraries(${CURRENT_TARGET_NAME}    ${Boost_LIBRARIES})
include_directories(${Boost_INCLUDE_DIRS})

# Add soss_svccli dependency
find_package(SossSvcCli REQUIRED)
target_link_libraries(${CURRENT_TARGET_NAME}    ${SOSS_SVCCLI_LIB})
include_directories(${SOSS_SVCCLI_INCLUDE_DIR})

# Add protobuf dependency

# 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)

	# use the 64-bit generated libraries if using a 64-bit Windows target
	if(CMAKE_CL64 OR CMAKE_GENERATOR MATCHES "Win64")
		STRING(REGEX REPLACE "Release" "x64/Release" PROTOBUF_LIBRARY ${PROTOBUF_LIBRARY})
		STRING(REGEX REPLACE "Debug" "x64/Debug" PROTOBUF_LIBRARY_DEBUG ${PROTOBUF_LIBRARY_DEBUG})
	endif(CMAKE_CL64 OR CMAKE_GENERATOR MATCHES "Win64")

	message(STATUS "Protobuf library discovered on local system: ${PROTOBUF_INCLUDE_DIRS} ${PROTOBUF_LIBRARY}")
	add_definitions(-DHAVE_PROTOBUF)
	# Target the release library for optimized versions, and the debug library for debug versions
	target_link_libraries(${CURRENT_TARGET_NAME} optimized ${PROTOBUF_LIBRARY} debug ${PROTOBUF_LIBRARY_DEBUG})
	include_directories(${PROTOBUF_INCLUDE_DIRS})

	# TODO: compile the SossIndex.proto file in the soss_client include
	# directory. It doesn't appear possible to adapt the standard protoc
	# compile facility to what we need:
	#
	#   PROTOBUF_GENERATE_CPP (appears to) hardcode the where it writes
	#   the generated .cc and .h files to the cmake binaries directory;
	#   we need customers to be able to include our .h file in their
	#   own protoc-generated header files, so we really want it to be
	#   written to our soss_client include directory.
	#
	#   It's not clear yet how to control multiple protoc import dirs
	#   to cause protoc to generate .cc includes with the leading
	#   soss_client directory - e.g. #include "soss_client/SossIndex.pb.h"
	#
	# For now, we check in pre-generated .h and .cc files to source
	# control. The .cc file will not be in the project compilation list
	# because we don't include .cc files as source files above. Instead,
	# we include a protobuf_soss_index.cpp file in the source tree which
	# includes the .cc file guarded by the HAVE_PROTOBUF #define.

	#file(GLOB PROTO_FILES "${SOSS_CLIENT_INCLUDE_DIR}/${CURRENT_TARGET_NAME}/*.proto")
	#set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIRS} ${SOSS_CLIENT_INCLUDE_DIR})
	#PROTOBUF_GENERATE_CPP(PROTOC_GENERATED_SOURCES PROTOC_GENERATED_HEADERS ${PROTO_FILES})
	# We would normally then add the PROTOC_GENERATED_HEADERS and
	# PROTOC_GENERATED_SOURCES to the add_library command, which means
	# this code needs to be broken up so part of it comes before the
	# add_library and part comes after.

endif(PROTOBUF_FOUND)

# Suffix the debug library with a "d" on Unix systems
if(UNIX)
	set_target_properties(${CURRENT_TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_NAME_DEBUG "${OUTPUT_NAME_CONFIG}d")
endif(UNIX)

# Install the compiled library to the library dir on Unix systems
if(UNIX)
	set(LIB_DIR /usr/lib)
	if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
		set(LIB_DIR     /usr/lib64)
	endif(${CMAKE_SIZEOF_VOID_P} EQUAL 8)

	INSTALL(TARGETS ${CURRENT_TARGET_NAME}
		LIBRARY DESTINATION ${LIB_DIR}
		ARCHIVE DESTINATION ${LIB_DIR})
endif(UNIX)
