cmake_minimum_required(VERSION 3.0)
project(ex1
  VERSION 0.1
  LANGUAGES CXX)

###########
# Debug or Release
###########
if (NOT CMAKE_BUILD_TYPE)
  ## set default to Debug
  set(CMAKE_BUILD_TYPE RelWithDebInfo)  # override with -DCMAKE_BUILD_TYPE=Release
  message("==> CMAKE_BUILD_TYPE empty. Changing it to Debug.")
else()
  message("==> CMAKE_BUILD_TYPE == ${CMAKE_BUILD_TYPE}.")
endif()



## set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG -DGLIBCXX_FORCE_NEW")

## https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
## During Debug, use debug version of libstdc++ (asserts on access to invalid iterators, etc!)
## set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_GLIBCXX_DEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

##############
# C++ Standard
##############
# set(CMAKE_CXX_STANDARD   14)
# C++17 gives me transform
set(CMAKE_CXX_STANDARD   17)
set(CMAKE_CXX_EXTENSIONS ON)

# The old fix for getting logging to work/link properly.
# ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)

# This seems to work for static linking the boost libraries
set(Boost_USE_STATIC_LIBS   ON)

# add log_setup log 
FIND_PACKAGE( Boost 1.71 COMPONENTS program_options log_setup log REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/yaml-cpp)
  message("***")
  message("*** ERROR/MISSING *** please run: git clone https://github.com/jbeder/yaml-cpp.git --depth 1")
  message("***")
endif()

add_subdirectory(yaml-cpp)

if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/googletest)
  message("***")
  message("*** ERROR/MISSING *** please run: git clone https://github.com/google/googletest.git --depth 1")
  message("***")
endif()

add_subdirectory(googletest)

option(gtest_build_samples "Build gtest's sample programs." OFF)
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)

### TESTS
add_executable(test-galaxy test-galaxy.cpp galaxy.cpp config.cpp utils.cpp buysell.cpp)
add_dependencies(test-galaxy gtest)
target_link_libraries(test-galaxy gtest_main ${Boost_LIBRARIES} yaml-cpp)

add_executable(test-director test-director.cpp galaxy.cpp utils.cpp buysell.cpp director.cpp dispatchers.cpp boxes.cpp scripts.cpp config.cpp)
add_dependencies(test-director gtest)
target_link_libraries(test-director gtest_main ${Boost_LIBRARIES} yaml-cpp)

enable_testing()
add_test(NAME test-galaxy
  COMMAND test-galaxy)
add_test(NAME test-director
  COMMAND test-director)

# dispatchers.cpp 
ADD_EXECUTABLE( twproxy twproxy.cpp utils.cpp session.cpp boxes.cpp director.cpp galaxy.cpp dispatchers.cpp scripts.cpp buysell.cpp config.cpp)

#Static start
set_target_properties(twproxy PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(twproxy PROPERTIES LINK_SEARCH_END_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
#Static Libs

#Set Linker flags
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

TARGET_LINK_LIBRARIES( twproxy -static ${Boost_LIBRARIES} pthread.a yaml-cpp)
# target_link_libraries(test -static)

# private does nothing here
# TARGET_LINK_LIBRARIES( twproxy PRIVATE ${Boost_LIBRARIES} pthread yaml-cpp)

target_precompile_headers(twproxy PRIVATE pch.hpp)