cmake_minimum_required (VERSION 2.8)

project (Sailfish)

# set (CMAKE_CXX_COMPILER g++)
set (CMAKE_CXX_FLAGS "-O3 -DHAVE_ANSI_TERM -DHAVE_SSTREAM -DHAVE_CONFIG_H -Wall -std=c++11")

# Compiler-specific C++11 activation.
# http://stackoverflow.com/questions/10984442/how-to-detect-c11-support-of-a-compiler-with-cmake
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
    execute_process(
        COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
    if (NOT (GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7))
        message(FATAL_ERROR "${PROJECT_NAME} requires g++ 4.7 or greater.")
    endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
else ()
    message(FATAL_ERROR "Your C++ compiler does not support C++11.")
endif ()



set (GAT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

set (CMAKE_VERBOSE_MAKEFILE true)

if ( DEFINED CUSTOM_BOOST_PATH )
	set (CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} ${CUSTOM_BOOST_PATH})
	set (CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${CUSTOM_BOOST_PATH}/lib )
endif ( DEFINED CUSTOM_BOOST_PATH )

set (Boost_USE_STATIC_LIBS OFF)
set (Boost_USE_MULTITHREADED ON)
set (Boost_USE_STATIC_RUNTIME OFF)

set(Boost_ADDITIONAL_VERSIONS "1.53" "1.53.0")
find_package(Boost 1.53.0 COMPONENTS iostreams filesystem system thread timer program_options)

if (NOT Boost_FOUND)
	message(FATAL_ERROR 
		"Sailfish cannot be compiled without Boost.\nPlease visit http://www.boost.org/ and install Boost")
endif()


find_package (ZLIB)

###
#
# Build external dependencies (except Boost)
#
###

set(EXTERNAL_LIBRARY_PATH $CMAKE_CURRENT_SOURCE_DIR/lib)

##
# Logging library; currently unused so the dependency is temporarily removed
##
# message(status "Building and Installing g2log")
# message(status "============================")
# include(ExternalProject)
# ExternalProject_Add(libg2log
#     DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external
#     URL http://www.cs.cmu.edu/~robp/files/g2log.tgz
#     SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/g2log
#     INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/install
#     CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
#     BUILD_COMMAND make
#     #BUILD_IN_SOURCE 1
#     INSTALL_COMMAND sh -c "cp <BINARY_DIR>/*.a <INSTALL_DIR>/lib && cp <SOURCE_DIR>/src/*.h <INSTALL_DIR>/include"
# )
# message(status "g2log installed.")

message(status "Building and Installing CMPH")
message(status "============================")
include(ExternalProject)
ExternalProject_Add(libcmph
    DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external
    URL http://www.cs.cmu.edu/~robp/files/cmph-2.0.tar.gz
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/cmph-2.0
    INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/install
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/external/cmph-2.0/configure --prefix=<INSTALL_DIR>
    BUILD_COMMAND ${MAKE}
    BUILD_IN_SOURCE 1
    INSTALL_COMMAND make install
)
message(status "CMPH installed.")

message(status "Downloading, Building and Installing Jellyfish")
message(status "============================")
ExternalProject_Add(libjellyfish
	DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external
	URL http://www.cbcb.umd.edu/software/jellyfish/jellyfish-1.1.10.tar.gz
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/jellyfish-1.1.10
    INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/install
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/external/jellyfish-1.1.10/configure --prefix=<INSTALL_DIR>
    BUILD_COMMAND ${MAKE}
    BUILD_IN_SOURCE 1
    INSTALL_COMMAND make install && cp config.h <INSTALL_DIR>/include/jellyfish-1.1.10/
)
message(status "Jellyfish installed.")

message(status "Downloading, Building and Installing Intel TBB")
message(status "============================")

# These are useful for the custom install step we'll do later
set(TBB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/tbb41_20130314oss)
set(TBB_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/install)
ExternalProject_Add(libtbb
	DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external
	URL http://threadingbuildingblocks.org/sites/default/files/software_releases/source/tbb41_20130314oss_src.tgz
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/tbb41_20130314oss
    INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/install
    CONFIGURE_COMMAND ""
    BUILD_COMMAND make cfg=release tbb_build_prefix=LIBS
    INSTALL_COMMAND sh -c "cp ${TBB_SOURCE_DIR}/build/LIBS_release/*.so* ${TBB_INSTALL_DIR}/lib && cp -r ${TBB_SOURCE_DIR}/include/* ${TBB_INSTALL_DIR}/include"
    BUILD_IN_SOURCE 1
)
message(status "TBB installed.")

###
#
# Done building external dependencies.
#
###

set (CPACK_SOURCE_IGNORE_FILES 
"/build/"
"/scripts/AggregateToGeneLevel.py"
"/scripts/ExpressionTools.py"
"/scripts/GenerateExpressionFiles.sh"
"/scripts/ParseSoftFile.py"
"/scripts/PlotCorrelation.py"
"/bin/"
"/external/"
".git/")

message("CPACK_SOURCE_IGNORE_FILES = ${CPACK_SOURCE_IGNORE_FILES}")

# Recurse into Sailfish source directory
add_subdirectory ( src )

# build a CPack driven installer package
include (CPack)

