cmake_minimum_required(VERSION 2.8)
project(WFR-lib_boost CXX)
	
	
# cmake options
OPTION(USE_BUILDIN_BOOST "Use buildin boost" ON)


# Check whether we are called by another script
get_directory_property(hasParent PARENT_DIRECTORY)

# Let's build our own copy of boost
if (USE_BUILDIN_BOOST)
	include(ExternalProject)
	
	# Set boost version
	if(hasParent)
		set(boost_version "1.57.0" PARENT_SCOPE)
		set(boost_version "1.57.0")
	else()
		set(boost_version "1.57.0")
	endif()
	
	# How to build boost
	ExternalProject_Add(Boost
		DEPENDS
		SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/boost/src/boost-${boost_version}
		DOWNLOAD_COMMAND mkdir -p boost-${boost_version} && tar xf ${CMAKE_CURRENT_SOURCE_DIR}/boost-${boost_version}.tar.gz --strip=1 -C boost-${boost_version} 
		PREFIX ${CMAKE_CURRENT_BINARY_DIR}/boost
		CONFIGURE_COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/boost/src/boost-${boost_version}  && ./bootstrap.sh --with-libraries=system,filesystem,program_options --prefix=${CMAKE_CURRENT_BINARY_DIR}/boost/install --libdir=${CMAKE_CURRENT_BINARY_DIR}/boost/install/lib
		BUILD_COMMAND cd ${CMAKE_CURRENT_BINARY_DIR}/boost/src/boost-${boost_version} && ./b2 install
		INSTALL_COMMAND echo ""
	)
	
	# Where to install boost
	install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/boost/install/include DESTINATION ${CMAKE_INSTALL_PREFIX})
	install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/boost/install/lib DESTINATION ${CMAKE_INSTALL_PREFIX})
	
	# Provide some variables to parent script
	if(hasParent)
		set(Boost_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/boost/install/include")
		set(Boost_INCLUDE_DIR "${Boost_INCLUDE_DIR}" PARENT_SCOPE)
		set(Boost_INCLUDE_DIRS "${Boost_INCLUDE_DIR}")
		set(Boost_INCLUDE_DIRS "${Boost_INCLUDE_DIRS}" PARENT_SCOPE)
		set(Boost_LIBRARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/boost/install/lib")
		set(Boost_LIBRARY_DIR "${Boost_LIBRARY_DIR}" PARENT_SCOPE)
		set(Boost_LIBRARY_DIRS "${Boost_LIBRARY_DIR}")
		set(Boost_LIBRARY_DIRS "${Boost_LIBRARY_DIRS}" PARENT_SCOPE)
		set(Boost_LIBRARIES 
			"${Boost_LIBRARY_DIR}/libboost_system.so" 
			"${Boost_LIBRARY_DIR}/libboost_filesystem.so" 
			"${Boost_LIBRARY_DIR}/libboost_program_options.so"
	    )
		set(Boost_LIBRARIES "${Boost_LIBRARIES}" PARENT_SCOPE)
	endif()
	
	
# Let's use an existing copy of boost
else()
    add_custom_target(Boost) # Dummy target for add_dependencies
	find_package(Boost COMPONENTS system filesystem program_options REQUIRED)
	if(hasParent)
		set(Boost_INCLUDE_DIR "${Boost_INCLUDE_DIR}" PARENT_SCOPE)
		set(Boost_INCLUDE_DIRS "${Boost_INCLUDE_DIRS}" PARENT_SCOPE)
		set(Boost_LIBRARY_DIR "${Boost_LIBRARY_DIR}" PARENT_SCOPE)
		set(Boost_LIBRARY_DIRS "${Boost_LIBRARY_DIRS}" PARENT_SCOPE)
		set(Boost_LIBRARIES "${Boost_LIBRARIES}" PARENT_SCOPE)
	endif()
endif ()


