####################### OPTIONS #########################
option(CROSS_COMPILE "Enables crosscompiling for raspberry pi" OFF)
option(BUILD_SHARED_LIB "Build a shared lib instead of a static." OFF)
option(ENABLE_TESTING "Enables testing." ON)
option(BUILD_NETWORKING "Build socket communication executable." ON)
option(BUILD_CLI "Makes a basic testing cli" OFF)

#################### CONFIGURATION ######################
set(OUTPUT_PATH bin)
set(LIB_DIR lib)
set(DEFAULT_BUILD_TYPE DEBUG)

set(CMAKE_CXX_STANDARD 14)

SET(CMAKE_CXX_FLAGS -pthread)

IF(NOT_PI)
    add_definitions(-DNOT_PI)
    message("Compiling not for Pi")
ENDIF()

project(FlippR-Driver)

###################### START_CMAKE #######################
cmake_minimum_required(VERSION 3.0.1)
project(FlippR-Driver VERSION 0.1.0)# LANGUAGES CXX)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

#set easylogging flags
add_definitions(-DELPP_NO_DEFAULT_LOG_FILE)

# Compile library to output_path
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/${OUTPUT_PATH})


# Default to Release build
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE})
endif(NOT CMAKE_BUILD_TYPE)


#set code-coverage flags if Debug mode
IF(CMAKE_BUILD_TYPE MATCHES DEBUG)
    message("Setting gcov flags")   
    SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
    SET(GCC_COVERAGE_LINK_FLAGS    "-lgcov")

    SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
    SET( CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG)

#################### Adding Library #####################
file(GLOB_RECURSE SOURCES src/*.cpp)

if(BUILD_SHARED_LIB)
  add_library(${PROJECT_NAME} SHARED ${SOURCES})
else()
  add_library(${PROJECT_NAME} STATIC ${SOURCES} cli/OutputInterpreter.cpp cli/OutputInterpreter.h src/output/items/detail/DriverBoardItem.cpp src/output/items/detail/DriverBoardItem.h include/DriverFactory.h src/utility/Colors.h)
endif(BUILD_SHARED_LIB)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_SOURCE_DIR}/include)

# Set libraries include path
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/${LIB_DIR})

######################### BOOST #########################
# Boost configuration
set(BOOST_COMPONENTS program_options thread timer chrono filesystem)
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
if(Boost_FOUND)
  # Include and link with boost
  target_include_directories(${PROJECT_NAME} PUBLIC ${Boost_INCLUDE_DIRS})
  LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
  target_link_libraries(${PROJECT_NAME} PUBLIC ${Boost_LIBRARIES})
  message ("Boost found and linked at ${Boost_INCLUDE_DIRS}")
else()
    message (FATAL_ERROR "Can't find Boost.")
endif(Boost_FOUND)

##################### WIRING_PI ##########################
find_library(wiringPi_LIB wiringPi)

add_definitions(-DNO_WIRING_PI)
if(NOT wiringPi_LIB)
  message(WARNING "Could not find wiringPi library, used testing wiring pi lib instead.")
    add_definitions(-DNO_WIRING_PI)
else()
    message(STATUS "Found wiring pi.")
    target_link_libraries(${PROJECT_NAME} PUBLIC ${wiringPi_LIB})
endif()

find_library(crypt_LIB crypt)
if(NOT crypt_LIB)
    message(FATAL_ERROR "Could not find crypt library")
endif()
target_link_libraries(${PROJECT_NAME} PUBLIC ${crypt_LIB})

##################### EASYLOGGING #########################
set(EASYLOGGING_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/${LIB_DIR}/easylogging)
add_library(Easylogging STATIC ${EASYLOGGING_INCLUDE_DIR}/easylogging++.cc)

target_include_directories(${PROJECT_NAME} PUBLIC ${EASYLOGGING_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC Easylogging)

######################## CATCH ############################
set(CATCH_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/tests)

add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})

####################### THREAD ############################
find_package(Threads REQUIRED)

if(NOT CMAKE_THREAD_LIBS_INIT)
    message(FATAL_ERROR, "Could not find libthread")
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE ${Threads_LIBRARIES})


if(ENABLE_TESTING)
  add_subdirectory(tests)
endif(ENABLE_TESTING)

if(BUILD_NETWORKING)
    add_subdirectory(networking)
endif(BUILD_NETWORKING)

if(BUILD_CLI)
    add_subdirectory(cli)
endif(BUILD_CLI)
####################### END_CMAKE ########################
